from functools import cached_property
from datetime import date
from onegov.core.elements import Link
from onegov.core.elements import LinkGroup
from onegov.swissvotes import _
from onegov.swissvotes.layouts.default import DefaultLayout
from typing import Any
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from onegov.core.types import RenderData
from onegov.swissvotes.models import SwissVote
from onegov.swissvotes.models import SwissVoteFile
from onegov.swissvotes.request import SwissvotesRequest
[docs]
class VoteLayout(DefaultLayout):
if TYPE_CHECKING:
def __init__(
self,
model: SwissVote,
request: SwissvotesRequest
) -> None: ...
@cached_property
[docs]
def title(self) -> str:
return self.model.short_title
@cached_property
[docs]
def editbar_links(self) -> list[Link | LinkGroup]:
result: list[Link | LinkGroup] = []
if self.request.has_role('admin', 'editor'):
result.append(
Link(
text=_('Manage attachments'),
url=self.request.link(self.model, name='upload'),
attrs={'class': 'upload-icon'}
)
)
result.append(
Link(
text=_('Campaign material'),
url=self.request.link(
self.model, name='manage-campaign-material'
),
attrs={'class': 'upload-icon'}
)
)
result.append(
Link(
text=_('Graphical campaign material for a Yes'),
url=self.request.link(
self.model, name='manage-campaign-material-yea'
),
attrs={'class': 'upload-icon'}
)
)
result.append(
Link(
text=_('Graphical campaign material for a No'),
url=self.request.link(
self.model, name='manage-campaign-material-nay'
),
attrs={'class': 'upload-icon'}
)
)
result.append(
Link(
text=_('Delete vote'),
url=self.request.link(self.model, name='delete'),
attrs={'class': 'delete-icon'}
)
)
return result
@cached_property
[docs]
def breadcrumbs(self) -> list[Link]:
return [
Link(_('Homepage'), self.homepage_url),
Link(_('Votes'), self.votes_url),
Link(self.title, '#'),
]
@cached_property
[docs]
def attachments(self) -> 'RenderData':
""" Returns a dictionary with static URLS and locale for attachments.
Note that only file / locale combinations with a file_name
definition have a static URL!
"""
result: RenderData = {}
for name, file in self.model.localized_files().items():
attachment = self.model.get_file(name)
result[name] = {
'url': self.request.link(
self.model,
file.static_views.get(
attachment.locale,
file.static_views['de_CH']
)
),
'locale': attachment.locale
} if attachment else {}
return result
@cached_property
[docs]
def search_results(
self
) -> list[tuple[int, str, str, bool, 'SwissVoteFile']]:
result = []
metadata = self.model.campaign_material_metadata or {}
labels = {
name: file.label
for name, file in self.model.localized_files().items()
}
codes = self.model.metadata_codes('language')
for file in self.model.search():
name = file.name.split('-')[0]
protected = False
if name in labels:
order = 0
title = self.request.translate(labels[name])
language = self.request.translate(
_((file.language or '').capitalize())
)
elif name == 'campaign_material_other':
data = metadata.get(file.filename.replace('.pdf', ''), {})
order = 1
title = data.get('title') or file.filename
language = ', '.join([
self.request.translate(codes[lang])
for lang in data.get('language', [])
])
protected = 'article' in data.get('doctype', ['article'])
else:
order = 3
title = file.filename
language = ''
result.append((order, title, language, protected, file))
result.sort(key=lambda x: (x[0], (x[1] or '').lower()))
return result
[docs]
class VoteDetailLayout(DefaultLayout):
if TYPE_CHECKING:
def __init__(
self,
model: SwissVote,
request: SwissvotesRequest
) -> None: ...
@cached_property
[docs]
def breadcrumbs(self) -> list[Link]:
return [
Link(_('Homepage'), self.homepage_url),
Link(_('Votes'), self.votes_url),
Link(self.model.short_title, self.request.link(self.model)),
Link(self.title, '#'),
]
[docs]
class VoteStrengthsLayout(VoteDetailLayout):
@cached_property
[docs]
def title(self) -> str:
return _('Voter strengths')
[docs]
class VoteCampaignMaterialLayout(VoteDetailLayout):
@cached_property
[docs]
def title(self) -> str:
return _('Documents from the campaign')
@cached_property
[docs]
def codes(self) -> dict[str, dict[str, str]]:
return {
key: self.model.metadata_codes(key)
for key in ('position', 'language', 'doctype')
}
[docs]
class UploadVoteAttachemtsLayout(VoteDetailLayout):
@cached_property
[docs]
def title(self) -> str:
return _('Manage attachments')
[docs]
class ManageCampaingMaterialLayout(VoteDetailLayout):
@cached_property
[docs]
def title(self) -> str:
return _('Campaign material')
[docs]
class ManageCampaingMaterialYeaLayout(VoteDetailLayout):
@cached_property
[docs]
def title(self) -> str:
return _('Graphical campaign material for a Yes')
[docs]
class ManageCampaingMaterialNayLayout(VoteDetailLayout):
@cached_property
[docs]
def title(self) -> str:
return _('Graphical campaign material for a No')
[docs]
class DeleteVoteLayout(VoteDetailLayout):
@cached_property
[docs]
def title(self) -> str:
return _('Delete vote')
[docs]
class DeleteVoteAttachmentLayout(DefaultLayout):
if TYPE_CHECKING:
def __init__(
self,
model: SwissVoteFile,
request: SwissvotesRequest
) -> None: ...
@cached_property
[docs]
def parent(self) -> 'SwissVote':
return self.model.linked_swissvotes[0]
@cached_property
[docs]
def breadcrumbs(self) -> list[Link]:
return [
Link(_('Homepage'), self.homepage_url),
Link(_('Votes'), self.votes_url),
Link(self.parent.short_title, self.request.link(self.parent)),
Link(self.title, '#'),
]
@cached_property
[docs]
def title(self) -> str:
return _('Delete attachment')