def _create_response(request, form): field_response_data = _extract_field_response_data(request.POST) try: forms_service.create_response(form, field_response_data) messages.success(request, 'DONE') except ValidationError as e: errors = e.error_dict return _render_form(request, form, field_response_data, errors) return redirect(reverse('response_done', args=(form.project, form.slug)))
def test_raises_error_and_stores_messages_when_attributes_invalid(self): form = mommy.make(Form) details = {'required': True} mommy.make(FormField, form=form, kind='short-answer', details=details) mommy.make(FormField, form=form, kind='single-choice', details=details) with self.assertRaises(ValidationError) as cm: forms_service.create_response(form, {}) errors = cm.exception.error_dict self.assertEqual(len(errors), 2) for field in form.formfield_set.all(): self.assertIn(str(field.id), errors) self.assertIsInstance(errors[str(field.id)], ValidationError)
def test_creates_response_from_form_and_supplied_attributes(self): form = mommy.make(Form) for i, kind in enumerate(map(lambda c: c[0], FormField.KIND_CHOICES)): if kind in ['single-choice', 'multiple-choice']: details = {'choices': [{'label': 'A'}, {'label': 'B'}]} else: details = {} id = i + 1 mommy.make(FormField, id=id, form=form, kind=kind, details=details) data = { '1': {'answer': 'none'}, '2': {'answer': 'short-answer'}, '3': {'answer': 'long-answer'}, '4': {'answer': 'A'}, '5': {'answers': [{'label': 'B', 'selected': True}]}, '6': {'addressLine1': '1', 'addressLine2': '2', 'city': 'c', 'state': 's', 'zip': '00000'}, } response = forms_service.create_response(form, data) for field_response in response.formfieldresponse_set.all(): id = str(field_response.form_field_id) self.assertEqual(field_response.details, data[id])
def test_sets_user_if_specified(self): form = mommy.make(Form) user = mommy.make(User) response = forms_service.create_response(form, {}, user) self.assertEqual(user, response.user)