Skip to content

Commit aafd607

Browse files
committed
Improve Sparse field handler
1 parent b1e7953 commit aafd607

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

geonode/metadata/handlers/doi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,4 @@ def get_jsonschema_instance(self, resource, field_name, context, errors, lang=No
4545
return resource.doi
4646

4747
def update_resource(self, resource, field_name, json_instance, context, errors, **kwargs):
48-
resource.doi = json_instance[field_name]
48+
resource.doi = json_instance.get(field_name, None)

geonode/metadata/handlers/sparse.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,14 @@ def load_serialization_context(self, resource, jsonschema: dict, context: dict):
8888
"schema": jsonschema,
8989
}
9090

91+
@staticmethod
92+
def get_sparse_field(context, fieldname):
93+
return context[CONTEXT_ID]["fields"].get(fieldname, None)
94+
95+
@staticmethod
96+
def set_sparse_field(context, fieldname, value):
97+
context[CONTEXT_ID]["fields"][fieldname] = value
98+
9199
@staticmethod
92100
def _check_type(declared, checked):
93101
return declared == checked or (type(declared) is list and checked in declared)

geonode/metadata/manager.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ def build_schema_instance(self, resource, lang=None):
9292

9393
instance = {}
9494
errors = {}
95+
required = schema.get("required", [])
9596
for fieldname, subschema in schema["properties"].items():
9697
# logger.debug(f"build_schema_instance: getting handler for property {fieldname}")
9798
handler_id = subschema.get("geonode:handler", None)
@@ -101,7 +102,8 @@ def build_schema_instance(self, resource, lang=None):
101102
handler = self.handlers[handler_id]
102103
try:
103104
content = handler.get_jsonschema_instance(resource, fieldname, context, errors, lang)
104-
instance[fieldname] = content
105+
if content is not None or fieldname in required:
106+
instance[fieldname] = content
105107
except UnsetFieldException:
106108
pass
107109

0 commit comments

Comments
 (0)