Skip to content

Commit 24aae53

Browse files
committed
[Fixes #12957] Metadata editor: customizable title and description labels
1 parent 9980edd commit 24aae53

File tree

5 files changed

+54
-29
lines changed

5 files changed

+54
-29
lines changed

geonode/metadata/handlers/abstract.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,14 @@ class MetadataHandler(metaclass=ABCMeta):
3535
"""
3636

3737
@abstractmethod
38-
def update_schema(self, jsonschema: dict, context, lang=None):
38+
def update_schema(self, jsonschema: dict, context: dict, lang=None):
3939
"""
4040
It is called by the MetadataManager when creating the JSON Schema
4141
It adds the subschema handled by the handler, and returns the
4242
augmented instance of the JSON Schema.
43+
Context is populated by the manager with some common info:
44+
- key "labels": contains the localized label loaded from the db as a dict, where key is the ThesaurusKeyword about
45+
and value is the localized ThesaurusKeywordLabel, or the AltLabel if the localized label does not exist.
4346
"""
4447
pass
4548

@@ -58,7 +61,7 @@ def update_resource(
5861
self, resource: ResourceBase, field_name: str, json_instance: dict, context: dict, errors: dict, **kwargs
5962
):
6063
"""
61-
Called when persisting data, updates the field field_name of the resource
64+
Called when persisting data, updates the field `field_name` of the resource
6265
with the content content, where json_instance is the full JSON Schema instance,
6366
in case the handler needs some cross related data contained in the resource.
6467
"""
@@ -135,15 +138,22 @@ def _set_error(errors: dict, path: list, msg: str):
135138

136139
@staticmethod
137140
def _localize_label(context, lang: str, text: str):
138-
# Try localization via thesaurus:
139-
label = context["labels"].get(text, None)
140-
# fallback: gettext()
141-
if not label:
142-
label = _(text)
141+
label = MetadataHandler._get_tkl_labels(context, lang, text)
142+
return label if label else _(text)
143143

144-
return label
144+
@staticmethod
145+
def _get_tkl_labels(context, lang: str, text: str):
146+
return context["labels"].get(text, None)
145147

146148
@staticmethod
147-
def _localize_subschema_label(context, subschema: dict, lang: str, annotation_name: str):
148-
if annotation_name in subschema:
149-
subschema[annotation_name] = MetadataHandler._localize_label(context, lang, subschema[annotation_name])
149+
def _localize_subschema_labels(context, subschema: dict, lang: str, property_name: str = None):
150+
for annotation_name, synt in (
151+
("title", ""),
152+
("description", "__descr"),
153+
):
154+
if annotation_name in subschema:
155+
subschema[annotation_name] = MetadataHandler._localize_label(context, lang, subschema[annotation_name])
156+
elif property_name: # arrays may not have a name
157+
label = MetadataHandler._get_tkl_labels(context, lang, f"{property_name}{synt}")
158+
if label:
159+
subschema[annotation_name] = label

geonode/metadata/handlers/base.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,7 @@ def update_schema(self, jsonschema, context, lang=None):
184184
self.base_schema = json.load(f)
185185
# building the full base schema
186186
for property_name, subschema in self.base_schema.items():
187-
self._localize_subschema_label(context, subschema, lang, "title")
188-
self._localize_subschema_label(context, subschema, lang, "description")
187+
self._localize_subschema_labels(context, subschema, lang, property_name)
189188

190189
jsonschema["properties"][property_name] = subschema
191190

geonode/metadata/handlers/sparse.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,24 +49,23 @@ class SparseHandler(MetadataHandler):
4949
Handles sparse in fields in the SparseField table
5050
"""
5151

52-
def _recurse_localization(self, context, schema, lang):
53-
self._localize_subschema_label(context, schema, lang, "title")
54-
self._localize_subschema_label(context, schema, lang, "description")
52+
def _recurse_localization(self, context, schema, lang, field_name=None):
53+
self._localize_subschema_labels(context, schema, lang, field_name)
5554

5655
match schema["type"]:
5756
case "object":
58-
for subschema in schema["properties"].values():
59-
self._recurse_localization(context, subschema, lang)
57+
for prop_name, subschema in schema["properties"].items():
58+
self._recurse_localization(context, subschema, lang, prop_name)
6059
case "array":
61-
self._recurse_localization(context, schema["items"], lang)
60+
self._recurse_localization(context, schema["items"], lang, None)
6261
case _:
6362
pass
6463

6564
def update_schema(self, jsonschema, context, lang=None):
6665
# add all registered fields
6766
for field_name, field_info in sparse_field_registry.fields().items():
6867
subschema = copy.deepcopy(field_info["schema"])
69-
self._recurse_localization(context, subschema, lang)
68+
self._recurse_localization(context, subschema, lang, field_name)
7069
self._add_subschema(jsonschema, field_name, subschema, after_what=field_info["after"])
7170

7271
# add the handler info to the dictionary if it doesn't exist

geonode/metadata/i18n.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,41 @@ def get_localized_tkeywords(lang, thesaurus_identifier: str):
1515
" tk.id,"
1616
" tk.about,"
1717
" tk.alt_label,"
18-
" tkl.label"
18+
" tkl.label,"
19+
" tkl.lang"
1920
" from"
2021
" base_thesaurus th,"
2122
" base_thesauruskeyword tk"
2223
" left outer join "
2324
" (select keyword_id, lang, label from base_thesauruskeywordlabel"
24-
" where lang = %s) as tkl"
25+
" where lang like %s) as tkl"
2526
" on (tk.id = tkl.keyword_id)"
2627
" where th.identifier = %s"
2728
" and tk.thesaurus_id = th.id"
2829
" order by label, alt_label"
2930
)
30-
ret = []
31+
ret = {}
32+
ovr = {}
3133
with connection.cursor() as cursor:
32-
cursor.execute(query, [lang, thesaurus_identifier])
33-
for id, about, alt, label in cursor.fetchall():
34-
ret.append({"id": id, "about": about, "label": label or alt})
35-
return sorted(ret, key=lambda i: i["label"].lower())
34+
cursor.execute(query, [f"{lang}%", thesaurus_identifier])
35+
for id, about, alt, label, dblang in cursor.fetchall():
36+
if not dblang or dblang == lang:
37+
# this is a properly localized label or an altlabel (when dblang is null)
38+
ret[id] = {"id": id, "about": about, "label": label or alt}
39+
elif dblang and dblang.endswith("-ovr"):
40+
# store overrides to be applied later
41+
ovr[id] = {"id": id, "about": about, "label": label or alt}
42+
else:
43+
logger.warning(f"Found unexpected lang {dblang}")
44+
for ovr_id, ovr_row in ovr.items(): # apply overrides
45+
if ovr_id in ret:
46+
logger.debug(f"overriding TK {ret[ovr_id]['about']}")
47+
ret[ovr_id]["label"] = ovr_row["label"]
48+
else:
49+
logger.debug(f"Setting ovr TK {ovr_row}")
50+
ret[ovr_id] = ovr_row
51+
52+
return sorted(ret.values(), key=lambda i: i["label"].lower())
3653

3754

3855
def get_localized_labels(lang, key="about"):

geonode/metadata/settings.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import os
2-
from geonode.settings import PROJECT_ROOT
2+
from geonode.settings import PROJECT_ROOT, SITEURL
33

44
MODEL_SCHEMA = {
55
"$schema": "https://json-schema.org/draft/2020-12/schema",
6-
"$id": "{GEONODE_SITE}/resource.json",
6+
"$id": f"{SITEURL}/resource.json",
77
"title": "GeoNode resource",
88
"type": "object",
99
"properties": {},

0 commit comments

Comments
 (0)