Skip to content

Commit

Permalink
#143: Add basic implementation of include filter
Browse files Browse the repository at this point in the history
- This filter currently accepts a single entity name, or a list of entity names. Dictionaries are not yet supported
  • Loading branch information
MRichards99 committed Sep 3, 2020
1 parent 32e6927 commit c724f72
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
20 changes: 18 additions & 2 deletions common/icat/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def apply_filter(self, query):
where_filter = self.create_condition(self.field, ">=", self.value)
elif self.operation == "in":
# Convert self.value into a string with brackets equivalent to tuple format.
# Cannot convert straight to tuple as single element tuples contain a
# Cannot convert straight to tuple as single element tuples contain a
# trailing comma which Python ICAT/JPQL doesn't accept
self.value = str(self.value).replace("[", "(").replace("]", ")")
where_filter = self.create_condition(self.field, "in", self.value)
Expand Down Expand Up @@ -160,6 +160,22 @@ def icat_set_limit(query, skip_number, limit_number):
class PythonICATIncludeFilter(IncludeFilter):
def __init__(self, included_filters):
super().__init__(included_filters)
# TODO - Adapt JSON input from request to Python ICAT
# Might end up removing the super constructor call

# Included entities must be in a list
if isinstance(self.included_filters, str):
self.included_filters = [self.included_filters]

# TODO - When a dictionary is used in self.included_filters

def apply_filter(self, query):
pass
log.debug(
f"Included filters: {self.included_filters}, Type: {type(self.included_filters)}"
)

try:
pass
query.addIncludes(self.included_filters)
except ValueError as e:
raise FilterError(e)
38 changes: 38 additions & 0 deletions common/icat/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ def execute_query(self, client, return_json_formattable=False):

try:
query_result = client.search(self.query)
log.debug("Query Result: %s", query_result)
except ICATValidationError as e:
raise PythonICATError(e)

Expand Down Expand Up @@ -182,7 +183,41 @@ def execute_query(self, client, return_json_formattable=False):
dict_result = result.as_dict()
distinct_result = {}

log.debug(f"Result: {result}")
log.debug(f"Dict Result: {dict_result}")

log.debug(
f"Includes: {self.query.includes},"
f" Type: {type(self.query.includes)}"
)

for entity_name in self.query.includes:
included_data = getattr(result, entity_name)
dict_result[entity_name] = []

for included_result in included_data:
# TODO - Test that there can be >1 element in this
dict_result[entity_name].append(included_result.as_dict())

for key in dict_result:
log.debug(f"Key: {key}, Type: {type(key)}")

if isinstance(dict_result[key], list):
for included_result in range(len(dict_result[key])):
for inner_key in dict_result[key][included_result]:
log.debug(f"Inner Key: {inner_key}")
# TODO - Remove duplication
if isinstance(
dict_result[key][included_result][inner_key],
datetime,
):
# Remove timezone data which isn't utilised in ICAT
dict_result[key][included_result][inner_key] = (
dict_result[key][included_result][inner_key]
.replace(tzinfo=None)
.strftime(Constants.ACCEPTED_DATE_FORMAT)
)

# Convert datetime objects to strings so they can be JSON
# serialisable
if isinstance(dict_result[key], datetime):
Expand Down Expand Up @@ -224,6 +259,9 @@ def check_attribute_name_for_distinct(self, key, value):
if value == Constants.PYTHON_ICAT_DISTNCT_CONDITION:
self.attribute_names.append(key)

def make_date_json_serialisable(self, data_dict, more_params_needed):
pass


def get_python_icat_entity_name(client, database_table_name):
"""
Expand Down

0 comments on commit c724f72

Please sign in to comment.