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)
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())
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)
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)
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)
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))
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
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
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
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)
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)