Source code for swissvotes.layouts.page

from functools import cached_property
from onegov.core.elements import Link
from onegov.core.elements import LinkGroup
from onegov.swissvotes import _
from onegov.swissvotes.collections import SwissVoteCollection
from onegov.swissvotes.layouts.default import DefaultLayout
from pathlib import Path


from typing import TYPE_CHECKING
from typing import NamedTuple
if TYPE_CHECKING:
    from onegov.swissvotes.models import TranslatablePage
    from onegov.swissvotes.models import TranslatablePageFile
    from onegov.swissvotes.request import SwissvotesRequest


[docs] class Slide(NamedTuple):
[docs] image: str
[docs] label: str
[docs] url: str
[docs] class PageLayout(DefaultLayout): if TYPE_CHECKING:
[docs] model: TranslatablePage
def __init__( self, model: TranslatablePage, request: SwissvotesRequest ) -> None: ... @cached_property
[docs] def title(self) -> str: return self.model.title or ''
@cached_property @cached_property
[docs] def breadcrumbs(self) -> list[Link]: if self.model.id == 'home': return [Link(_('Homepage'), self.homepage_url)] return [ Link(_('Homepage'), self.homepage_url), Link(self.title, '#'), ]
[docs] def get_file_url(self, file: 'TranslatablePageFile') -> str: lang = file.locale.split('_')[0] if 'QUELLEN' in file.filename or 'REFERENCES' in file.filename: return self.request.link( self.model, name=f'kurzbeschreibung-{lang}.pdf') if 'CODEBOOK' in file.filename: return self.request.link(self.model, name=f'codebook-{lang}.pdf') if 'DATASET CSV' in file.filename: return self.request.link(self.model, name='swissvotes_dataset.csv') if 'DATASET XLSX' in file.filename: return self.request.link( self.model, name='swissvotes_dataset.xlsx') return self.request.link(file)
@cached_property
[docs] def slides(self) -> list[Slide]: votes = SwissVoteCollection(self.app) result = [] for image in self.model.slider_images: bfs_number = Path(image.filename).stem.split('-', 1)[0] vote = votes.by_bfs_number(bfs_number) result.append( Slide( image=self.request.link(image), label=vote.title if vote else image.filename, url=self.request.link(vote) if vote else '' ) ) return result
[docs] class AddPageLayout(DefaultLayout): @cached_property
[docs] def title(self) -> str: return _('Add page')
@cached_property
[docs] def breadcrumbs(self) -> list[Link]: return [ Link(_('Homepage'), self.homepage_url), Link(self.title, '#'), ]
[docs] class PageDetailLayout(DefaultLayout): if TYPE_CHECKING:
[docs] model: TranslatablePage
def __init__( self, model: TranslatablePage, request: SwissvotesRequest ) -> None: ... @cached_property
[docs] def breadcrumbs(self) -> list[Link]: return [ Link(_('Homepage'), self.homepage_url), Link(self.model.title, self.request.link(self.model)), Link(self.title, '#'), ]
[docs] class EditPageLayout(PageDetailLayout): @cached_property
[docs] def title(self) -> str: return _('Edit page')
[docs] class DeletePageLayout(PageDetailLayout): @cached_property
[docs] def title(self) -> str: return _('Delete page')
[docs] class ManagePageAttachmentsLayout(PageDetailLayout): @cached_property
[docs] def title(self) -> str: return _('Manage attachments')
[docs] class ManagePageSliderImagesLayout(PageDetailLayout): @cached_property
[docs] def title(self) -> str: return _('Manage slider images')
[docs] class DeletePageAttachmentLayout(DefaultLayout): if TYPE_CHECKING:
[docs] model: TranslatablePageFile
def __init__( self, model: TranslatablePageFile, request: SwissvotesRequest ) -> None: ... @cached_property
[docs] def title(self) -> str: return _('Delete attachment')
@cached_property
[docs] def parent(self) -> 'TranslatablePage': return self.model.linked_swissvotes_page[0]
@cached_property
[docs] def breadcrumbs(self) -> list[Link]: return [ Link(_('Homepage'), self.homepage_url), Link(self.parent.title, self.request.link(self.parent)), Link(self.title, '#'), ]