api: add API to retrieve agenda detail (#17188)
This commit is contained in:
parent
ffcda3904a
commit
28028b681d
|
@ -20,6 +20,7 @@ from . import views
|
|||
|
||||
urlpatterns = [
|
||||
url(r'agenda/$', views.agendas),
|
||||
url(r'agenda/(?P<agenda_identifier>[\w-]+)/$', views.agenda_detail),
|
||||
|
||||
url(r'agenda/(?P<agenda_identifier>[\w-]+)/datetimes/$', views.datetimes, name='api-agenda-datetimes'),
|
||||
url(r'agenda/(?P<agenda_identifier>[\w-]+)/fillslot/(?P<event_pk>[\w:-]+)/$',
|
||||
|
|
|
@ -29,38 +29,54 @@ from rest_framework.views import APIView
|
|||
from ..agendas.models import Agenda, Event, Booking, MeetingType, TimePeriod
|
||||
|
||||
|
||||
def get_agenda_detail(request, agenda):
|
||||
agenda_detail = {
|
||||
'id': agenda.id,
|
||||
'slug': agenda.slug,
|
||||
'text': agenda.label,
|
||||
'kind': agenda.kind,
|
||||
}
|
||||
|
||||
if agenda.kind == 'events':
|
||||
agenda_detail['api'] = {
|
||||
'datetimes_url': request.build_absolute_uri(
|
||||
reverse('api-agenda-datetimes',
|
||||
kwargs={'agenda_identifier': agenda.slug}))
|
||||
}
|
||||
elif agenda.kind == 'meetings':
|
||||
agenda_detail['api'] = {
|
||||
'meetings_url': request.build_absolute_uri(
|
||||
reverse('api-agenda-meetings',
|
||||
kwargs={'agenda_identifier': agenda.slug}))
|
||||
}
|
||||
|
||||
return agenda_detail
|
||||
|
||||
|
||||
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}))
|
||||
}
|
||||
agendas.append(agenda_data)
|
||||
|
||||
agendas = [get_agenda_detail(request, agenda)
|
||||
for agenda in Agenda.objects.all().order_by('label')]
|
||||
return Response({'data': agendas})
|
||||
|
||||
agendas = Agendas.as_view()
|
||||
|
||||
|
||||
class AgendaDetail(GenericAPIView):
|
||||
'''
|
||||
Retrieve an agenda instance.
|
||||
'''
|
||||
permission_classes = ()
|
||||
|
||||
def get(self, request, agenda_identifier):
|
||||
agenda = get_object_or_404(Agenda, slug=agenda_identifier)
|
||||
return Response({'data': get_agenda_detail(request, agenda)})
|
||||
|
||||
agenda_detail = AgendaDetail.as_view()
|
||||
|
||||
|
||||
class Datetimes(GenericAPIView):
|
||||
permission_classes = ()
|
||||
|
||||
|
|
|
@ -588,3 +588,16 @@ 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_detail_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/'
|
||||
|
||||
# unknown
|
||||
app.get('/api/agenda/whatever/', status=404)
|
||||
|
|
Loading…
Reference in New Issue