From bdd2990cc6ebb5e340468c90cf9ef3676a73266d Mon Sep 17 00:00:00 2001 From: Josue Kouka Date: Mon, 26 Jun 2017 14:51:36 +0200 Subject: [PATCH] api: add single agenda infos api (#17188) --- chrono/api/urls.py | 2 +- chrono/api/views.py | 38 ++++++++++++++++++++++++++++++++++++++ tests/test_api.py | 11 +++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/chrono/api/urls.py b/chrono/api/urls.py index 23c5b04..de9b09f 100644 --- a/chrono/api/urls.py +++ b/chrono/api/urls.py @@ -20,7 +20,7 @@ from . import views urlpatterns = [ url(r'agenda/$', views.agendas), - + url(r'agenda/(?P[\w-]+)/$', views.agenda), url(r'agenda/(?P[\w-]+)/datetimes/$', views.datetimes, name='api-agenda-datetimes'), url(r'agenda/(?P[\w-]+)/fillslot/(?P[\w:-]+)/$', views.fillslot, name='api-fillslot'), diff --git a/chrono/api/views.py b/chrono/api/views.py index 8805397..b4d6fec 100644 --- a/chrono/api/views.py +++ b/chrono/api/views.py @@ -61,6 +61,44 @@ class Agendas(GenericAPIView): agendas = Agendas.as_view() +class AgendaAPIView(GenericAPIView): + """View returning detail of a single agenda + """ + permission_classes = () + + def get(self, request, agenda_identifier): + try: + agenda = Agenda.objects.get(slug=agenda_identifier) + except Agenda.DoesNotExist: + try: + # legacy access by agenda id + agenda = Agenda.objects.get(id=int(agenda_identifier)) + except (ValueError, Agenda.DoesNotExist): + raise Http404() + + agenda_data = { + 'id': agenda.id, + 'slug': agenda.slug, + 'text': agenda.label, + 'kind': agenda.kind, + } + if agenda.kind == 'events': + agenda_data['api'] = { + 'datetimes_url': request.build_absolute_uri( + reverse('api-agenda-datetimes', + kwargs={'agenda_identifier': agenda.slug}))} + elif agenda.kind == 'meetings': + agenda_data['api'] = { + 'meetings_url': request.build_absolute_uri( + reverse('api-agenda-meetings', + kwargs={'agenda_identifier': agenda.slug}))} + + return Response({'data': agenda_data}) + + +agenda = AgendaAPIView.as_view() + + class Datetimes(GenericAPIView): permission_classes = () diff --git a/tests/test_api.py b/tests/test_api.py index 9900832..2660daa 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -588,3 +588,14 @@ def test_multiple_booking_api(app, some_data, user): resp = app.post(resp3.json['api']['accept_url']) assert event.booked_places == 7 assert event.waiting_list == 0 + + +def test_agenda_api(app, some_data): + agenda = Agenda.objects.get(slug='foo-bar') + resp = app.get('/api/agenda/%s/' % agenda.slug) + data = resp.json['data'] + assert data['id'] == 1 + assert data['slug'] == 'foo-bar' + assert data['text'] == 'Foo bar' + assert data['kind'] == 'events' + assert data['api']['datetimes_url'] == 'http://testserver/api/agenda/foo-bar/datetimes/' -- 2.11.0