def _get_form_version(request, number, version): form = forms_service.version(number, version) exclude = ['user', 'created', 'modified'] update = {'username': lambda f: f.user.username, 'date': lambda f: _format_date(f.modified), 'fields': lambda f: [model_to_dict(fld, 'form') for fld in f.formfield_set.all()]} return JSONResponse({'form': model_to_dict(form, *exclude, **update)})
def form_versions(request, number): forms = forms_service.versions(number) exclude = ['user', 'created', 'modified'] update = {'username': lambda f: f.user.username, 'date': lambda f: _format_date(f.modified), 'fields': lambda f: [model_to_dict(fld, 'form') for fld in f.formfield_set.all()], 'numResponses': lambda f: f.num_responses} forms_data = [model_to_dict(f, *exclude, **update) for f in forms] return JSONResponse({'forms': forms_data})
def _response_to_dict(response): exclude = ['form', 'user', 'created'] update = { 'username': lambda r: r.user.username if r.user else '---', 'date': lambda r: str(r.created), 'fieldResponses': lambda r: [ model_to_dict(fr, 'form_response', 'form_field') for fr in r.formfieldresponse_set.all() ], } return model_to_dict(response, *exclude, **update)
def test_sets_new_form_current_and_previous_forms_old(self): user = mommy.make(Form, number=3, version=1, current=True).user form = mommy.prepare(Form, number=3, user=user) form_data = model_to_dict(form) form = forms_service.create(**form_data) self.assertTrue(form.current) self.assertFalse(Form.objects.get(number=3, version=1).current)
def _get_current_forms(request): forms = forms_service.current_forms() exclude = ['user', 'created', 'modified'] update = {'username': lambda f: f.user.username, 'date': lambda f: _format_date(f.modified), 'prevPublished': lambda f: f.prev_published, 'numResponses': lambda f: f.num_responses} forms_data = [model_to_dict(f, *exclude, **update) for f in forms] return JSONResponse({'forms': forms_data})
def test_destroys_old_fields_and_creates_new_ones(self): form = mommy.make(Form, number=3, version=2, name='x') field = mommy.make(FormField, form=form, kind='info') new_field = mommy.prepare(FormField, kind='short-answer') new_field_data = model_to_dict(new_field) forms_service.update(3, 2, fields=[new_field_data]) self.assertRaises(FormField.DoesNotExist, FormField.objects.get, id=field.id) fields = FormField.objects.filter(form=form) self.assertEqual(len(fields), 1) self.assertEqual(fields[0].kind, 'short-answer')
def _set_request_data(self, form, *exclude, **update): data = {'form': model_to_dict(form, *exclude, **update)} new_request = self._create_request(data) new_request.user = self.request.user self.request = new_request
def test_raises_error_when_params_invalid(self): form_data = model_to_dict(mommy.prepare(Form)) self.assertRaises(ValidationError, forms_service.create, **form_data)
def test_creates_any_specified_form_fields(self): form = mommy.prepare(Form, user=mommy.make(User)) form_data = model_to_dict(form) form_data['fields'] = [{'form': form, 'kind': 'info'}] form = forms_service.create(**form_data) self.assertEqual(form.formfield_set.first().kind, 'info')
def test_increases_version_when_unspecified_and_number_specified(self): user = mommy.make(Form, number=3, version=4).user new_form = mommy.prepare(Form, number=3, user=user) form_data = model_to_dict(new_form, 'version') form = forms_service.create(**form_data) self.assertEqual(form.version, 5)
def test_increases_number_when_unspecified(self): user = mommy.make(Form, number=3).user new_form = mommy.prepare(Form, user=user) form_data = model_to_dict(new_form, 'number') form = forms_service.create(**form_data) self.assertEqual(form.number, 4)