From 013e4fdc4fbef2ed17c26a7ab7368fe811441365 Mon Sep 17 00:00:00 2001
From: florian <f.spreckelsen@inidscale.com>
Date: Tue, 24 Oct 2023 10:49:45 +0200
Subject: [PATCH] MAINT: Move RT treatment to separate function

---
 src/caosadvancedtools/json_schema_exporter.py | 73 +++++++++++++------
 1 file changed, 50 insertions(+), 23 deletions(-)

diff --git a/src/caosadvancedtools/json_schema_exporter.py b/src/caosadvancedtools/json_schema_exporter.py
index 7bccae9d..4854fb52 100644
--- a/src/caosadvancedtools/json_schema_exporter.py
+++ b/src/caosadvancedtools/json_schema_exporter.py
@@ -120,6 +120,53 @@ def _make_text_property(description="", text_format=None, text_pattern=None):
     return prop
 
 
+def _retrieve_enum_values(role: str):
+
+    possible_values = db.execute_query(f"SELECT name, id FROM '{role}'")
+
+    vals = []
+
+    for val in possible_values:
+
+        if val.name:
+            vals.append(f"{val.id}, {val.name}")
+        else:
+            vals.append(f"{val.id}")
+
+    return vals
+
+
+def _treat_recordtype(rt: db.RecordType, additional_properties: bool = True,
+                      name_and_description_in_properties: bool = False,
+                      additional_options_for_text_props: Optional[dict] = None,
+                      units_in_description: bool = True):
+    schema = {
+        "type": "object"
+    }
+
+    schema["required"] = _make_required_list(rt)
+    schema["additionalProperties"] = additional_properties
+
+    props = {}
+    if name_and_description_in_properties:
+        props["name"] = _make_text_property("The name of the Record to be created")
+        props["description"] = _make_text_property("The description of the Record to be created")
+
+    for prop in rt.properties:
+        if prop.name in props:
+            # Multi property
+            raise NotImplementedError(
+                "Creating a schema for multi-properties is not specified. "
+                f"Property {prop.name} occurs more than once."
+            )
+        props[prop.name] = _make_prop_from_prop(
+            prop, additional_options_for_text_props, units_in_description)
+
+    schema["properties"] = props
+
+    return schema
+
+
 def recordtype_to_json_schema(rt: db.RecordType, additional_properties: bool = True,
                               name_and_description_in_properties: bool = False,
                               additional_options_for_text_props: Optional[dict] = None,
@@ -156,32 +203,12 @@ def recordtype_to_json_schema(rt: db.RecordType, additional_properties: bool = T
     if additional_options_for_text_props is None:
         additional_options_for_text_props = {}
 
-    schema = {
-        "$schema": "https://json-schema.org/draft/2019-09/schema",
-        "type": "object"
-    }
+    schema = _treat_recordtype(rt, additional_properties, name_and_description_in_properties,
+                               additional_options_for_text_props, units_in_description)
+    schema["$schema"] = "https://json-schema.org/draft/2019-09/schema"
     if rt.name:
         schema["title"] = rt.name
     if rt.description:
         schema["description"] = rt.description
 
-    schema["required"] = _make_required_list(rt)
-    schema["additionalProperties"] = additional_properties
-
-    props = {}
-    if name_and_description_in_properties:
-        props["name"] = _make_text_property("The name of the Record to be created")
-        props["description"] = _make_text_property("The description of the Record to be created")
-
-    for prop in rt.properties:
-        if prop.name in props:
-            # Multi property
-            raise NotImplementedError(
-                "Creating a schema for multi-properties is not specified. "
-                f"Property {prop.name} occurs more than once."
-            )
-        props[prop.name] = _make_prop_from_prop(
-            prop, additional_options_for_text_props, units_in_description)
-
-    schema["properties"] = props
     return schema
-- 
GitLab