api: subscription detail endpoint (#61161)
This commit is contained in:
parent
246043f3d4
commit
d5fdebb208
|
@ -90,6 +90,11 @@ urlpatterns = [
|
|||
),
|
||||
url(
|
||||
r'^agenda/(?P<agenda_identifier>[\w-]+)/subscription/$',
|
||||
views.subscriptions,
|
||||
name='api-agenda-subscriptions',
|
||||
),
|
||||
url(
|
||||
r'^agenda/(?P<agenda_identifier>[\w-]+)/subscription/(?P<subscription_pk>\d+)/$',
|
||||
views.subscription,
|
||||
name='api-agenda-subscription',
|
||||
),
|
||||
|
|
|
@ -1893,7 +1893,7 @@ class SubscriptionFilter(filters.FilterSet):
|
|||
]
|
||||
|
||||
|
||||
class SubscriptionAPI(ListAPIView):
|
||||
class SubscriptionsAPI(ListAPIView):
|
||||
filter_backends = (filters.DjangoFilterBackend,)
|
||||
serializer_class = serializers.SubscriptionSerializer
|
||||
filterset_class = SubscriptionFilter
|
||||
|
@ -1930,6 +1930,29 @@ class SubscriptionAPI(ListAPIView):
|
|||
return Response({'err': 0, 'id': subscription.pk})
|
||||
|
||||
|
||||
subscriptions = SubscriptionsAPI.as_view()
|
||||
|
||||
|
||||
class SubscriptionAPI(APIView):
|
||||
permission_classes = (permissions.IsAuthenticated,)
|
||||
serializer_class = serializers.SubscriptionSerializer
|
||||
|
||||
def initial(self, request, *args, **kwargs):
|
||||
super().initial(request, *args, **kwargs)
|
||||
self.subscription = get_object_or_404(
|
||||
Subscription,
|
||||
pk=kwargs.get('subscription_pk'),
|
||||
agenda__kind='events',
|
||||
agenda__slug=kwargs.get('agenda_identifier'),
|
||||
)
|
||||
|
||||
def get(self, request, *args, **kwargs):
|
||||
serializer = self.serializer_class(self.subscription)
|
||||
response = serializer.data
|
||||
response.update({'err': 0})
|
||||
return Response(response)
|
||||
|
||||
|
||||
subscription = SubscriptionAPI.as_view()
|
||||
|
||||
|
||||
|
|
|
@ -209,3 +209,67 @@ def test_api_create_subscription(app, user):
|
|||
}
|
||||
resp = app.post('/api/agenda/%s/subscription/' % agenda.slug, params=params, status=400)
|
||||
assert resp.json['errors']['non_field_errors'][0] == 'start_datetime must be before end_datetime'
|
||||
|
||||
|
||||
def test_api_get_subscription(app, user):
|
||||
agenda = Agenda.objects.create(label='Foo bar', kind='events')
|
||||
subscription = Subscription.objects.create(
|
||||
agenda=agenda,
|
||||
user_external_id='xxx',
|
||||
user_first_name='Foo',
|
||||
user_last_name='BAR',
|
||||
user_email='foo@bar.com',
|
||||
user_phone_number='06',
|
||||
extra_data={'foo': 'bar'},
|
||||
date_start=datetime.date(year=2021, month=9, day=1),
|
||||
date_end=datetime.date(year=2021, month=10, day=1),
|
||||
)
|
||||
other_agenda = Agenda.objects.create(label='Foo bar', kind='events')
|
||||
other_subscription = Subscription.objects.create(
|
||||
agenda=other_agenda,
|
||||
user_external_id='xxx',
|
||||
date_start=datetime.date(year=2021, month=9, day=1),
|
||||
date_end=datetime.date(year=2021, month=10, day=1),
|
||||
)
|
||||
|
||||
resp = app.get('/api/agenda/%s/subscription/%s/' % (agenda.slug, subscription.pk), status=401)
|
||||
|
||||
app.authorization = ('Basic', ('john.doe', 'password'))
|
||||
|
||||
resp = app.get('/api/agenda/%s/subscription/%s/' % (agenda.slug, subscription.pk))
|
||||
assert resp.json == {
|
||||
'id': subscription.pk,
|
||||
'user_external_id': 'xxx',
|
||||
'user_first_name': 'Foo',
|
||||
'user_last_name': 'BAR',
|
||||
'user_email': 'foo@bar.com',
|
||||
'user_phone_number': '06',
|
||||
'date_start': '2021-09-01',
|
||||
'date_end': '2021-10-01',
|
||||
'extra_data': {
|
||||
'foo': 'bar',
|
||||
},
|
||||
'err': 0,
|
||||
}
|
||||
|
||||
resp = app.get('/api/agenda/%s/subscription/%s/' % (other_agenda.slug, other_subscription.pk))
|
||||
assert resp.json == {
|
||||
'id': other_subscription.pk,
|
||||
'user_external_id': 'xxx',
|
||||
'user_first_name': '',
|
||||
'user_last_name': '',
|
||||
'user_email': '',
|
||||
'user_phone_number': '',
|
||||
'date_start': '2021-09-01',
|
||||
'date_end': '2021-10-01',
|
||||
'extra_data': None,
|
||||
'err': 0,
|
||||
}
|
||||
|
||||
app.get('/api/agenda/%s/subscription/%s/' % (agenda.slug, other_subscription.pk), status=404)
|
||||
app.get('/api/agenda/%s/subscription/%s/' % (agenda.slug, 0), status=404)
|
||||
app.get('/api/agenda/%s/subscription/%s/' % ('unknown', subscription.pk), status=404)
|
||||
for kind in ['meetings', 'virtual']:
|
||||
agenda.kind = kind
|
||||
agenda.save()
|
||||
app.get('/api/agenda/%s/subscription/%s/' % (agenda.slug, subscription.pk), status=404)
|
||||
|
|
Loading…
Reference in New Issue