示例#1
0
文件: tests.py 项目: gridl/urlkey
    def test_urlaction_object(self):
        action_type = 'test_type'
        data = {'test': True, 'welcome': 123}

        action = URLAction.create(action_type, data)
        self._action_executed = False

        @register_urlaction(action_type)
        def simple(action, **kwargs):
            self.assertEqual(action.action_type, action_type)
            self.assertDictEqual(action.data, data)

            m = timedelta(minutes=1)
            n = now() + URLKEY_EXPIRATION_TIMEDELTA

            self.assertTrue((n - m) < action.expired < (n + m))
            self.assertIn('request', kwargs)
            self._action_executed = True

        test_url = '/urlkey_test_view/'
        self.assertEqual(action.wrap_url(test_url),
                         test_url + '?' + URLKEY_NAME + '=' + action.id)

        response = self.client.get(test_url)
        self.assertEqual(response.status_code, OK)
        self.assertFalse(self._action_executed)

        response = self.client.get(action.wrap_url(test_url))
        self.assertEqual(response.status_code, OK)
        self.assertTrue(self._action_executed)
        del self._action_executed
示例#2
0
文件: tests.py 项目: Bahus/urlkey
    def test_urlaction_object(self):
        action_type = 'test_type'
        data = {'test': True, 'welcome': 123}

        action = URLAction.create(action_type, data)
        self._action_executed = False

        @register_urlaction(action_type)
        def simple(action, **kwargs):
            self.assertEqual(action.action_type, action_type)
            self.assertDictEqual(action.data, data)

            m = timedelta(minutes=1)
            n = now() + URLKEY_EXPIRATION_TIMEDELTA

            self.assertTrue((n - m) < action.expired < (n + m))
            self.assertIn('request', kwargs)
            self._action_executed = True

        test_url = '/urlkey_test_view/'
        self.assertEqual(
            action.wrap_url(test_url),
            test_url + '?' + URLKEY_NAME + '=' + action.id
        )

        response = self.client.get(test_url)
        self.assertEqual(response.status_code, OK)
        self.assertFalse(self._action_executed)

        response = self.client.get(action.wrap_url(test_url))
        self.assertEqual(response.status_code, OK)
        self.assertTrue(self._action_executed)
        del self._action_executed
示例#3
0
文件: tests.py 项目: Bahus/urlkey
    def test_user_custom_action(self):
        action_type = 'set_is_staff'

        url_action = URLAction.create(action_type, {'user_id': self.user.id})

        @register_urlaction(action_type)
        def action_set_stuff(action, **kwargs):
            user_id = action.data.get('user_id')
            user = get_object_or_404(User, pk=user_id)
            user.is_staff = True
            user.save()
            self.assertIn('test', kwargs)

        self.assertFalse(self.user.is_staff)
        url_action.execute(test=True)
        user = User.objects.get(pk=self.user.pk)
        self.assertTrue(user.is_staff)
示例#4
0
文件: tests.py 项目: gridl/urlkey
    def test_user_custom_action(self):
        action_type = 'set_is_staff'

        url_action = URLAction.create(action_type, {'user_id': self.user.id})

        @register_urlaction(action_type)
        def action_set_stuff(action, **kwargs):
            user_id = action.data.get('user_id')
            user = get_object_or_404(User, pk=user_id)
            user.is_staff = True
            user.save()
            self.assertIn('test', kwargs)

        self.assertFalse(self.user.is_staff)
        url_action.execute(test=True)
        user = User.objects.get(pk=self.user.pk)
        self.assertTrue(user.is_staff)
示例#5
0
 def process_request(self, request):
     action = URLAction.get_from_request(request)
     if action:
         action.execute(request=request)
示例#6
0
 def process_request(self, request):
     action = URLAction.get_from_request(request)
     if action:
         action.execute(request=request)
示例#7
0
 def handle(self, **options):
     logger.info('URLActions cleanup process started')
     expired_qs = URLAction.get_expired()
     logger.info('Expired actions count: %s', expired_qs.count())
     expired_qs.delete()
     logger.info('URLActions cleanup process ended')