Get jsonschema from questions JSON
This commit is contained in:
@@ -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')
|
||||
|
||||
Reference in New Issue
Block a user