Projet

Général

Profil

0002-api-move-serializers-43077.patch

Lauréline Guérin, 01 octobre 2020 16:22

Télécharger (6,43 ko)

Voir les différences:

Subject: [PATCH 2/4] api: move serializers (#43077)

 chrono/api/serializers.py | 56 +++++++++++++++++++++++++++++++++++++++
 chrono/api/views.py       | 48 ++++-----------------------------
 2 files changed, 61 insertions(+), 43 deletions(-)
 create mode 100644 chrono/api/serializers.py
chrono/api/serializers.py
1
# chrono - agendas system
2
# Copyright (C) 2020  Entr'ouvert
3
#
4
# This program is free software: you can redistribute it and/or modify it
5
# under the terms of the GNU Affero General Public License as published
6
# by the Free Software Foundation, either version 3 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU Affero General Public License for more details.
13
#
14
# You should have received a copy of the GNU Affero General Public License
15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16

  
17
from rest_framework import serializers
18

  
19

  
20
class SlotSerializer(serializers.Serializer):
21
    '''
22
    payload to fill one slot. The slot (event id) is in the URL.
23
    '''
24

  
25
    label = serializers.CharField(max_length=250, allow_blank=True)
26
    user_external_id = serializers.CharField(max_length=250, allow_blank=True)
27
    user_name = serializers.CharField(max_length=250, allow_blank=True)
28
    user_display_label = serializers.CharField(max_length=250, allow_blank=True)
29
    user_email = serializers.CharField(max_length=250, allow_blank=True)
30
    user_phone_number = serializers.CharField(max_length=16, allow_blank=True)
31
    form_url = serializers.CharField(max_length=250, allow_blank=True)
32
    backoffice_url = serializers.URLField(allow_blank=True)
33
    cancel_callback_url = serializers.URLField(allow_blank=True)
34
    count = serializers.IntegerField(min_value=1)
35
    cancel_booking_id = serializers.CharField(max_length=250, allow_blank=True, allow_null=True)
36
    force_waiting_list = serializers.BooleanField(default=False)
37

  
38

  
39
class StringOrListField(serializers.ListField):
40
    def to_internal_value(self, data):
41
        if isinstance(data, str):
42
            data = [s.strip() for s in data.split(',')]
43
        return super(StringOrListField, self).to_internal_value(data)
44

  
45

  
46
class SlotsSerializer(SlotSerializer):
47
    '''
48
    payload to fill multiple slots: same as SlotSerializer, but the
49
    slots list is in the payload.
50
    '''
51

  
52
    slots = StringOrListField(required=True, child=serializers.CharField(max_length=64, allow_blank=False))
53

  
54

  
55
class ResizeSerializer(serializers.Serializer):
56
    count = serializers.IntegerField(min_value=1)
chrono/api/views.py
32 32
from django.utils.translation import gettext_noop
33 33
from django.utils.translation import ugettext_lazy as _
34 34

  
35
from rest_framework import permissions, serializers, status
35
from rest_framework import permissions, status
36 36

  
37 37
from rest_framework.views import APIView
38 38

  
39 39
from chrono.api.utils import Response
40
from chrono.api import serializers
40 41
from ..agendas.models import Agenda, Event, Booking, MeetingType, TimePeriodException, Desk
41 42
from ..interval import IntervalSet
42 43

  
......
652 653
agenda_desk_list = AgendaDeskList.as_view()
653 654

  
654 655

  
655
class SlotSerializer(serializers.Serializer):
656
    '''
657
    payload to fill one slot. The slot (event id) is in the URL.
658
    '''
659

  
660
    label = serializers.CharField(max_length=250, allow_blank=True)
661
    user_external_id = serializers.CharField(max_length=250, allow_blank=True)
662
    user_name = serializers.CharField(max_length=250, allow_blank=True)
663
    user_display_label = serializers.CharField(max_length=250, allow_blank=True)
664
    user_email = serializers.CharField(max_length=250, allow_blank=True)
665
    user_phone_number = serializers.CharField(max_length=16, allow_blank=True)
666
    form_url = serializers.CharField(max_length=250, allow_blank=True)
667
    backoffice_url = serializers.URLField(allow_blank=True)
668
    cancel_callback_url = serializers.URLField(allow_blank=True)
669
    count = serializers.IntegerField(min_value=1)
670
    cancel_booking_id = serializers.CharField(max_length=250, allow_blank=True, allow_null=True)
671
    force_waiting_list = serializers.BooleanField(default=False)
672

  
673

  
674
class StringOrListField(serializers.ListField):
675
    def to_internal_value(self, data):
676
        if isinstance(data, str):
677
            data = [s.strip() for s in data.split(',')]
678
        return super(StringOrListField, self).to_internal_value(data)
679

  
680

  
681
class SlotsSerializer(SlotSerializer):
682
    '''
683
    payload to fill multiple slots: same as SlotSerializer, but the
684
    slots list is in the payload.
685
    '''
686

  
687
    slots = StringOrListField(required=True, child=serializers.CharField(max_length=64, allow_blank=False))
688

  
689

  
690 656
class Fillslots(APIView):
691 657
    permission_classes = (permissions.IsAuthenticated,)
692
    serializer_class = SlotsSerializer
658
    serializer_class = serializers.SlotsSerializer
693 659

  
694 660
    def post(self, request, agenda_identifier=None, event_identifier=None, format=None):
695 661
        try:
......
1054 1020

  
1055 1021

  
1056 1022
class Fillslot(Fillslots):
1057
    serializer_class = SlotSerializer
1023
    serializer_class = serializers.SlotSerializer
1058 1024

  
1059 1025
    def post(self, request, agenda_identifier=None, event_identifier=None, format=None):
1060 1026
        try:
......
1210 1176
suspend_booking = SuspendBooking.as_view()
1211 1177

  
1212 1178

  
1213
class ResizeSerializer(serializers.Serializer):
1214
    count = serializers.IntegerField(min_value=1)
1215

  
1216

  
1217 1179
class ResizeBooking(APIView):
1218 1180
    '''
1219 1181
    Resize a booking.
......
1225 1187
    '''
1226 1188

  
1227 1189
    permission_classes = (permissions.IsAuthenticated,)
1228
    serializer_class = ResizeSerializer
1190
    serializer_class = serializers.ResizeSerializer
1229 1191

  
1230 1192
    def post(self, request, booking_pk=None, format=None):
1231 1193
        serializer = self.serializer_class(data=request.data)
1232
-