-
Notifications
You must be signed in to change notification settings - Fork 4
/
filters.py
278 lines (238 loc) · 8.29 KB
/
filters.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
from django import forms
from django.contrib.postgres.aggregates import StringAgg
from django.db.models import CharField
from django.db.models import Q
from django.urls import reverse_lazy
from django_filters import BooleanFilter
from django_filters import CharFilter
from django_filters import ChoiceFilter
from django_filters import DateFilter
from additional_codes.models import AdditionalCode
from certificates.models import Certificate
from commodities.helpers import get_measures_on_declarable_commodities
from commodities.models.orm import GoodsNomenclature
from common.filters import AutoCompleteFilter
from common.filters import TamatoFilter
from common.filters import TamatoFilterBackend
from common.forms import DateInputFieldFixed
from common.util import EndDate
from common.util import StartDate
from common.validators import NumericValidator
from footnotes.models import Footnote
from geo_areas.models import GeographicalArea
from measures.forms import MeasureFilterForm
from measures.models import Measure
from measures.models import MeasureCondition
from measures.models import MeasureType
from quotas.models import QuotaOrderNumber
from regulations.models import Regulation
from workbaskets.models import WorkBasket
BEFORE_EXACT_AFTER_CHOICES = (
("exact", "is"),
("before", "before"),
("after", "after"),
)
GOV_UK_TWO_THIRDS = "govuk-!-width-two-thirds"
class GovUKDateFilter(DateFilter):
field_class = DateInputFieldFixed
class MeasureTypeFilterBackend(TamatoFilterBackend):
search_fields = (
StringAgg("sid", delimiter=" ", output_field=CharField),
StringAgg("description", delimiter=" ", output_field=CharField),
) # XXX order is significant
class MeasureFilter(TamatoFilter):
def __init__(self, *args, **kwargs):
if kwargs["data"]:
data = kwargs["data"].copy()
if "start_date_modifier" not in data:
data["start_date_modifier"] = "exact"
if "end_date_modifier" not in data:
data["end_date_modifier"] = "exact"
kwargs["data"] = data
super(MeasureFilter, self).__init__(*args, **kwargs)
sid = CharFilter(
label="ID",
widget=forms.TextInput(),
validators=[
NumericValidator,
],
)
measure_type = AutoCompleteFilter(
label="Type",
field_name="measure_type__sid",
queryset=MeasureType.objects.all(),
attrs={
"display_class": GOV_UK_TWO_THIRDS,
"min_length": 3,
},
)
goods_nomenclature = AutoCompleteFilter(
label="Specific commodity code",
field_name="goods_nomenclature__item_id",
queryset=GoodsNomenclature.objects.all(),
attrs={
"display_class": GOV_UK_TWO_THIRDS,
"min_length": 4,
},
)
# measures on declarable commodities
modc = BooleanFilter(
label="Include inherited measures",
widget=forms.CheckboxInput(),
field_name="goods_nomenclature",
method="commodity_modifier",
)
measure_filters_modifier = BooleanFilter(
label="Filter by current workbasket",
widget=forms.CheckboxInput(),
method="measures_filter",
required=False,
)
goods_nomenclature__item_id = CharFilter(
label="Commodity code starting with",
widget=forms.TextInput(
attrs={
"class": GOV_UK_TWO_THIRDS,
},
),
lookup_expr="startswith",
)
additional_code = AutoCompleteFilter(
label="Additional code",
field_name="additional_code",
queryset=AdditionalCode.objects.all(),
attrs={
"display_class": GOV_UK_TWO_THIRDS,
"min_length": 3,
},
)
geographical_area = AutoCompleteFilter(
label="Geographical area",
field_name="geographical_area",
queryset=GeographicalArea.objects.all(),
attrs={
"display_class": GOV_UK_TWO_THIRDS,
"min_length": 2,
},
)
order_number = AutoCompleteFilter(
label="Quota order number",
field_name="order_number__order_number",
queryset=QuotaOrderNumber.objects.all(),
attrs={
"display_class": GOV_UK_TWO_THIRDS,
"min_length": 4,
},
)
regulation = AutoCompleteFilter(
label="Regulation",
field_name="generating_regulation__regulation_id",
queryset=Regulation.objects.all(),
attrs={
"display_class": GOV_UK_TWO_THIRDS,
"min_length": 3,
},
)
footnote = AutoCompleteFilter(
label="Footnote",
field_name="footnotes",
queryset=Footnote.objects.all(),
attrs={
"display_class": GOV_UK_TWO_THIRDS,
"min_length": 2,
},
)
start_date = GovUKDateFilter(
label="",
method="filter_start_date",
)
start_date_modifier = ChoiceFilter(
label="Start date",
widget=forms.RadioSelect,
method="date_modifier",
empty_label=None,
choices=BEFORE_EXACT_AFTER_CHOICES,
)
end_date_modifier = ChoiceFilter(
label="End date",
widget=forms.RadioSelect,
method="date_modifier",
empty_label=None,
choices=BEFORE_EXACT_AFTER_CHOICES,
)
end_date = GovUKDateFilter(
label="",
method="filter_end_date",
)
certificates = AutoCompleteFilter(
label="Certificates",
field_name="conditions__required_certificate",
queryset=Certificate.objects.current(),
method="certificates_filter",
attrs={
"display_class": GOV_UK_TWO_THIRDS,
},
)
clear_url = reverse_lazy("measure-ui-search")
def date_modifier(self, queryset, name, value):
return queryset
def commodity_modifier(self, queryset, name, value):
if value:
if self.data["goods_nomenclature"]:
commodity = (
GoodsNomenclature.objects.filter(id=self.data["goods_nomenclature"])
.current()
.first()
)
queryset = get_measures_on_declarable_commodities(
WorkBasket.get_current_transaction(self.request),
commodity.item_id,
)
return queryset
def filter_start_date(self, queryset, name, value):
if value:
modifier = self.data["start_date_modifier"]
if modifier == "after":
filter_query = Q(start_date__gt=value)
elif modifier == "before":
filter_query = Q(start_date__lt=value)
else:
filter_query = Q(start_date=value)
queryset = queryset.annotate(
start_date=StartDate("valid_between"),
).filter(filter_query)
return queryset
def filter_end_date(self, queryset, name, value):
if value:
modifier = self.data["end_date_modifier"]
if modifier == "after":
filter_query = Q(end_date__gt=value) | Q(end_date__isnull=True)
elif modifier == "before":
filter_query = Q(end_date__lt=value)
else:
filter_query = Q(end_date=value)
queryset = (
queryset.with_effective_valid_between()
.annotate(end_date=EndDate("db_effective_valid_between"))
.filter(filter_query)
)
return queryset
def measures_filter(self, queryset, name, value):
if value:
queryset = WorkBasket.current(self.request).measures
return queryset
def certificates_filter(self, queryset, name, value):
"""Collects the measure_ids from MeasureCondition, filtered on value (a
Certificate object) Filters the queryset for Measures associated with
that Certificate via MeasureCondition."""
if value:
measure_ids = MeasureCondition.objects.filter(
required_certificate=value,
).values_list("dependent_measure_id", flat=True)
queryset = queryset.filter(id__in=measure_ids)
return queryset
class Meta:
model = Measure
form = MeasureFilterForm
# Defines the order shown in the form.
fields = ["search", "sid"]