-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#4396] Added the prefill available attributes functionality
This is based on retrieving the specific objecttype version and based on the JsonSchema we define the available attributes.
- Loading branch information
Showing
2 changed files
with
61 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from typing import Any, Iterable | ||
|
||
from referencing.jsonschema import ObjectSchema | ||
|
||
from openforms.registrations.contrib.objects_api.client import get_objecttypes_client | ||
|
||
|
||
def parse_schema_properties( | ||
schema: ObjectSchema, parent_key: str = "" | ||
) -> list[tuple[str, str]]: | ||
properties = [] | ||
|
||
if schema["type"] == "object": | ||
for prop, prop_schema in schema.get("properties", {}).items(): | ||
full_key = f"{parent_key}.{prop}" if parent_key else prop | ||
prop_type = prop_schema.get("type", "unknown") | ||
properties.append((full_key, prop_type)) | ||
if prop_type == "object" or ( | ||
prop_type == "array" and "items" in prop_schema | ||
): | ||
properties.extend(parse_schema_properties(prop_schema, full_key)) | ||
elif schema["type"] == "array": | ||
items_schema = schema.get("items", {}) | ||
if isinstance(items_schema, dict): | ||
properties.extend(parse_schema_properties(items_schema, f"{parent_key}[]")) | ||
elif isinstance(items_schema, list): | ||
for i, item_schema in enumerate(items_schema): | ||
properties.extend( | ||
parse_schema_properties(item_schema, f"{parent_key}[{i}]") | ||
) | ||
else: | ||
properties.append((parent_key, schema["type"])) | ||
|
||
# also remove the props of type object since it's not needed e.g.-> (name, object) | ||
# it's the parent prop of the object, we need the real properties and their types | ||
return [ | ||
(prop[0].replace(".", " > "), prop[1]) | ||
for prop in properties | ||
if prop[1] != "object" | ||
] | ||
|
||
|
||
def retrieve_properties( | ||
reference: dict[str, Any] | None = None, | ||
) -> Iterable[tuple[str, str]]: | ||
assert reference is not None | ||
|
||
with get_objecttypes_client(reference["objects_api_group"]) as client: | ||
json_schema = client.get_objecttype_version( | ||
reference["objects_api_objecttype_uuid"], | ||
reference["objects_api_objecttype_version"], | ||
)["jsonSchema"] | ||
|
||
properties = parse_schema_properties(json_schema) | ||
return [(prop[0], f"{prop[0]} ({prop[1]})") for prop in properties] | ||