Exemplo 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)
Exemplo 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)
Exemplo n.º 3
0
    def test_no_commit(self):
        previous_context_count = DataContext.objects.count()

        form = ContextForm(self.request, {})
        instance = form.save(commit=False)

        self.assertIsNone(instance.pk)
        self.assertEqual(previous_context_count, DataContext.objects.count())
Exemplo n.º 4
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)
Exemplo n.º 5
0
    def test_no_commit(self):
        previous_context_count = DataContext.objects.count()

        form = ContextForm(self.request, {})
        instance = form.save(commit=False)

        self.assertIsNone(instance.pk)
        self.assertEqual(previous_context_count, DataContext.objects.count())
Exemplo n.º 6
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)
Exemplo n.º 7
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)
Exemplo n.º 8
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)
Exemplo 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))
Exemplo n.º 10
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))
Exemplo n.º 11
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
Exemplo 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
Exemplo n.º 13
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
Exemplo n.º 14
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
Exemplo 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
Exemplo n.º 16
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
Exemplo n.º 17
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)
Exemplo n.º 18
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)
Exemplo 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)