Move SignupForm validation schema building to front end
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
# Generated by Django 2.1.5 on 2020-07-23 19:18
|
||||
|
||||
import django.contrib.postgres.fields.jsonb
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('webapp', '0068_signupform_quota'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='signupform',
|
||||
name='schema',
|
||||
field=django.contrib.postgres.fields.jsonb.JSONField(default=[]),
|
||||
preserve_default=False,
|
||||
),
|
||||
]
|
||||
+1
-55
@@ -97,6 +97,7 @@ class SignupForm(models.Model):
|
||||
start_time = models.DateTimeField(default=timezone.now)
|
||||
end_time = models.DateTimeField(default=timezone.now)
|
||||
questions = JSONField()
|
||||
schema = JSONField()
|
||||
visible = models.BooleanField(default=True)
|
||||
quota = models.PositiveIntegerField(blank=True, null=True)
|
||||
|
||||
@@ -107,61 +108,6 @@ class SignupForm(models.Model):
|
||||
def signups(self):
|
||||
return Signup.objects.filter(signupForm=self)
|
||||
|
||||
@property
|
||||
def schema(self):
|
||||
questions = self.questions
|
||||
properties = {}
|
||||
|
||||
for q in questions:
|
||||
id = q["id"]
|
||||
question_type = q["type"]
|
||||
if question_type == "text":
|
||||
properties[id] = {
|
||||
"type": "string"
|
||||
}
|
||||
elif question_type == "email":
|
||||
# Format is just a "FYI" field, so we also have pattern.
|
||||
properties[id] = {
|
||||
"type": "string",
|
||||
"format": "email",
|
||||
"pattern": EMAIL_REGEX
|
||||
}
|
||||
elif question_type == "radiobutton":
|
||||
options = q["options"]
|
||||
regexes = map(lambda x: f"^{x}$", options)
|
||||
pattern = "|".join(regexes)
|
||||
properties[id] = {
|
||||
"type": "string",
|
||||
# Remove last regex or
|
||||
"pattern": pattern,
|
||||
|
||||
}
|
||||
elif question_type == "checkbox":
|
||||
options = q["options"]
|
||||
regexes = map(lambda x: f"^{x}$", options)
|
||||
pattern = "|".join(regexes)
|
||||
|
||||
properties[id] = {
|
||||
"type": "array",
|
||||
"uniqueItems": True,
|
||||
"maxItems": len(options),
|
||||
"items": {
|
||||
"type": "string",
|
||||
"pattern": pattern
|
||||
}
|
||||
}
|
||||
else:
|
||||
raise Exception("invalid question type!")
|
||||
|
||||
ids = list(map(lambda x: x["id"], questions))
|
||||
return {
|
||||
"type": "object",
|
||||
"required": ids,
|
||||
"minProperties": len(ids),
|
||||
"maxProperties": len(ids),
|
||||
"properties": properties
|
||||
}
|
||||
|
||||
class Meta:
|
||||
verbose_name = _('Signup form')
|
||||
verbose_name_plural = _('Signup forms')
|
||||
|
||||
@@ -48,7 +48,7 @@ class SignupFormSerializer(serializers.HyperlinkedModelSerializer):
|
||||
|
||||
class Meta:
|
||||
model = SignupForm
|
||||
fields = ('id', 'title', 'start_time', 'end_time', 'questions', 'signups', 'quota')
|
||||
fields = ('id', 'title', 'start_time', 'end_time', 'questions', 'schema', 'signups', 'quota')
|
||||
|
||||
|
||||
class EventSerializer(serializers.ModelSerializer):
|
||||
|
||||
Reference in New Issue
Block a user