From 1298ac3001b5c2350230bd96db9c44ebc547e81d Mon Sep 17 00:00:00 2001
From: Julian Leucker <leuckerj@gmail.com>
Date: Wed, 26 Oct 2022 18:19:02 +0200
Subject: [PATCH] Move creation and addition of fields and sections to separate
 methods

---
 django_forms_as_jsonschema/jsonschema.py | 95 +++++++++---------------
 1 file changed, 36 insertions(+), 59 deletions(-)

diff --git a/django_forms_as_jsonschema/jsonschema.py b/django_forms_as_jsonschema/jsonschema.py
index 5aa2abf..9ca2d92 100644
--- a/django_forms_as_jsonschema/jsonschema.py
+++ b/django_forms_as_jsonschema/jsonschema.py
@@ -1,5 +1,7 @@
 from django import forms
 
+from django_forms_as_jsonschema.layout import _Section
+
 
 class JSONSchema:
 
@@ -10,63 +12,8 @@ class JSONSchema:
             "properties": {},
         }
 
-    # example_json = {
-    #     "type": "object",
-    #     "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):
+    @staticmethod
+    def generate_field(field):
         new_field = {
             "type": "string",
             "title": str(field.label or ""),
@@ -162,6 +109,36 @@ class JSONSchema:
             ...
 
         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)
-- 
GitLab