Skip to content

Commit

Permalink
[#4396] Added the attributes retrival functionality
Browse files Browse the repository at this point in the history
This is not the whole prefill plugin functionality. This commit handles
the procedures in order to be able to send to the frontend the available
attributes (for mapping the form variable with the objecttype attribute)
depending on the selected objecttype's version.
  • Loading branch information
vaszig committed Aug 27, 2024
1 parent 0ee299f commit 482bc20
Show file tree
Hide file tree
Showing 16 changed files with 620 additions and 29 deletions.
19 changes: 5 additions & 14 deletions src/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3692,7 +3692,7 @@ paths:
schema:
type: array
items:
$ref: '#/components/schemas/PrefillObjectsAPIAttribute'
$ref: '#/components/schemas/PrefillAttribute'
description: ''
headers:
X-Session-Expires-In:
Expand Down Expand Up @@ -8492,6 +8492,9 @@ components:
* `main` - Main
* `authorizee` - Authorizee
prefillOptions:
type: object
additionalProperties: {}
dataType:
allOf:
- $ref: '#/components/schemas/DataTypeEnum'
Expand Down Expand Up @@ -8522,6 +8525,7 @@ components:
- form
- key
- name
- prefillOptions
- source
FormVersion:
type: object
Expand Down Expand Up @@ -9582,19 +9586,6 @@ components:
description: |-
* `main` - Main
* `authorizee` - Authorizee
PrefillObjectsAPIAttribute:
type: object
properties:
value:
type: string
title: ID
description: The unique attribute identifier
label:
type: string
description: The human-readable name for an attribute.
required:
- label
- value
PrefillPlugin:
type: object
properties:
Expand Down
2 changes: 2 additions & 0 deletions src/openforms/forms/admin/form_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class FormVariableAdmin(admin.ModelAdmin):
"prefill_plugin",
"prefill_attribute",
"prefill_identifier_role",
"prefill_options",
"data_type",
"is_sensitive_data",
"initial_value",
Expand All @@ -31,6 +32,7 @@ class FormVariableAdmin(admin.ModelAdmin):
"prefill_plugin",
"prefill_attribute",
"prefill_identifier_role",
"prefill_options",
"data_type",
"data_format",
"is_sensitive_data",
Expand Down
69 changes: 68 additions & 1 deletion src/openforms/forms/api/serializers/form_variable.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
from collections import defaultdict

from django.db.models import Q
from django.urls import reverse
from django.utils.translation import gettext_lazy as _

from drf_spectacular.utils import OpenApiExample, extend_schema_serializer
from rest_framework import serializers
from rest_framework.exceptions import ValidationError

from openforms.api.fields import RelatedFieldFromContext
from openforms.api.fields import (
PrimaryKeyRelatedAsChoicesField,
RelatedFieldFromContext,
)
from openforms.api.serializers import ListWithChildSerializer
from openforms.formio.api.fields import FormioVariableKeyField
from openforms.registrations.contrib.objects_api.models import ObjectsAPIGroupConfig
from openforms.utils.mixins import JsonSchemaSerializerMixin
from openforms.variables.api.serializers import ServiceFetchConfigurationSerializer
from openforms.variables.constants import FormVariableSources
from openforms.variables.models import ServiceFetchConfiguration
Expand All @@ -16,6 +24,63 @@
from ...models import Form, FormDefinition, FormVariable


@extend_schema_serializer(
examples=[
OpenApiExample(
name="Variable mapping example",
value={
"variable_key": "a_component_variable",
"target_path": ["path", "to.the", "target"],
},
)
]
)
class ObjecttypeVariableMappingSerializer(serializers.Serializer):
"""A mapping between a form variable key and the corresponding Objecttype attribute."""

variable_key = FormioVariableKeyField(
label=_("variable key"),
help_text=_(
"The 'dotted' path to a form variable key. The format should comply to how Formio handles nested component keys."
),
)
target_path = serializers.ListField(
child=serializers.CharField(label=_("Segment of a JSON path")),
label=_("target path"),
help_text=_(
"Representation of the JSON target location as a list of string segments."
),
)


class FormVariableOptionsSerializer(JsonSchemaSerializerMixin, serializers.Serializer):
objects_api_group = PrimaryKeyRelatedAsChoicesField(
queryset=ObjectsAPIGroupConfig.objects.exclude(
Q(objects_service=None)
| Q(objecttypes_service=None)
| Q(drc_service=None)
| Q(catalogi_service=None)
),
label=("Objects API group"),
required=False,
help_text=_("Which Objects API group to use."),
)
objecttype_uuid = serializers.UUIDField(
label=_("objecttype"),
required=False,
help_text=_("UUID of the objecttype in the Objecttypes API. "),
)
objecttype_version = serializers.IntegerField(
label=_("objecttype version"),
help_text=_("Version of the objecttype in the Objecttypes API."),
)
variables_mapping = ObjecttypeVariableMappingSerializer(
label=_("variables mapping"),
many=True,
required=False,
)


class FormVariableListSerializer(ListWithChildSerializer):
def get_child_serializer_class(self):
return FormVariableSerializer
Expand Down Expand Up @@ -110,6 +175,7 @@ class FormVariableSerializer(serializers.HyperlinkedModelSerializer):
service_fetch_configuration = ServiceFetchConfigurationSerializer(
required=False, allow_null=True
)
prefill_options = serializers.DictField(label=_("prefill options"))

class Meta:
model = FormVariable
Expand All @@ -124,6 +190,7 @@ class Meta:
"prefill_plugin",
"prefill_attribute",
"prefill_identifier_role",
"prefill_options",
"data_type",
"data_format",
"is_sensitive_data",
Expand Down
21 changes: 21 additions & 0 deletions src/openforms/forms/api/typing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from typing import TYPE_CHECKING, Required, TypedDict


if TYPE_CHECKING:
from openforms.registrations.contrib.objects_api.models import ObjectsAPIGroupConfig


class _BasePrefillOptions(TypedDict):
prefill_plugin: str


class ObjecttypeVariableMapping(TypedDict):
variable_key: str
target_path: list[str]


class ObjectsAPIPrefillOptions(_BasePrefillOptions):
objects_api_group: Required[ObjectsAPIGroupConfig]
objecttype_uuid: Required[str]
objecttype_version: Required[int]
variables_mapping: Required[list[ObjecttypeVariableMapping]]
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 4.2.15 on 2024-08-21 11:04

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("forms", "0097_v267_to_v270"),
]

operations = [
migrations.AddField(
model_name="formvariable",
name="prefill_options",
field=models.JSONField(
blank=True, default=dict, verbose_name="prefill options"
),
),
]
5 changes: 5 additions & 0 deletions src/openforms/forms/models/form_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ class FormVariable(models.Model):
default=IdentifierRoles.main,
max_length=100,
)
prefill_options = models.JSONField(
_("prefill options"),
default=dict,
blank=True,
)
data_type = models.CharField(
verbose_name=_("data type"),
help_text=_("The type of the value that will be associated with this variable"),
Expand Down
Loading

0 comments on commit 482bc20

Please sign in to comment.