Skip to content

Commit

Permalink
#150: Move QueryFilterFactory to its own file
Browse files Browse the repository at this point in the history
- It used to be in the database helpers file, but this class is used across both backends
- The imports have been moved inside the staticmethod because the Flask TEST_BACKEND config option won't have been set by the time QueryFilterFactory has been imported, so the imports which decide which set of filters to import must be put in some code.
  • Loading branch information
MRichards99 committed Dec 3, 2020
1 parent 6902921 commit 7a6a8ef
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 37 deletions.
36 changes: 0 additions & 36 deletions datagateway_api/common/database/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,42 +202,6 @@ def execute_query(self):
self.commit_changes()


class QueryFilterFactory(object):
@staticmethod
def get_query_filter(request_filter):
"""
Given a filter return a matching Query filter object
This factory is not in common.filters so the created filter can be for the
correct backend. Moving the factory into that file would mean the filters would
be based off the abstract classes (because they're in the same file) which won't
enable filters to be unique to the backend
:param request_filter: dict - The filter to create the QueryFilter for
:return: The QueryFilter object created
"""
filter_name = list(request_filter)[0].lower()
if filter_name == "where":
field = list(request_filter[filter_name].keys())[0]
operation = list(request_filter[filter_name][field].keys())[0]
value = request_filter[filter_name][field][operation]
return WhereFilter(field, value, operation)
elif filter_name == "order":
field = request_filter["order"].split(" ")[0]
direction = request_filter["order"].split(" ")[1]
return OrderFilter(field, direction)
elif filter_name == "skip":
return SkipFilter(request_filter["skip"])
elif filter_name == "limit":
return LimitFilter(request_filter["limit"])
elif filter_name == "include":
return IncludeFilter(request_filter["include"])
elif filter_name == "distinct":
return DistinctFieldFilter(request_filter["distinct"])
else:
raise FilterError(f" Bad filter: {request_filter}")


def insert_row_into_table(table, row):
"""
Insert the given row into its table
Expand Down
2 changes: 1 addition & 1 deletion datagateway_api/common/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from flask_restful import reqparse
from sqlalchemy.exc import IntegrityError

from datagateway_api.common.database.helpers import QueryFilterFactory
from datagateway_api.common.query_filter import QueryFilterFactory
from datagateway_api.common.exceptions import (
ApiError,
AuthenticationError,
Expand Down
72 changes: 72 additions & 0 deletions datagateway_api/common/query_filter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import logging

from datagateway_api.common.config import config
from datagateway_api.common.exceptions import (
ApiError,
FilterError,
)

log = logging.getLogger()


class QueryFilterFactory(object):
@staticmethod
def get_query_filter(request_filter):
"""
Given a filter return a matching Query filter object
This factory is not in common.filters so the created filter can be for the
correct backend. Moving the factory into that file would mean the filters would
be based off the abstract classes (because they're in the same file) which won't
enable filters to be unique to the backend
:param request_filter: dict - The filter to create the QueryFilter for
:return: The QueryFilter object created
"""

backend_type = config.get_backend_type()
print(f"Backend inside staticmethod: {backend_type}")
if backend_type == "db":
from datagateway_api.common.database.filters import (
DatabaseDistinctFieldFilter as DistinctFieldFilter,
DatabaseIncludeFilter as IncludeFilter,
DatabaseLimitFilter as LimitFilter,
DatabaseOrderFilter as OrderFilter,
DatabaseSkipFilter as SkipFilter,
DatabaseWhereFilter as WhereFilter,
)
elif backend_type == "python_icat":
from datagateway_api.common.icat.filters import (
PythonICATDistinctFieldFilter as DistinctFieldFilter,
PythonICATIncludeFilter as IncludeFilter,
PythonICATLimitFilter as LimitFilter,
PythonICATOrderFilter as OrderFilter,
PythonICATSkipFilter as SkipFilter,
PythonICATWhereFilter as WhereFilter,
)
else:
raise ApiError(
"Cannot select which implementation of filters to import, check the"
" config file has a valid backend type",
)

filter_name = list(request_filter)[0].lower()
if filter_name == "where":
field = list(request_filter[filter_name].keys())[0]
operation = list(request_filter[filter_name][field].keys())[0]
value = request_filter[filter_name][field][operation]
return WhereFilter(field, value, operation)
elif filter_name == "order":
field = request_filter["order"].split(" ")[0]
direction = request_filter["order"].split(" ")[1]
return OrderFilter(field, direction)
elif filter_name == "skip":
return SkipFilter(request_filter["skip"])
elif filter_name == "limit":
return LimitFilter(request_filter["limit"])
elif filter_name == "include":
return IncludeFilter(request_filter["include"])
elif filter_name == "distinct":
return DistinctFieldFilter(request_filter["distinct"])
else:
raise FilterError(f" Bad filter: {request_filter}")

0 comments on commit 7a6a8ef

Please sign in to comment.