Skip to content

Commit 088dfa6

Browse files
authored
Merge pull request #793 from hubmapconsortium/karlburke/NewProvMetadataEndpoint
Initial #781 changes, exclude nested fields from public responses
2 parents 44a1717 + 9c3e602 commit 088dfa6

File tree

4 files changed

+41
-18
lines changed

4 files changed

+41
-18
lines changed

src/app.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -797,13 +797,6 @@ def get_entity_by_id(id):
797797
# Response with the dict
798798
if public_entity and not user_in_hubmap_read_group(request):
799799
final_result = schema_manager.exclude_properties_from_response(fields_to_exclude, final_result)
800-
if normalized_entity_type == 'Collection':
801-
for i, dataset in enumerate(final_result.get('datasets', [])):
802-
if _get_entity_visibility(normalized_entity_type='Dataset', entity_dict=dataset) != DataVisibilityEnum.PUBLIC or user_in_hubmap_read_group(request):
803-
# If the dataset is non-public, or if the user has read-group access, there is no need to remove fields, continue to the next dataset
804-
continue
805-
dataset_excluded_fields = schema_manager.get_fields_to_exclude('Dataset')
806-
final_result.get('datasets')[i] = schema_manager.exclude_properties_from_response(dataset_excluded_fields, dataset)
807800
return jsonify(final_result)
808801

809802
"""

src/dev_entity_worker.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -329,15 +329,6 @@ def _get_entity_by_id_for_auth_level(self, entity_id:Annotated[str, 32], valid_u
329329
#if public_entity and not user_in_hubmap_read_group(request):
330330
if public_entity and not user_authorized:
331331
final_result = self.schemaMgr.exclude_properties_from_response(fields_to_exclude, final_result)
332-
if normalized_entity_type == 'Collection':
333-
for i, dataset in enumerate(final_result.get('datasets', [])):
334-
if self._get_entity_visibility( entity_dict=dataset) != DataVisibilityEnum.PUBLIC \
335-
or user_authorized: # or user_in_hubmap_read_group(request):
336-
# If the dataset is public, or if the user has read-group access, there is
337-
# no need to remove fields, continue to the next dataset
338-
continue
339-
dataset_excluded_fields = self.schemaMgr.get_fields_to_exclude('Dataset')
340-
final_result.get('datasets')[i] = self.schemaMgr.exclude_properties_from_response(dataset_excluded_fields, dataset)
341332
return final_result
342333

343334
'''

src/schema/provenance_schema.yaml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,11 @@ shared_entity_properties: &shared_entity_properties
192192
ENTITIES:
193193
############################################# Collection #############################################
194194
Collection:
195+
excluded_properties_from_public_response:
196+
- datasets:
197+
- lab_dataset_id
198+
- metadata:
199+
- lab_id
195200
# Collection can not be derivation source but not target
196201
derivation:
197202
source: false
@@ -304,6 +309,15 @@ ENTITIES:
304309
- lab_dataset_id
305310
- metadata:
306311
- lab_id
312+
- direct_ancestors:
313+
# Sample ancestors of a Dataset must have these fields removed
314+
- lab_tissue_sample_id
315+
- submission_id
316+
# Dataset ancestors of a Dataset must have these fields removed
317+
- lab_dataset_id
318+
# Both Sample and Dataset ancestors of a Dataset must have these fields removed
319+
- metadata:
320+
- lab_id
307321
derivation:
308322
source: true
309323
target: true
@@ -870,6 +884,18 @@ ENTITIES:
870884
excluded_properties_from_public_response:
871885
- lab_tissue_sample_id
872886
- submission_id
887+
- metadata:
888+
- lab_id
889+
- direct_ancestor:
890+
# Donor ancestors of a Sample must have these fields removed
891+
- lab_donor_id
892+
- label
893+
# Sample ancestors of a Sample must have these fields removed
894+
- lab_tissue_sample_id
895+
- metadata:
896+
- lab_id
897+
# Both Sample and Donor ancestors of a Sample must have these fields removed
898+
- submission_id
873899
properties:
874900
<<: *shared_properties
875901
<<: *shared_entity_properties

src/schema/schema_manager.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,13 +306,26 @@ def delete_nested_field(data, nested_path):
306306
if isinstance(value, list):
307307
for nested_field in value:
308308
if isinstance(nested_field, dict):
309-
delete_nested_field(data[key], nested_field)
309+
if isinstance(data[key], list):
310+
for item in data[key]:
311+
delete_nested_field(item, nested_field)
312+
else:
313+
delete_nested_field(data[key], nested_field)
314+
elif isinstance(data[key], list):
315+
for item in data[key]:
316+
if nested_field in item:
317+
del item[nested_field]
310318
elif nested_field in data[key]:
311319
del data[key][nested_field]
312320
elif isinstance(value, dict):
313321
delete_nested_field(data[key], value)
314322
elif nested_path in data:
315-
del data[nested_path]
323+
if isinstance(data[nested_path], list):
324+
for item in data[nested_path]:
325+
if nested_path in item:
326+
del item[nested_path]
327+
else:
328+
del data[nested_path]
316329

317330
for field in excluded_fields:
318331
delete_nested_field(output_dict, field)

0 commit comments

Comments
 (0)