api: add resources api (#53272)

This commit is contained in:
Lauréline Guérin 2021-04-22 15:17:51 +02:00
parent e9c9655064
commit aa175d1306
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
3 changed files with 63 additions and 0 deletions

View File

@ -49,6 +49,11 @@ urlpatterns = [
views.meeting_info,
name='api-agenda-meetings',
),
url(
r'^agenda/(?P<agenda_identifier>[\w-]+)/resources/$',
views.agenda_resource_list,
name='api-agenda-resources',
),
url(r'^agenda/(?P<agenda_identifier>[\w-]+)/desks/$', views.agenda_desk_list, name='api-agenda-desks'),
url(
r'^agenda/(?P<agenda_identifier>[\w-]+)/meetings/(?P<meeting_identifier>[\w-]+)/datetimes/$',

View File

@ -353,6 +353,14 @@ def get_agenda_detail(request, agenda, check_events=False):
reverse('api-agenda-desks', kwargs={'agenda_identifier': agenda.slug})
),
}
if agenda.kind == 'meetings':
agenda_detail['api'].update(
{
'resources_url': request.build_absolute_uri(
reverse('api-agenda-resources', kwargs={'agenda_identifier': agenda.slug})
),
}
)
agenda_detail['api']['fillslots_url'] = request.build_absolute_uri(
reverse('api-agenda-fillslots', kwargs={'agenda_identifier': agenda.slug})
)
@ -850,6 +858,21 @@ class AgendaDeskList(APIView):
agenda_desk_list = AgendaDeskList.as_view()
class AgendaResourceList(APIView):
permission_classes = ()
def get(self, request, agenda_identifier=None, format=None):
agenda = get_object_or_404(Agenda, slug=agenda_identifier, kind='meetings')
resources = [
{'id': x.slug, 'text': x.label, 'description': x.description} for x in agenda.resources.all()
]
return Response({'data': resources})
agenda_resource_list = AgendaResourceList.as_view()
class SlotSerializer(serializers.Serializer):
"""
payload to fill one slot. The slot (event id) is in the URL.

View File

@ -179,6 +179,7 @@ def test_agendas_api(app):
'api': {
'meetings_url': 'http://testserver/api/agenda/foo-bar-meeting/meetings/',
'desks_url': 'http://testserver/api/agenda/foo-bar-meeting/desks/',
'resources_url': 'http://testserver/api/agenda/foo-bar-meeting/resources/',
'fillslots_url': 'http://testserver/api/agenda/foo-bar-meeting/fillslots/',
},
},
@ -193,6 +194,7 @@ def test_agendas_api(app):
'api': {
'meetings_url': 'http://testserver/api/agenda/foo-bar-meeting-2/meetings/',
'desks_url': 'http://testserver/api/agenda/foo-bar-meeting-2/desks/',
'resources_url': 'http://testserver/api/agenda/foo-bar-meeting-2/resources/',
'fillslots_url': 'http://testserver/api/agenda/foo-bar-meeting-2/fillslots/',
},
},
@ -379,6 +381,39 @@ def test_agendas_desks_api(app, some_data, meetings_agenda):
resp = app.get('/api/agenda/xxxx/desks/', status=404)
def test_agendas_resources_api(app):
agenda = Agenda.objects.create(label='Foo bar', kind='meetings')
resource1 = Resource.objects.create(label='Resource 1', description='Foo bar Resource 1')
resource2 = Resource.objects.create(label='Resource 2')
agenda.resources.add(resource1, resource2)
resp = app.get('/api/agenda/%s/resources/' % agenda.slug)
assert resp.json == {
'data': [
{
'text': 'Resource 1',
'id': 'resource-1',
'description': 'Foo bar Resource 1',
},
{
'text': 'Resource 2',
'id': 'resource-2',
'description': '',
},
]
}
# unknown
resp = app.get('/api/agenda/xxxx/resources/', status=404)
# wrong kind
agenda.kind = 'virtual'
agenda.save()
resp = app.get('/api/agenda/%s/resources/' % agenda.slug, status=404)
agenda.kind = 'events'
agenda.save()
resp = app.get('/api/agenda/%s/resources/' % agenda.slug, status=404)
def test_datetimes_api(app, some_data):
agenda = Agenda.objects.filter(label=u'Foo bar')[0]