diff --git a/webapp/questionSchema.json b/webapp/questionSchema.json new file mode 100644 index 0000000..25a274f --- /dev/null +++ b/webapp/questionSchema.json @@ -0,0 +1,291 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "definitions": { + "CheckboxQuestion": { + "properties": { + "id": { + "type": "string" + }, + "options": { + "properties": { + "enum": { + "items": { + "type": "string" + }, + "type": "array" + }, + "enumNames_en": { + "items": { + "type": "string" + }, + "type": "array" + }, + "enumNames_fi": { + "items": { + "type": "string" + }, + "type": "array" + } + }, + "type": "object" + }, + "required": { + "type": "boolean" + }, + "title_en": { + "type": "string" + }, + "title_fi": { + "type": "string" + }, + "type": { + "enum": [ + "checkbox" + ], + "type": "string" + } + }, + "type": "object" + }, + "EmailQuestion": { + "properties": { + "id": { + "type": "string" + }, + "required": { + "type": "boolean" + }, + "title_en": { + "type": "string" + }, + "title_fi": { + "type": "string" + }, + "type": { + "enum": [ + "email" + ], + "type": "string" + } + }, + "type": "object" + }, + "InfoQuestion": { + "properties": { + "description_en": { + "type": "string" + }, + "description_fi": { + "type": "string" + }, + "id": { + "type": "string" + }, + "required": { + "enum": [ + false + ], + "type": "boolean" + }, + "title_en": { + "type": "string" + }, + "title_fi": { + "type": "string" + }, + "type": { + "enum": [ + "info" + ], + "type": "string" + } + }, + "type": "object" + }, + "IntegerQuestion": { + "properties": { + "id": { + "type": "string" + }, + "options": { + "properties": { + "enum": { + "anyOf": [ + { + "items": [ + { + "items": { + "type": "string" + }, + "type": "array" + } + ], + "maxItems": 1, + "minItems": 1, + "type": "array" + }, + { + "items": [ + { + "items": { + "type": "string" + }, + "type": "array" + }, + { + "items": { + "type": "string" + }, + "type": "array" + } + ], + "maxItems": 2, + "minItems": 2, + "type": "array" + } + ] + } + }, + "type": "object" + }, + "required": { + "type": "boolean" + }, + "title_en": { + "type": "string" + }, + "title_fi": { + "type": "string" + }, + "type": { + "enum": [ + "integer" + ], + "type": "string" + } + }, + "type": "object" + }, + "NameQuestion": { + "properties": { + "id": { + "type": "string" + }, + "required": { + "type": "boolean" + }, + "title_en": { + "type": "string" + }, + "title_fi": { + "type": "string" + }, + "type": { + "enum": [ + "name" + ], + "type": "string" + } + }, + "type": "object" + }, + "RadioQuestion": { + "properties": { + "id": { + "type": "string" + }, + "options": { + "properties": { + "enum": { + "items": { + "type": "string" + }, + "type": "array" + }, + "enumNames_en": { + "items": { + "type": "string" + }, + "type": "array" + }, + "enumNames_fi": { + "items": { + "type": "string" + }, + "type": "array" + } + }, + "type": "object" + }, + "required": { + "enum": [ + true + ], + "type": "boolean" + }, + "title_en": { + "type": "string" + }, + "title_fi": { + "type": "string" + }, + "type": { + "enum": [ + "radiobutton" + ], + "type": "string" + } + }, + "type": "object" + }, + "TextQuestion": { + "properties": { + "id": { + "type": "string" + }, + "required": { + "type": "boolean" + }, + "title_en": { + "type": "string" + }, + "title_fi": { + "type": "string" + }, + "type": { + "enum": [ + "text" + ], + "type": "string" + } + }, + "type": "object" + } + }, + "items": { + "anyOf": [ + { + "$ref": "#/definitions/TextQuestion" + }, + { + "$ref": "#/definitions/InfoQuestion" + }, + { + "$ref": "#/definitions/IntegerQuestion" + }, + { + "$ref": "#/definitions/RadioQuestion" + }, + { + "$ref": "#/definitions/CheckboxQuestion" + }, + { + "$ref": "#/definitions/EmailQuestion" + }, + { + "$ref": "#/definitions/NameQuestion" + } + ] + }, + "type": "array" +} diff --git a/webapp/views.py b/webapp/views.py index c534caf..c66ecd2 100644 --- a/webapp/views.py +++ b/webapp/views.py @@ -1,5 +1,6 @@ """Webapp views.""" +import json from jwt import decode from jwt.exceptions import InvalidTokenError from django.utils import timezone @@ -29,6 +30,10 @@ from webapp.serializers import * from webapp.utils import admin_send_email_signupees, decode_base64_file +with open("./webapp/questionSchema.json", "r") as file: + QUESTION_SCHEMA = json.load(file) + + class SignupPermission(BasePermission): def has_permission(self, request, view): if request.method == "POST": @@ -125,9 +130,7 @@ class SignupFormViewSet(ModelViewSet): def create(self, request, *args, **kwargs): try: - schema = { - "type": "array", - } + schema = QUESTION_SCHEMA validate(instance=request.data["questions"], schema=schema) return super().create(request, *args, **kwargs) except ValidationError as err: