0005-api-subscription-detail-endpoint-61161.patch
chrono/api/urls.py | ||
---|---|---|
90 | 90 |
), |
91 | 91 |
url( |
92 | 92 |
r'^agenda/(?P<agenda_identifier>[\w-]+)/subscription/$', |
93 |
views.subscriptions, |
|
94 |
name='api-agenda-subscriptions', |
|
95 |
), |
|
96 |
url( |
|
97 |
r'^agenda/(?P<agenda_identifier>[\w-]+)/subscription/(?P<subscription_pk>\d+)/$', |
|
93 | 98 |
views.subscription, |
94 | 99 |
name='api-agenda-subscription', |
95 | 100 |
), |
chrono/api/views.py | ||
---|---|---|
1893 | 1893 |
] |
1894 | 1894 | |
1895 | 1895 | |
1896 |
class SubscriptionAPI(ListAPIView): |
|
1896 |
class SubscriptionsAPI(ListAPIView):
|
|
1897 | 1897 |
filter_backends = (filters.DjangoFilterBackend,) |
1898 | 1898 |
serializer_class = serializers.SubscriptionSerializer |
1899 | 1899 |
filterset_class = SubscriptionFilter |
... | ... | |
1930 | 1930 |
return Response({'err': 0, 'id': subscription.pk}) |
1931 | 1931 | |
1932 | 1932 | |
1933 |
subscriptions = SubscriptionsAPI.as_view() |
|
1934 | ||
1935 | ||
1936 |
class SubscriptionAPI(APIView): |
|
1937 |
permission_classes = (permissions.IsAuthenticated,) |
|
1938 |
serializer_class = serializers.SubscriptionSerializer |
|
1939 | ||
1940 |
def initial(self, request, *args, **kwargs): |
|
1941 |
super().initial(request, *args, **kwargs) |
|
1942 |
self.subscription = get_object_or_404( |
|
1943 |
Subscription, |
|
1944 |
pk=kwargs.get('subscription_pk'), |
|
1945 |
agenda__kind='events', |
|
1946 |
agenda__slug=kwargs.get('agenda_identifier'), |
|
1947 |
) |
|
1948 | ||
1949 |
def get(self, request, *args, **kwargs): |
|
1950 |
serializer = self.serializer_class(self.subscription) |
|
1951 |
response = serializer.data |
|
1952 |
response.update( |
|
1953 |
{ |
|
1954 |
'err': 0, |
|
1955 |
} |
|
1956 |
) |
|
1957 |
return Response(response) |
|
1958 | ||
1959 | ||
1933 | 1960 |
subscription = SubscriptionAPI.as_view() |
1934 | 1961 | |
1935 | 1962 |
tests/api/test_subscription.py | ||
---|---|---|
211 | 211 |
} |
212 | 212 |
resp = app.post('/api/agenda/%s/subscription/' % agenda.slug, params=params, status=400) |
213 | 213 |
assert resp.json['errors']['non_field_errors'][0] == 'start_datetime must be before end_datetime' |
214 | ||
215 | ||
216 |
def test_api_get_subscription(app, user): |
|
217 |
agenda = Agenda.objects.create(label='Foo bar', kind='events') |
|
218 |
subscription = Subscription.objects.create( |
|
219 |
agenda=agenda, |
|
220 |
user_external_id='xxx', |
|
221 |
user_first_name='Foo', |
|
222 |
user_last_name='BAR', |
|
223 |
user_email='foo@bar.com', |
|
224 |
user_phone_number='06', |
|
225 |
extra_data={'foo': 'bar'}, |
|
226 |
date_start=datetime.date(year=2021, month=9, day=1), |
|
227 |
date_end=datetime.date(year=2021, month=10, day=1), |
|
228 |
) |
|
229 |
other_agenda = Agenda.objects.create(label='Foo bar', kind='events') |
|
230 |
other_subscription = Subscription.objects.create( |
|
231 |
agenda=other_agenda, |
|
232 |
user_external_id='xxx', |
|
233 |
date_start=datetime.date(year=2021, month=9, day=1), |
|
234 |
date_end=datetime.date(year=2021, month=10, day=1), |
|
235 |
) |
|
236 | ||
237 |
resp = app.get('/api/agenda/%s/subscription/%s/' % (agenda.slug, subscription.pk), status=401) |
|
238 | ||
239 |
app.authorization = ('Basic', ('john.doe', 'password')) |
|
240 | ||
241 |
resp = app.get('/api/agenda/%s/subscription/%s/' % (agenda.slug, subscription.pk)) |
|
242 |
assert resp.json == { |
|
243 |
'id': subscription.pk, |
|
244 |
'user_external_id': 'xxx', |
|
245 |
'user_first_name': 'Foo', |
|
246 |
'user_last_name': 'BAR', |
|
247 |
'user_email': 'foo@bar.com', |
|
248 |
'user_phone_number': '06', |
|
249 |
'date_start': '2021-09-01', |
|
250 |
'date_end': '2021-10-01', |
|
251 |
'err': 0, |
|
252 |
} |
|
253 | ||
254 |
app.get('/api/agenda/%s/subscription/%s/' % (agenda.slug, other_subscription.pk), status=404) |
|
255 |
app.get('/api/agenda/%s/subscription/%s/' % (agenda.slug, 0), status=404) |
|
256 |
app.get('/api/agenda/%s/subscription/%s/' % ('unknown', subscription.pk), status=404) |
|
257 |
for kind in ['meetings', 'virtual']: |
|
258 |
agenda.kind = kind |
|
259 |
agenda.save() |
|
260 |
app.get('/api/agenda/%s/subscription/%s/' % (agenda.slug, subscription.pk), status=404) |
|
214 |
- |