Source code for translator_directory.app

from functools import cached_property
from onegov.core import utils
from onegov.gis import Coordinates
from onegov.translator_directory.initial_content import create_new_organisation
from onegov.town6 import TownApp
from onegov.town6.app import get_common_asset as default_common_asset
from onegov.town6.app import get_i18n_localedirs as get_town_i18n_localedirs
from onegov.org.models import Organisation, GeneralFile, GeneralFileCollection
from onegov.translator_directory.request import TranslatorAppRequest
from onegov.translator_directory.theme import TranslatorDirectoryTheme
from purl import URL
from sqlalchemy import and_


from typing import Any, TYPE_CHECKING
if TYPE_CHECKING:
    from collections.abc import Callable, Iterator
    from morepath.authentication import Identity, NoIdentity
    from onegov.gis.models.coordinates import AnyCoordinates


[docs] class TranslatorDirectoryApp(TownApp):
[docs] send_ticket_statistics = False
[docs] request_class = TranslatorAppRequest
[docs] def configure_organisation( self, *, enable_user_registration: bool = False, enable_yubikey: bool = False, disable_password_reset: bool = False, **cfg: Any ) -> None: super().configure_organisation( enable_user_registration=enable_user_registration, enable_yubikey=enable_yubikey, disable_password_reset=disable_password_reset, **cfg )
@property
[docs] def coordinates(self) -> 'AnyCoordinates': return self.org.meta.get('translator_directory_home') or Coordinates()
@coordinates.setter def coordinates(self, value: 'AnyCoordinates') -> None: self.org.meta['translator_directory_home'] = value or {}
[docs] def redirect_after_login( self, identity: 'Identity | NoIdentity', request: TranslatorAppRequest, # type:ignore[override] default: str ) -> str | None: if default != '/' and '/auth/login' not in str(default): return None return URL(request.class_link(Organisation)).path()
# We deliberately do not use @orm_cached here, because if the user # uploads a new file, it's not immediately visible in the list of # available templates. @property
[docs] def mail_templates(self) -> list[str]: """ Templates are special docx files which are filled with variables. These files are manually uploaded. """ query = GeneralFileCollection(self.session()).query().filter( and_( GeneralFile.name.like('Vorlage%'), GeneralFile.name.like('%.docx') ) ).with_entities(GeneralFile.name) return [filename for filename, in query]
@cached_property
@TranslatorDirectoryApp.template_directory()
[docs] def get_template_directory() -> str: return 'templates'
@TranslatorDirectoryApp.static_directory()
[docs] def get_static_directory() -> str: return 'static'
@TranslatorDirectoryApp.setting(section='core', name='theme')
[docs] def get_theme() -> TranslatorDirectoryTheme: return TranslatorDirectoryTheme()
@TranslatorDirectoryApp.setting(section='org', name='create_new_organisation')
[docs] def get_create_new_organisation_factory( ) -> 'Callable[[TranslatorDirectoryApp, str], Organisation]': return create_new_organisation
@TranslatorDirectoryApp.setting(section='i18n', name='localedirs')
[docs] def get_i18n_localedirs() -> list[str]: mine = utils.module_path('onegov.translator_directory', 'locale') return [mine, *get_town_i18n_localedirs()]
@TranslatorDirectoryApp.webasset_path()
[docs] def get_js_path() -> str: return 'assets/js'
@TranslatorDirectoryApp.webasset_output()
[docs] def get_webasset_output() -> str: return 'assets/bundles'
@TranslatorDirectoryApp.webasset('common')
[docs] def get_common_asset() -> 'Iterator[str]': yield from default_common_asset() yield 'translator_directory.js'