Skip to content

Commit

Permalink
fix: use alternative ICAT mapping for Sample pid when pid is None #314
Browse files Browse the repository at this point in the history
  • Loading branch information
VKTB committed Feb 4, 2022
1 parent c96a34a commit 7e211f7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
36 changes: 23 additions & 13 deletions datagateway_api/src/search_api/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,33 @@ def apply_filter(self, query):
panosc_mapping_name, field_name,
)

# An edge case for ICAT has been somewhat hardcoded here, to deal with
# ICAT's different parameter value field names. The following mapping is
# assumed (where order matters):
# {"Parameter": {"value": ["numericValue", "stringValue", "dateTimeValue"]}}
# Edge cases for ICAT have been somewhat hardcoded here, to deal with
# ICAT's different parameter value and sample pid field names.
if isinstance(icat_field_name, list):
if isinstance(self.value, int) or isinstance(self.value, float):
icat_field_name = icat_field_name[0]
elif isinstance(self.value, datetime):
icat_field_name = icat_field_name[2]
elif isinstance(self.value, str):
if DateHandler.is_str_a_date(self.value):
# The following mapping is assumed for parameter value (where order
# matters):
# {"Parameter": {"value": ["numericValue", "stringValue", "dateTimeValue"]}} # noqa: B950
if field_name == "value":
if isinstance(self.value, int) or isinstance(self.value, float):
icat_field_name = icat_field_name[0]
elif isinstance(self.value, datetime):
icat_field_name = icat_field_name[2]
elif isinstance(self.value, str):
if DateHandler.is_str_a_date(self.value):
icat_field_name = icat_field_name[2]
else:
icat_field_name = icat_field_name[1]
else:
self.value = str(self.value)
icat_field_name = icat_field_name[1]
else:
self.value = str(self.value)
icat_field_name = icat_field_name[1]
# The following mapping is assumed for sample pid (where order matters):
# {"Sample": {"pid": ["pid", "id"]}}
elif field_name == "pid":
if "pid:" in self.value:
icat_field_name = icat_field_name[1]
self.value = self.value.replace("pid:", "")
else:
icat_field_name = icat_field_name[0]

icat_field_names.append(icat_field_name)

Expand Down
4 changes: 4 additions & 0 deletions datagateway_api/src/search_api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,10 @@ class Sample(PaNOSCAttribute):

datasets: Optional[List[Dataset]] = []

@validator("pid", pre=True, always=True)
def set_pid(cls, value): # noqa: B902, N805
return f"pid:{value}" if isinstance(value, int) else value

@classmethod
def from_icat(cls, icat_data, required_related_fields):
return super(Sample, cls).from_icat(icat_data, required_related_fields)
Expand Down

0 comments on commit 7e211f7

Please sign in to comment.