from __future__ import annotations
from markupsafe import Markup
from onegov.core.html import html_to_text
from onegov.core.orm import Base
from onegov.fsi.i18n import _
from onegov.search import ORMSearchable
from sedate import utcnow
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.orm import mapped_column
from sqlalchemy.orm import relationship
from sqlalchemy.orm import DynamicMapped
from sqlalchemy.orm import Mapped
from uuid import uuid4, UUID
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from sqlalchemy.orm import Query
from .course_event import CourseEvent
[docs]
class Course(Base, ORMSearchable):
[docs]
__tablename__ = 'fsi_courses'
[docs]
fts_type_title = _('Courses')
[docs]
fts_title_property = 'name'
[docs]
fts_properties = {
'name': {'type': 'localized', 'weight': 'A'},
'description': {'type': 'localized', 'weight': 'B'},
}
[docs]
id: Mapped[UUID] = mapped_column(
primary_key=True,
default=uuid4
)
[docs]
name: Mapped[str] = mapped_column(unique=True)
[docs]
description: Mapped[Markup]
# saved as integer (years), accessed as years
[docs]
refresh_interval: Mapped[int | None]
# If the course has to be refreshed after some interval
[docs]
mandatory_refresh: Mapped[bool] = mapped_column(default=False)
# hides the course in the collection for non-admins
[docs]
hidden_from_public: Mapped[bool] = mapped_column(default=False)
[docs]
evaluation_url: Mapped[str | None]
[docs]
events: DynamicMapped[CourseEvent] = relationship(back_populates='course')
@property
[docs]
def title(self) -> str:
return self.name
@property
[docs]
def lead(self) -> str:
text = html_to_text(self.description)
if len(text) > 160:
return text[:160] + '…'
else:
return text
@property
[docs]
def description_html(self) -> Markup:
"""
Returns the description that is saved as HTML from the redactor js
plugin.
"""
return self.description
@hybrid_property
[docs]
def future_events(self) -> Query[CourseEvent]:
from onegov.fsi.models import CourseEvent
return self.events.filter(CourseEvent.start > utcnow()).order_by(
CourseEvent.start)