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

✨ [#147] Convenience endpoint for Klantcontact/Betrokkene/Onderwerpobject #260

Draft
wants to merge 6 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
Expand Up @@ -209,7 +209,7 @@ class Meta:
},
}

def get_volledige_naam(self, obj):
def get_volledige_naam(self, obj) -> str:
return obj.get_full_name()

@transaction.atomic
Expand Down Expand Up @@ -367,7 +367,7 @@ class Meta:
extra_kwargs = {
"uuid": {"read_only": True},
"url": {
"view_name": "klantinteracties:klantcontact-detail",
"view_name": "klantinteracties:onderwerpobject-detail",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙈

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how this wasn't caught by tests 😬

"lookup_field": "uuid",
"help_text": _("De unieke URL van dit klantcontact binnen deze API."),
},
Expand Down Expand Up @@ -537,3 +537,59 @@ def create(self, validated_data):
)

return super().create(validated_data)


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

self.fields["had_klantcontact"].read_only = True


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

self.fields["klantcontact"].read_only = True


class KlantContactConvenienceSerializer(serializers.Serializer):
klantcontact = KlantcontactSerializer()
betrokkene = BetrokkeneConvenienceSerializer(required=False)
onderwerpobject = OnderwerpobjectConvenienceSerializer(required=False)

@transaction.atomic
def create(self, validated_data):
"""
Create the objects and use the original serializers to ensure all the correct
fields show up in the response
"""
klantcontact_data = validated_data["klantcontact"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens here when no klantcontact was given in the request data? Will it raise a KeyError?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it is required on the serializer, you will get a validation error:

{
  "type": "http://localhost:8000/ref/fouten/ValidationError/",
  "code": "invalid",
  "title": "Invalid input.",
  "status": 400,
  "detail": "",
  "instance": "urn:uuid:5f54a6d6-5327-4ad5-a831-27b7be3c1035",
  "invalidParams": [
    {
      "name": "klantcontact",
      "code": "required",
      "reason": "Dit veld is vereist."
    }
  ]
}

klantcontact = Klantcontact.objects.create(**klantcontact_data)

betrokkene = None
if betrokkene_data := validated_data.pop("betrokkene", None):
betrokkene_data["had_klantcontact"] = {"uuid": str(klantcontact.uuid)}
# TODO for some reason `was_partij` is converted to `partij` by the serializer
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is caused by the source option given to the was_partij field

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm I see, I don't think there's a way around this, since it always gets deserialized to partij right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I never knew this was how the source option behaved 😮. The comment can be removed though right?

betrokkene_data.setdefault(
"was_partij", betrokkene_data.get("partij", None)
)
betrokkene_serializer = BetrokkeneSerializer(data=betrokkene_data)
betrokkene_serializer.is_valid()
betrokkene = betrokkene_serializer.save()

onderwerpobject = None
if onderwerpobject_data := validated_data.pop("onderwerpobject", None):
onderwerpobject_data["klantcontact"] = {"uuid": str(klantcontact.uuid)}
onderwerpobject_data.setdefault("was_klantcontact", None)
onderwerpobject_serializer = OnderwerpobjectSerializer(
data=onderwerpobject_data
)
onderwerpobject_serializer.is_valid()
onderwerpobject = onderwerpobject_serializer.save()

return {
"klantcontact": klantcontact,
"betrokkene": betrokkene,
"onderwerpobject": onderwerpobject,
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class Meta:
},
}

def get_categorie_naam(self, obj):
def get_categorie_naam(self, obj) -> str | None:
if obj.categorie:
return obj.categorie.naam

Expand Down Expand Up @@ -301,7 +301,7 @@ class Meta:
"volledige_naam",
)

def get_volledige_naam(self, obj):
def get_volledige_naam(self, obj) -> str:
return obj.get_full_name()


Expand Down Expand Up @@ -333,7 +333,7 @@ class Meta:
"volledige_naam",
)

def get_volledige_naam(self, obj):
def get_volledige_naam(self, obj) -> str:
return obj.get_full_name()

@transaction.atomic
Expand Down
59 changes: 59 additions & 0 deletions src/openklant/components/klantinteracties/api/tests/factories.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice this saved the tests some code 👍

Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import factory


class KlantContactDataFactory(factory.DictFactory):
nummer = "7948723947"
kanaal = "changed"
onderwerp = "changed"
inhoud = "changed"
indicatieContactGelukt = False
taal = "de"
vertrouwelijk = False
plaatsgevondenOp = "2020-08-24T14:15:22Z"


class BezoekAdresDataFactory(factory.DictFactory):
nummeraanduidingId = "4a282b5c-16d7-401d-9737-28e98c865ab2"
adresregel1 = "adres1"
adresregel2 = "adres2"
adresregel3 = "adres3"
land = "6030"


class CorrespondentieAdresDataFactory(factory.DictFactory):
nummeraanduidingId = "c06918d9-899b-4d98-a10d-08436ebc6c20"
adresregel1 = "adres1"
adresregel2 = "adres2"
adresregel3 = "adres3"
land = "6030"


class ContactNaamDataFactory(factory.DictFactory):
voorletters = "P"
voornaam = "Phil"
voorvoegselAchternaam = ""
achternaam = "Bozeman"


class BetrokkeneDataFactory(factory.DictFactory):
wasPartij = None
bezoekadres = factory.SubFactory(BezoekAdresDataFactory)
correspondentieadres = factory.SubFactory(CorrespondentieAdresDataFactory)
contactnaam = factory.SubFactory(ContactNaamDataFactory)
rol = "vertegenwoordiger"
organisatienaam = "Whitechapel"
initiator = True


class OnderwerpObjectIdentificatorDataFactory(factory.DictFactory):
codeObjecttype = "codeObjecttype"
codeSoortObjectId = "codeSoortObjectId"
objectId = "objectId"
codeRegister = "codeRegister"


class OnderwerpObjectDataFactory(factory.DictFactory):
wasKlantcontact = None
onderwerpobjectidentificator = factory.SubFactory(
OnderwerpObjectIdentificatorDataFactory
)
Loading
Loading