From e1b9c701325885015ea94dc0a6628f4bb609c2f0 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 | 65 +++++++++++++++++++++++++++++++++++++---------------- tests/test_api.py | 11 +++++++++ 3 files changed, 58 insertions(+), 20 deletions(-) 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..36ab425 100644 --- a/chrono/api/views.py +++ b/chrono/api/views.py @@ -29,31 +29,36 @@ from rest_framework.views import APIView from ..agendas.models import Agenda, Event, Booking, MeetingType, TimePeriod +def get_agenda_data(request, agenda): + 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 agenda_data + + class Agendas(GenericAPIView): permission_classes = () def get(self, request, format=None): agendas = [] for agenda in Agenda.objects.all().order_by('label'): - 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})) - } + agenda_data = get_agenda_data(request, agenda) agendas.append(agenda_data) return Response({'data': agendas}) @@ -61,6 +66,28 @@ 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 = get_agenda_data(request, agenda) + 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