Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • AlekSIS/libs/django-forms-as-jsonschema
1 result
Show changes
Commits on Source (3)
from django.forms import Form
from django_forms_as_jsonschema.schema import Schema
from .jsonschema import JSONSchema
class JSONSchemaFormMixin:
def as_jsonschema(self: Form) -> str:
schema = Schema()
def as_jsonschema(self: Form) -> dict:
schema = JSONSchema()
for name, field in self.fields.items():
schema.add_field(name, field)
return schema.schema
import copy
import json
from django import forms
class Schema:
SCHEMA = {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {},
}
class JSONSchema:
def __init__(self):
self.schema = copy.deepcopy(self.SCHEMA)
self.schema = {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {},
}
# example_json = {
# "type": "object",
......@@ -70,40 +66,38 @@ class Schema:
# }
# }
def build(self):
return json.dumps(self.schema)
def add_field(self, name, field):
type_ = "string"
format_ = None
x_display = None
enum = None
new_field = {
"type": "string",
"title": str(field.label),
"description": str(field.help_text),
}
# string, number, integer, boolean.
match type(field.widget):
case forms.TextInput:
type_ = "string"
new_field["type"] = "string"
case forms.NumberInput:
type_ = "number"
new_field["type"] = "integer" if type(field) == forms.IntegerField else "number"
case forms.EmailInput:
type_ = "string"
format_ = "email"
new_field["type"] = "string"
new_field["format"] = "email"
case forms.URLInput:
type_ = "string"
format_ = "url"
new_field["type"] = "string"
new_field["format"] = "url"
case forms.PasswordInput:
type_ = "string"
format_ = "password"
x_display = "password"
new_field["type"] = "string"
new_field["format"] = "password"
new_field["x-display"] = "password"
case forms.HiddenInput:
type_ = "string"
format_ = "hidden"
new_field["type"] = "string"
new_field["format"] = "hidden"
case forms.MultipleHiddenInput:
...
......@@ -115,26 +109,26 @@ class Schema:
...
case forms.Textarea:
x_display = "textarea"
new_field["x-display"] = "textarea"
case forms.DateInput:
type_ = "string"
format_ = "date"
new_field["type"] = "string"
new_field["format"] = "date"
case forms.DateTimeInput:
type_ = "string"
format_ = "date-time"
new_field["type"] = "string"
new_field["format"] = "date-time"
case forms.TimeInput:
type_ = "string"
format_ = "time"
new_field["type"] = "string"
new_field["format"] = "time"
case forms.CheckboxInput:
type_ = "boolean"
new_field["type"] = "boolean"
case forms.Select:
type_ = "string"
enum = [] # Fixme: load data from widget.options(…)
new_field["type"] = "string"
new_field["enum"] = [] # Fixme: load data from widget.options(…)
case forms.NullBooleanSelect:
...
......@@ -157,14 +151,4 @@ class Schema:
case forms.SelectDateWidget:
...
self.schema["properties"][name] = {
"type": type_,
"title": str(field.label),
"description": str(field.help_text),
}
if x_display:
self.schema["properties"][name]["x-display"] = x_display
if format_:
self.schema["properties"][name]["format"] = format_
if enum:
self.schema["properties"][name]["enum"] = enum
self.schema["properties"][name] = new_field