Projet

Général

Profil

0003-api-move-serializers-43077.patch

Lauréline Guérin, 22 mai 2020 10:58

Télécharger (5,86 ko)

Voir les différences:

Subject: [PATCH 3/5] api: move serializers (#43077)

 chrono/api/serializers.py | 52 +++++++++++++++++++++++++++++++++++++++
 chrono/api/views.py       | 44 ++++-----------------------------
 2 files changed, 57 insertions(+), 39 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
    backoffice_url = serializers.URLField(allow_blank=True)
30
    count = serializers.IntegerField(min_value=1)
31
    cancel_booking_id = serializers.CharField(max_length=250, allow_blank=True, allow_null=True)
32
    force_waiting_list = serializers.BooleanField(default=False)
33

  
34

  
35
class StringOrListField(serializers.ListField):
36
    def to_internal_value(self, data):
37
        if isinstance(data, str):
38
            data = [s.strip() for s in data.split(',')]
39
        return super(StringOrListField, self).to_internal_value(data)
40

  
41

  
42
class SlotsSerializer(SlotSerializer):
43
    '''
44
    payload to fill multiple slots: same as SlotSerializer, but the
45
    slots list is in the payload.
46
    '''
47

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

  
50

  
51
class ResizeSerializer(serializers.Serializer):
52
    count = serializers.IntegerField(min_value=1)
chrono/api/views.py
30 30
from django.utils.translation import gettext_noop
31 31
from django.utils.translation import ugettext_lazy as _
32 32

  
33
from rest_framework import permissions, serializers, status
33
from rest_framework import permissions, status
34 34

  
35 35
from rest_framework.views import APIView
36 36

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

  
......
545 546
agenda_desk_list = AgendaDeskList.as_view()
546 547

  
547 548

  
548
class SlotSerializer(serializers.Serializer):
549
    '''
550
    payload to fill one slot. The slot (event id) is in the URL.
551
    '''
552

  
553
    label = serializers.CharField(max_length=250, allow_blank=True)
554
    user_external_id = serializers.CharField(max_length=250, allow_blank=True)
555
    user_name = serializers.CharField(max_length=250, allow_blank=True)
556
    user_display_label = serializers.CharField(max_length=250, allow_blank=True)
557
    backoffice_url = serializers.URLField(allow_blank=True)
558
    count = serializers.IntegerField(min_value=1)
559
    cancel_booking_id = serializers.CharField(max_length=250, allow_blank=True, allow_null=True)
560
    force_waiting_list = serializers.BooleanField(default=False)
561

  
562

  
563
class StringOrListField(serializers.ListField):
564
    def to_internal_value(self, data):
565
        if isinstance(data, str):
566
            data = [s.strip() for s in data.split(',')]
567
        return super(StringOrListField, self).to_internal_value(data)
568

  
569

  
570
class SlotsSerializer(SlotSerializer):
571
    '''
572
    payload to fill multiple slots: same as SlotSerializer, but the
573
    slots list is in the payload.
574
    '''
575

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

  
578

  
579 549
class Fillslots(APIView):
580 550
    permission_classes = (permissions.IsAuthenticated,)
581
    serializer_class = SlotsSerializer
551
    serializer_class = serializers.SlotsSerializer
582 552

  
583 553
    def post(self, request, agenda_identifier=None, event_identifier=None, format=None):
584 554
        try:
......
915 885

  
916 886

  
917 887
class Fillslot(Fillslots):
918
    serializer_class = SlotSerializer
888
    serializer_class = serializers.SlotSerializer
919 889

  
920 890
    def post(self, request, agenda_identifier=None, event_identifier=None, format=None):
921 891
        try:
......
1071 1041
suspend_booking = SuspendBooking.as_view()
1072 1042

  
1073 1043

  
1074
class ResizeSerializer(serializers.Serializer):
1075
    count = serializers.IntegerField(min_value=1)
1076

  
1077

  
1078 1044
class ResizeBooking(APIView):
1079 1045
    '''
1080 1046
    Resize a booking.
......
1086 1052
    '''
1087 1053

  
1088 1054
    permission_classes = (permissions.IsAuthenticated,)
1089
    serializer_class = ResizeSerializer
1055
    serializer_class = serializers.ResizeSerializer
1090 1056

  
1091 1057
    def post(self, request, booking_pk=None, format=None):
1092 1058
        serializer = self.serializer_class(data=request.data)
1093
-