from functools import cached_property
from markupsafe import Markup
from onegov.core.layout import ChameleonLayout
from onegov.core.static import StaticFile
from onegov.gazette import _
from onegov.gazette.collections import CategoryCollection
from onegov.gazette.collections import GazetteNoticeCollection
from onegov.gazette.collections import IssueCollection
from onegov.gazette.collections import OrganizationCollection
from onegov.gazette.models import Issue
from onegov.gazette.models import OrganizationMove
from onegov.gazette.models import Principal
from onegov.user import Auth
from onegov.user import UserCollection
from onegov.user import UserGroupCollection
from sedate import to_timezone
from sedate import utcnow
from typing import Any
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from chameleon import PageTemplateFile
from datetime import date
from datetime import datetime
from onegov.gazette.models import GazetteNotice
from onegov.gazette.request import GazetteRequest
from onegov.user import User
from typing import TypeAlias
[docs]
class Layout(ChameleonLayout):
[docs]
request: 'GazetteRequest'
def __init__(self, model: Any, request: 'GazetteRequest') -> None:
super().__init__(model, request)
self.request.include('frameworks')
self.request.include('chosen')
self.request.include('quill')
self.request.include('common')
# FIXME: We don't actually seem to be using any breadcrumbs
[docs]
self.breadcrumbs: list[str] = []
[docs]
self.session = request.session
[docs]
def title(self) -> str:
return ''
@cached_property
[docs]
def principal(self) -> Principal:
return self.request.app.principal
@cached_property
[docs]
def user(self) -> 'User | None':
username = self.request.identity.userid
if username:
return UserCollection(
self.session, username=username
).query().first()
return None
@cached_property
[docs]
def font_awesome_path(self) -> str:
static_file = StaticFile.from_application(
self.app, 'font-awesome/css/font-awesome.min.css'
)
return self.request.link(static_file)
@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 copyright_year(self) -> int:
return utcnow().year
@cached_property
[docs]
def homepage_link(self) -> str:
return self.request.link(self.principal)
@cached_property
[docs]
def manage_users_link(self) -> str:
return self.request.link(UserCollection(self.session))
@cached_property
[docs]
def manage_groups_link(self) -> str:
return self.request.link(UserGroupCollection(self.session))
@cached_property
[docs]
def manage_organizations_link(self) -> str:
return self.request.link(OrganizationCollection(self.session))
@cached_property
[docs]
def manage_categories_link(self) -> str:
return self.request.link(CategoryCollection(self.session))
@cached_property
[docs]
def manage_issues_link(self) -> str:
return self.request.link(IssueCollection(self.session))
@cached_property
[docs]
def manage_notices_link(self) -> str:
return self.request.link(
GazetteNoticeCollection(self.session, state='submitted')
)
@cached_property
[docs]
def dashboard_link(self) -> str:
return self.request.link(self.principal, name='dashboard')
@property
[docs]
def dashboard_or_notices_link(self) -> str:
if self.request.is_secret(self.model):
return self.manage_notices_link
return self.dashboard_link
@cached_property
[docs]
def export_users_link(self) -> str:
return self.request.class_link(UserCollection, name='export')
@cached_property
[docs]
def export_issues_link(self) -> str:
return self.request.class_link(IssueCollection, name='export')
@cached_property
[docs]
def export_organisation_link(self) -> str:
return self.request.class_link(OrganizationCollection, name='export')
@cached_property
[docs]
def export_categories_link(self) -> str:
return self.request.class_link(CategoryCollection, name='export')
@cached_property
[docs]
def login_link(self) -> str | None:
if self.request.is_logged_in:
return None
return self.request.link(
Auth.from_request(self.request, to=self.homepage_link),
name='login'
)
@cached_property
[docs]
def logout_link(self) -> str | None:
if not self.request.is_logged_in:
return None
return self.request.link(
Auth.from_request(self.request, to=self.homepage_link),
name='logout'
)
@cached_property
[docs]
def sortable_url_template(self) -> str:
return self.csrf_protected_url(
self.request.class_link(
OrganizationMove,
{
'subject_id': '{subject_id}',
'target_id': '{target_id}',
'direction': '{direction}'
}
)
)
@cached_property
[docs]
def publishing(self) -> bool:
return self.request.app.principal.publishing
@cached_property
[docs]
def importation(self) -> bool:
return True if self.request.app.principal.sogc_import else False
@property
@property
[docs]
def current_issue(self) -> Issue | None:
return IssueCollection(self.session).current_issue
[docs]
def format_text(self, text: str | None) -> str:
return Markup('<br>').join((text or '').splitlines())
[docs]
class MailLayout(Layout):
""" A special layout for creating HTML E-Mails. """
@cached_property
[docs]
def base(self) -> 'PageTemplateFile':
return self.template_loader['mail_layout.pt']
@cached_property
[docs]
def primary_color(self) -> str:
return self.app.theme_options.get('primary-color', '#fff')