|
1 | 1 | from django.contrib.auth.models import User
|
| 2 | +from django.db.models import Q |
2 | 3 |
|
3 | 4 | import django_filters
|
4 | 5 |
|
5 |
| -from crowdsourcer.models import ResponseType, Section |
| 6 | +from crowdsourcer.models import Option, Question, Response, ResponseType, Section |
6 | 7 |
|
7 | 8 |
|
8 | 9 | def filter_not_empty(queryset, name, value):
|
@@ -32,3 +33,59 @@ class Meta:
|
32 | 33 | "marker__response_type": ["exact"],
|
33 | 34 | "is_active": ["exact"],
|
34 | 35 | }
|
| 36 | + |
| 37 | + |
| 38 | +class ResponseFilter(django_filters.FilterSet): |
| 39 | + AUTHORITY_TYPES = [ |
| 40 | + ("COMB", "Combined Authority"), |
| 41 | + ("COI", "Isles of Scilly"), |
| 42 | + ("CTY", "County Council"), |
| 43 | + ("DIS", "District Council"), |
| 44 | + ("LBO", "London Borough"), |
| 45 | + ("UTA", "Unitary (includes Scotland and Wales)"), |
| 46 | + ("MTD", "Metropolitan District"), |
| 47 | + ("LGD", "Northern Ireland"), |
| 48 | + ] |
| 49 | + response_type = django_filters.ChoiceFilter( |
| 50 | + label="Stage", |
| 51 | + choices=ResponseType.choices(), |
| 52 | + method="response_type_check", |
| 53 | + ) |
| 54 | + question__section = django_filters.ChoiceFilter( |
| 55 | + label="Section", |
| 56 | + empty_label=None, |
| 57 | + choices=Section.objects.values_list("id", "title"), |
| 58 | + ) |
| 59 | + question = django_filters.ChoiceFilter( |
| 60 | + field_name="question", |
| 61 | + label="Question", |
| 62 | + choices=Question.objects.values_list("id", "number"), |
| 63 | + ) |
| 64 | + option = django_filters.ChoiceFilter( |
| 65 | + field_name="option", |
| 66 | + label="Answer", |
| 67 | + method="option_check", |
| 68 | + choices=Option.objects.values_list("id", "description"), |
| 69 | + ) |
| 70 | + |
| 71 | + authority__type = django_filters.ChoiceFilter( |
| 72 | + label="Authority Type", |
| 73 | + method="response_type_check", |
| 74 | + choices=AUTHORITY_TYPES, |
| 75 | + ) |
| 76 | + |
| 77 | + def option_check(self, queryset, name, value): |
| 78 | + queryset = queryset.filter(Q(option=value) | Q(multi_option=value)) |
| 79 | + return queryset |
| 80 | + |
| 81 | + def response_type_check(self, queryset, name, value): |
| 82 | + if value != "": |
| 83 | + queryset = queryset.filter(**{name: value}) |
| 84 | + return queryset |
| 85 | + |
| 86 | + class Meta: |
| 87 | + model = Response |
| 88 | + fields = { |
| 89 | + "question": ["exact"], |
| 90 | + "option": ["exact"], |
| 91 | + } |
0 commit comments