Skip to content
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

[Draft] Feature/split digitaaladres #277

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from django.contrib import admin

from ..models.digitaal_adres import BetrokkeneAdres
from .digitaal_adres import BaseAdresAdminForm


class BetrokkeneAdresAdminForm(BaseAdresAdminForm):
class Meta:
model = BetrokkeneAdres
fields = "__all__"


@admin.register(BetrokkeneAdres)
class BetrokkeneAdresAdmin(admin.ModelAdmin):
readonly_fields = ("uuid",)
search_fields = ("adres",)
autocomplete_fields = ("betrokkene",)
form = BetrokkeneAdresAdminForm
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
from ..models.digitaal_adres import DigitaalAdres


class DigitaalAdresAdminForm(forms.ModelForm):
class BaseAdresAdminForm(forms.ModelForm):
class Meta:
model = DigitaalAdres
fields = "__all__"

def clean_adres(self):
Expand All @@ -16,6 +15,12 @@ def clean_adres(self):
return data["adres"]


class DigitaalAdresAdminForm(BaseAdresAdminForm):
class Meta:
model = DigitaalAdres
fields = "__all__"


@admin.register(DigitaalAdres)
class DigitaalAdresAdmin(admin.ModelAdmin):
readonly_fields = ("uuid",)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
from openklant.components.klantinteracties.models.digitaal_adres import DigitaalAdres
from openklant.components.utils.filters import ExpandFilter


class DigitaalAdresDetailFilterSet(FilterSet):
expand = ExpandFilter(serializer_class=DigitaalAdresSerializer)
# class DigitaalAdresDetailFilterSet(FilterSet):
# expand = ExpandFilter(serializer_class=DigitaalAdresSerializer)


class DigitaalAdresFilterSet(FilterSet):
Expand Down Expand Up @@ -74,7 +73,7 @@ class DigitaalAdresFilterSet(FilterSet):
),
)

expand = ExpandFilter(serializer_class=DigitaalAdresSerializer)
# expand = ExpandFilter(serializer_class=DigitaalAdresSerializer)

class Meta:
model = DigitaalAdres
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
from django.db import transaction
from django.utils.translation import gettext_lazy as _

from rest_framework import serializers

from openklant.components.klantinteracties.api.serializers.constants import (
SERIALIZER_PATH,
)
from openklant.components.klantinteracties.api.validators import (
OptionalEmailValidator,
betrokkene_adres_exists,
)
from openklant.components.klantinteracties.models.digitaal_adres import BetrokkeneAdres
from openklant.components.klantinteracties.models.klantcontacten import Betrokkene
from openklant.utils.serializers import get_field_value


class BetrokkeneAdresForeignKeySerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = BetrokkeneAdres
fields = (
"uuid",
"url",
)
extra_kwargs = {
"uuid": {"required": True, "validators": [betrokkene_adres_exists]},
"url": {
"view_name": "klantinteracties:betrokkeneadres-detail",
"lookup_field": "uuid",
"help_text": _(
"De unieke URL van dit betrokkene adres binnen deze API."
),
},
}


class BetrokkeneAdresSerializer(serializers.HyperlinkedModelSerializer):
from openklant.components.klantinteracties.api.serializers.klantcontacten import (
BetrokkeneForeignKeySerializer,
)

verstrekt_door_betrokkene = BetrokkeneForeignKeySerializer(
required=True,
allow_null=True,
help_text=_(
"Digitaal adres dat een betrokkene bij klantcontact verstrekte voor gebruik bij "
"opvolging van een klantcontact."
),
source="betrokkene",
)

inclusion_serializers = {
# 1 level
"verstrekt_door_betrokkene": f"{SERIALIZER_PATH}.klantcontacten.BetrokkeneSerializer",
# 2 levels
"verstrekt_door_betrokkene.had_klantcontact": f"{SERIALIZER_PATH}.klantcontacten.KlantcontactSerializer",
# 3 levels
"verstrekt_door_betrokkene.had_klantcontact.leidde_tot_interne_taken": f"{SERIALIZER_PATH}"
".internetaken.InterneTaakSerializer",
}

class Meta:
model = BetrokkeneAdres
fields = (
"uuid",
"url",
"verstrekt_door_betrokkene",
"adres",
"soort_digitaal_adres",
"omschrijving",
)
extra_kwargs = {
"uuid": {"read_only": True},
"url": {
"view_name": "klantinteracties:betrokkeneadres-detail",
"lookup_field": "uuid",
"help_text": _(
"De unieke URL van dit betrokkene adres binnen deze API."
),
},
}

def validate_adres(self, adres):
"""
Define the validator here, to avoid DRF spectacular marking the format for
`adres` as `email`
"""
soort_digitaal_adres = get_field_value(
self, self.initial_data, "soort_digitaal_adres"
)
OptionalEmailValidator()(adres, soort_digitaal_adres)
return adres

@transaction.atomic
def update(self, instance, validated_data):
if "betrokkene" in validated_data:
if betrokkene := validated_data.pop("betrokkene", None):
betrokkene = Betrokkene.objects.get(uuid=str(betrokkene.get("uuid")))

validated_data["betrokkene"] = betrokkene

return super().update(instance, validated_data)

@transaction.atomic
def create(self, validated_data):
if betrokkene := validated_data.pop("betrokkene", None):
validated_data["betrokkene"] = Betrokkene.objects.get(
uuid=str(betrokkene.get("uuid"))
)

return super().create(validated_data)
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
from django.db import transaction
from django.utils.translation import gettext_lazy as _

from rest_framework import serializers
from rest_framework import serializers, validators

from openklant.components.klantinteracties.api.serializers.constants import (
SERIALIZER_PATH,
)
from openklant.components.klantinteracties.api.validators import (
OptionalEmailValidator,
digitaal_adres_exists,
Expand Down Expand Up @@ -34,9 +31,6 @@ class Meta:


class DigitaalAdresSerializer(serializers.HyperlinkedModelSerializer):
from openklant.components.klantinteracties.api.serializers.klantcontacten import (
BetrokkeneForeignKeySerializer,
)
from openklant.components.klantinteracties.api.serializers.partijen import (
PartijForeignKeySerializer,
)
Expand All @@ -50,35 +44,16 @@ class DigitaalAdresSerializer(serializers.HyperlinkedModelSerializer):
),
source="partij",
)
verstrekt_door_betrokkene = BetrokkeneForeignKeySerializer(
required=True,
allow_null=True,
help_text=_(
"Digitaal adres dat een betrokkene bij klantcontact verstrekte voor gebruik bij "
"opvolging van een klantcontact."
),
source="betrokkene",
)

inclusion_serializers = {
# 1 level
"verstrekt_door_betrokkene": f"{SERIALIZER_PATH}.klantcontacten.BetrokkeneSerializer",
# 2 levels
"verstrekt_door_betrokkene.had_klantcontact": f"{SERIALIZER_PATH}.klantcontacten.KlantcontactSerializer",
# 3 levels
"verstrekt_door_betrokkene.had_klantcontact.leidde_tot_interne_taken": f"{SERIALIZER_PATH}"
".internetaken.InterneTaakSerializer",
}

class Meta:
model = DigitaalAdres
fields = (
"uuid",
"url",
"verstrekt_door_betrokkene",
"verstrekt_door_partij",
"adres",
"soort_digitaal_adres",
"is_standaard_adres",
"omschrijving",
)
extra_kwargs = {
Expand All @@ -90,6 +65,20 @@ class Meta:
},
}

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

if "soort_digitaal_adres" in self.fields:
# Avoid validating the UniqueConstraint for `soort_digitaal_adres` with
# `is_standaard_adres=True`. We want to enforce the constraint at the database
# level, but not via the API, because setting a new default sets all other
# `is_standaard_adres=False` (via DigitaalAdres.save)
self.fields["soort_digitaal_adres"].validators = [
validator
for validator in self.fields["soort_digitaal_adres"].validators
if not isinstance(validator, validators.UniqueValidator)
]

def validate_adres(self, adres):
"""
Define the validator here, to avoid DRF spectacular marking the format for
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
from openklant.components.klantinteracties.api.serializers.constants import (
SERIALIZER_PATH,
)
from openklant.components.klantinteracties.api.serializers.digitaal_adres import (
DigitaalAdresForeignKeySerializer,
)
from openklant.components.klantinteracties.api.validators import (
FKUniqueTogetherValidator,
betrokkene_exists,
Expand Down Expand Up @@ -129,6 +126,9 @@ class Meta:
class BetrokkeneSerializer(
NestedGegevensGroepMixin, serializers.HyperlinkedModelSerializer
):
from openklant.components.klantinteracties.api.serializers.betrokkene_adres import (
BetrokkeneAdresForeignKeySerializer,
)
from openklant.components.klantinteracties.api.serializers.partijen import (
PartijForeignKeySerializer,
)
Expand All @@ -147,7 +147,7 @@ class BetrokkeneSerializer(
),
source="klantcontact",
)
digitale_adressen = DigitaalAdresForeignKeySerializer(
digitale_adressen = BetrokkeneAdresForeignKeySerializer(
read_only=True,
help_text=_("Digitale adressen van de betrokkene bij klantcontact."),
source="digitaaladres_set",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
from openklant.components.klantinteracties.api.serializers.digitaal_adres import (
DigitaalAdresForeignKeySerializer,
)
from openklant.components.klantinteracties.api.serializers.klantcontacten import (
BetrokkeneForeignKeySerializer,
)

# from openklant.components.klantinteracties.api.serializers.klantcontacten import (
# BetrokkeneForeignKeySerializer,
# )
from openklant.components.klantinteracties.api.validators import (
FKUniqueTogetherValidator,
categorie_exists,
Expand Down Expand Up @@ -418,6 +419,9 @@ def create(self, validated_data):


class PartijSerializer(NestedGegevensGroepMixin, PolymorphicSerializer):
from openklant.components.klantinteracties.api.serializers.klantcontacten import (
BetrokkeneForeignKeySerializer,
)
from openklant.components.klantinteracties.api.serializers.rekeningnummers import (
RekeningnummerForeignKeySerializer,
)
Expand Down
Loading
Loading