-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[#4396] Objects API prefill plugin #4566
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,4 @@ Prefill plugins | |
kvk | ||
stuf_bg | ||
suwinet | ||
objects_api |
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" | ||
), | ||
), | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,37 @@ | ||
from typing import Any | ||
|
||
from django.utils.translation import gettext_lazy as _ | ||
|
||
from drf_spectacular.utils import extend_schema, extend_schema_view | ||
from drf_spectacular.types import OpenApiTypes | ||
from drf_spectacular.utils import OpenApiParameter, extend_schema, extend_schema_view | ||
from rest_framework import authentication, permissions | ||
from rest_framework.exceptions import NotFound | ||
from rest_framework.views import APIView | ||
|
||
from openforms.api.views import ListMixin | ||
from openforms.registrations.contrib.objects_api.api.serializers import ( | ||
ObjectsAPIGroupInputSerializer, | ||
) | ||
from openforms.registrations.contrib.objects_api.client import get_objecttypes_client | ||
|
||
from ..registry import register | ||
from .serializers import ( | ||
ChoiceWrapper, | ||
PrefillAttributeSerializer, | ||
PrefillObjectsAPIAttributeSerializer, | ||
PrefillObjectsAPIObjecttypeSerializer, | ||
PrefillObjectsAPIObjecttypeVersionSerializer, | ||
PrefillPluginQueryParameterSerializer, | ||
PrefillPluginSerializer, | ||
) | ||
|
||
OBJECTS_API_GROUP_QUERY_PARAMETER = OpenApiParameter( | ||
name="objects_api_group", | ||
type=OpenApiTypes.STR, | ||
location=OpenApiParameter.QUERY, | ||
description=_("Which Objects API group to use."), | ||
) | ||
|
||
|
||
@extend_schema_view( | ||
get=extend_schema( | ||
|
@@ -67,3 +84,90 @@ def get_objects(self): | |
choices = plugin.get_available_attributes() | ||
|
||
return [ChoiceWrapper(choice) for choice in choices] | ||
|
||
|
||
@extend_schema_view( | ||
get=extend_schema(summary=_("List available objecttypes for Objects API")), | ||
parameters=[OBJECTS_API_GROUP_QUERY_PARAMETER], | ||
) | ||
class PluginObjectsAPIObjecttypeListView(ListMixin, APIView): | ||
""" | ||
List the available prefill objecttypes for Objects API plugin. | ||
""" | ||
|
||
authentication_classes = (authentication.SessionAuthentication,) | ||
permission_classes = (permissions.IsAdminUser,) | ||
serializer_class = PrefillObjectsAPIObjecttypeSerializer | ||
|
||
def get_objects(self) -> list[dict[str, Any]]: | ||
input_serializer = ObjectsAPIGroupInputSerializer( | ||
data=self.request.query_params | ||
) | ||
input_serializer.is_valid(raise_exception=True) | ||
|
||
config_group = input_serializer.validated_data["objects_api_group"] | ||
|
||
with get_objecttypes_client(config_group) as client: | ||
return client.list_objecttypes() | ||
|
||
|
||
@extend_schema_view( | ||
get=extend_schema(summary=_("List available objecttype versions for Objects API")), | ||
parameters=[OBJECTS_API_GROUP_QUERY_PARAMETER], | ||
) | ||
class PluginObjectsAPIObjecttypeVersionListView(ListMixin, APIView): | ||
""" | ||
List the available prefill objecttype versions for Objects API plugin. | ||
""" | ||
|
||
authentication_classes = (authentication.SessionAuthentication,) | ||
permission_classes = (permissions.IsAdminUser,) | ||
serializer_class = PrefillObjectsAPIObjecttypeVersionSerializer | ||
|
||
def get_objects(self): | ||
input_serializer = ObjectsAPIGroupInputSerializer( | ||
data=self.request.query_params | ||
) | ||
input_serializer.is_valid(raise_exception=True) | ||
|
||
config_group = input_serializer.validated_data["objects_api_group"] | ||
objecttype_uuid = self.kwargs["objects_api_objecttype_uuid"] | ||
|
||
with get_objecttypes_client(config_group) as client: | ||
return client.list_objecttype_versions(objecttype_uuid) | ||
|
||
|
||
@extend_schema_view( | ||
get=extend_schema(summary=_("List available attributes for Objects API")), | ||
parameters=[OBJECTS_API_GROUP_QUERY_PARAMETER], | ||
) | ||
class PluginObjectsAPIAttributesListView(ListMixin, APIView): | ||
""" | ||
List the available attributes for Objects API plugin. | ||
""" | ||
|
||
authentication_classes = (authentication.SessionAuthentication,) | ||
permission_classes = (permissions.IsAdminUser,) | ||
serializer_class = PrefillObjectsAPIAttributeSerializer | ||
|
||
def get_objects(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this method should return a list of tuples with each a label and value (e.g. choices)? EDIT: I just saw |
||
plugin = register["objects_api"] | ||
input_serializer = ObjectsAPIGroupInputSerializer( | ||
data=self.request.query_params | ||
) | ||
input_serializer.is_valid(raise_exception=True) | ||
|
||
config_group = input_serializer.validated_data["objects_api_group"] | ||
choices = plugin.get_available_attributes( | ||
reference={ | ||
"objects_api_group": config_group, | ||
"objects_api_objecttype_uuid": self.kwargs[ | ||
"objects_api_objecttype_uuid" | ||
], | ||
"objects_api_objecttype_version": self.kwargs[ | ||
"objects_api_objecttype_version" | ||
], | ||
} | ||
) | ||
|
||
return choices |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
""" | ||
Objects API prefill plugin. | ||
""" |
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 |
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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if we'll need this, this was probably from your initial changes that you made before? @vaszig There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, probably not |
||
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") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both of these views do already seem to exist for registrations, so we might be able to use those instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes I can use them as the base class and have the new one here for the different urls as well. I guess you mean I have to do the same for the serializers since some of them are the same with the ones in registrations.