Source code for agency.layout

from functools import cached_property
from itertools import islice
from onegov.agency.collections import ExtendedAgencyCollection
from onegov.agency.collections import ExtendedPersonCollection
from onegov.agency.models import AgencyMembershipMoveWithinAgency
from onegov.agency.models import AgencyMembershipMoveWithinPerson
from onegov.agency.models import AgencyMove
from onegov.core.elements import Block
from onegov.core.elements import Confirm
from onegov.core.elements import Intercooler
from onegov.core.elements import Link
from onegov.core.elements import LinkGroup
from onegov.core.security import Private
from onegov.org import _
from onegov.org.layout import AdjacencyListLayout
from onegov.org.layout import DefaultLayout
from onegov.org.layout import PageLayout as OrgPageLayout
from onegov.org.layout import PersonCollectionLayout
from onegov.org.layout import PersonLayout as OrgPersonLayout


from typing import Any
from typing import TYPE_CHECKING
if TYPE_CHECKING:
    from collections.abc import Collection
    from collections.abc import Iterator
    from collections.abc import Sequence
    from onegov.agency.models import ExtendedAgency
    from onegov.agency.request import AgencyRequest
    from onegov.org.elements import Trait


[docs] class PageLayout(OrgPageLayout): @cached_property
[docs] class PersonLayout(OrgPersonLayout): @cached_property
[docs] class MoveAgencyMixin: if TYPE_CHECKING: request: AgencyRequest def csrf_protected_url(self, url: str) -> str: ... @cached_property
[docs] def move_agency_url_template(self) -> str: return self.csrf_protected_url( self.request.class_link( AgencyMove, { 'subject_id': '{subject_id}', 'target_id': '{target_id}', 'direction': '{direction}' } ) )
[docs] class AgencyCollectionLayout( DefaultLayout, MoveAgencyMixin, NavTreeMixin ):
[docs] request: 'AgencyRequest'
@cached_property
[docs] def breadcrumbs(self) -> list[Link]: return [ Link(_("Homepage"), self.homepage_url), Link(_("Agencies"), self.request.link(self.model)) ]
@cached_property
[docs] class AgencyLayout( AdjacencyListLayout, MoveAgencyMixin ):
[docs] request: 'AgencyRequest'
[docs] def include_editor(self) -> None: self.request.include('redactor') self.request.include('editor')
@cached_property
[docs] def collection(self) -> ExtendedAgencyCollection: return ExtendedAgencyCollection(self.request.session)
@cached_property
[docs] def breadcrumbs(self) -> list[Link]: return [ Link(_("Homepage"), self.homepage_url), Link(_("Agencies"), self.request.link(self.collection)), *islice(self.get_breadcrumbs(self.model), 1, None) ]
@cached_property @cached_property
[docs] def move_membership_within_agency_url_template(self) -> str: return self.csrf_protected_url( self.request.class_link( AgencyMembershipMoveWithinAgency, { 'subject_id': '{subject_id}', 'target_id': '{target_id}', 'direction': '{direction}' } ) )
[docs] class AgencyPathMixin: if TYPE_CHECKING: request: 'AgencyRequest'
[docs] def get_ancestors( self, item: 'ExtendedAgency', with_item: bool = True, levels: 'Collection[int] | None' = None ) -> 'Iterator[Link]': for ix, ancestor in enumerate(item.ancestors, 1): if levels is None: yield Link(ancestor.title, self.request.link(ancestor)) elif ix in levels: yield Link(ancestor.title, self.request.link(ancestor)) if with_item: yield Link(item.title, self.request.link(item))
[docs] def parent_path(self, agency: 'ExtendedAgency') -> str: levels = self.request.app.org.agency_display_levels return ' > '.join( ln.text or ln.title for ln in self.get_ancestors(agency, False, levels) )
[docs] def agency_path(self, agency: 'ExtendedAgency') -> str: return ' > '.join( ln.text or ln.title for ln in self.get_ancestors(agency) )
[docs] class MembershipLayout(DefaultLayout): @cached_property
[docs] def breadcrumbs(self) -> list[Link]: return AgencyLayout(self.model.agency, self.request).breadcrumbs + [ Link(self.model.title, self.request.link(self.model)) ]
@cached_property
[docs] class ExtendedPersonCollectionLayout( PersonCollectionLayout, AgencyPathMixin ):
[docs] request: 'AgencyRequest'
@cached_property
[docs] class ExtendedPersonLayout(PersonLayout, AgencyPathMixin):
[docs] request: 'AgencyRequest'
@cached_property
[docs] def collection(self) -> ExtendedPersonCollection: # type:ignore return ExtendedPersonCollection(self.request.session)
@cached_property
[docs] def move_membership_within_person_url_template(self) -> str: return self.csrf_protected_url( self.request.class_link( AgencyMembershipMoveWithinPerson, { 'subject_id': '{subject_id}', 'target_id': '{target_id}', 'direction': '{direction}' } ) )
@property
[docs] def default_membership_title(self) -> str: return self.request.translate(_('Member'))
[docs] class AgencySearchLayout(DefaultLayout, AgencyPathMixin):
[docs] request: 'AgencyRequest'