Add AlchemyLocalDateTimeFilter

This commit is contained in:
Lance Edgar 2017-06-16 16:37:40 -07:00
parent 80d2912874
commit b468bc4b2b

View file

@ -565,6 +565,96 @@ class AlchemyDateTimeFilter(AlchemyDateFilter):
return query.filter(self.column < time) return query.filter(self.column < time)
class AlchemyLocalDateTimeFilter(AlchemyDateTimeFilter):
"""
SQLAlchemy filter for *local* datetime values. This assumes datetime
values in the database are for local timezone, as opposed to UTC.
"""
def filter_equal(self, query, value):
"""
Find all dateimes which fall on the given date.
"""
date = self.make_date(value)
if not date:
return query
start = datetime.datetime.combine(date, datetime.time(0))
start = localtime(self.config, start, tzinfo=False)
stop = datetime.datetime.combine(date + datetime.timedelta(days=1), datetime.time(0))
stop = localtime(self.config, stop, tzinfo=False)
return query.filter(self.column >= start)\
.filter(self.column < stop)
def filter_not_equal(self, query, value):
"""
Find all dateimes which do *not* fall on the given date.
"""
date = self.make_date(value)
if not date:
return query
start = datetime.datetime.combine(date, datetime.time(0))
start = localtime(self.config, start, tzinfo=False)
stop = datetime.datetime.combine(date + datetime.timedelta(days=1), datetime.time(0))
stop = localtime(self.config, stop, tzinfo=False)
return query.filter(sa.or_(
self.column < start,
self.column <= stop))
def filter_greater_than(self, query, value):
"""
Find all datetimes which fall after the given date.
"""
date = self.make_date(value)
if not date:
return query
time = datetime.datetime.combine(date + datetime.timedelta(days=1), datetime.time(0))
time = localtime(self.config, time, tzinfo=False)
return query.filter(self.column >= time)
def filter_greater_equal(self, query, value):
"""
Find all datetimes which fall on or after the given date.
"""
date = self.make_date(value)
if not date:
return query
time = datetime.datetime.combine(date, datetime.time(0))
time = localtime(self.config, time, tzinfo=False)
return query.filter(self.column >= time)
def filter_less_than(self, query, value):
"""
Find all datetimes which fall before the given date.
"""
date = self.make_date(value)
if not date:
return query
time = datetime.datetime.combine(date, datetime.time(0))
time = localtime(self.config, time, tzinfo=False)
return query.filter(self.column < time)
def filter_less_equal(self, query, value):
"""
Find all datetimes which fall on or before the given date.
"""
date = self.make_date(value)
if not date:
return query
time = datetime.datetime.combine(date + datetime.timedelta(days=1), datetime.time(0))
time = localtime(self.config, time, tzinfo=False)
return query.filter(self.column < time)
class AlchemyGPCFilter(AlchemyGridFilter): class AlchemyGPCFilter(AlchemyGridFilter):
""" """
GPC filter for SQLAlchemy. GPC filter for SQLAlchemy.