-
Notifications
You must be signed in to change notification settings - Fork 3
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
base: master
Are you sure you want to change the base?
Changes from all commits
abec9a7
c4664f6
2483b95
74b50b2
13203d1
ba76269
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 | ||
---|---|---|---|---|
|
@@ -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 | ||||
|
@@ -367,7 +367,7 @@ class Meta: | |||
extra_kwargs = { | ||||
"uuid": {"read_only": True}, | ||||
"url": { | ||||
"view_name": "klantinteracties:klantcontact-detail", | ||||
"view_name": "klantinteracties:onderwerpobject-detail", | ||||
"lookup_field": "uuid", | ||||
"help_text": _("De unieke URL van dit klantcontact binnen deze API."), | ||||
}, | ||||
|
@@ -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"] | ||||
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. What happens here when no 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. Because it is required on the serializer, you will get a validation error:
|
||||
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 | ||||
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. It is caused by the open-klant/src/openklant/components/klantinteracties/api/serializers/klantcontacten.py Line 139 in 2963f4c
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. Hmm I see, I don't think there's a way around this, since it always gets deserialized to 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. Yeah, I never knew this was how the |
||||
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, | ||||
} |
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. 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 | ||
) |
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.
🙈
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.
Not sure how this wasn't caught by tests 😬