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): 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)
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 load_timers(request): """get bundle of timers from ajax request""" if request.is_ajax(): try: reqdata = request.GET start = reqdata.get('start') end = reqdata.get('end') tz = reqdata.get('tz') tl_id = reqdata.get('tl_id') start = parseStr2utc(start, tz) end = parseStr2utc(end, tz) if tl_id: ts = timer.get_timelist_timers(request.user, int(tl_id), start, end) else: uinfo = get_user_info(request) ts = timer.get_timers(uinfo, start, end) # first is the time range info retts = [[start.strftime(timer.TIME_FORMAT), end.strftime(timer.TIME_FORMAT)]] for t in ts: retts.append(format_timer_str(t)) return JsonResponse(retts) except PermissionDenied: return JsonResponse({'ret':'Permission Denied'})
def edit(user, tid, time, title, desc, tlid, tzname, reminders): """reminders format: ['remind_type:lead_time']""" if not tid: raise IndexError t = Timer.objects.get(pk=tid) # check user auth for this timer if t.owner.id != user.id: raise PermissionDenied if time != None: t.time=parseStr2utc(time, tzname) if title != None: t.title=title if desc != None: t.desc=desc if tlid != None: t.timelist_id=tlid if reminders != None: t.set_reminders(reminders) t.save() cache.delete('timer:%s' % tid) cache_clear_current_timer(user) return t