-
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] First attempt to implement objects_api prefill plugin
- Loading branch information
Showing
9 changed files
with
154 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,4 @@ Prefill plugins | |
kvk | ||
stuf_bg | ||
suwinet | ||
objects_api |
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
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,3 @@ | ||
""" | ||
Objects API prefill plugin. | ||
""" |
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,12 @@ | ||
from django.apps import AppConfig | ||
from django.utils.translation import gettext_lazy as _ | ||
|
||
|
||
class ObjectsApiApp(AppConfig): | ||
name = "openforms.prefill.contrib.objects_api" | ||
label = "objects_api" | ||
verbose_name = _("Objects API prefill plugin") | ||
|
||
def ready(self): | ||
# register the plugin | ||
from . import plugin # noqa |
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,19 @@ | ||
from django.db import models | ||
from django.utils.translation import gettext_lazy as _ | ||
|
||
|
||
class ObjectsAPIAttributes(models.TextChoices): | ||
url = "url", _("Url") | ||
uuid = "uuid", _("UUID") | ||
type = "type", _("Type") | ||
record_index = "record.index", _("Record > Index") | ||
record_typeVersion = "record.typeVersion", _("Record > Type version") | ||
|
||
record_data_ = "", _("Record > Data") | ||
|
||
record_geometry = "record.geometry", _("Record > Geometry") | ||
record_startAt = "record.startAt", _("Record > Start at") | ||
record_endAt = "record.endAt", _("Record > End at") | ||
record_registrationAt = "record.registrationAt", _("Record > Registration at") | ||
record_correctionFor = "record.correctionFor", _("Record > Correction for") | ||
record_correctedBy = "record_correctedBy", _("Record > Corrected by") |
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,108 @@ | ||
import logging | ||
from typing import Any, Iterable | ||
|
||
from click import option | ||
from django.urls import reverse | ||
from django.utils.translation import gettext_lazy as _ | ||
|
||
from glom import GlomError, glom | ||
|
||
from openforms.authentication.service import AuthAttribute | ||
from openforms.registrations.contrib.objects_api.client import ( | ||
get_objects_client, | ||
get_objecttypes_client, | ||
) | ||
from openforms.registrations.contrib.objects_api.models import ObjectsAPIGroupConfig | ||
from openforms.submissions.models import Submission | ||
from openforms.typing import JSONEncodable | ||
from ...base import BasePlugin | ||
from ...constants import IdentifierRoles | ||
from ...registry import register | ||
from .constants import ObjectsAPIAttributes | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
PLUGIN_IDENTIFIER = "objects_api" | ||
|
||
|
||
@register(PLUGIN_IDENTIFIER) | ||
class ObjectsAPIPrefill(BasePlugin): | ||
verbose_name = _("Objects API") | ||
requires_auth = AuthAttribute.bsn | ||
|
||
@classmethod | ||
def parse_schema_properties( | ||
cls, schema, 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( | ||
cls.parse_schema_properties(prop_schema, full_key) | ||
) | ||
elif schema["type"] == "array": | ||
items_schema = schema.get("items", {}) | ||
if isinstance(items_schema, dict): | ||
properties.extend( | ||
cls.parse_schema_properties(items_schema, f"{parent_key}[]") | ||
) | ||
elif isinstance(items_schema, list): | ||
for i, item_schema in enumerate(items_schema): | ||
properties.extend( | ||
cls.parse_schema_properties(item_schema, f"{parent_key}[{i}]") | ||
) | ||
else: | ||
properties.append((parent_key, schema["type"])) | ||
|
||
return properties | ||
|
||
@staticmethod | ||
def get_available_attributes() -> Iterable[tuple[str, str]]: | ||
# attrs = ObjectsAPIAttributes.choices | ||
# attrs += dynamic_properties | ||
pass | ||
|
||
@classmethod | ||
def get_prefill_values( | ||
cls, | ||
submission: Submission, | ||
attributes: list[str], | ||
identifier_role: IdentifierRoles = IdentifierRoles.main, | ||
) -> dict[str, JSONEncodable]: | ||
# for testing purposes, this will be defined in the modal by the user | ||
# frontend is not ready yet | ||
options = submission.registration_backend.options | ||
config = ObjectsAPIGroupConfig.objects.get(pk=options["objects_api_group"]) | ||
|
||
if object_uuid := submission.initial_data_reference: | ||
with get_objecttypes_client(config) as objecttypes_client: | ||
objecttype = objecttypes_client.get_objecttype_version( | ||
options["objecttype"], options["objecttype_version"] | ||
) | ||
|
||
if objecttype["status"] != "published": | ||
logger.warning( | ||
"object type '%s' is not published yet", objecttype["url"] | ||
) | ||
return {} | ||
|
||
if json_schema := objecttype.get("jsonSchema"): | ||
properties = cls.parse_schema_properties(json_schema) | ||
|
||
return {} | ||
|
||
@classmethod | ||
def get_co_sign_values( | ||
cls, submission: Submission, identifier: str | ||
) -> tuple[dict[str, Any], str]: | ||
pass | ||
|
||
def check_config(self): | ||
pass |
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