def get_timelists(request): """ output all timelist belong to user """ if request.is_ajax(): try: uinfo = get_user_info(request) from timelist import get_timelists_by_user tls = get_timelists_by_user(uinfo) ret = [] if request.user.is_authenticated(): for tl in tls: data = {'id':tl.id, 'name':tl.name, 'desc':tl.desc, 'color_id':tl.color_id, 'is_public':tl.is_public} if tl.owner != request.user: data['is_on'] = True data['is_editable'] = False is_home = uinfo.get('is_home') if is_home: data['owner'] = [tl.owner.id, tl.owner.name] is_subscribed = False if request.user.tlistsubscribe_set.filter(timelist=tl): is_subscribed = True data['is_subscribed'] = is_subscribed else: data['is_on'] = tl.is_on data['is_editable'] = True ret.append(data) else: for tl in tls: # all timelist is on for anyone data = {'id':tl.id, 'name':tl.name, 'desc':tl.desc, 'is_on':True, 'color_id':tl.color_id, 'is_editable': False} ret.append(data) return JsonResponse({'timelists': ret}) except PermissionDenied: return JsonResponse({'ret':'Permission Denied'})
def test(self): owner = User.objects.create(email='*****@*****.**', name='test', pwd='abc') tl1 = Timelist.objects.create(owner=owner, name='Timelist test1', desc='description test') tl2 = Timelist.objects.create(owner=owner, name='Timelist test2', desc='description test') tl3 = Timelist.objects.create(owner=owner, name='Timelist test3', desc='description test') # get_timelists_by_user self.assertEqual(len(timelist.get_timelists_by_user({'user': owner, 'is_home': True})), 3) self.assertEqual(len(timelist.get_timelists_by_user({'user': owner})), 3) tl1.is_public = False tl1.save() self.assertEqual(len(timelist.get_timelists_by_user({'user': owner, 'is_home': True})), 3) self.assertEqual(len(timelist.get_timelists_by_user({'user': owner})), 2) # subscribe another_user = User.objects.create(email='*****@*****.**', name='another', pwd='abc') tl4 = Timelist.objects.create(owner=another_user, name='Timelist test4', desc='description test') timelist.subscribe(another_user, tl2.id, True) timelist.subscribe(another_user, tl3.id, True) self.assertEqual(len(timelist.get_timelists_by_user({'user': another_user, 'is_home': True})), 3) timelist.subscribe(another_user, tl2.id, False) self.assertEqual(len(timelist.get_timelists_by_user({'user': another_user, 'is_home': True})), 2) timelist.subscribe(another_user, tl3.id, False) self.assertEqual(len(timelist.get_timelists_by_user({'user': another_user, 'is_home': True})), 1) # create tl5 = timelist.create(user=owner, name='test create', desc='desc', color_id=None) self.assertEqual(len(owner.timelist_set.all()), 4) # delete timelist.delete(owner, tl5.id) self.assertEqual(len(owner.timelist_set.all()), 3) # set_public timelist.set_public(user=owner, id=tl1.id, is_public=True) self.assertEqual(owner.timelist_set.get(id=tl1.id).is_public, True) timelist.set_public(user=owner, id=tl1.id, is_public=False) self.assertEqual(owner.timelist_set.get(id=tl1.id).is_public, False) # edit test_name = 'Test edit name' test_desc = 'Test edit desc' test_color_id = 3 timelist.edit(owner, tl1.id, test_name, test_desc, test_color_id) self.assertEqual(owner.timelist_set.get(id=tl1.id).name, test_name) self.assertEqual(owner.timelist_set.get(id=tl1.id).desc, test_desc) self.assertEqual(owner.timelist_set.get(id=tl1.id).color_id, test_color_id) # set_on timelist.set_on(owner, tl1.id, True) self.assertEqual(owner.timelist_set.get(id=tl1.id).is_on, True) timelist.set_on(owner, tl1.id, False) self.assertEqual(owner.timelist_set.get(id=tl1.id).is_on, False) # set_only_on timelist.set_only_on(owner, tl1.id) self.assertEqual(owner.timelist_set.get(id=tl1.id).is_on, True) self.assertEqual(owner.timelist_set.get(id=tl2.id).is_on, False) self.assertEqual(owner.timelist_set.get(id=tl3.id).is_on, False) timelist.set_only_on(owner, tl2.id) self.assertEqual(owner.timelist_set.get(id=tl1.id).is_on, False) self.assertEqual(owner.timelist_set.get(id=tl2.id).is_on, True) self.assertEqual(owner.timelist_set.get(id=tl3.id).is_on, False) # set_order # tl1, tl2 and tl3 remain self.assertEqual(len(timelist.get_timelists_by_user({'user': owner, 'is_home': True})), 3) self.assertEqual(owner.get_profile().tlist_order, ':'.join([str(tl1.id), str(tl2.id), str(tl3.id)])) timelist.set_order(owner, '3', '1') self.assertEqual(owner.get_profile().tlist_order, ':'.join([str(tl1.id), str(tl3.id), str(tl2.id)])) timelist.set_order(owner, '1', '3') self.assertEqual(owner.get_profile().tlist_order, ':'.join([str(tl3.id), str(tl1.id), str(tl2.id)])) timelist.set_order(owner, '2') self.assertEqual(owner.get_profile().tlist_order, ':'.join([str(tl2.id), str(tl3.id), str(tl1.id)])) timelist.set_order(owner, '2', '1') self.assertEqual(owner.get_profile().tlist_order, ':'.join([str(tl3.id), str(tl1.id), str(tl2.id)])) timelist.set_order(owner, '3', '2') self.assertEqual(owner.get_profile().tlist_order, ':'.join([str(tl1.id), str(tl2.id), str(tl3.id)])) # subscribe a new timelist for test timelist.subscribe(owner, tl4.id, True) self.assertEqual(len(timelist.get_timelists_by_user({'user': owner, 'is_home': True})), 4) timelist.set_order(owner, '4') self.assertEqual(owner.get_profile().tlist_order, ':'.join([str(tl4.id), str(tl1.id), str(tl2.id), str(tl3.id)])) timelist.set_order(owner, '4', '1') self.assertEqual(owner.get_profile().tlist_order, ':'.join([str(tl1.id), str(tl4.id), str(tl2.id), str(tl3.id)]))