Projet

Général

Profil

0001-api-add-agenda-filter-in-booking-statistics-55267.patch

Valentin Deniaud, 01 juillet 2021 11:29

Télécharger (4,32 ko)

Voir les différences:

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(-)
chrono/api/views.py
2194 2194
        category_options = [{'id': '_all', 'label': _('All')}] + [
2195 2195
            {'id': x.slug, 'label': x.label} for x in categories
2196 2196
        ]
2197
        agendas = Agenda.objects.all()
2198
        agenda_options = [{'id': '_all', 'label': _('All')}] + [
2199
            {'id': x.slug, 'label': x.label} for x in agendas
2200
        ]
2197 2201
        return Response(
2198 2202
            {
2199 2203
                'data': [
......
2216 2220
                                'required': False,
2217 2221
                                'default': '_all',
2218 2222
                            },
2223
                            {
2224
                                'id': 'agenda',
2225
                                'label': _('Agenda'),
2226
                                'options': agenda_options,
2227
                                'required': False,
2228
                                'default': '_all',
2229
                            },
2219 2230
                        ],
2220 2231
                    }
2221 2232
                ]
......
2231 2242
    start = serializers.DateTimeField(required=False, input_formats=['iso-8601', '%Y-%m-%d'])
2232 2243
    end = serializers.DateTimeField(required=False, input_formats=['iso-8601', '%Y-%m-%d'])
2233 2244
    category = serializers.SlugField(required=False, allow_blank=False, max_length=256)
2245
    agenda = serializers.SlugField(required=False, allow_blank=False, max_length=256)
2234 2246

  
2235 2247

  
2236 2248
class BookingsStatistics(APIView):
......
2256 2268
        if 'category' in data and data['category'] != '_all':
2257 2269
            bookings = bookings.filter(event__agenda__category__slug=data['category'])
2258 2270

  
2271
        if 'agenda' in data and data['agenda'] != '_all':
2272
            bookings = bookings.filter(event__agenda__slug=data['agenda'])
2273

  
2259 2274
        bookings = bookings.annotate(day=TruncDay('event__start_datetime'))
2260 2275
        bookings = bookings.values('day', 'user_was_present').annotate(total=Count('id')).order_by('day')
2261 2276

  
tests/api/test_statistics.py
9 9

  
10 10

  
11 11
def test_statistics_list(app, user):
12
    Agenda.objects.create(label='Foo bar')
13
    Agenda.objects.create(label='Bar foo')
12 14
    Category.objects.create(label='Category A')
13 15
    Category.objects.create(label='Category B')
14 16

  
......
19 21
    resp = app.get('/api/statistics/')
20 22
    category_filter = [x for x in resp.json['data'][0]['filters'] if x['id'] == 'category'][0]
21 23
    assert len(category_filter['options']) == 3
24
    agenda_filter = [x for x in resp.json['data'][0]['filters'] if x['id'] == 'agenda'][0]
25
    assert len(agenda_filter['options']) == 3
22 26

  
23 27

  
24 28
def test_statistics_bookings(app, user, freezer):
......
54 58
    }
55 59

  
56 60
    category = Category.objects.create(label='Category A', slug='category-a')
57
    agenda = Agenda.objects.create(label='Foo bar', kind='events', category=category)
61
    agenda = Agenda.objects.create(label='Bar foo', kind='events', category=category)
58 62
    event3 = Event.objects.create(start_datetime=now().replace(day=25), places=5, agenda=agenda)
59 63
    Booking.objects.create(event=event3)
60 64

  
......
65 69
        'series': [{'label': 'Bookings Count', 'data': [1]}],
66 70
    }
67 71

  
72
    # agenda filter
73
    resp = app.get(url + '?agenda=bar-foo')
74
    assert resp.json['data'] == {
75
        'x_labels': ['2020-10-25'],
76
        'series': [{'label': 'Bookings Count', 'data': [1]}],
77
    }
78

  
68 79
    # invalid time_interval
69 80
    resp = app.get(url + '?time_interval=month', status=400)
70 81
    assert resp.json['err'] == 1
71
-