Coverage for website/payments/api/v2/filters.py: 75.00%
34 statements
« prev ^ index » next coverage.py v7.6.7, created at 2025-08-14 10:31 +0000
« prev ^ index » next coverage.py v7.6.7, created at 2025-08-14 10:31 +0000
1from rest_framework import filters
2from rest_framework.exceptions import ValidationError
4from utils.snippets import extract_date_range, strtobool
7class CreatedAtFilter(filters.BaseFilterBackend):
8 """Allows you to filter by payment creation dates."""
10 def filter_queryset(self, request, queryset, view):
11 start, end = extract_date_range(request, allow_empty=True)
13 if start is not None: 13 ↛ 14line 13 didn't jump to line 14 because the condition on line 13 was never true
14 queryset = queryset.filter(created_at__gte=start)
15 if end is not None: 15 ↛ 16line 15 didn't jump to line 16 because the condition on line 15 was never true
16 queryset = queryset.filter(created_at__lte=end)
18 return queryset
20 def get_schema_operation_parameters(self, view):
21 return [
22 {
23 "name": "start",
24 "required": False,
25 "in": "query",
26 "description": "Filter payments by ISO date, starting with this parameter (i.e. 2021-03-30T04:20:00) where `payment.created_at >= value`",
27 "schema": {
28 "type": "string",
29 },
30 },
31 {
32 "name": "end",
33 "required": False,
34 "in": "query",
35 "description": "Filter payments by ISO date, ending with this parameter (i.e. 2021-05-16T13:37:00) where `payment.created_at <= value`",
36 "schema": {
37 "type": "string",
38 },
39 },
40 ]
43class PaymentTypeFilter(filters.BaseFilterBackend):
44 """Allows you to filter by payment type."""
46 def filter_queryset(self, request, queryset, view):
47 payment_type = request.query_params.get("type", None)
49 if payment_type: 49 ↛ 50line 49 didn't jump to line 50 because the condition on line 49 was never true
50 queryset = queryset.filter(type__in=payment_type.split(","))
52 return queryset
54 def get_schema_operation_parameters(self, view):
55 return [
56 {
57 "name": "type",
58 "required": False,
59 "in": "query",
60 "description": "Filter by payment type, accepts a comma separated list",
61 "schema": {
62 "type": "string",
63 },
64 }
65 ]
68class PaymentSettledFilter(filters.BaseFilterBackend):
69 """Allows you to filter by settled status."""
71 def filter_queryset(self, request, queryset, view):
72 settled = request.query_params.get("settled", None)
74 if settled is None:
75 return queryset
77 try:
78 if strtobool(settled):
79 return queryset.filter(batch__processed=True)
80 except ValueError as e:
81 raise ValidationError({"settled": "Invalid filter value."}) from e
83 return queryset.exclude(batch__processed=True)
85 def get_schema_operation_parameters(self, view):
86 return [
87 {
88 "name": "settled",
89 "required": False,
90 "in": "query",
91 "description": "Filter by settled status",
92 "schema": {
93 "type": "boolean",
94 },
95 }
96 ]