From d3436d8e0b05ecd7411d6ba822204afd32d4e394 Mon Sep 17 00:00:00 2001 From: Valentin Deniaud Date: Tue, 26 Jul 2022 10:16:11 +0200 Subject: [PATCH] api: allow updating shared custody agenda date start (#66932) --- chrono/api/serializers.py | 8 +++++++- chrono/api/urls.py | 5 +++++ chrono/api/views.py | 20 +++++++++++++++++++- tests/api/test_shared_custody.py | 23 +++++++++++++++++++++++ 4 files changed, 54 insertions(+), 2 deletions(-) diff --git a/chrono/api/serializers.py b/chrono/api/serializers.py index 93999d5a..eaba1f94 100644 --- a/chrono/api/serializers.py +++ b/chrono/api/serializers.py @@ -580,7 +580,7 @@ class PersonSerializer(serializers.ModelSerializer): extra_kwargs = {'user_external_id': {'validators': []}} -class SharedCustodyAgendaSerializer(serializers.Serializer): +class SharedCustodyAgendaCreateSerializer(serializers.Serializer): period_mirrors = { 'even': 'odd', 'odd': 'even', @@ -715,3 +715,9 @@ class SharedCustodyAgendaSerializer(serializers.Serializer): def get_settings_url(self, obj): request = self.context.get('request') return request.build_absolute_uri(obj.get_settings_url()) + + +class SharedCustodyAgendaSerializer(serializers.ModelSerializer): + class Meta: + model = SharedCustodyAgenda + fields = ['date_start'] diff --git a/chrono/api/urls.py b/chrono/api/urls.py index 0870f542..f1609733 100644 --- a/chrono/api/urls.py +++ b/chrono/api/urls.py @@ -117,6 +117,11 @@ urlpatterns = [ url(r'^booking/(?P\d+)/resize/$', views.resize_booking, name='api-resize-booking'), url(r'^booking/(?P\d+)/ics/$', views.booking_ics, name='api-booking-ics'), url(r'^shared-custody/$', views.shared_custody_agendas, name='api-shared-custody-agendas'), + url( + r'^shared-custody/(?P\d+)/$', + views.shared_custody_agenda, + name='api-shared-custody-agenda', + ), url( r'^shared-custody/(?P\d+)/add-child/$', views.shared_custody_agenda_add_child, diff --git a/chrono/api/views.py b/chrono/api/views.py index cc8fadc8..a559fd7c 100644 --- a/chrono/api/views.py +++ b/chrono/api/views.py @@ -2956,7 +2956,7 @@ booking_ics = BookingICS.as_view() class SharedCustodyAgendas(APIView): permission_classes = (permissions.IsAuthenticated,) - serializer_class = serializers.SharedCustodyAgendaSerializer + serializer_class = serializers.SharedCustodyAgendaCreateSerializer def post(self, request): serializer = self.serializer_class(data=request.data) @@ -2975,6 +2975,24 @@ class SharedCustodyAgendas(APIView): shared_custody_agendas = SharedCustodyAgendas.as_view() +class SharedCustodyAgendaAPI(APIView): + permission_classes = (permissions.IsAuthenticated,) + serializer_class = serializers.SharedCustodyAgendaSerializer + + def patch(self, request, agenda_pk): + agenda = get_object_or_404(SharedCustodyAgenda, pk=agenda_pk) + + serializer = self.serializer_class(agenda, data=request.data) + if not serializer.is_valid(): + raise APIErrorBadRequest(N_('invalid payload'), errors=serializer.errors) + agenda = serializer.save() + + return Response({'err': 0}) + + +shared_custody_agenda = SharedCustodyAgendaAPI.as_view() + + class SharedCustodyAgendaAddChild(APIView): permission_classes = (permissions.IsAuthenticated,) serializer_class = serializers.PersonSerializer diff --git a/tests/api/test_shared_custody.py b/tests/api/test_shared_custody.py index 145544c3..5f997b37 100644 --- a/tests/api/test_shared_custody.py +++ b/tests/api/test_shared_custody.py @@ -1,3 +1,5 @@ +import datetime + import pytest from django.core.files.base import ContentFile from django.utils.timezone import now @@ -248,3 +250,24 @@ def test_shared_custody_agenda_add_child(app, user, settings): resp = app.post_json('/api/shared-custody/%s/add-child/' % other_agenda.pk, params=params) assert resp.json['err'] == 1 assert resp.json['err_desc'] == 'This child already has one custody agenda.' + + +def test_shared_custody_agenda_update_date_start(app, user, settings): + father = Person.objects.create(user_external_id='father_id', first_name='John', last_name='Doe') + mother = Person.objects.create(user_external_id='mother_id', first_name='Jane', last_name='Doe') + agenda = SharedCustodyAgenda.objects.create( + first_guardian=father, second_guardian=mother, date_start=now() + ) + + app.authorization = ('Basic', ('john.doe', 'password')) + resp = app.patch_json('/api/shared-custody/%s/' % agenda.pk, params={'date_start': '2020-10-20'}) + assert resp.json['err'] == 0 + + agenda.refresh_from_db() + assert agenda.date_start == datetime.date(year=2020, month=10, day=20) + + resp = app.patch_json('/api/shared-custody/%s/' % agenda.pk, params={'first_guardian': 'xxx'}, status=400) + app.patch_json('/api/shared-custody/%s/' % agenda.pk, params={}, status=400) + + agenda.delete() + app.patch_json('/api/shared-custody/1/', params={'date_start': '2020-10-20'}, status=404)