Projet

Général

Profil

0001-api-status-view-get-Event-by-slug-and-agenda-45520.patch

Lauréline Guérin, 24 août 2020 14:46

Télécharger (7,03 ko)

Voir les différences:

Subject: [PATCH] api: status view, get Event by slug and agenda (#45520)

 chrono/api/views.py | 12 ++++---
 tests/test_api.py   | 77 ++++++++++++++++++++++++++++++---------------
 2 files changed, 59 insertions(+), 30 deletions(-)
chrono/api/views.py
1332 1332
class SlotStatus(APIView):
1333 1333
    permission_classes = (permissions.IsAuthenticated,)
1334 1334

  
1335
    def get_object(self, event_identifier):
1335
    def get_object(self, agenda_identifier, event_identifier):
1336 1336
        try:
1337
            return Event.objects.get(slug=event_identifier)
1337
            return Event.objects.get(
1338
                slug=event_identifier, agenda__slug=agenda_identifier, agenda__kind='events'
1339
            )
1338 1340
        except Event.DoesNotExist:
1339 1341
            try:
1340 1342
                # legacy access by event id
1341
                return Event.objects.get(pk=int(event_identifier))
1343
                return Event.objects.get(
1344
                    pk=int(event_identifier), agenda=int(agenda_identifier), agenda__kind='events'
1345
                )
1342 1346
            except (ValueError, Event.DoesNotExist):
1343 1347
                raise Http404()
1344 1348

  
1345 1349
    def get(self, request, agenda_identifier=None, event_identifier=None, format=None):
1346
        event = self.get_object(event_identifier)
1350
        event = self.get_object(agenda_identifier, event_identifier)
1347 1351
        response = {
1348 1352
            'err': 0,
1349 1353
            'places': get_event_places(event),
tests/test_api.py
1706 1706
    assert resp.json['err_desc'] == 'sold out'
1707 1707

  
1708 1708

  
1709
def test_status(app, some_data, user):
1710
    agenda_id = Agenda.objects.filter(label=u'Foo bar')[0].id
1711
    event = Event.objects.filter(agenda_id=agenda_id)[0]
1712
    Booking(event=event).save()
1709
def test_status(app, user):
1710
    agenda = Agenda.objects.create(label='Foo bar', kind='events', minimal_booking_delay=0)
1711
    event = Event.objects.create(
1712
        slug='event-slug',
1713
        start_datetime=(now() + datetime.timedelta(days=5)).replace(hour=10, minute=0),
1714
        places=10,
1715
        agenda=agenda,
1716
    )
1717
    agenda2 = Agenda.objects.create(label='Foo bar2', kind='events', minimal_booking_delay=0)
1718
    # other event with the same slug but in another agenda
1719
    Event.objects.create(
1720
        slug='event-slug',
1721
        start_datetime=(now() + datetime.timedelta(days=5)).replace(hour=10, minute=0),
1722
        places=5,
1723
        agenda=agenda2,
1724
    )
1725
    Booking.objects.create(event=event)
1713 1726

  
1714
    resp = app.get('/api/agenda/%s/status/%s/' % (agenda_id, event.id), status=401)
1727
    app.get('/api/agenda/%s/status/%s/' % (agenda.pk, event.pk), status=401)
1715 1728

  
1716 1729
    app.authorization = ('Basic', ('john.doe', 'password'))
1717
    resp = app.get('/api/agenda/%s/status/%s/' % (agenda_id, 0), status=404)
1718
    resp = app.get('/api/agenda/%s/status/%s/' % (agenda_id, 'xx'), status=404)
1730
    app.get('/api/agenda/%s/status/%s/' % (agenda.pk, 0), status=404)
1731
    app.get('/api/agenda/%s/status/%s/' % (agenda.pk, 'xx'), status=404)
1719 1732

  
1720
    resp = app.get('/api/agenda/%s/status/%s/' % (agenda_id, event.id))
1733
    resp = app.get('/api/agenda/%s/status/%s/' % (agenda.pk, event.pk))
1721 1734
    assert resp.json['err'] == 0
1722 1735
    assert resp.json['places']['total'] == 10
1723 1736
    assert resp.json['places']['available'] == 9
1724 1737
    assert resp.json['places']['reserved'] == 1
1725
    assert not 'waiting_list_total' in resp.json['places']
1738
    assert 'waiting_list_total' not in resp.json['places']
1726 1739

  
1727 1740
    Booking(event=event, in_waiting_list=True).save()
1728 1741
    event.waiting_list_places = 5
1729 1742
    event.save()
1730
    resp = app.get('/api/agenda/%s/status/%s/' % (agenda_id, event.id))
1743
    resp = app.get('/api/agenda/%s/status/%s/' % (agenda.pk, event.pk))
1731 1744
    assert resp.json['places']['waiting_list_total'] == 5
1732 1745
    assert resp.json['places']['waiting_list_available'] == 4
1733 1746
    assert resp.json['places']['waiting_list_reserved'] == 1
1734 1747

  
1735
    # access by slug
1736
    event.slug = 'bar'
1737
    event.save()
1738
    resp = app.get('/api/agenda/%s/status/%s/' % (agenda_id, event.slug))
1739
    # not found event
1740
    resp = app.get('/api/agenda/%s/status/%s/' % (agenda_id, 'unknown'), status=404)
1748
    # unknown agenda
1749
    app.get('/api/agenda/0/status/%s/' % (event.pk), params={'user_external_id': '42'}, status=404)
1750
    app.get('/api/agenda/foobar/status/%s/' % (event.slug), params={'user_external_id': '42'}, status=404)
1751
    # unknown event
1752
    app.get('/api/agenda/%s/status/0/' % (agenda.pk), params={'user_external_id': '42'}, status=404)
1753
    app.get('/api/agenda/%s/status/foobar/' % (agenda.slug), params={'user_external_id': '42'}, status=404)
1754

  
1755
    # wrong kind
1756
    agenda.kind = 'meetings'
1757
    agenda.save()
1758
    app.get(
1759
        '/api/agenda/%s/status/%s/' % (agenda.pk, event.pk), status=404,
1760
    )
1761
    app.get(
1762
        '/api/agenda/%s/status/%s/' % (agenda.slug, event.slug), status=404,
1763
    )
1741 1764

  
1742 1765

  
1743 1766
def test_bookings(app, user):
......
1749 1772
        waiting_list_places=5,
1750 1773
        agenda=agenda,
1751 1774
    )
1775
    agenda2 = Agenda.objects.create(label='Foo bar2', kind='events', minimal_booking_delay=0)
1776
    # other event with the same slug but in another agenda
1777
    Event.objects.create(
1778
        slug='event-slug',
1779
        start_datetime=(now() + datetime.timedelta(days=5)).replace(hour=10, minute=0),
1780
        places=5,
1781
        agenda=agenda2,
1782
    )
1752 1783

  
1753 1784
    # create a booking not on the waiting list
1754 1785
    primary1 = Booking.objects.create(event=event, in_waiting_list=False, user_external_id='42')
......
1762 1793
    app.authorization = ('Basic', ('john.doe', 'password'))
1763 1794

  
1764 1795
    # unknown agenda
1765
    resp = app.get(
1766
        '/api/agenda/foobar/bookings/%s/' % (event.pk), params={'user_external_id': '42'}, status=404
1767
    )
1796
    app.get('/api/agenda/foobar/bookings/%s/' % (event.pk), params={'user_external_id': '42'}, status=404)
1768 1797
    # unknown event
1769
    resp = app.get(
1770
        '/api/agenda/%s/bookings/0/' % (agenda.slug), params={'user_external_id': '42'}, status=404
1771
    )
1772
    resp = app.get(
1773
        '/api/agenda/%s/bookings/foobar/' % (agenda.slug), params={'user_external_id': '42'}, status=404
1774
    )
1798
    app.get('/api/agenda/%s/bookings/0/' % (agenda.slug), params={'user_external_id': '42'}, status=404)
1799
    app.get('/api/agenda/%s/bookings/foobar/' % (agenda.slug), params={'user_external_id': '42'}, status=404)
1775 1800

  
1776 1801
    # search for '42' external user
1777 1802
    resp = app.get(
......
1819 1844
    # wrong kind
1820 1845
    agenda.kind = 'meetings'
1821 1846
    agenda.save()
1822
    resp = app.get(
1847
    app.get(
1823 1848
        '/api/agenda/%s/bookings/%s/' % (agenda.slug, event.slug),
1824 1849
        params={'user_external_id': '42'},
1825 1850
        status=404,
1826
-