api: list subscriptions endpoint (#61079)

This commit is contained in:
Lauréline Guérin 2022-01-27 09:55:42 +01:00
parent 2955505c59
commit 246043f3d4
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
3 changed files with 165 additions and 3 deletions

View File

@ -346,6 +346,7 @@ class SubscriptionSerializer(serializers.ModelSerializer):
class Meta:
model = Subscription
fields = [
'id',
'user_external_id',
'user_first_name',
'user_last_name',
@ -353,7 +354,9 @@ class SubscriptionSerializer(serializers.ModelSerializer):
'user_phone_number',
'date_start',
'date_end',
'extra_data',
]
read_only_fields = ['id', 'extra_data']
def validate(self, attrs):
super().validate(attrs)

View File

@ -1880,12 +1880,44 @@ class MultipleAgendasEventsFillslots(EventsFillslots):
agendas_events_fillslots = MultipleAgendasEventsFillslots.as_view()
class SubscriptionAPI(APIView):
class SubscriptionFilter(filters.FilterSet):
date_start = filters.DateFilter(lookup_expr='gte')
date_end = filters.DateFilter(lookup_expr='lte')
class Meta:
model = Subscription
fields = [
'user_external_id',
'date_start',
'date_end',
]
class SubscriptionAPI(ListAPIView):
filter_backends = (filters.DjangoFilterBackend,)
serializer_class = serializers.SubscriptionSerializer
filterset_class = SubscriptionFilter
permission_classes = (permissions.IsAuthenticated,)
def get_agenda(self, agenda_identifier):
return get_object_or_404(Agenda, slug=agenda_identifier, kind='events')
def get(self, request, agenda_identifier):
self.agenda = self.get_agenda(agenda_identifier)
try:
subscriptions = self.filter_queryset(self.get_queryset())
except ValidationError as e:
raise APIErrorBadRequest(N_('invalid filters'), errors=e.detail)
serializer = self.serializer_class(subscriptions, many=True)
return Response({'err': 0, 'data': serializer.data})
def get_queryset(self):
return self.agenda.subscriptions.order_by('date_start', 'date_end', 'user_external_id', 'pk')
def post(self, request, agenda_identifier):
agenda = get_object_or_404(Agenda, slug=agenda_identifier, kind='events')
self.agenda = self.get_agenda(agenda_identifier)
serializer = self.serializer_class(data=request.data)
if not serializer.is_valid():
@ -1893,7 +1925,7 @@ class SubscriptionAPI(APIView):
extra_data = {k: v for k, v in request.data.items() if k not in serializer.validated_data}
subscription = Subscription.objects.create(
agenda=agenda, extra_data=extra_data, **serializer.validated_data
agenda=self.agenda, extra_data=extra_data, **serializer.validated_data
)
return Response({'err': 0, 'id': subscription.pk})

View File

@ -7,6 +7,133 @@ from chrono.agendas.models import Agenda, Subscription
pytestmark = pytest.mark.django_db
def test_api_list_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),
)
resp = app.get('/api/agenda/%s/subscription/' % agenda.slug, status=401)
app.authorization = ('Basic', ('john.doe', 'password'))
resp = app.get('/api/agenda/%s/subscription/' % agenda.slug)
assert len(resp.json['data']) == 1
assert resp.json['data'][0] == {
'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',
},
}
def test_api_list_subscription_filter_user_external_id(app, user):
agenda = Agenda.objects.create(label='Foo bar', kind='events')
subscription1 = Subscription.objects.create(
agenda=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),
)
subscription2 = Subscription.objects.create(
agenda=agenda,
user_external_id='yyy',
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 2', kind='events')
Subscription.objects.create(
agenda=other_agenda,
user_external_id='xxx',
date_start=datetime.date(year=2000, month=1, day=1),
date_end=datetime.date(year=2099, month=12, day=31),
)
app.authorization = ('Basic', ('john.doe', 'password'))
resp = app.get('/api/agenda/%s/subscription/' % agenda.slug, params={'user_external_id': 'xxx'})
assert [d['id'] for d in resp.json['data']] == [subscription1.pk]
resp = app.get('/api/agenda/%s/subscription/' % agenda.slug, params={'user_external_id': 'yyy'})
assert [d['id'] for d in resp.json['data']] == [subscription2.pk]
resp = app.get('/api/agenda/%s/subscription/' % agenda.slug, params={'user_external_id': 'zzz'})
assert [d['id'] for d in resp.json['data']] == []
def test_api_list_subscription_filter_date_start(app, user):
agenda = Agenda.objects.create(label='Foo bar', kind='events')
subscription1 = Subscription.objects.create(
agenda=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),
)
subscription2 = Subscription.objects.create(
agenda=agenda,
user_external_id='xxx',
date_start=datetime.date(year=2022, month=9, day=1),
date_end=datetime.date(year=2022, month=10, day=1),
)
app.authorization = ('Basic', ('john.doe', 'password'))
resp = app.get('/api/agenda/%s/subscription/' % agenda.slug, params={'date_start': '2021-08-31'})
assert [d['id'] for d in resp.json['data']] == [subscription1.pk, subscription2.pk]
resp = app.get('/api/agenda/%s/subscription/' % agenda.slug, params={'date_start': '2021-09-02'})
assert [d['id'] for d in resp.json['data']] == [subscription2.pk]
resp = app.get('/api/agenda/%s/subscription/' % agenda.slug, params={'date_start': '2022-09-02'})
assert [d['id'] for d in resp.json['data']] == []
resp = app.get(
'/api/agenda/%s/subscription/' % agenda.slug, params={'date_start': 'wrong-format'}, status=400
)
assert resp.json['err_class'] == 'invalid filters'
def test_api_list_subscription_filter_date_end(app, user):
agenda = Agenda.objects.create(label='Foo bar', kind='events')
subscription1 = Subscription.objects.create(
agenda=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),
)
subscription2 = Subscription.objects.create(
agenda=agenda,
user_external_id='xxx',
date_start=datetime.date(year=2022, month=9, day=1),
date_end=datetime.date(year=2022, month=10, day=1),
)
app.authorization = ('Basic', ('john.doe', 'password'))
resp = app.get('/api/agenda/%s/subscription/' % agenda.slug, params={'date_end': '2022-10-02'})
assert [d['id'] for d in resp.json['data']] == [subscription1.pk, subscription2.pk]
resp = app.get('/api/agenda/%s/subscription/' % agenda.slug, params={'date_end': '2021-10-02'})
assert [d['id'] for d in resp.json['data']] == [subscription1.pk]
resp = app.get('/api/agenda/%s/subscription/' % agenda.slug, params={'date_end': '2021-09-30'})
assert [d['id'] for d in resp.json['data']] == []
resp = app.get(
'/api/agenda/%s/subscription/' % agenda.slug, params={'date_end': 'wrong-format'}, status=400
)
assert resp.json['err_class'] == 'invalid filters'
def test_api_create_subscription(app, user):
agenda = Agenda.objects.create(label='Foo bar', kind='events')