def get_timelist_current_timer(user, tl_id): try: tl = Timelist.objects.get(Q(id=tl_id), Q(is_public=True)|Q(owner=user)) t = Timer.objects.filter(timelist=tl, time__gt=get_utcnow()).order_by('time')[0] return t except (Timelist.DoesNotExist, IndexError): return None
def get_current_timer_own(user): k = 'current_timer_own:%s' % user.id t = cache.get(k) if not t: t = user.timer_set.filter(timelist__is_on=True, time__gt=get_utcnow()).order_by('time')[0] cache.set(k, t) return t
def get_current_timer_home(user): k = 'current_timer_home:%s' % user.id t = cache.get(k) if not t: tls = Timelist.objects.filter(Q(owner=user)|Q(tlistsubscribe__user=user)) t = Timer.objects.filter(timelist__in=tls, timelist__is_on=True, time__gt=get_utcnow()).order_by('time')[0] cache.set(k, t) return t
def get_recent_timers(): """for start page show, display recent public timers""" now = get_utcnow() tls = Timer.objects.filter(timelist__is_public=True, time__range=(now, now+timedelta(weeks=12))).order_by('time').values('timelist_id').annotate(Count('timelist'))[:7] ts = [] for tl in tls: ts.append(Timer.objects.filter(timelist__id=tl.get('timelist_id'), timelist__is_public=True, time__gt=now).order_by('time')[0]) return ts
def setUp(self): self.time_str = '20110809T091606' time = parseStr2utc(self.time_str, 'Asia/Shanghai') self.create_time = get_utcnow() self.title = 'Timer title' self.desc = 'Timer desc' self.owner = User.objects.create(email='*****@*****.**', name='test2', pwd='abc') self.timelist = Timelist.objects.create(owner=self.owner, name='Timelist test') self.timer = Timer.objects.create(time=time, create_time=self.create_time, title=self.title, desc=self.desc, owner = self.owner, timelist=self.timelist)
def test(self): tz_str = 'Asia/Shanghai' utc_time = tz.parseStr2utc('20110417T155959', tz_str) self.assertEqual(utc_time is not None, True) tz_time = tz.parseutc2tz(utc_time, tz_str) self.assertEqual(tz_time is not None, True) self.assertEqual(tz.get_utcnow() is not None, True) # guess timezone # offset from javascript new Date().getTimezoneOffset() mins = -480 # shanghai self.assertEqual(tz.guess_timezone(mins), 'Asia/Shanghai')
def create(time, title, desc, user, tlid, tzname): try: tl = user.timelist_set.get(id=tlid) # check tlist belong to user except Timelist.DoesNotExist: tl = user.timelist_set.get_or_create() tlid = tl.id t=Timer.objects.create(time=parseStr2utc(time, tzname), create_time=get_utcnow(), title=title, desc=desc, owner=user, timelist_id=tlid) cache_clear_current_timer(user) return t
def test(self): now = get_utcnow() from datetime import timedelta time = now + timedelta(days=10) time_range = {'start': parseStr2utc(time - timedelta(days=1), self.tz_name), 'end': parseStr2utc(time + timedelta(days=1), self.tz_name)} # time = parseStr2utc('20110809T091606', self.tz_name) t = timer.create(time=time, title='Timer title', desc='Timer desc', user=self.owner, tlid=self.timelist.id, tzname=self.tz_name) # get_timer self.assertEqual(timer.get_timer(self.owner.id, t.id) is None, False); # get_timers self.assertEqual(len(timer.get_timers({'user': self.owner}, time_range.get('start'), time_range.get('end'))), 1) self.assertEqual(len(timer.get_timers({'user': self.owner, 'is_home': True}, time_range.get('start'), time_range.get('end'))), 1) self.assertEqual(len(timer.get_timers({'user': self.owner, 'is_own': True}, time_range.get('start'), time_range.get('end'))), 1) # error range err_time_range = {'start': parseStr2utc(time + timedelta(days=1), self.tz_name), 'end': parseStr2utc(time + timedelta(days=2), self.tz_name)} self.assertEqual(len(timer.get_timers({'user': self.owner}, err_time_range.get('start'), err_time_range.get('end'))), 0) # get_timelist_timers self.assertEqual(len(timer.get_timelist_timers(self.owner, self.timelist.id, time_range.get('start'), time_range.get('end'))), 1) self.assertEqual(len(timer.get_timelist_timers(self.owner, self.timelist.id, err_time_range.get('start'), err_time_range.get('end'))), 0) # get_crrent_timer self.assertEqual(timer.get_current_timer({'user': self.owner, 'is_own': True}).id, t.id) self.assertEqual(timer.get_current_timer({'user': self.owner, 'is_home': True}).id, t.id) self.assertEqual(timer.get_current_timer({'user': self.owner}).id, t.id) # get _timelist_current_timer self.assertEqual(timer.get_timelist_current_timer(self.owner, self.timelist.id).id, t.id) # edit title = 'test title edit' desc = 'test desc edit' t = timer.edit(user=self.owner, tid=t.id, time=time, title=title, desc=desc, tlid=self.timelist.id, tzname=self.tz_name, reminders=None) self.assertEqual(t.title, title) self.assertEqual(t.desc, desc) # delete timer.delete(self.owner, t.id) self.assertEqual(len(timer.get_timers({'user': self.owner}, time_range.get('start'), time_range.get('end'))), 0)