Skip to content

Commit

Permalink
Merge pull request #169 from ral-facilities/feature/where-filter-incl…
Browse files Browse the repository at this point in the history
…uded-columns-#144

Allow WHERE Filter to use Included Fields for Python ICAT Backend
  • Loading branch information
MRichards99 authored Nov 4, 2020
2 parents f4e3e6c + 9a1b060 commit 8e3e161
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 22 deletions.
32 changes: 32 additions & 0 deletions common/database/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,44 @@
from common.models import db_models

from sqlalchemy import asc, desc
import logging

log = logging.getLogger()


class DatabaseWhereFilter(WhereFilter):
def __init__(self, field, value, operation):
super().__init__(field, value, operation)

self.included_field = None
self.included_included_field = None
self._extract_filter_fields(field)

def _extract_filter_fields(self, field):
"""
Extract the related fields names and put them into separate variables
:param field: ICAT field names, separated by dots
:type field: :class:`str`
"""

fields = field.split(".")
include_depth = len(fields)

log.debug("Fields: %s, Include Depth: %d", fields, include_depth)

if include_depth == 1:
self.field = fields[0]
elif include_depth == 2:
self.field = fields[0]
self.included_field = fields[1]
elif include_depth == 3:
self.field = fields[0]
self.included_field = fields[1]
self.included_included_field = fields[2]
else:
raise ValueError(f"Maximum include depth exceeded. {field}'s depth > 3")

def apply_filter(self, query):
try:
field = getattr(query.table, self.field)
Expand Down
24 changes: 2 additions & 22 deletions common/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@ class WhereFilter(QueryFilter):
precedence = 1

def __init__(self, field, value, operation):
# The field is set to None as a precaution but this should be set by the
# individual backend since backends deal with this data differently
self.field = None
self.included_field = None
self.included_included_field = None
self._extract_filter_fields(field)

self.value = value
self.operation = operation

Expand All @@ -36,24 +34,6 @@ def __init__(self, field, value, operation):
" be in a list format e.g. [1, 2, 3]"
)

def _extract_filter_fields(self, field):
fields = field.split(".")
include_depth = len(fields)

log.debug("Fields: %s, Include Depth: %d", fields, include_depth)

if include_depth == 1:
self.field = fields[0]
elif include_depth == 2:
self.field = fields[0]
self.included_field = fields[1]
elif include_depth == 3:
self.field = fields[0]
self.included_field = fields[1]
self.included_included_field = fields[2]
else:
raise ValueError(f"Maximum include depth exceeded. {field}'s depth > 3")


class DistinctFieldFilter(QueryFilter):
precedence = 0
Expand Down
1 change: 1 addition & 0 deletions common/icat/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
class PythonICATWhereFilter(WhereFilter):
def __init__(self, field, value, operation):
super().__init__(field, value, operation)
self.field = field

def apply_filter(self, query):
log.info("Creating condition for ICAT where filter")
Expand Down

0 comments on commit 8e3e161

Please sign in to comment.