From 33bd3592d566200722519a474e29af3100a4c869 Mon Sep 17 00:00:00 2001 From: Valentin Deniaud Date: Wed, 30 Jun 2021 16:56:40 +0200 Subject: [PATCH] api: add agenda filter in booking statistics (#55267) --- chrono/api/views.py | 15 +++++++++++++++ tests/api/test_statistics.py | 13 ++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/chrono/api/views.py b/chrono/api/views.py index 2836abc..f3a2bf7 100644 --- a/chrono/api/views.py +++ b/chrono/api/views.py @@ -2194,6 +2194,10 @@ class StatisticsList(APIView): category_options = [{'id': '_all', 'label': _('All')}] + [ {'id': x.slug, 'label': x.label} for x in categories ] + agendas = Agenda.objects.all() + agenda_options = [{'id': '_all', 'label': _('All')}] + [ + {'id': x.slug, 'label': x.label} for x in agendas + ] return Response( { 'data': [ @@ -2216,6 +2220,13 @@ class StatisticsList(APIView): 'required': False, 'default': '_all', }, + { + 'id': 'agenda', + 'label': _('Agenda'), + 'options': agenda_options, + 'required': False, + 'default': '_all', + }, ], } ] @@ -2231,6 +2242,7 @@ class StatisticsFiltersSerializer(serializers.Serializer): start = serializers.DateTimeField(required=False, input_formats=['iso-8601', '%Y-%m-%d']) end = serializers.DateTimeField(required=False, input_formats=['iso-8601', '%Y-%m-%d']) category = serializers.SlugField(required=False, allow_blank=False, max_length=256) + agenda = serializers.SlugField(required=False, allow_blank=False, max_length=256) class BookingsStatistics(APIView): @@ -2256,6 +2268,9 @@ class BookingsStatistics(APIView): if 'category' in data and data['category'] != '_all': bookings = bookings.filter(event__agenda__category__slug=data['category']) + if 'agenda' in data and data['agenda'] != '_all': + bookings = bookings.filter(event__agenda__slug=data['agenda']) + bookings = bookings.annotate(day=TruncDay('event__start_datetime')) bookings = bookings.values('day', 'user_was_present').annotate(total=Count('id')).order_by('day') diff --git a/tests/api/test_statistics.py b/tests/api/test_statistics.py index cba4c92..954545a 100644 --- a/tests/api/test_statistics.py +++ b/tests/api/test_statistics.py @@ -9,6 +9,8 @@ pytestmark = pytest.mark.django_db def test_statistics_list(app, user): + Agenda.objects.create(label='Foo bar') + Agenda.objects.create(label='Bar foo') Category.objects.create(label='Category A') Category.objects.create(label='Category B') @@ -19,6 +21,8 @@ def test_statistics_list(app, user): resp = app.get('/api/statistics/') category_filter = [x for x in resp.json['data'][0]['filters'] if x['id'] == 'category'][0] assert len(category_filter['options']) == 3 + agenda_filter = [x for x in resp.json['data'][0]['filters'] if x['id'] == 'agenda'][0] + assert len(agenda_filter['options']) == 3 def test_statistics_bookings(app, user, freezer): @@ -54,7 +58,7 @@ def test_statistics_bookings(app, user, freezer): } category = Category.objects.create(label='Category A', slug='category-a') - agenda = Agenda.objects.create(label='Foo bar', kind='events', category=category) + agenda = Agenda.objects.create(label='Bar foo', kind='events', category=category) event3 = Event.objects.create(start_datetime=now().replace(day=25), places=5, agenda=agenda) Booking.objects.create(event=event3) @@ -65,6 +69,13 @@ def test_statistics_bookings(app, user, freezer): 'series': [{'label': 'Bookings Count', 'data': [1]}], } + # agenda filter + resp = app.get(url + '?agenda=bar-foo') + assert resp.json['data'] == { + 'x_labels': ['2020-10-25'], + 'series': [{'label': 'Bookings Count', 'data': [1]}], + } + # invalid time_interval resp = app.get(url + '?time_interval=month', status=400) assert resp.json['err'] == 1 -- 2.20.1