Ejemplo n.º 1
0
def set_on(request):
    if request.is_ajax():
        try:
            user = request.user
            reqdata = request.POST
            id = reqdata.get('tlid')
            on = reqdata.get('on')
            from ninemitutil import str2bool
            on = str2bool(on)
            from timelist import set_on
            set_on(user, id, on)
        except IndexError:
            ret='err, tlid wrong'
        except Timelist.DoesNotExist:
            ret='err, no this list'
        except PermissionDenied:
            ret='Permission Denied'
        else:
            ret='ok'
        return JsonResponse({'ret':ret})
Ejemplo n.º 2
0
    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)]))