Projet

Général

Profil

0001-api-add-add-event-endpoint-47337.patch

Nicolas Roche, 09 décembre 2020 13:39

Télécharger (5,99 ko)

Voir les différences:

Subject: [PATCH] api: add add-event endpoint (#47337)

 chrono/api/urls.py  |  5 +++
 chrono/api/views.py | 40 ++++++++++++++++++++++++
 tests/test_api.py   | 74 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 119 insertions(+)
chrono/api/urls.py
33 33
        views.slot_status,
34 34
        name='api-event-status',
35 35
    ),
36 36
    url(
37 37
        r'^agenda/(?P<agenda_identifier>[\w-]+)/bookings/(?P<event_identifier>[\w-]+)/$',
38 38
        views.slot_bookings,
39 39
        name='api-event-bookings',
40 40
    ),
41
    url(
42
        r'^agenda/(?P<agenda_identifier>[\w-]+)/add-event/$',
43
        views.agenda_add_event,
44
        name='api-agenda-add-event',
45
    ),
41 46
    url(
42 47
        r'^agenda/meetings/(?P<meeting_identifier>[\w-]+)/datetimes/$',
43 48
        views.meeting_datetimes,
44 49
        name='api-agenda-meeting-datetimes-legacy',
45 50
    ),
46 51
    url(r'^agenda/(?P<agenda_identifier>[\w-]+)/meetings/$', views.meeting_list, name='api-agenda-meetings'),
47 52
    url(
48 53
        r'^agenda/(?P<agenda_identifier>[\w-]+)/meetings/(?P<meeting_identifier>[\w-]+)/$',
chrono/api/views.py
1483 1483

  
1484 1484
    def get(self, request, booking_pk=None, format=None):
1485 1485
        booking = get_object_or_404(Booking, id=booking_pk)
1486 1486
        response = HttpResponse(booking.get_ics(request), content_type='text/calendar')
1487 1487
        return response
1488 1488

  
1489 1489

  
1490 1490
booking_ics = BookingICS.as_view()
1491

  
1492

  
1493
class EventSerializer(serializers.ModelSerializer):
1494
    class Meta:
1495
        model = Event
1496
        fields = [
1497
            'start_datetime',
1498
            'duration',
1499
            'publication_date',
1500
            'places',
1501
            'waiting_list_places',
1502
            'label',
1503
            'description',
1504
            'pricing',
1505
            'url',
1506
        ]
1507

  
1508

  
1509
class AgendaAddEventView(APIView):
1510
    permission_classes = (permissions.IsAuthenticated, )
1511
    serializer_class = EventSerializer
1512

  
1513
    def post(self, request, agenda_identifier):
1514
        agenda = get_object_or_404(Agenda, slug=agenda_identifier, kind='events')
1515

  
1516
        serializer = self.serializer_class(data=request.data)
1517
        if not serializer.is_valid():
1518
            return APIError(
1519
                _('invalid payload'),
1520
                err_class='invalid payload',
1521
                errors=serializer.errors
1522
            ).to_response()
1523

  
1524
        payload = serializer.validated_data
1525
        event = Event.objects.create(agenda=agenda, **payload)
1526
        response = {'err': 0, 'event_id': event.pk}
1527
        return Response(response)
1528

  
1529

  
1530
agenda_add_event = AgendaAddEventView.as_view()
tests/test_api.py
4573 4573
    agenda_foo.save()
4574 4574
    resp = app.get(foo_api_url, params=params)
4575 4575
    assert len(resp.json['data']) == 16
4576 4576
    # also on virtual agenda
4577 4577
    virtual_agenda.maximal_booking_delay = 5
4578 4578
    virtual_agenda.save()
4579 4579
    resp = app.get(virtual_api_url, params=params)
4580 4580
    assert len(resp.json['data']) == 16
4581

  
4582

  
4583
def test_add_event(app, user):
4584
    agenda = Agenda(label=u'Foo bar')
4585
    agenda.maximal_booking_delay = 0
4586
    agenda.save()
4587

  
4588
    params = {
4589
        'start_datetime': '2020-12-08 15:38',
4590
        'places': 10,
4591
        'slug': 123,
4592
    }
4593
    api_url = '/api/agenda/%s/add-event/' % (agenda.slug)
4594
    app.authorization = ('Basic', ('john.doe', 'password'))
4595

  
4596
    # add an event
4597
    resp = app.post(api_url, params=params)
4598
    assert not resp.json['err']
4599
    event_id = resp.json['event_id']
4600
    assert Event.objects.filter(agenda=agenda).count() == 1
4601
    event = Event.objects.filter(agenda=agenda, id=event_id)[0]
4602
    assert str(event.start_datetime) == '2020-12-08 14:38:00+00:00'
4603
    assert str(event.start_datetime.tzinfo) == 'UTC'
4604
    assert event.places == 10
4605
    assert event.publication_date is None
4606

  
4607
    # add with a description
4608
    params = {
4609
        'start_datetime': '2020-12-08 15:38',
4610
        'publication_date': '2020-05-11',
4611
        'description': 'A description',
4612
        'places': 11,
4613
    }
4614
    resp = app.post(api_url, params=params)
4615
    event_id = resp.json['event_id']
4616
    event = Event.objects.filter(agenda=agenda, id=event_id)[0]
4617
    assert event.description == 'A description'
4618
    assert event.publication_date == datetime.date(2020, 5, 11)
4619

  
4620
    # add with errors in datetime parts
4621
    params = {
4622
        'start_datetime': '2020-12-08 minuit',
4623
        'places': 10,
4624
    }
4625
    resp = app.post(api_url, params=params, status=200)
4626
    assert resp.json['err']
4627
    assert resp.json['err_desc'] == 'invalid payload'
4628
    assert 'Datetime has wrong format' in resp.json['errors']['start_datetime'][0]
4629
    assert Event.objects.filter(agenda=agenda).count() == 2
4630

  
4631

  
4632
def test_add_event_on_missing_agenda(app, user):
4633
    agenda = Agenda(label=u'Foo bar Meeting', kind='meetings')
4634
    agenda.save()
4635
    api_url = '/api/agenda/%s/add-event/' % ('999')
4636

  
4637
    # no authentication
4638
    resp = app.post(api_url, status=401)
4639
    assert resp.json['detail'] == 'Authentication credentials were not provided.'
4640

  
4641
    # wrong password
4642
    app.authorization = ('Basic', ('john.doe', 'wrong'))
4643
    resp = app.post(api_url, status=401)
4644
    assert resp.json['detail'] == 'Invalid username/password.'
4645

  
4646
    # missing agenda
4647
    app.authorization = ('Basic', ('john.doe', 'password'))
4648
    resp = app.post(api_url, status=404)
4649
    assert resp.json['detail'] == 'Not found.'
4650

  
4651
    # meeting agenda
4652
    api_url = '/api/agenda/%s/add-event/' % (agenda.slug)
4653
    resp = app.post(api_url, status=404)
4654
    assert resp.json['detail'] == 'Not found.'
4581
-