diff --git a/doc/api/resources/vouchers.rst b/doc/api/resources/vouchers.rst index 87fdd47b83..3f7a5fd4d9 100644 --- a/doc/api/resources/vouchers.rst +++ b/doc/api/resources/vouchers.rst @@ -116,6 +116,7 @@ Endpoints :query integer page: The page number in case of a multi-page result set, default is 1 :query string code: Only show the voucher with the given voucher code. + :query string search: Only show the voucher with the given query found in the code, tag, or comment. :query integer max_usages: Only show vouchers with the given maximal number of usages. :query integer redeemed: Only show vouchers with the given number of redemptions. Note that this doesn't tell you if the voucher can still be redeemed, as this also depends on ``max_usages``. See the diff --git a/src/pretix/api/views/voucher.py b/src/pretix/api/views/voucher.py index 5527a11e5e..5709ad4ec6 100644 --- a/src/pretix/api/views/voucher.py +++ b/src/pretix/api/views/voucher.py @@ -40,6 +40,7 @@ with scopes_disabled(): class VoucherFilter(FilterSet): active = BooleanFilter(method='filter_active') code = CharFilter(lookup_expr='iexact') + search = CharFilter(method='search_qs') class Meta: model = Voucher @@ -54,6 +55,9 @@ with scopes_disabled(): return queryset.filter(Q(redeemed__gte=F('max_usages')) | (Q(valid_until__isnull=False) & Q(valid_until__lte=now()))) + def search_qs(self, qs, name, value): + return qs.filter(Q(code__icontains=value) | Q(tag__icontains=value) | Q(comment__icontains=value)) + class VoucherViewSet(viewsets.ModelViewSet): serializer_class = VoucherSerializer diff --git a/src/tests/api/test_vouchers.py b/src/tests/api/test_vouchers.py index c06079b5e3..8061d1c4a6 100644 --- a/src/tests/api/test_vouchers.py +++ b/src/tests/api/test_vouchers.py @@ -115,6 +115,15 @@ def test_voucher_list(token_client, organizer, event, voucher, item, quota, sube ) assert [] == resp.data['results'] + resp = token_client.get( + '/api/v1/organizers/{}/events/{}/vouchers/?search={}'.format(organizer.slug, event.slug, voucher.code[:3]) + ) + assert [res] == resp.data['results'] + resp = token_client.get( + '/api/v1/organizers/{}/events/{}/vouchers/?code=RAvRcnbDehWD59oywV5x'.format(organizer.slug, event.slug) + ) + assert [] == resp.data['results'] + resp = token_client.get( '/api/v1/organizers/{}/events/{}/vouchers/?max_usages=1'.format(organizer.slug, event.slug) )