Esempio n. 1
0
 def test_session(self):
     form = ContextForm(self.request, {})
     self.assertTrue(form.is_valid())
     instance = form.save()
     self.assertIsNone(instance.user)
     self.assertEqual(instance.session_key,
                      self.request.session.session_key)
Esempio n. 2
0
 def test_session(self):
     form = ContextForm(self.request, {})
     self.assertTrue(form.is_valid())
     instance = form.save()
     self.assertIsNone(instance.user)
     self.assertEqual(instance.session_key,
                      self.request.session.session_key)
Esempio n. 3
0
    def test_json(self):
        expected_count = Employee.objects.filter(title__salary__gt=1000).count()

        form = ContextForm(self.request, {"json": {"field": "tests.title.salary", "operator": "gt", "value": "1000"}})
        self.assertTrue(form.is_valid())

        instance = form.save()
        self.assertEqual(instance.count, expected_count)
Esempio n. 4
0
    def test_user(self):
        user = User.objects.create_user(username='******', password='******')
        self.request.user = user

        form = ContextForm(self.request, {})
        self.assertTrue(form.is_valid())
        instance = form.save()
        self.assertEqual(instance.user, user)
        self.assertEqual(instance.session_key, None)
Esempio n. 5
0
    def test_user(self):
        user = User.objects.create_user(username='******', password='******')
        self.request.user = user

        form = ContextForm(self.request, {})
        self.assertTrue(form.is_valid())
        instance = form.save()
        self.assertEqual(instance.user, user)
        self.assertEqual(instance.session_key, None)
Esempio n. 6
0
 def test_json(self):
     form = ContextForm(
         self.request, {
             'json': {
                 'field': 'tests.title.salary',
                 'operator': 'gt',
                 'value': '1000'
             }
         })
     self.assertTrue(form.is_valid())
Esempio n. 7
0
    def test_json(self):
        expected_count = \
            Employee.objects.filter(title__salary__gt=1000).count()

        form = ContextForm(self.request, {'json': {
            'field': 'tests.title.salary', 'operator': 'gt', 'value': '1000'}})
        self.assertTrue(form.is_valid())

        instance = form.save()
        self.assertEqual(instance.count, expected_count)
Esempio n. 8
0
    def get_default(self, request):
        default = self.model.objects.get_default_template()

        if not default:
            log.warning('No default template for context objects')
            return

        form = ContextForm(request, {'json': default.json, 'session': True})

        if form.is_valid():
            instance = form.save()
            return instance

        log.error('Error creating default context', extra=dict(form.errors))
Esempio n. 9
0
    def get_default(self, request):
        default = self.model.objects.get_default_template()

        if not default:
            log.warning('No default template for context objects')
            return

        form = ContextForm(request, {'json': default.json, 'session': True})

        if form.is_valid():
            instance = form.save()
            return instance

        log.error('Error creating default context', extra=dict(form.errors))
Esempio n. 10
0
    def post(self, request):
        form = ContextForm(request, request.data)

        if form.is_valid():
            instance = form.save()
            usage.log('create', instance=instance, request=request)
            response = self.render(request, self.prepare(request, instance),
                                   status=codes.created)
        else:
            data = {
                'message': 'Error creating context',
                'errors': dict(form.errors),
            }
            response = self.render(request, data,
                                   status=codes.unprocessable_entity)
        return response
Esempio n. 11
0
    def put(self, request, **kwargs):
        instance = self.get_object(request, **kwargs)

        form = ContextForm(request, request.data, instance=instance)

        if form.is_valid():
            instance = form.save()
            usage.log('update', instance=instance, request=request)
            response = self.render(request, self.prepare(request, instance))
        else:
            data = {
                'message': 'Error updating context',
                'errors': dict(form.errors),
            }
            response = self.render(request, data,
                                   status=codes.unprocessable_entity)
        return response
Esempio n. 12
0
    def put(self, request, **kwargs):
        instance = self.get_object(request, **kwargs)

        form = ContextForm(request, request.data, instance=instance)

        if form.is_valid():
            instance = form.save()
            usage.log('update', instance=instance, request=request)
            response = self.render(request, self.prepare(request, instance))
        else:
            data = {
                'message': 'Error updating context',
                'errors': dict(form.errors),
            }
            response = self.render(request,
                                   data,
                                   status=codes.unprocessable_entity)
        return response
Esempio n. 13
0
    def post(self, request):
        form = ContextForm(request, request.data)

        if form.is_valid():
            instance = form.save()
            usage.log('create', instance=instance, request=request)
            response = self.render(request,
                                   self.prepare(request, instance),
                                   status=codes.created)
        else:
            data = {
                'message': 'Error creating context',
                'errors': dict(form.errors),
            }
            response = self.render(request,
                                   data,
                                   status=codes.unprocessable_entity)
        return response
Esempio n. 14
0
    def post(self, request):
        params = self.get_params(request)
        form = ContextForm(request, request.data)

        if form.is_valid():
            instance = form.save()
            usage.log('create', instance=instance, request=request)

            request.session.modified = True

            data = self.prepare(request, instance, tree=params['tree'])
            return self.render(request, data, status=codes.created)

        data = {
            'message': 'Error creating context',
            'errors': dict(form.errors),
        }

        response = self.render(request, data,
                               status=codes.unprocessable_entity)
        return response
Esempio n. 15
0
    def post(self, request):
        params = self.get_params(request)
        form = ContextForm(request, request.data)

        if form.is_valid():
            instance = form.save()
            usage.log('create', instance=instance, request=request)

            request.session.modified = True

            data = self.prepare(request, instance, tree=params['tree'])
            return self.render(request, data, status=codes.created)

        data = {
            'message': 'Error creating context',
            'errors': dict(form.errors),
        }

        response = self.render(request,
                               data,
                               status=codes.unprocessable_entity)
        return response
Esempio n. 16
0
    def put(self, request, **kwargs):
        params = self.get_params(request)
        tree = params['tree']
        processor = params['processor']

        instance = self.get_object(request, **kwargs)

        form = ContextForm(request, request.data, instance=instance,
                           processor=processor, tree=tree)

        if form.is_valid():
            instance = form.save()
            usage.log('update', instance=instance, request=request)

            request.session.modified = True

            data = self.prepare(request, instance, tree=params['tree'])
            return self.render(request, data)

        data = {
            'message': 'Error updating context',
            'errors': dict(form.errors),
        }
        return self.render(request, data, status=codes.unprocessable_entity)
Esempio n. 17
0
 def test_force_count(self):
     expected_count = Employee.objects.distinct().count()
     form = ContextForm(self.request, {}, force_count=True)
     self.assertTrue(form.is_valid())
     instance = form.save()
     self.assertEqual(instance.count, expected_count)
Esempio n. 18
0
 def test_json(self):
     form = ContextForm(self.request, {'json': {
         'field': 'tests.title.salary', 'operator': 'gt', 'value': '1000'}})
     self.assertTrue(form.is_valid())
Esempio n. 19
0
 def test_force_count(self):
     expected_count = Employee.objects.distinct().count()
     form = ContextForm(self.request, {}, force_count=True)
     self.assertTrue(form.is_valid())
     instance = form.save()
     self.assertEqual(instance.count, expected_count)