Commit 71bd5d91 authored by sarsonl's avatar sarsonl
Browse files

Refactoring code to try and minimise use of same code multiple times. Have to...

Refactoring code to try and minimise use of same code multiple times. Have to discuss the use of None filtering in query as I am not sure about the purpose.
parent 81d47a23
......@@ -74,6 +74,56 @@ class BoreholeResource(ResourceBase):
def get(self, borehole_id):
pass
class FilterQuery(object):
def __init__(self, query):
self.query = query
def _filter_query(self, query_name, orm_tablename, orm_paramname, filter_op):
#operator_methods = {'egt': 'egt_query_filter',
# 'elt': 'elt_query_filter',
# 'eq': 'eq_query_filter',
# 'neq': 'neq_query_filter'}
query_param = query_params.get(query_name)
if query_param:
orm_methodname = getattr(orm, orm_tablename)
orm_param = getattr(orm_methodname, prm_paramname)
try:
#getattr(self, operator_methods[filter_op])(orm_param, query_param)
getattr(self, filter_op)(orm_param, query_param)
except:
raise ValueError('No filter method exists for: {}'.format(filter_op))
#if filter_op == 'egt':
# self.egt_query_filter(orm_param, query_parameter)
#elif filter_op == 'elt':
# self.elt_query_filter(orm_param, query_parameter)
#elif filter_op == 'eq':
# self.eq_query_filter(orm_param, query_parameter)
#elif filter_op == 'neq':
# self.neq_query_filter(orm_param, query_parameter)
#else:
# raise ValueError('No filter method exists for: {}'.format(filter_op))
def egt_query_filter(self, orn_param, query_param):
query = self.query.filter(orm_param >= query_param)
def elt_query_filter(self, query_name, orm_tablename, orm_paramname, filter_op):
query = self.query.filter(orm_param <= query_param)
def eq_query_filter(self, query_name, orm_tablename, orm_paramname, filter_op):
query = self.query.filter(orm_param == query_param)
def neq_query_filter(self, query_name, orm_tablename, orm_paramname, filter_op):
query = self.query.filter(orm_param != query_param)
class BoreholeHydraulicDataListResource(ResourceBase):
......@@ -111,21 +161,38 @@ class BoreholeHydraulicDataListResource(ResourceBase):
join(orm.BoreholeSection).\
filter(orm.Borehole.m_publicid==borehole_id)
filter_statement = FilterQuery(query)
# XXX(damb): Emulate QuakeML type Epoch (though on DB level it is
# defined as QuakeML type OpenEpoch
starttime = query_params.get('starttime')
if starttime:
query = query.\
filter((orm.BoreholeSection.m_starttime >= starttime) & # noqa
(orm.BoreholeSection.m_starttime != None)) # noqa
endtime = query_params.get('endtime')
if endtime:
query = query.\
filter((orm.BoreholeSection.m_endtime <= endtime) | # noqa
(orm.BoreholeSection.m_endtime == None)) # noqa
filter_statement.filter_query('starttime', 'BoreholeSection', 'm_starttime', 'egt_filter_query')
filter_statement.filter_query('endtime', 'BoreholeSection', 'm_endtime', 'lgt_filter_query')
#starttime = query_params.get('starttime')
#if starttime:
# query = query.\
# filter((orm.BoreholeSection.m_starttime >= starttime) & # noqa
# (orm.BoreholeSection.m_starttime != None)) # Is this None filter needed?
#endtime = query_params.get('endtime')
#if endtime:
# query = query.\
# filter((orm.BoreholeSection.m_endtime <= endtime) | # noqa
# (orm.BoreholeSection.m_endtime == None)) # If this line is present, unfinished sections are always included
# TODO(damb): Add additional filter criteria
try:
return query.one()
except NoResultFound:
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment