from functools import cached_property
from onegov.election_day import _
from onegov.election_day.collections import DataSourceCollection
from onegov.election_day.collections import DataSourceItemCollection
from onegov.election_day.collections import ElectionCollection
from onegov.election_day.collections import ElectionCompoundCollection
from onegov.election_day.collections import EmailSubscriberCollection
from onegov.election_day.collections import ScreenCollection
from onegov.election_day.collections import SmsSubscriberCollection
from onegov.election_day.collections import SubscriberCollection
from onegov.election_day.collections import UploadTokenCollection
from onegov.election_day.collections import VoteCollection
from onegov.election_day.layouts.default import DefaultLayout
from onegov.election_day.layouts.election import ElectionLayout
from onegov.election_day.layouts.election_compound import (
ElectionCompoundLayout)
from onegov.election_day.layouts.vote import VoteLayout
from onegov.election_day.models import Election
from onegov.election_day.models import ElectionCompound
from onegov.election_day.models import EmailSubscriber
from onegov.election_day.models import SmsSubscriber
from onegov.election_day.models import Vote
from onegov.election_day.utils import pdf_filename
from onegov.election_day.utils import svg_filename
from typing import Any
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from collections.abc import Collection
from onegov.election_day.models import DataSource
from onegov.election_day.models import Subscriber
from onegov.election_day.models import UploadToken
from onegov.election_day.request import ElectionDayRequest
from .election import NestedMenu
[docs]
class ManageLayout(DefaultLayout):
def __init__(self, model: Any, request: 'ElectionDayRequest'):
super().__init__(model, request)
self.request.include('backend_common')
self.request.include('chosen')
[docs]
self.breadcrumbs = [
(_('Manage'), super().manage_link, 'unavailable'),
]
@cached_property
[docs]
def manage_model_link(self) -> str:
return self.request.link(self.model)
@cached_property
[docs]
def title(self) -> str:
try:
return self.breadcrumbs[-1][0]
except (IndexError, TypeError):
return ''
[docs]
class ManageElectionsLayout(ManageLayout):
[docs]
model: Election | ElectionCollection
def __init__(
self,
model: Election | ElectionCollection,
request: 'ElectionDayRequest'
) -> None:
super().__init__(model, request)
self.breadcrumbs.append(
(_('Elections'), request.link(self.model), '')
)
@cached_property
[docs]
def manage_model_link(self) -> str:
return self.request.link(
ElectionCollection(self.request.session)
)
[docs]
class ManageElectionCompoundsLayout(ManageLayout):
[docs]
model: ElectionCompound | ElectionCompoundCollection
def __init__(
self,
model: ElectionCompound | ElectionCompoundCollection,
request: 'ElectionDayRequest'
) -> None:
super().__init__(model, request)
self.breadcrumbs.append(
(_('Compounds of elections'), request.link(self.model), '')
)
@cached_property
[docs]
def manage_model_link(self) -> str:
return self.request.link(
ElectionCompoundCollection(self.request.session)
)
[docs]
class ManageVotesLayout(ManageLayout):
[docs]
model: Vote | VoteCollection
def __init__(
self,
model: Vote | VoteCollection,
request: 'ElectionDayRequest'
) -> None:
super().__init__(model, request)
self.breadcrumbs.append(
(_('Votes'), request.link(self.model), ''),
)
@cached_property
[docs]
def manage_model_link(self) -> str:
return self.request.link(
VoteCollection(self.request.session)
)
[docs]
class ManageSubscribersLayout(ManageLayout):
[docs]
model: 'SubscriberCollection[Any] | Subscriber'
def __init__(
self,
model: 'SubscriberCollection[Any] | Subscriber',
request: 'ElectionDayRequest'
) -> None:
super().__init__(model, request)
if isinstance(self.model, EmailSubscriberCollection):
self.breadcrumbs.append(
(_('Email subscribers'), request.link(self.model), ''),
)
elif isinstance(self.model, SmsSubscriberCollection):
self.breadcrumbs.append(
(_('SMS subscribers'), request.link(self.model), ''),
)
else:
self.breadcrumbs.append(
(_('Subscribers'), request.link(self.model), ''),
)
@cached_property
[docs]
def manage_model_link(self) -> str:
if isinstance(self.model, SubscriberCollection):
return self.request.link(self.model)
if isinstance(self.model, SmsSubscriber):
return self.request.link(
SmsSubscriberCollection(self.request.session)
)
elif isinstance(self.model, EmailSubscriber):
return self.request.link(
EmailSubscriberCollection(self.request.session)
)
raise NotImplementedError()
[docs]
class ManageUploadTokensLayout(ManageLayout):
[docs]
model: 'UploadToken | UploadTokenCollection'
def __init__(
self,
model: 'UploadToken | UploadTokenCollection',
request: 'ElectionDayRequest'
) -> None:
super().__init__(model, request)
self.breadcrumbs.append(
(_('Upload tokens'), request.link(self.model), ''),
)
@cached_property
[docs]
def manage_model_link(self) -> str:
return self.request.link(
UploadTokenCollection(self.request.session)
)
[docs]
class ManageDataSourcesLayout(ManageLayout):
def __init__(self, model: Any, request: 'ElectionDayRequest') -> None:
super().__init__(model, request)
self.breadcrumbs.append(
(_('Wabsti data sources'), request.link(self.model), ''),
)
@cached_property
[docs]
def manage_model_link(self) -> str:
return self.request.link(
DataSourceCollection(self.request.session)
)
[docs]
class ManageDataSourceItemsLayout(ManageLayout):
[docs]
model: 'DataSource | DataSourceItemCollection'
def __init__(
self,
model: 'DataSource | DataSourceItemCollection',
request: 'ElectionDayRequest'
) -> None:
super().__init__(model, request)
self.breadcrumbs.append(
(
_('Wabsti data sources'),
self.request.link(
DataSourceCollection(self.request.session)
),
''
),
)
self.breadcrumbs.append(
(_('Mappings'), request.link(self.model), ''),
)
@cached_property
[docs]
def manage_model_link(self) -> str:
return self.request.link(
DataSourceItemCollection(
self.request.session,
self.model.id
)
)
[docs]
class ManageScreensLayout(ManageLayout):
def __init__(self, model: Any, request: 'ElectionDayRequest') -> None:
super().__init__(model, request)
self.breadcrumbs.append(
(_('Screens'), request.link(self.model), ''),
)
@cached_property
[docs]
def manage_model_link(self) -> str:
return self.request.link(
ScreenCollection(self.request.session)
)