Skip to content

Commit

Permalink
#140: Move create_condition() to PythonICATWhereFilter
Browse files Browse the repository at this point in the history
- This is to remove a circular dependency that I found while implementing an order filter
- Also added a couple of __init__.py files which didn't previously exist
  • Loading branch information
MRichards99 committed Aug 18, 2020
1 parent 19a286b commit 51924f8
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 32 deletions.
Empty file added common/database/__init__.py
Empty file.
Empty file added common/icat/__init__.py
Empty file.
41 changes: 33 additions & 8 deletions common/icat/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
IncludeFilter,
)
from common.exceptions import FilterError
from common.icat.helpers import create_condition

log = logging.getLogger()

Expand All @@ -19,30 +18,56 @@ def __init__(self, field, value, operation):
super().__init__(field, value, operation)

def apply_filter(self, query):
log.info("Creating condition for ICAT where filter")
if self.operation == "eq":
where_filter = create_condition(self.field, "=", self.value)
where_filter = self.create_condition(self.field, "=", self.value)
elif self.operation == "like":
where_filter = create_condition(self.field, "like", self.value)
where_filter = self.create_condition(self.field, "like", self.value)
elif self.operation == "lt":
where_filter = create_condition(self.field, "<", self.value)
where_filter = self.create_condition(self.field, "<", self.value)
elif self.operation == "lte":
where_filter = create_condition(self.field, "<=", self.value)
where_filter = self.create_condition(self.field, "<=", self.value)
elif self.operation == "gt":
where_filter = create_condition(self.field, ">", self.value)
where_filter = self.create_condition(self.field, ">", self.value)
elif self.operation == "gte":
where_filter = create_condition(self.field, ">=", self.value)
where_filter = self.create_condition(self.field, ">=", self.value)
elif self.operation == "in":
where_filter = create_condition(self.field, "in", tuple(self.value))
where_filter = self.create_condition(self.field, "in", tuple(self.value))
else:
raise FilterError(f"Bad operation given to where filter: {self.operation}")

log.debug("ICAT Where Filter: %s", where_filter)
try:
log.info("Adding ICAT where filter to query")
query.addConditions(where_filter)
except ValueError:
raise FilterError(
"Something went wrong when adding WHERE filter to ICAT query"
)

@staticmethod
def create_condition(attribute_name, operator, value):
"""
Construct and return a Python dictionary containing conditions to be used in a
Query object
:param attribute_name: Attribute name to search
:type attribute_name: :class:`str`
:param operator: Operator to use when filtering the data
:type operator: :class:`str`
:param value: What ICAT will use to filter the data
:type value: :class:`str` or :class:`tuple` (when using an IN expression)
:return: Condition (of type :class:`dict`) ready to be added to a Python ICAT
Query object
"""

conditions = {}
# Removing quote marks when doing conditions with IN expressions
jpql_value = f"{value}" if isinstance(value, tuple) else f"'{value}'"
conditions[attribute_name] = f"{operator} {jpql_value}"

return conditions


class PythonICATDistinctFieldFilter(DistinctFieldFilter):
def __init__(self, fields):
Expand Down
26 changes: 2 additions & 24 deletions common/icat/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
)
from common.filter_order_handler import FilterOrderHandler
from common.constants import Constants
from common.icat.filters import PythonICATLimitFilter, PythonICATWhereFilter


log = logging.getLogger()
Expand Down Expand Up @@ -206,29 +207,6 @@ def get_python_icat_entity_name(client, database_table_name):
return python_icat_entity_name


def create_condition(attribute_name, operator, value):
"""
Construct and return a Python dictionary containing conditions to be used in a
Query object
:param attribute_name: Attribute name to search
:type attribute_name: :class:`str`
:param operator: Operator to use when filtering the data
:type operator: :class:`str`
:param value: What ICAT will use to filter the data
:type value: :class:`str` or :class:`tuple` (when using an IN expression)
:return: Condition (of type :class:`dict`) ready to be added to a Python ICAT Query
object
"""

conditions = {}
# Removing quote marks when doing conditions with IN expressions
jpql_value = f"{value}" if isinstance(value, tuple) else f"'{value}'"
conditions[attribute_name] = f"{operator} {jpql_value}"

return conditions


def str_to_datetime_object(icat_attribute, data):
"""
Where data is stored as dates in ICAT (which this function determines), convert
Expand Down Expand Up @@ -321,7 +299,7 @@ def get_entity_by_id(client, table_name, id_, return_json_formattable_data):
"""

# Set query condition for the selected ID
id_condition = create_condition("id", "=", id_)
id_condition = PythonICATWhereFilter.create_condition("id", "=", id_)

selected_entity_name = get_python_icat_entity_name(client, table_name)

Expand Down

0 comments on commit 51924f8

Please sign in to comment.