Skip to content

Commit

Permalink
Introduce 'seconds_of_the_day' to Django field
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreiPashkin committed Jun 18, 2015
1 parent f7cd265 commit db657e4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
24 changes: 15 additions & 9 deletions tempo/django/fields.py
Expand Up @@ -17,7 +17,7 @@ def db_type(self, connection):
def schedule_to_dict(cls, schedule):
return {attr: getattr(schedule, attr) for attr in
('years', 'months', 'days', 'weekdays', 'hours', 'minutes',
'seconds')}
'seconds', 'seconds_of_the_day')}

@classmethod
def schedule_from_dict(cls, dictionary):
Expand Down Expand Up @@ -79,14 +79,17 @@ class Contains(models.Lookup):
" (value->'weekdays' @> '%(weekday)s'::jsonb OR "
" value->'weekdays' = 'null'::jsonb)) "
" AND "
" (value->'hours' @> '%(hour)s'::jsonb OR "
" value->'hours' = 'null'::jsonb) "
" AND "
" (value->'minutes' @> '%(minute)s'::jsonb OR "
" value->'minutes' = 'null'::jsonb) "
" AND "
" (value->'seconds' @> '%(second)s'::jsonb OR "
" value->'seconds' = 'null'::jsonb)"
" (((value->'hours' @> '%(hour)s'::jsonb OR "
" value->'hours' = 'null'::jsonb) "
" AND "
" (value->'minutes' @> '%(minute)s'::jsonb OR "
" value->'minutes' = 'null'::jsonb) "
" AND "
" (value->'seconds' @> '%(second)s'::jsonb OR "
" value->'seconds' = 'null'::jsonb))"
" OR "
" (value->'seconds_of_the_day' @> '%(second_of_the_day)s'::jsonb OR "
" value->'seconds_of_the_day' = 'null'::jsonb))"
)

LOOKUP = (
Expand All @@ -109,6 +112,9 @@ def as_sql(self, compiler, connection):
'hour': self.rhs.hour,
'minute': self.rhs.minute,
'second': self.rhs.second,
'second_of_the_day': (self.rhs.hour * 60 * 60 +
self.rhs.minute * 60 +
self.rhs.second),
'field': lhs % lhs_params}
return self.LOOKUP % params, []

Expand Down
6 changes: 5 additions & 1 deletion tests/test_django/test_fields.py
Expand Up @@ -17,7 +17,11 @@
([ScheduleSet(include=[Schedule(weekdays=[2], days=[15])]),
ScheduleSet(include=[Schedule(weekdays=[4], days=[1])])],
dt.datetime(2014, 1, 1))
dt.datetime(2014, 1, 1)),
([ScheduleSet(include=[Schedule(seconds_of_the_day=[2], seconds=[15])]),
ScheduleSet(include=[Schedule(seconds_of_the_day=[15], seconds=[15])])],
dt.datetime(2014, 1, 1, 0, 0, 2))
])
@pytest.mark.django_db
def test_contains(schedulesets, datetime):
Expand Down

0 comments on commit db657e4

Please sign in to comment.