Skip to content

Commit

Permalink
#142: Move skeleton filters implementation to separate files
Browse files Browse the repository at this point in the history
- This adds filters.py to be able to implement filters using Python ICAT
  • Loading branch information
MRichards99 committed Jul 28, 2020
1 parent e0180d3 commit 01bd4bb
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 32 deletions.
31 changes: 1 addition & 30 deletions common/database_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from common.models.db_models import INVESTIGATIONUSER, INVESTIGATION, INSTRUMENT, FACILITYCYCLE, \
INVESTIGATIONINSTRUMENT, FACILITY, SESSION
from common.session_manager import session_manager
from common.filters import FilterOrderHandler

log = logging.getLogger()

Expand Down Expand Up @@ -329,36 +330,6 @@ def get_query_filter(filter):
raise BadFilterError(f" Bad filter: {filter}")


class FilterOrderHandler(object):
"""
The FilterOrderHandler takes in filters, sorts them according to the order of operations, then applies them.
"""

def __init__(self):
self.filters = []

def add_filter(self, filter):
self.filters.append(filter)

def add_filters(self, filters):
self.filters.extend(filters)

def sort_filters(self):
"""
Sorts the filters according to the order of operations
"""
self.filters.sort(key=lambda x: x.precedence)

def apply_filters(self, query):
"""
Given a query apply the filters the handler has in the correct order.
:param query: The query to have filters applied to
"""
self.sort_filters()
for filter in self.filters:
filter.apply_filter(query)


def insert_row_into_table(table, row):
"""
Insert the given row into its table
Expand Down
96 changes: 96 additions & 0 deletions common/filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
from abc import ABC, abstractmethod

class QueryFilter(ABC):
@property
@abstractmethod
def precedence(self):
pass

@abstractmethod
def apply_filter(self, query):
pass


class WhereFilter(QueryFilter):
precedence = 1

def __init__(self, field, value, operation):
self.field = field
self.included_field = None
self.included_included_field = None
self._set_filter_fields()
self.value = value
self.operation = operation
#super().__init__()

def _set_filter_fields(self):
if self.field.count(".") == 1:
self.included_field = self.field.split(".")[1]
self.field = self.field.split(".")[0]

if self.field.count(".") == 2:
self.included_included_field = self.field.split(".")[2]
self.included_field = self.field.split(".")[1]
self.field = self.field.split(".")[0]

class DistinctFieldFilter(QueryFilter):
precedence = 0

def __init__(self, fields):
# This allows single string distinct filters
self.fields = fields if type(fields) is list else [fields]

class OrderFilter(QueryFilter):
precedence = 2

def __init__(self, field, direction):
self.field = field
self.direction = direction

class SkipFilter(QueryFilter):
precedence = 3

def __init__(self, skip_value):
self.skip_value = skip_value

class LimitFilter(QueryFilter):
precedence = 4

def __init__(self, limit_value):
self.limit_value = limit_value

class IncludeFilter(QueryFilter):
precedence = 5

def __init__(self, included_filters):
self.included_filters = included_filters["include"]


class FilterOrderHandler(object):
"""
The FilterOrderHandler takes in filters, sorts them according to the order of operations, then applies them.
"""

def __init__(self):
self.filters = []

def add_filter(self, filter):
self.filters.append(filter)

def add_filters(self, filters):
self.filters.extend(filters)

def sort_filters(self):
"""
Sorts the filters according to the order of operations
"""
self.filters.sort(key=lambda x: x.precedence)

def apply_filters(self, query):
"""
Given a query apply the filters the handler has in the correct order.
:param query: The query to have filters applied to
"""
self.sort_filters()
for filter in self.filters:
filter.apply_filter(query)
5 changes: 3 additions & 2 deletions test/test_database_helpers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from unittest import TestCase

from common.database_helpers import QueryFilterFactory, OrderFilter, LimitFilter, SkipFilter, WhereFilter, \
IncludeFilter, DistinctFieldFilter
#from common.filters import QueryFilterFactory
from common.database_helpers import OrderFilter, LimitFilter, SkipFilter, WhereFilter, \
IncludeFilter, DistinctFieldFilter, QueryFilterFactory


class TestQueryFilterFactory(TestCase):
Expand Down

0 comments on commit 01bd4bb

Please sign in to comment.