- alive=LeaseFilter.quote(alive)
- return LeaseFilter.sql_time_in_range(alive,'t_from','t_until')
- elif isinstance (alive,tuple):
- (f,u)=alive
- f=LeaseFilter.quote(f)
- u=LeaseFilter.quote(u)
- return LeaseFilter.sql_time_intersect (f,u,'t_from','t_until')
- else: raise PLCInvalidArgument ("LeaseFilter: alive field %r"%alive)
-
- def sql_clip (self, clip):
- if isinstance (clip,int) or isinstance (clip, StringTypes):
- start=LeaseFilter.quote(clip)
- return LeaseFilter.sql_timeslot_after('t_from','t_until',start)
- elif isinstance (clip,tuple):
- (f,u)=clip
- f=LeaseFilter.quote(f)
- u=LeaseFilter.quote(u)
- return LeaseFilter.sql_time_intersect(f,u,'t_from','t_until')
- else: raise PLCInvalidArgument ("LeaseFilter: clip field %r"%clip)
-
-
- ## supersede the generic Filter 'sql' method
- def sql(self, api, join_with = "AND"):
+ alive = LeaseFilter.quote(alive)
+ return LeaseFilter.sql_time_in_range(alive, 't_from', 't_until')
+ elif isinstance(alive, (tuple, list)):
+ (f, u) = alive
+ f = LeaseFilter.quote(f)
+ u = LeaseFilter.quote(u)
+ return LeaseFilter.sql_time_intersect(f, u, 't_from', 't_until')
+ else:
+ raise PLCInvalidArgument("LeaseFilter: alive field {}"
+ .format(alive))
+
+ def sql_clip(self, clip):
+ if isinstance(clip, int) or isinstance(clip, str):
+ start = LeaseFilter.quote(clip)
+ return LeaseFilter.sql_timeslot_after('t_from', 't_until', start)
+ elif isinstance(clip, (tuple, list)):
+ (f, u) = clip
+ f = LeaseFilter.quote(f)
+ u = LeaseFilter.quote(u)
+ return LeaseFilter.sql_time_intersect(f, u, 't_from', 't_until')
+ else:
+ raise PLCInvalidArgument("LeaseFilter: clip field {}"
+ .format(clip))
+
+ # the whole key to implementing day is to compute today's beginning
+ def today_start(self):
+ # a struct_time
+ st = time.localtime()
+ seconds_today = st.tm_hour * 3600 + st.tm_min * 60 + st.tm_sec
+ return int(time.time()) - seconds_today
+
+ # supersede the generic Filter 'sql' method
+ def sql(self, api, join_with="AND"):
+ # implement 'day' as a clip
+ if 'day' in self:
+ if 'clip' in self:
+ raise PLCInvalidArgument("LeaseFilter cannot have both 'clip' and 'day'")
+ today = self.today_start()
+ nb_days = self['day']
+ if nb_days == 0:
+ self['clip'] = today
+ else:
+ self['clip'] = (today, today + nb_days * 24 * 3600)
+ del self['day']
+