Skip to content

Commit

Permalink
[#4031] Set geometry as part of the JSON Schema
Browse files Browse the repository at this point in the history
  • Loading branch information
Viicos committed Apr 3, 2024
1 parent 7dbc779 commit d48a379
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 27 deletions.
7 changes: 6 additions & 1 deletion src/openforms/contrib/objects_api/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@ def prepare_data_for_registration(
record_data: dict[str, Any],
objecttype: str,
objecttype_version: int,
geometry_data: dict[str, Any] | None = None,
) -> dict[str, Any]:
"""Prepare the submission data for sending it to the Objects API."""

return {
data = {
"type": objecttype,
"record": {
"typeVersion": objecttype_version,
"data": record_data,
"startAt": get_today(),
},
}
if geometry_data is not None:
data["record"]["geometry"] = geometry_data

return data
14 changes: 13 additions & 1 deletion src/openforms/registrations/contrib/objects_api/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,22 @@ def post(self, request: Request, *args: Any, **kwargs: Any):

with get_objecttypes_client() as client:

json_schema = client.get_objecttype_version(
allow_geometry = client.get_objecttype(objecttype_uuid).get(
"allowGeometry", True
)

_json_schema = client.get_objecttype_version(
objecttype_uuid, input_serializer.validated_data["objecttype_version"]
)["jsonSchema"]

json_schema = {
"type": "object",
"properties": {"data": {"type": "object", "properties": _json_schema}},
}

if allow_geometry:
json_schema["properties"]["geometry"] = {"type": "object"}

return_data = [
{
"target_path": json_path.segments,
Expand Down
12 changes: 1 addition & 11 deletions src/openforms/registrations/contrib/objects_api/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,23 +150,13 @@ class ObjectsAPIOptionsSerializer(JsonSchemaSerializerMixin, serializers.Seriali
required=False,
)

# As `record.geometry` is outside `record.data`, we special case this attribute:
geometry_variable_key = FormioVariableKeyField(
label=_("geometry variable"),
help_text=_(
"The 'dotted' path to a form variable key that should be mapped to the `record.geometry` attribute."
),
required=False,
allow_blank=True,
)

def validate(self, attrs: dict[str, Any]) -> dict[str, Any]:
v1_only_fields = {
"productaanvraag_type",
"content_json",
"payment_status_update_json",
}
v2_only_fields = {"variables_mapping", "geometry_variable_key"}
v2_only_fields = {"variables_mapping"}

version = get_from_serializer_data_or_instance("version", attrs, self)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
SubmissionFileAttachment,
SubmissionReport,
)
from openforms.typing import JSONObject
from openforms.variables.constants import FormVariableSources
from openforms.variables.service import get_static_variables
from openforms.variables.utils import get_variables_for_context
Expand Down Expand Up @@ -363,10 +362,10 @@ def get_update_payment_status_data(
class ObjectsAPIV2Handler(ObjectsAPIRegistrationHandler[RegistrationOptionsV2]):

@staticmethod
def _get_record_data(
def _get_payload_data(
variables_values: FormioData, variables_mapping: list[ObjecttypeVariableMapping]
) -> JSONObject:
record_data: JSONObject = {}
) -> dict[str, Any]:
payload_data: dict[str, Any] = {}

for mapping in variables_mapping:
variable_key = mapping["variable_key"]
Expand All @@ -383,9 +382,9 @@ def _get_record_data(
if isinstance(value, (datetime, date)):
value = value.isoformat()

glom.assign(record_data, glom.Path(*target_path), value, missing=dict)
glom.assign(payload_data, glom.Path(*target_path), value, missing=dict)

return record_data
return payload_data

@staticmethod
def _process_value(value: Any, component: Component) -> Any:
Expand Down Expand Up @@ -455,17 +454,15 @@ def get_object_data(

variables_values = FormioData({**dynamic_values, **static_values})
variables_mapping = options["variables_mapping"]
record_data = self._get_record_data(variables_values, variables_mapping)
payload_data = self._get_payload_data(variables_values, variables_mapping)

object_data = prepare_data_for_registration(
record_data=record_data,
record_data=payload_data.get("data", {}),
geometry_data=payload_data.get("geometry"),
objecttype=options["objecttype"],
objecttype_version=options["objecttype_version"],
)

if geometry_variable_key := options.get("geometry_variable_key"):
object_data["record"]["geometry"] = variables_values[geometry_variable_key]

return object_data

@override
Expand All @@ -485,10 +482,11 @@ def get_update_payment_status_data(

variables_values = FormioData(values)
variables_mapping = options["variables_mapping"]
record_data = self._get_record_data(variables_values, variables_mapping)
payload_data = self._get_payload_data(variables_values, variables_mapping)

object_data = prepare_data_for_registration(
record_data=record_data,
record_data=payload_data.get("data", {}),
geometry_data=payload_data.get("geometry"),
objecttype=options["objecttype"],
objecttype_version=options["objecttype_version"],
)
Expand Down
1 change: 0 additions & 1 deletion src/openforms/registrations/contrib/objects_api/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ class ObjecttypeVariableMapping(TypedDict):
class RegistrationOptionsV2(_BaseRegistrationOptions, total=False):
version: Required[Literal[2]]
variables_mapping: Required[list[ObjecttypeVariableMapping]]
geometry_variable_key: str


RegistrationOptions: TypeAlias = RegistrationOptionsV1 | RegistrationOptionsV2
Expand Down

0 comments on commit d48a379

Please sign in to comment.