Skip to content
Snippets Groups Projects
Commit 013e4fdc authored by florian's avatar florian
Browse files

MAINT: Move RT treatment to separate function

parent 54aab5dd
No related branches found
No related tags found
2 merge requests!89ENH: JsonSchemaExporter accepts do_not_create parameter.,!81F schema export references
......@@ -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
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