From 7ba9d3373f37281431b8171b6bbea5c71670f8c1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Wed, 20 May 2020 11:45:10 +0200
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

diff --git a/chrono/api/serializers.py b/chrono/api/serializers.py
new file mode 100644
index 0000000..37b996b
--- /dev/null
+++ b/chrono/api/serializers.py
@@ -0,0 +1,56 @@
+# chrono - agendas system
+# Copyright (C) 2020  Entr'ouvert
+#
+# This program is free software: you can redistribute it and/or modify it
+# under the terms of the GNU Affero General Public License as published
+# by the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+from rest_framework import serializers
+
+
+class SlotSerializer(serializers.Serializer):
+    '''
+    payload to fill one slot. The slot (event id) is in the URL.
+    '''
+
+    label = serializers.CharField(max_length=250, allow_blank=True)
+    user_external_id = serializers.CharField(max_length=250, allow_blank=True)
+    user_name = serializers.CharField(max_length=250, allow_blank=True)
+    user_display_label = serializers.CharField(max_length=250, allow_blank=True)
+    user_email = serializers.CharField(max_length=250, allow_blank=True)
+    user_phone_number = serializers.CharField(max_length=16, allow_blank=True)
+    form_url = serializers.CharField(max_length=250, allow_blank=True)
+    backoffice_url = serializers.URLField(allow_blank=True)
+    cancel_callback_url = serializers.URLField(allow_blank=True)
+    count = serializers.IntegerField(min_value=1)
+    cancel_booking_id = serializers.CharField(max_length=250, allow_blank=True, allow_null=True)
+    force_waiting_list = serializers.BooleanField(default=False)
+
+
+class StringOrListField(serializers.ListField):
+    def to_internal_value(self, data):
+        if isinstance(data, str):
+            data = [s.strip() for s in data.split(',')]
+        return super(StringOrListField, self).to_internal_value(data)
+
+
+class SlotsSerializer(SlotSerializer):
+    '''
+    payload to fill multiple slots: same as SlotSerializer, but the
+    slots list is in the payload.
+    '''
+
+    slots = StringOrListField(required=True, child=serializers.CharField(max_length=64, allow_blank=False))
+
+
+class ResizeSerializer(serializers.Serializer):
+    count = serializers.IntegerField(min_value=1)
diff --git a/chrono/api/views.py b/chrono/api/views.py
index ba50827..bc8a36b 100644
--- a/chrono/api/views.py
+++ b/chrono/api/views.py
@@ -32,11 +32,12 @@ from django.utils.timezone import now, make_aware, localtime
 from django.utils.translation import gettext_noop
 from django.utils.translation import ugettext_lazy as _
 
-from rest_framework import permissions, serializers, status
+from rest_framework import permissions, status
 
 from rest_framework.views import APIView
 
 from chrono.api.utils import Response
+from chrono.api import serializers
 from ..agendas.models import Agenda, Event, Booking, MeetingType, TimePeriodException, Desk
 from ..interval import IntervalSet
 
@@ -652,44 +653,9 @@ class AgendaDeskList(APIView):
 agenda_desk_list = AgendaDeskList.as_view()
 
 
-class SlotSerializer(serializers.Serializer):
-    '''
-    payload to fill one slot. The slot (event id) is in the URL.
-    '''
-
-    label = serializers.CharField(max_length=250, allow_blank=True)
-    user_external_id = serializers.CharField(max_length=250, allow_blank=True)
-    user_name = serializers.CharField(max_length=250, allow_blank=True)
-    user_display_label = serializers.CharField(max_length=250, allow_blank=True)
-    user_email = serializers.CharField(max_length=250, allow_blank=True)
-    user_phone_number = serializers.CharField(max_length=16, allow_blank=True)
-    form_url = serializers.CharField(max_length=250, allow_blank=True)
-    backoffice_url = serializers.URLField(allow_blank=True)
-    cancel_callback_url = serializers.URLField(allow_blank=True)
-    count = serializers.IntegerField(min_value=1)
-    cancel_booking_id = serializers.CharField(max_length=250, allow_blank=True, allow_null=True)
-    force_waiting_list = serializers.BooleanField(default=False)
-
-
-class StringOrListField(serializers.ListField):
-    def to_internal_value(self, data):
-        if isinstance(data, str):
-            data = [s.strip() for s in data.split(',')]
-        return super(StringOrListField, self).to_internal_value(data)
-
-
-class SlotsSerializer(SlotSerializer):
-    '''
-    payload to fill multiple slots: same as SlotSerializer, but the
-    slots list is in the payload.
-    '''
-
-    slots = StringOrListField(required=True, child=serializers.CharField(max_length=64, allow_blank=False))
-
-
 class Fillslots(APIView):
     permission_classes = (permissions.IsAuthenticated,)
-    serializer_class = SlotsSerializer
+    serializer_class = serializers.SlotsSerializer
 
     def post(self, request, agenda_identifier=None, event_identifier=None, format=None):
         try:
@@ -1054,7 +1020,7 @@ fillslots = Fillslots.as_view()
 
 
 class Fillslot(Fillslots):
-    serializer_class = SlotSerializer
+    serializer_class = serializers.SlotSerializer
 
     def post(self, request, agenda_identifier=None, event_identifier=None, format=None):
         try:
@@ -1210,10 +1176,6 @@ class SuspendBooking(APIView):
 suspend_booking = SuspendBooking.as_view()
 
 
-class ResizeSerializer(serializers.Serializer):
-    count = serializers.IntegerField(min_value=1)
-
-
 class ResizeBooking(APIView):
     '''
     Resize a booking.
@@ -1225,7 +1187,7 @@ class ResizeBooking(APIView):
     '''
 
     permission_classes = (permissions.IsAuthenticated,)
-    serializer_class = ResizeSerializer
+    serializer_class = serializers.ResizeSerializer
 
     def post(self, request, booking_pk=None, format=None):
         serializer = self.serializer_class(data=request.data)
-- 
2.28.0

