from babel import Locale
from decimal import Decimal
from decimal import ROUND_HALF_UP
from functools import cached_property
from markupsafe import Markup
from numbers import Integral
from numbers import Number
from onegov.core.elements import Link
from onegov.core.elements import LinkGroup
from onegov.core.i18n import SiteLocale
from onegov.core.layout import ChameleonLayout
from onegov.core.static import StaticFile
from onegov.swissvotes import _
from onegov.swissvotes.collections import SwissVoteCollection
from onegov.swissvotes.collections import TranslatablePageCollection
from onegov.swissvotes.models import TranslatablePageMove
from onegov.user import Auth
from typing import Any
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from onegov.swissvotes.app import SwissvotesApp
from onegov.swissvotes.models import SwissVote
from onegov.swissvotes.request import SwissvotesRequest
[docs]
class DefaultLayout(ChameleonLayout):
[docs]
request: 'SwissvotesRequest'
def __init__(self, model: Any, request: 'SwissvotesRequest') -> None:
super().__init__(model, request)
self.request.include('frameworks')
self.request.include('chosen')
self.request.include('common')
if 'swissvotes.ch' in request.url:
self.request.include('stats')
[docs]
self.pages = TranslatablePageCollection(self.request.session)
@cached_property
[docs]
def title(self) -> str:
return ''
@cached_property
[docs]
def top_navigation(self) -> list[Link]:
result = [
Link(
page.title,
self.request.link(page),
sortable_id=page.id,
)
for page in self.pages.query()
if page.id not in self.request.app.static_content_pages
]
result.insert(0, Link(_('Votes'), self.votes_url))
return result
@cached_property
[docs]
def editbar_links(self) -> list[Link | LinkGroup]:
return []
@cached_property
[docs]
def breadcrumbs(self) -> list[Link]:
return [Link(_('Homepage'), self.homepage_url)]
@cached_property
[docs]
def static_path(self) -> str:
return self.request.link(self.app.principal, 'static')
@cached_property
[docs]
def sentry_init_path(self) -> str:
static_file = StaticFile.from_application(
self.app, 'sentry/js/sentry-init.js'
)
return self.request.link(static_file)
@cached_property
[docs]
def homepage_url(self) -> str:
return self.request.link(self.app.principal)
@cached_property
[docs]
def disclaimer_link(self) -> Link:
page = self.pages.setdefault('disclaimer')
return Link(page.title, self.request.link(page))
@cached_property
[docs]
def imprint_link(self) -> Link:
page = self.pages.setdefault('imprint')
return Link(page.title, self.request.link(page))
@cached_property
[docs]
def data_protection_link(self) -> Link:
page = self.pages.setdefault('data-protection')
return Link(page.title, self.request.link(page))
@cached_property
[docs]
def votes_url(self) -> str:
return self.request.link(SwissVoteCollection(self.request.app))
@cached_property
[docs]
def login_url(self) -> str | None:
if not self.request.is_logged_in:
return self.request.link(
Auth.from_request(self.request, to=self.homepage_url),
name='login'
)
return None
@cached_property
[docs]
def logout_url(self) -> str | None:
if self.request.is_logged_in:
return self.request.link(
Auth.from_request(self.request, to=self.homepage_url),
name='logout'
)
return None
@cached_property
[docs]
def move_page_url_template(self) -> str:
return self.csrf_protected_url(
self.request.class_link(
TranslatablePageMove,
{
'subject_id': '{subject_id}',
'target_id': '{target_id}',
'direction': '{direction}'
}
)
)
@cached_property
[docs]
def locales(self) -> list[tuple[str, str, str, str]]:
result = []
assert self.app.locales == {'de_CH', 'fr_CH', 'en_US'}
for locale_code in ('de_CH', 'fr_CH', 'en_US'):
locale = Locale.parse(locale_code)
language_name = locale.get_language_name() or locale.language
result.append((
locale_code,
locale.language,
language_name.capitalize(),
SiteLocale(locale_code).link(self.request, self.request.url)
))
return result