api: accept slots as a string (#38333)

This commit is contained in:
Emmanuel Cazenave 2019-12-10 15:28:25 +01:00
parent 5f09ce7fdb
commit c420dde65d
2 changed files with 27 additions and 3 deletions

View File

@ -387,15 +387,20 @@ class SlotSerializer(serializers.Serializer):
cancel_booking_id = serializers.CharField(max_length=250, allow_blank=True, allow_null=True)
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 = serializers.ListField(
required=True, child=serializers.CharField(max_length=64, allow_blank=False)
)
slots = StringOrListField(required=True, child=serializers.CharField(max_length=64, allow_blank=False))
class Fillslots(APIView):

View File

@ -695,6 +695,25 @@ def test_booking_api_fillslots(app, some_data, user):
resp = app.post('/api/agenda/233/fillslots/', status=404)
def test_booking_api_fillslots_slots_string_param(app, some_data, user):
agenda = Agenda.objects.filter(label=u'Foo bar')[0]
events_ids = [x.id for x in Event.objects.filter(agenda=agenda) if x.in_bookable_period()]
assert len(events_ids) == 3
app.authorization = ('Basic', ('john.doe', 'password'))
# empty string
resp = app.post_json('/api/agenda/%s/fillslots/' % agenda.slug, params={'slots': ''}, status=400)
assert resp.json['err'] == 1
assert resp.json['err_class'] == 'invalid payload'
assert resp.json['err_desc'] == 'invalid payload'
slots_string_param = ','.join([str(e) for e in events_ids])
resp = app.post_json('/api/agenda/%s/fillslots/' % agenda.slug, params={'slots': slots_string_param})
primary_booking_id = resp.json['booking_id']
Booking.objects.get(id=primary_booking_id)
assert Booking.objects.count() == 3
def test_booking_api_meeting(app, meetings_agenda, user):
agenda_id = meetings_agenda.slug
meeting_type = MeetingType.objects.get(agenda=meetings_agenda)