def create(request): if request.is_ajax(): try: user = request.user reqdata = request.POST name = reqdata.get('name') desc = reqdata.get('desc') try: color_id = reqdata.get('color_id') except TypeError: color_id = None from timelist import create tl = create(user=user, name=name, desc=desc, color_id=color_id) if tl: data = {'id':tl.id, 'name':tl.name, 'desc':tl.desc, 'is_on':tl.is_on, 'color_id':tl.color_id, 'is_editable': True} return JsonResponse({'ret': 'create success', 'data': data}) except IndexError: return JsonResponse({'ret': 'Lists count limited', 'valid':False}) except ValueError, PermissionDenied: return JsonResponse({'ret': 'Can not create list', 'valid':False})
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)]))