diff --git a/webapp/models.py b/webapp/models.py index c84bdd4..f81f8f7 100644 --- a/webapp/models.py +++ b/webapp/models.py @@ -100,6 +100,52 @@ class SignupForm(models.Model): def __str__(self): return _('#{} {}').format(self.id, self.title) + @property + def schema(self): + questions = self.questions + ids = list(map(lambda x: x["id"], questions)) + properties = { + "required": ids, + "minProperties": len(ids), + "maxProperties": len(ids) + } + + for q in questions: + id = q["id"] + question_type = q["type"] + if question_type == "text": + properties[id] = { + "type": "string" + } + elif question_type == "radiobutton": + options = q["options"] + pattern = "" + map(lambda x: pattern.join(f"^{x}$|"), options) + properties[id] = { + "type": "string", + # Remove last regex or + "pattern": pattern[:-1], + + } + elif question_type == "checkbox": + options = q["options"] + pattern = "" + map(lambda x: pattern.join(f"^{x}$|"), options) + + properties[id] = { + "type": "array", + "uniqueItems": True, + "maxItems": len(options), + "items": { + "type": "string", + "pattern": pattern[:-1] + } + } + else: raise Exception("invalid question type!") + + + return properties + class Meta: verbose_name = _('Signup form') verbose_name_plural = _('Signup forms') diff --git a/webapp/tests/test_signup.py b/webapp/tests/test_signup.py index 66b0427..845aa99 100644 --- a/webapp/tests/test_signup.py +++ b/webapp/tests/test_signup.py @@ -20,7 +20,7 @@ class SignupTestCase(APITestCase): self.hiddenForm = createSignupForm(visible=False) self.signup1 = createSignupObject(self.signupForm, ALL_QUESTION_TYPES) - self.signup2 = createSignupObject(self.signupForm, []) + self.signup2 = createSignupObject(self.signupForm, ALL_QUESTION_TYPES) username, password = "test_admin", "password123" self.authClient = User.objects.create_superuser(username, "myemail@test.com", password) @@ -58,7 +58,6 @@ class SignupTestCase(APITestCase): def test_create_signup(self): new = createSignupJSON(self.signupForm.id, ALL_QUESTION_TYPES_ANSWER) response = self.client.post(URL, new, format="json") - print(response.data) self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(Signup.objects.count(), 3) @@ -77,10 +76,11 @@ class SignupTestCase(APITestCase): def test_get_hidden_forms_admin(self): pass - @skip("NotImplemented") def test_create_malformed_answer(self): - response = self.client.post(URL, createSignupJSON(self.signupForm.id, []), format="json") + malformed = createSignupJSON(self.signupForm.id, []) + response = self.client.post(URL, malformed, format="json") self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) + self.assertEqual(Signup.objects.count(), 2) # Update and Delete are available for super admin (Django Admin) # and to the user that signed up (uid token) diff --git a/webapp/views.py b/webapp/views.py index 9b60b43..136bc82 100644 --- a/webapp/views.py +++ b/webapp/views.py @@ -84,8 +84,13 @@ class SignupViewSet(viewsets.ModelViewSet): try: form = SignupForm.objects.get(id=request.data["signupForm_id"]) if (form.visible): + signup = json.loads(request.data["answer"]) + # Throws error if not valid + print(form.schema) + validate(instance=signup, schema=form.schema) return super().create(request, *args, **kwargs) - except: + except Exception as inst: + print(inst) return HttpResponseBadRequest() else: return HttpResponseBadRequest()