Skip to content

Commit

Permalink
🚚 [#4606] Move CatalogueSerializer to shared library code
Browse files Browse the repository at this point in the history
Objects API and ZGW APIs registration backends both use the same
option structure for the catalogue, so we can re-use the serializer to
have a single source of truth.
  • Loading branch information
sergei-maertens committed Aug 19, 2024
1 parent 401b543 commit 2750190
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 37 deletions.
44 changes: 44 additions & 0 deletions src/openforms/contrib/zgw/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from django.utils.translation import gettext_lazy as _

from rest_framework import serializers

from openforms.api.validators import AllOrNoneTruthyFieldsValidator
from openforms.utils.validators import RSINValidator, validate_uppercase


class CatalogueSerializer(serializers.Serializer):
"""
Specify which catalogue to use to look up case/document types.
Certain configuration models also define default fields like ``catalogue_domain``
and ``catalogue_rsin`` to fall back on, such as:
- :class:`openforms.registrations.contrib.objects_api.models.ObjectsAPIGroupConfig`
- :class:`openforms.registrations.contrib.zgw_apis.models.ZGWApiGroupConfig`
"""

domain = serializers.CharField(
label=_("domain"),
required=False,
max_length=5,
help_text=_(
"The 'domein' attribute for the Catalogus resource in the Catalogi API."
),
default="",
validators=[validate_uppercase],
)
rsin = serializers.CharField(
label=_("RSIN"),
required=False,
max_length=9,
validators=[RSINValidator()],
help_text=_(
"The 'rsin' attribute for the Catalogus resource in the Catalogi API."
),
default="",
)

class Meta:
validators = [
AllOrNoneTruthyFieldsValidator("domain", "rsin"),
]
39 changes: 2 additions & 37 deletions src/openforms/registrations/contrib/objects_api/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@

from openforms.api.fields import PrimaryKeyRelatedAsChoicesField
from openforms.api.utils import get_from_serializer_data_or_instance
from openforms.api.validators import AllOrNoneTruthyFieldsValidator
from openforms.contrib.zgw.serializers import CatalogueSerializer
from openforms.formio.api.fields import FormioVariableKeyField
from openforms.template.validators import DjangoTemplateValidator
from openforms.utils.mixins import JsonSchemaSerializerMixin
from openforms.utils.validators import RSINValidator, validate_rsin, validate_uppercase
from openforms.utils.validators import validate_rsin

from .client import get_catalogi_client, get_objecttypes_client
from .models import ObjectsAPIGroupConfig
Expand All @@ -26,41 +26,6 @@ class VersionChoices(IntegerChoices):
V2 = 2, _("v2, variables mapping")


class CatalogueSerializer(serializers.Serializer):
"""
Specify which catalogus to use to look up document types.
This matches the fields ``catalogue_domain`` and ``catalogue_rsin`` on the
:class:`ObjectsAPIGroupConfig` model.
"""

domain = serializers.CharField(
label=_("domain"),
required=False,
max_length=5,
help_text=_(
"The 'domein' attribute for the Catalogus resource in the Catalogi API."
),
default="",
validators=[validate_uppercase],
)
rsin = serializers.CharField(
label=_("RSIN"),
required=False,
max_length=9,
validators=[RSINValidator()],
help_text=_(
"The 'rsin' attribute for the Catalogus resource in the Catalogi API."
),
default="",
)

class Meta:
validators = [
AllOrNoneTruthyFieldsValidator("domain", "rsin"),
]


@extend_schema_serializer(
examples=[
OpenApiExample(
Expand Down

0 comments on commit 2750190

Please sign in to comment.