Coverage for website/sales/api/v2/filters.py: 60.47%

35 statements  

« 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 

3 

4from utils.snippets import extract_date_range, strtobool 

5 

6 

7class ShiftActiveFilter(filters.BaseFilterBackend): 

8 """Allows you to filter by active status.""" 

9 

10 def filter_queryset(self, request, queryset, view): 

11 active = request.query_params.get("active", None) 

12 

13 if active is not None: 13 ↛ 14line 13 didn't jump to line 14 because the condition on line 13 was never true

14 try: 

15 queryset = queryset.filter(active=strtobool(active)) 

16 except ValueError as e: 

17 raise ValidationError({"active": "Invalid filter value."}) from e 

18 

19 return queryset 

20 

21 def get_schema_operation_parameters(self, view): 

22 return [ 

23 { 

24 "name": "active", 

25 "required": False, 

26 "in": "query", 

27 "description": "Filter by active status", 

28 "schema": { 

29 "type": "boolean", 

30 }, 

31 } 

32 ] 

33 

34 

35class ShiftLockedFilter(filters.BaseFilterBackend): 

36 """Allows you to filter by locked status.""" 

37 

38 def filter_queryset(self, request, queryset, view): 

39 locked = request.query_params.get("locked", None) 

40 

41 if locked is not None: 41 ↛ 42line 41 didn't jump to line 42 because the condition on line 41 was never true

42 try: 

43 queryset = queryset.filter(locked=strtobool(locked)) 

44 except ValueError as e: 

45 raise ValidationError({"locked": "Invalid filter value."}) from e 

46 

47 return queryset 

48 

49 def get_schema_operation_parameters(self, view): 

50 return [ 

51 { 

52 "name": "locked", 

53 "required": False, 

54 "in": "query", 

55 "description": "Filter by locked status", 

56 "schema": { 

57 "type": "boolean", 

58 }, 

59 } 

60 ] 

61 

62 

63class ShiftDateFilter(filters.BaseFilterBackend): 

64 """Allows you to filter by event start/end dates.""" 

65 

66 def filter_queryset(self, request, queryset, view): 

67 start, end = extract_date_range(request, allow_empty=True) 

68 

69 if start is not None: 69 ↛ 70line 69 didn't jump to line 70 because the condition on line 69 was never true

70 queryset = queryset.filter(end__gte=start) 

71 if end is not None: 71 ↛ 72line 71 didn't jump to line 72 because the condition on line 71 was never true

72 queryset = queryset.filter(start__lte=end) 

73 

74 return queryset 

75 

76 def get_schema_operation_parameters(self, view): 

77 return [ 

78 { 

79 "name": "start", 

80 "required": False, 

81 "in": "query", 

82 "description": "Filter shifts by ISO date, starting with this parameter (i.e. 2021-03-30T04:20:00) where `event.end >= value`", 

83 "schema": { 

84 "type": "string", 

85 }, 

86 }, 

87 { 

88 "name": "end", 

89 "required": False, 

90 "in": "query", 

91 "description": "Filter shifts by ISO date, ending with this parameter (i.e. 2021-05-16T13:37:00) where `event.start <= value`", 

92 "schema": { 

93 "type": "string", 

94 }, 

95 }, 

96 ]