Projet

Général

Profil

0004-api-list-subscriptions-endpoint-61079.patch

Lauréline Guérin, 27 janvier 2022 11:01

Télécharger (9,36 ko)

Voir les différences:

Subject: [PATCH 4/4] api: list subscriptions endpoint (#61079)

 chrono/api/serializers.py      |   2 +
 chrono/api/views.py            |  43 ++++++++++-
 tests/api/test_subscription.py | 130 +++++++++++++++++++++++++++++++++
 3 files changed, 172 insertions(+), 3 deletions(-)
chrono/api/serializers.py
335 335
    class Meta:
336 336
        model = Subscription
337 337
        fields = [
338
            'id',
338 339
            'user_external_id',
339 340
            'user_first_name',
340 341
            'user_last_name',
......
343 344
            'date_start',
344 345
            'date_end',
345 346
        ]
347
        read_only_fields = ['id']
346 348

  
347 349
    def validate(self, attrs):
348 350
        super().validate(attrs)
chrono/api/views.py
1880 1880
agendas_events_fillslots = MultipleAgendasEventsFillslots.as_view()
1881 1881

  
1882 1882

  
1883
class SubscriptionAPI(APIView):
1883
class SubscriptionFilter(filters.FilterSet):
1884
    date_start = filters.DateFilter(lookup_expr='gte')
1885
    date_end = filters.DateFilter(lookup_expr='lte')
1886

  
1887
    class Meta:
1888
        model = Subscription
1889
        fields = [
1890
            'user_external_id',
1891
            'date_start',
1892
            'date_end',
1893
        ]
1894

  
1895

  
1896
class SubscriptionAPI(ListAPIView):
1897
    filter_backends = (filters.DjangoFilterBackend,)
1884 1898
    serializer_class = serializers.SubscriptionSerializer
1899
    filterset_class = SubscriptionFilter
1885 1900
    permission_classes = (permissions.IsAuthenticated,)
1886 1901

  
1902
    def get_agenda(self, agenda_identifier):
1903
        return get_object_or_404(Agenda, slug=agenda_identifier, kind='events')
1904

  
1905
    def get(self, request, agenda_identifier):
1906
        self.agenda = self.get_agenda(agenda_identifier)
1907

  
1908
        try:
1909
            subscriptions = self.filter_queryset(self.get_queryset())
1910
        except ValidationError as e:
1911
            raise APIErrorBadRequest(N_('invalid payload'), errors=e.detail)
1912

  
1913
        data = []
1914
        for subscription in subscriptions:
1915
            serialized_subscription = self.serializer_class(subscription).data
1916
            serialized_subscription.update(subscription.extra_data or {})
1917
            data.append(serialized_subscription)
1918

  
1919
        return Response({'err': 0, 'data': data})
1920

  
1921
    def get_queryset(self):
1922
        return self.agenda.subscriptions.order_by('date_start', 'date_end', 'user_external_id', 'pk')
1923

  
1887 1924
    def post(self, request, agenda_identifier):
1888
        agenda = get_object_or_404(Agenda, slug=agenda_identifier, kind='events')
1925
        self.agenda = self.get_agenda(agenda_identifier)
1889 1926

  
1890 1927
        serializer = self.serializer_class(data=request.data)
1891 1928
        if not serializer.is_valid():
......
1893 1930
        extra_data = {k: v for k, v in request.data.items() if k not in serializer.validated_data}
1894 1931

  
1895 1932
        subscription = Subscription.objects.create(
1896
            agenda=agenda, extra_data=extra_data, **serializer.validated_data
1933
            agenda=self.agenda, extra_data=extra_data, **serializer.validated_data
1897 1934
        )
1898 1935
        return Response({'err': 0, 'id': subscription.pk})
1899 1936

  
tests/api/test_subscription.py
7 7
pytestmark = pytest.mark.django_db
8 8

  
9 9

  
10
def test_api_list_subscription(app, user):
11
    agenda = Agenda.objects.create(label='Foo bar', kind='events')
12
    subscription = Subscription.objects.create(
13
        agenda=agenda,
14
        user_external_id='xxx',
15
        user_first_name='Foo',
16
        user_last_name='BAR',
17
        user_email='foo@bar.com',
18
        user_phone_number='06',
19
        extra_data={'foo': 'bar'},
20
        date_start=datetime.date(year=2021, month=9, day=1),
21
        date_end=datetime.date(year=2021, month=10, day=1),
22
    )
23

  
24
    resp = app.get('/api/agenda/%s/subscription/' % agenda.slug, status=401)
25

  
26
    app.authorization = ('Basic', ('john.doe', 'password'))
27

  
28
    resp = app.get('/api/agenda/%s/subscription/' % agenda.slug)
29
    assert len(resp.json['data']) == 1
30
    assert resp.json['data'][0] == {
31
        'id': subscription.pk,
32
        'user_external_id': 'xxx',
33
        'user_first_name': 'Foo',
34
        'user_last_name': 'BAR',
35
        'user_email': 'foo@bar.com',
36
        'user_phone_number': '06',
37
        'date_start': '2021-09-01',
38
        'date_end': '2021-10-01',
39
        'foo': 'bar',
40
    }
41

  
42

  
43
def test_api_list_subscription_filter_user_external_id(app, user):
44
    agenda = Agenda.objects.create(label='Foo bar', kind='events')
45
    subscription1 = Subscription.objects.create(
46
        agenda=agenda,
47
        user_external_id='xxx',
48
        user_first_name='Foo',
49
        user_last_name='BAR',
50
        user_email='foo@bar.com',
51
        user_phone_number='06',
52
        extra_data={'foo': 'bar'},
53
        date_start=datetime.date(year=2021, month=9, day=1),
54
        date_end=datetime.date(year=2021, month=10, day=1),
55
    )
56
    subscription2 = Subscription.objects.create(
57
        agenda=agenda,
58
        user_external_id='yyy',
59
        date_start=datetime.date(year=2021, month=9, day=1),
60
        date_end=datetime.date(year=2021, month=10, day=1),
61
    )
62
    other_agenda = Agenda.objects.create(label='Foo bar 2', kind='events')
63
    Subscription.objects.create(
64
        agenda=other_agenda,
65
        user_external_id='xxx',
66
        date_start=datetime.date(year=2000, month=1, day=1),
67
        date_end=datetime.date(year=2099, month=12, day=31),
68
    )
69

  
70
    app.authorization = ('Basic', ('john.doe', 'password'))
71

  
72
    resp = app.get('/api/agenda/%s/subscription/' % agenda.slug, params={'user_external_id': 'xxx'})
73
    assert [d['id'] for d in resp.json['data']] == [subscription1.pk]
74
    resp = app.get('/api/agenda/%s/subscription/' % agenda.slug, params={'user_external_id': 'yyy'})
75
    assert [d['id'] for d in resp.json['data']] == [subscription2.pk]
76
    resp = app.get('/api/agenda/%s/subscription/' % agenda.slug, params={'user_external_id': 'zzz'})
77
    assert [d['id'] for d in resp.json['data']] == []
78

  
79

  
80
def test_api_list_subscription_filter_date_start(app, user):
81
    agenda = Agenda.objects.create(label='Foo bar', kind='events')
82
    subscription1 = Subscription.objects.create(
83
        agenda=agenda,
84
        user_external_id='xxx',
85
        user_first_name='Foo',
86
        user_last_name='BAR',
87
        user_email='foo@bar.com',
88
        user_phone_number='06',
89
        extra_data={'foo': 'bar'},
90
        date_start=datetime.date(year=2021, month=9, day=1),
91
        date_end=datetime.date(year=2021, month=10, day=1),
92
    )
93
    subscription2 = Subscription.objects.create(
94
        agenda=agenda,
95
        user_external_id='xxx',
96
        date_start=datetime.date(year=2022, month=9, day=1),
97
        date_end=datetime.date(year=2022, month=10, day=1),
98
    )
99

  
100
    app.authorization = ('Basic', ('john.doe', 'password'))
101

  
102
    resp = app.get('/api/agenda/%s/subscription/' % agenda.slug, params={'date_start': '2021-08-31'})
103
    assert [d['id'] for d in resp.json['data']] == [subscription1.pk, subscription2.pk]
104
    resp = app.get('/api/agenda/%s/subscription/' % agenda.slug, params={'date_start': '2021-09-02'})
105
    assert [d['id'] for d in resp.json['data']] == [subscription2.pk]
106
    resp = app.get('/api/agenda/%s/subscription/' % agenda.slug, params={'date_start': '2022-09-02'})
107
    assert [d['id'] for d in resp.json['data']] == []
108

  
109

  
110
def test_api_list_subscription_filter_date_end(app, user):
111
    agenda = Agenda.objects.create(label='Foo bar', kind='events')
112
    subscription1 = Subscription.objects.create(
113
        agenda=agenda,
114
        user_external_id='xxx',
115
        user_first_name='Foo',
116
        user_last_name='BAR',
117
        user_email='foo@bar.com',
118
        user_phone_number='06',
119
        extra_data={'foo': 'bar'},
120
        date_start=datetime.date(year=2021, month=9, day=1),
121
        date_end=datetime.date(year=2021, month=10, day=1),
122
    )
123
    subscription2 = Subscription.objects.create(
124
        agenda=agenda,
125
        user_external_id='xxx',
126
        date_start=datetime.date(year=2022, month=9, day=1),
127
        date_end=datetime.date(year=2022, month=10, day=1),
128
    )
129

  
130
    app.authorization = ('Basic', ('john.doe', 'password'))
131

  
132
    resp = app.get('/api/agenda/%s/subscription/' % agenda.slug, params={'date_end': '2022-10-02'})
133
    assert [d['id'] for d in resp.json['data']] == [subscription1.pk, subscription2.pk]
134
    resp = app.get('/api/agenda/%s/subscription/' % agenda.slug, params={'date_end': '2021-10-02'})
135
    assert [d['id'] for d in resp.json['data']] == [subscription1.pk]
136
    resp = app.get('/api/agenda/%s/subscription/' % agenda.slug, params={'date_end': '2021-09-30'})
137
    assert [d['id'] for d in resp.json['data']] == []
138

  
139

  
10 140
def test_api_create_subscription(app, user):
11 141
    agenda = Agenda.objects.create(label='Foo bar', kind='events')
12 142

  
13
-