api: allow updating shared custody agenda date start (#66932)
This commit is contained in:
parent
c38817fdf4
commit
d3436d8e0b
|
@ -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']
|
||||
|
|
|
@ -117,6 +117,11 @@ urlpatterns = [
|
|||
url(r'^booking/(?P<booking_pk>\d+)/resize/$', views.resize_booking, name='api-resize-booking'),
|
||||
url(r'^booking/(?P<booking_pk>\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<agenda_pk>\d+)/$',
|
||||
views.shared_custody_agenda,
|
||||
name='api-shared-custody-agenda',
|
||||
),
|
||||
url(
|
||||
r'^shared-custody/(?P<agenda_pk>\d+)/add-child/$',
|
||||
views.shared_custody_agenda_add_child,
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue