Skip to content

Commit

Permalink
#148: Map distinct entities to attribute names
Browse files Browse the repository at this point in the history
- This data will be used later on in the function to filter the distinct fields from the rest of the data
  • Loading branch information
MRichards99 committed Sep 15, 2020
1 parent 9a1b060 commit e55fbeb
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions common/icat/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def datetime_object_to_str(self, date_obj):
"""
return date_obj.replace(tzinfo=None).strftime(Constants.ACCEPTED_DATE_FORMAT)

def entity_to_dict(self, entity, includes):
def entity_to_dict(self, entity, includes, distinct_fields=None):
"""
This expands on Python ICAT's implementation of `icat.entity.Entity.as_dict()`
to use set operators to create a version of the entity as a dictionary
Expand All @@ -256,6 +256,17 @@ def entity_to_dict(self, entity, includes):
# Split up the fields separated by dots and flatten the resulting lists
flat_includes = [m for n in (field.split(".") for field in includes) for m in n]

# Mapping which entities have distinct fields
distinct_field_dict = {}
for field in distinct_fields:
split_fields = field.split(".")
try:
distinct_field_dict[split_fields[-2]]
except KeyError:
distinct_field_dict[split_fields[-2]] = []

distinct_field_dict[split_fields[-2]].append(split_fields[-1])

# Verifying that `flat_includes` only has fields which are related to the entity
include_set = (entity.InstRel | entity.InstMRel) & set(flat_includes)
for key in entity.InstAttr | entity.MetaAttr | include_set:
Expand All @@ -271,12 +282,12 @@ def entity_to_dict(self, entity, includes):
" cause an issue further on in the request"
)
if isinstance(target, Entity):
d[key] = self.entity_to_dict(target, includes_copy)
d[key] = self.entity_to_dict(target, includes_copy, distinct_fields)
# Related fields with one-many relationships are stored as EntityLists
elif isinstance(target, EntityList):
d[key] = []
for e in target:
d[key].append(self.entity_to_dict(e, includes_copy))
d[key].append(self.entity_to_dict(e, includes_copy, distinct_fields))
# Add actual piece of data to the dictionary
else:
entity_data = getattr(entity, key)
Expand Down

0 comments on commit e55fbeb

Please sign in to comment.