Source code for org.models.document_form
from __future__ import annotations
from uuid import uuid4
from onegov.chat.models.message import associated
from onegov.core.collection import GenericCollection
from onegov.core.orm import Base
from onegov.core.orm.mixins import (
ContentMixin, TimestampMixin, dict_property, content_property,
meta_property)
from onegov.core.orm.mixins.content import dict_markup_property
from onegov.core.utils import normalize_for_url
from onegov.file import File, MultiAssociatedFiles
from onegov.form import FormCollection
from onegov.org.i18n import _
from onegov.org.models.extensions import PersonLinkExtension
from onegov.org.observer import observes
from onegov.reservation import ResourceCollection
from onegov.org.models import (AccessExtension, ContactExtension,
CoordinatesExtension, GeneralFileLinkExtension,
HoneyPotExtension)
from onegov.search import SearchableContent
from sqlalchemy.orm import mapped_column, Mapped
from uuid import UUID
from typing import Any, TYPE_CHECKING
if TYPE_CHECKING:
from sqlalchemy.orm import Query, Session
[docs]
class FormDocument(Base, ContentMixin, TimestampMixin, AccessExtension,
SearchableContent, MultiAssociatedFiles,
ContactExtension, PersonLinkExtension,
HoneyPotExtension, CoordinatesExtension,
GeneralFileLinkExtension):
[docs]
fts_properties = {
'title': {'type': 'localized', 'weight': 'A'},
'lead': {'type': 'localized', 'weight': 'B'},
'pdf_extract': {'type': 'localized', 'weight': 'C'},
}
#: An internal id for references (not public)
#: A nice id for the url, readable by humans
#: Describes the document briefly
#: Describes the document in detail
[docs]
pdf = associated(
DocumentFormFile, 'pdf', 'one-to-one', uselist=False,
backref_suffix='pdf')
# The collection name this model should appear in
#: The normalized title for sorting
@observes('pdf')
[docs]
def pdf_observer(self, pdf: DocumentFormFile | None) -> None:
self.pdf_extract = pdf.extract if pdf is not None else None
@observes('title')
[docs]
class FormDocumentCollection(GenericCollection[FormDocument]):
def __init__(
self,
session: Session,
member_of: str | None = None,
group: str | None = None,
type: str | None = None
):
super().__init__(session)
@staticmethod
[docs]
def translatable_name(model_class: type[object]) -> str:
""" Most collections have a base model whose name can be guessed
from the collection name. """
name = model_class.__name__.lower()
name = name.replace('collection', '').rstrip('s')
return f'{name.capitalize()}s'
[docs]
def form_choices(self) -> tuple[tuple[str, str], ...]:
if self.type in self.supported_collections:
collection = self.supported_collections[self.type]
return (
(collection.__name__, self.translatable_name(collection)),
)
else:
return tuple(
(m.__name__, self.translatable_name(m))
for m in self.supported_collections.values()
)
[docs]
def by_name(self, name: str) -> FormDocument | None:
""" Returns the given form by name or None. """
return self.query().filter(FormDocument.name == name).first()
@classmethod
[docs]
def collection_by_name(cls) -> dict[str, type[GenericCollection[Any]]]:
return {m.__name__: m for m in cls.supported_collections.values()}
@property
@classmethod
[docs]
def target(
cls,
external_link: FormDocument
) -> type[GenericCollection[Any]]:
assert external_link.member_of is not None
return cls.collection_by_name()[external_link.member_of]