0002-api-allow-updating-shared-custody-agenda-date-start-.patch
chrono/api/serializers.py | ||
---|---|---|
580 | 580 |
extra_kwargs = {'user_external_id': {'validators': []}} |
581 | 581 | |
582 | 582 | |
583 |
class SharedCustodyAgendaSerializer(serializers.Serializer): |
|
583 |
class SharedCustodyAgendaCreateSerializer(serializers.Serializer):
|
|
584 | 584 |
period_mirrors = { |
585 | 585 |
'even': 'odd', |
586 | 586 |
'odd': 'even', |
... | ... | |
715 | 715 |
def get_settings_url(self, obj): |
716 | 716 |
request = self.context.get('request') |
717 | 717 |
return request.build_absolute_uri(obj.get_settings_url()) |
718 | ||
719 | ||
720 |
class SharedCustodyAgendaSerializer(serializers.ModelSerializer): |
|
721 |
class Meta: |
|
722 |
model = SharedCustodyAgenda |
|
723 |
fields = ['date_start'] |
chrono/api/urls.py | ||
---|---|---|
117 | 117 |
url(r'^booking/(?P<booking_pk>\d+)/resize/$', views.resize_booking, name='api-resize-booking'), |
118 | 118 |
url(r'^booking/(?P<booking_pk>\d+)/ics/$', views.booking_ics, name='api-booking-ics'), |
119 | 119 |
url(r'^shared-custody/$', views.shared_custody_agendas, name='api-shared-custody-agendas'), |
120 |
url( |
|
121 |
r'^shared-custody/(?P<agenda_pk>\d+)/$', |
|
122 |
views.shared_custody_agenda, |
|
123 |
name='api-shared-custody-agenda', |
|
124 |
), |
|
120 | 125 |
url( |
121 | 126 |
r'^shared-custody/(?P<agenda_pk>\d+)/add-child/$', |
122 | 127 |
views.shared_custody_agenda_add_child, |
chrono/api/views.py | ||
---|---|---|
2956 | 2956 | |
2957 | 2957 |
class SharedCustodyAgendas(APIView): |
2958 | 2958 |
permission_classes = (permissions.IsAuthenticated,) |
2959 |
serializer_class = serializers.SharedCustodyAgendaSerializer |
|
2959 |
serializer_class = serializers.SharedCustodyAgendaCreateSerializer
|
|
2960 | 2960 | |
2961 | 2961 |
def post(self, request): |
2962 | 2962 |
serializer = self.serializer_class(data=request.data) |
... | ... | |
2975 | 2975 |
shared_custody_agendas = SharedCustodyAgendas.as_view() |
2976 | 2976 | |
2977 | 2977 | |
2978 |
class SharedCustodyAgendaAPI(APIView): |
|
2979 |
permission_classes = (permissions.IsAuthenticated,) |
|
2980 |
serializer_class = serializers.SharedCustodyAgendaSerializer |
|
2981 | ||
2982 |
def patch(self, request, agenda_pk): |
|
2983 |
agenda = get_object_or_404(SharedCustodyAgenda, pk=agenda_pk) |
|
2984 | ||
2985 |
serializer = self.serializer_class(agenda, data=request.data) |
|
2986 |
if not serializer.is_valid(): |
|
2987 |
raise APIErrorBadRequest(N_('invalid payload'), errors=serializer.errors) |
|
2988 |
agenda = serializer.save() |
|
2989 | ||
2990 |
return Response({'err': 0}) |
|
2991 | ||
2992 | ||
2993 |
shared_custody_agenda = SharedCustodyAgendaAPI.as_view() |
|
2994 | ||
2995 | ||
2978 | 2996 |
class SharedCustodyAgendaAddChild(APIView): |
2979 | 2997 |
permission_classes = (permissions.IsAuthenticated,) |
2980 | 2998 |
serializer_class = serializers.PersonSerializer |
tests/api/test_shared_custody.py | ||
---|---|---|
1 |
import datetime |
|
2 | ||
1 | 3 |
import pytest |
2 | 4 |
from django.core.files.base import ContentFile |
3 | 5 |
from django.utils.timezone import now |
... | ... | |
248 | 250 |
resp = app.post_json('/api/shared-custody/%s/add-child/' % other_agenda.pk, params=params) |
249 | 251 |
assert resp.json['err'] == 1 |
250 | 252 |
assert resp.json['err_desc'] == 'This child already has one custody agenda.' |
253 | ||
254 | ||
255 |
def test_shared_custody_agenda_update_date_start(app, user, settings): |
|
256 |
father = Person.objects.create(user_external_id='father_id', first_name='John', last_name='Doe') |
|
257 |
mother = Person.objects.create(user_external_id='mother_id', first_name='Jane', last_name='Doe') |
|
258 |
agenda = SharedCustodyAgenda.objects.create( |
|
259 |
first_guardian=father, second_guardian=mother, date_start=now() |
|
260 |
) |
|
261 | ||
262 |
app.authorization = ('Basic', ('john.doe', 'password')) |
|
263 |
resp = app.patch_json('/api/shared-custody/%s/' % agenda.pk, params={'date_start': '2020-10-20'}) |
|
264 | ||
265 |
agenda.refresh_from_db() |
|
266 |
assert agenda.date_start == datetime.date(year=2020, month=10, day=20) |
|
267 | ||
268 |
resp = app.patch_json('/api/shared-custody/%s/' % agenda.pk, params={'first_guardian': 'xxx'}, status=400) |
|
269 |
app.patch_json('/api/shared-custody/%s/' % agenda.pk, params={}, status=400) |
|
270 | ||
271 |
agenda.delete() |
|
272 |
app.patch_json('/api/shared-custody/1/', params={'date_start': '2020-10-20'}, status=404) |
|
251 |
- |