From 2a648c91fb52f6612e4851c2c35d21f83ee62559 Mon Sep 17 00:00:00 2001 From: Alexander Watzinger Date: Fri, 22 Sep 2023 14:35:28 +0200 Subject: [PATCH] Resolving/reducing imports at localization --- openatlas/__init__.py | 5 ++-- openatlas/display/util.py | 5 +--- openatlas/models/cidoc_class.py | 22 ++++++----------- openatlas/models/cidoc_property.py | 38 ++++++++++-------------------- 4 files changed, 24 insertions(+), 46 deletions(-) diff --git a/openatlas/__init__.py b/openatlas/__init__.py index 97438a874..92a75bf56 100644 --- a/openatlas/__init__.py +++ b/openatlas/__init__.py @@ -29,6 +29,7 @@ file, hierarchy, index, imports, link, login, model, note, overlay, profile, search, sql, type as type_, user, vocabs) + @babel.localeselector def get_locale() -> str: if 'language' in session: @@ -55,8 +56,8 @@ def before_request() -> None: g.cursor = g.db.cursor(cursor_factory=extras.DictCursor) g.settings = Settings.get_settings() session['language'] = get_locale() - g.cidoc_classes = CidocClass.get_all() - g.properties = CidocProperty.get_all() + g.cidoc_classes = CidocClass.get_all(session['language']) + g.properties = CidocProperty.get_all(session['language']) g.classes = OpenatlasClass.get_all() g.types = Type.get_all() g.reference_systems = ReferenceSystem.get_all() diff --git a/openatlas/display/util.py b/openatlas/display/util.py index 7ab463d7c..fa89332d5 100644 --- a/openatlas/display/util.py +++ b/openatlas/display/util.py @@ -739,10 +739,7 @@ def datetime64_to_timestamp(date: Optional[numpy.datetime64]) -> Optional[str]: def get_entities_linked_to_type_recursive( id_: int, data: list[Entity]) -> list[Entity]: - for entity in g.types[id_].get_linked_entities( - ['P2', 'P89'], - inverse=True, - types=True): + for entity in g.types[id_].get_linked_entities(['P2', 'P89'], True, True): data.append(entity) for sub_id in g.types[id_].subs: get_entities_linked_to_type_recursive(sub_id, data) diff --git a/openatlas/models/cidoc_class.py b/openatlas/models/cidoc_class.py index 1f4c95a08..fa8c683b4 100644 --- a/openatlas/models/cidoc_class.py +++ b/openatlas/models/cidoc_class.py @@ -11,7 +11,7 @@ class CidocClass: def __init__(self, data: dict[str, Any]) -> None: - self._name = data['name'] + self.name = data['name'] self.code = data['code'] self.id = data['id'] self.comment = data['comment'] @@ -20,25 +20,17 @@ def __init__(self, data: dict[str, Any]) -> None: self.sub: list[CidocClass] = [] self.super: list[CidocClass] = [] - @property - def name(self) -> str: - return self.get_i18n() - - def get_i18n(self) -> str: - from openatlas import get_locale - name = getattr(self, '_name') - if get_locale() in self.i18n: - name = self.i18n[get_locale()] - elif g.settings['default_language'] in self.i18n: - name = self.i18n[g.settings['default_language']] - return name - @staticmethod - def get_all() -> dict[str, CidocClass]: + def get_all(language: str) -> dict[str, CidocClass]: classes = {row['code']: CidocClass(row) for row in Db.get_classes()} for row in Db.get_hierarchy(): classes[row['super_code']].sub.append(row['sub_code']) classes[row['sub_code']].super.append(row['super_code']) for row in Db.get_translations(app.config['LANGUAGES']): classes[row['class_code']].i18n[row['language_code']] = row['text'] + for class_ in classes.values(): + if language in class_.i18n: + class_.name = class_.i18n[language] + elif g.settings['default_language'] in class_.i18n: + class_.name = class_.i18n[g.settings['default_language']] return classes diff --git a/openatlas/models/cidoc_property.py b/openatlas/models/cidoc_property.py index 341a70ff7..f859218f6 100644 --- a/openatlas/models/cidoc_property.py +++ b/openatlas/models/cidoc_property.py @@ -12,8 +12,8 @@ class CidocProperty: def __init__(self, data: dict[str, Any]) -> None: self.id = data['id'] - self._name = data['name'] - self._name_inverse = data['name_inverse'] + self.name = data['name'] + self.name_inverse = data['name_inverse'] self.code = data['code'] self.comment = data['comment'] self.domain_class_code = data['domain_class_code'] @@ -24,28 +24,6 @@ def __init__(self, data: dict[str, Any]) -> None: self.i18n: dict[str, str] = {} self.i18n_inverse: dict[str, str] = {} - @property - def name(self) -> str: - from openatlas import get_locale - locale_session = get_locale() - name = getattr(self, '_name') - if locale_session in self.i18n: - name = self.i18n[locale_session] - elif g.settings['default_language'] in self.i18n: - name = self.i18n[g.settings['default_language']] - return name - - @property - def name_inverse(self) -> str: - from openatlas import get_locale - locale_session = get_locale() - name = getattr(self, '_name_inverse') - if locale_session in self.i18n_inverse: - name = self.i18n_inverse[locale_session] - elif g.settings['default_language'] in self.i18n_inverse: - name = self.i18n_inverse[g.settings['default_language']] - return name - def find_object(self, attr: str, class_id: int) -> bool: valid_domain_id = getattr(self, attr) if valid_domain_id == class_id: @@ -70,7 +48,7 @@ def find_subs( return False @staticmethod - def get_all() -> dict[str, CidocProperty]: + def get_all(language: str) -> dict[str, CidocProperty]: properties = { row['code']: CidocProperty(row) for row in Db.get_properties()} for row in Db.get_hierarchy(): @@ -81,4 +59,14 @@ def get_all() -> dict[str, CidocProperty]: row['text'] properties[row['property_code']].i18n_inverse[ row['language_code']] = row['text_inverse'] + for property_ in properties.values(): + default = g.settings['default_language'] + if language in property_.i18n: + property_.name = property_.i18n[language] + elif default in property_.i18n: + property_.name = property_.i18n[default] + if language in property_.i18n_inverse: + property_.name_inverse = property_.i18n_inverse[language] + elif default in property_.i18n_inverse: + property_.name_inverse = property_.i18n_inverse[default] return properties