Skip to content
Snippets Groups Projects
Commit 1298ac30 authored by Julian's avatar Julian
Browse files

Move creation and addition of fields and sections to separate methods

parent 1ba1d2c8
No related branches found
No related tags found
No related merge requests found
from django import forms from django import forms
from django_forms_as_jsonschema.layout import _Section
class JSONSchema: class JSONSchema:
...@@ -10,63 +12,8 @@ class JSONSchema: ...@@ -10,63 +12,8 @@ class JSONSchema:
"properties": {}, "properties": {},
} }
# example_json = { @staticmethod
# "type": "object", def generate_field(field):
# "properties": {
# "stringProp": {
# "type": "string",
# "title": "I'm a string",
# "description": "This description is used as a help message."
# },
# "stringTextareaProp": {
# "type": "string",
# "title": "I'm a string in a textarea",
# "x-display": "textarea"
# },
# "numberProp": {
# "type": "number",
# "title": "I'm a number"
# },
# "integerProp": {
# "type": "integer",
# "title": "I'm an integer"
# },
# "integerSliderProp": {
# "type": "integer",
# "title": "I'm an integer in a slider",
# "x-display": "slider",
# "minimum": 0,
# "maximum": 5
# },
# "booleanProp": {
# "type": "boolean",
# "title": "I'm a boolean",
# "description": "This description is used as a help message."
# },
# "booleanSwitchProp": {
# "type": "boolean",
# "title": "I'm a boolean with switch display",
# "x-display": "switch",
# "description": "This description is used as a help message."
# },
# "stringArrayProp": {
# "type": "array",
# "title": "I'm an array of strings",
# "items": {
# "type": "string"
# }
# },
# "integerArrayProp": {
# "type": "array",
# "title": "I'm an array of integers",
# "items": {
# "type": "integer"
# }
# }
# }
# }
def add_field(self, name, field):
new_field = { new_field = {
"type": "string", "type": "string",
"title": str(field.label or ""), "title": str(field.label or ""),
...@@ -162,6 +109,36 @@ class JSONSchema: ...@@ -162,6 +109,36 @@ class JSONSchema:
... ...
else: else:
print(field, type(field), type(field.widget)) print("[Django-forms-as-jsonschema] Unsupported field/widget detected: ")
print(f"{field=}, {type(field)=}, {type(field.widget)=}")
return new_field
def add_field(self, name, field, metadata: dict = None):
new_field = self.generate_field(field)
if metadata and metadata.get("section_name") and self.schema["properties"].get(
metadata["section_name"]
) is not None:
self.schema["properties"][metadata["section_name"]]["properties"] = \
self.schema["properties"][metadata["section_name"]]["properties"] or {}
self.schema["properties"][metadata["section_name"]]["properties"][name] = new_field
else:
self.schema["properties"][name] = new_field
@staticmethod
def generate_section(section: _Section):
sec = {
"title": section.title,
"type": "object",
"properties": {}
}
if section.description:
sec["description"] = section.description
return sec
def add_section(self, section: _Section):
self.schema["properties"][section.codename] = self.generate_section(section)
self.schema["properties"][name] = new_field def update_properties(self, props: dict):
self.schema["properties"].update(props)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment