Skip to content

Commit

Permalink
Resolving/reducing imports at localization
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderWatzinger committed Sep 22, 2023
1 parent a94daa4 commit 2a648c9
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 46 deletions.
5 changes: 3 additions & 2 deletions openatlas/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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()
Expand Down
5 changes: 1 addition & 4 deletions openatlas/display/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 7 additions & 15 deletions openatlas/models/cidoc_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand All @@ -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
38 changes: 13 additions & 25 deletions openatlas/models/cidoc_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand All @@ -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:
Expand All @@ -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():
Expand All @@ -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

0 comments on commit 2a648c9

Please sign in to comment.