-
Notifications
You must be signed in to change notification settings - Fork 32
/
forms.py
349 lines (299 loc) · 13.4 KB
/
forms.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
from django import forms
from django.forms import ModelForm
from django.utils.translation import ugettext_lazy as _
from pyparsing import ParseException
import six
import json
from ..constants import START_YEAR
from .models import TaxSaveInputs
from .helpers import (is_number,
int_to_nth, is_string, string_to_float_array,
check_wildcards, default_taxcalc_data, expand_list,
propagate_user_list, convert_val, INPUT, INPUTS_META)
from .param_displayers import TaxCalcField, TaxCalcParam, default_policy
from .param_formatters import (get_default_policy_param,
ParameterLookUpException)
import taxcalc
def bool_like(x):
b = True if x == 'True' or x == True else False
return b
def parameter_name(param):
if not param.startswith("_"):
param = "_" + param
is_multi_param = any(param.endswith("_" + suffix) for suffix in ("0", "1", "2", "3"))
if is_multi_param:
return param[:-2]
else:
return param
def expand_unless_empty(param_values, param_name, param_column_name, form, new_len):
''' Take a list of parameters and, unless the list is empty, fill in any
wildcards and/or expand the list to the desired number of years, using
the proper inflation rates if necessary
If the list is empty, return it.
param_values: list of current values
param_name: name of the parameter
param_column_name: eg. _II_brk2_1 (names the sub-field)
form: The form object that has some data for the calculation
new_len: the new desired length of the return list
Returns: list of length new_len, unless the empty list is passed
'''
if param_values == []:
return param_values
has_wildcards = check_wildcards(param_values)
if len(param_values) < new_len or has_wildcards:
#Only process wildcards and floats from this point on
param_values = [convert_val(x) for x in param_values]
# Discover the CPI setting for this parameter
cpi_flag = form.discover_cpi_flag(param_name, form.cleaned_data)
default_data = form._default_taxcalc_data[param_name]
expnded_defaults = expand_list(default_data, new_len)
is_multi_param = any(param_column_name.endswith("_" + suffix) for suffix in ("0", "1", "2", "3"))
if is_multi_param:
idx = int(param_column_name[-1])
else:
idx = -1
param_values = propagate_user_list(param_values,
name=param_name,
defaults=expnded_defaults,
cpi=cpi_flag,
first_budget_year=form._first_year,
multi_param_idx=idx)
param_values = [float(x) for x in param_values]
return param_values
class PolicyBrainForm:
def add_fields(self, args):
if not args:
return args
parsed_data = {}
args_data = args[0]
raw_fields = {}
for k, v in list(args_data.items()):
if k not in INPUTS_META:
raw_fields[k] = v
elif k in ('first_year', 'data_source'):
parsed_data[k] = v
else:
pass
parsed_data["raw_input_fields"] = json.dumps(raw_fields)
parsed_data["input_fields"] = json.dumps("")
return (parsed_data,)
def add_errors_on_extra_inputs(self):
ALLOWED_EXTRAS = {'has_errors', 'start_year', 'csrfmiddlewaretoken',
'data_source'}
all_inputs = set(self.data.keys())
allowed_inputs = set(self.fields.keys())
extra_inputs = all_inputs - allowed_inputs - ALLOWED_EXTRAS
for _input in extra_inputs:
self.add_error(None,
"Extra input '{0}' not allowed".format(_input))
all_fields = self.cleaned_data['raw_input_fields']
default_policy_data = getattr(self.Meta, 'default_policy_data', None)
allowed_fields = getattr(self.Meta, 'allowed_fields', None)
for _field in all_fields:
if default_policy_data:
try:
get_default_policy_param(_field, default_policy_data)
except ParameterLookUpException as exn:
self.add_error(None, str(exn))
elif _field not in allowed_fields:
msg = "Received unexpected parameter: {}".format(_field)
self.add_error(None, msg)
def do_taxcalc_validations(self):
"""
Do minimal type checking to make sure that we did not get any
malicious input
"""
fields = self.cleaned_data['raw_input_fields']
for param_name, value in fields.items():
# make sure the text parses OK
if param_name == 'data_source':
assert value in ('CPS', 'PUF')
elif isinstance(value, six.string_types) and len(value) > 0:
try:
INPUT.parseString(value)
except (ParseException, AssertionError):
# Parse Error - we don't recognize what they gave us
self.add_error(param_name, "Unrecognized value: {}".format(value))
try:
# reverse character is not at the beginning
assert value.find('<') <= 0
except AssertionError:
self.add_error(
param_name,
("Operator '<' can only be used "
"at the beginning")
)
else:
assert isinstance(value, bool) or len(value) == 0
@staticmethod
def set_form(defaults):
"""
Setup all of the form fields and widgets with the the 2016 default data
"""
widgets = {}
labels = {}
update_fields = {}
boolean_fields = []
for param in list(defaults.values()):
for field in param.col_fields:
attrs = {
'class': 'form-control',
'placeholder': field.default_value,
}
if param.coming_soon:
attrs['disabled'] = True
if param.tc_id in boolean_fields:
checkbox = forms.CheckboxInput(attrs=attrs, check_test=bool_like)
widgets[field.id] = checkbox
update_fields[field.id] = forms.BooleanField(
label=field.label,
widget=widgets[field.id],
required=False,
disabled=param.gray_out
)
else:
widgets[field.id] = forms.TextInput(attrs=attrs)
update_fields[field.id] = forms.fields.CharField(
label=field.label,
widget=widgets[field.id],
required=False,
disabled=param.gray_out
)
labels[field.id] = field.label
if getattr(param, "inflatable", False):
field = param.cpi_field
attrs = {
'class': 'form-control sr-only',
'placeholder': bool(field.default_value),
}
widgets[field.id] = forms.NullBooleanSelect(attrs=attrs)
update_fields[field.id] = forms.NullBooleanField(
label=field.label,
widget=widgets[field.id],
required=False,
disabled=param.gray_out
)
return widgets, labels, update_fields
TAXCALC_DEFAULTS = {
(int(START_YEAR), True): default_policy(int(START_YEAR),
use_puf_not_cps=True)
}
class TaxBrainForm(PolicyBrainForm, ModelForm):
def __init__(self, first_year, use_puf_not_cps, *args, **kwargs):
# the start year and the data source form object for later access.
# This should be refactored into `process_model`
if first_year is None:
first_year = START_YEAR
self._first_year = int(first_year)
# reset form data; form data from the `Meta` class is not updated each
# time a new `TaxBrainForm` instance is created
self.set_form_data(self._first_year, use_puf_not_cps)
# move parameter fields into `raw_fields` JSON object
args = self.add_fields(args)
# Override `initial` with `instance`. The only relevant field
# in `instance` is `raw_input_fields` which contains all of the user
# input data from the stored run. By overriding the `initial` kw
# argument we are making all of the user input from the previous run
# as stored in the `raw_input_fields` field of `instance` available
# to the fields attribute in django forms. This front-end data is
# derived from this fields attribute.
# Take a look at the source code for more info:
# https://github.com/django/django/blob/1.9/django/forms/models.py#L284-L285
if "instance" in kwargs:
kwargs["initial"] = kwargs["instance"].raw_input_fields
# Update CPI flags if either
# 1. initial is specified in `kwargs` (reform has warning/error msgs)
# 2. if `instance` is specified and `initial` is added above
# (edit parameters page)
if kwargs.get("initial", False):
for k, v in kwargs["initial"].items():
if k.endswith("cpi") and v:
# raw data is stored as choices 1, 2, 3 with the following
# mapping:
# '1': unknown
# '2': True
# '3': False
# value_from_datadict unpacks this data:
# https://github.com/django/django/blob/1.9/django/forms/widgets.py#L582-L589
if v == '1':
continue
django_val = self.widgets[k].value_from_datadict(
kwargs["initial"],
None,
k
)
self.widgets[k].attrs["placeholder"] = django_val
super(TaxBrainForm, self).__init__(*args, **kwargs)
# update fields in a similar way as
# https://www.pydanny.com/overloading-form-fields.html
self.fields.update(self.update_fields.copy())
def clean(self):
"""
" This method should be used to provide custom model validation, and to
modify attributes on your model if desired. For instance, you could use
it to automatically provide a value for a field, or to do validation
that requires access to more than a single field."
per https://docs.djangoproject.com/en/1.8/ref/models/instances/
Note that this can be defined both on forms and on the model, but is
only automatically called on form submissions.
"""
self.do_taxcalc_validations()
self.add_errors_on_extra_inputs()
def add_error(self, field, error):
"""
Safely adds errors. There was an issue where the `cleaned_data`
attribute wasn't created after `is_valid` was called. This ensures
that the `cleaned_data` attribute is there.
"""
if getattr(self, "cleaned_data", None) is None or self.cleaned_data is None:
self.cleaned_data = {}
ModelForm.add_error(self, field, error)
def set_form_data(self, start_year, use_puf_not_cps):
defaults_key = (start_year, use_puf_not_cps)
if defaults_key not in TAXCALC_DEFAULTS:
TAXCALC_DEFAULTS[defaults_key] = default_policy(start_year, use_puf_not_cps)
defaults = TAXCALC_DEFAULTS[defaults_key]
(self.widgets, self.labels,
self.update_fields) = PolicyBrainForm.set_form(defaults)
class Meta:
model = TaxSaveInputs
# we are only updating the "first_year", "raw_fields", and "fields"
# fields
fields = ['first_year', 'data_source', 'raw_input_fields',
'input_fields']
start_year = int(START_YEAR)
pol = taxcalc.Policy()
pol.set_year(int(START_YEAR))
default_policy_data = pol.metadata()
defaults_key = (start_year, True)
if defaults_key not in TAXCALC_DEFAULTS:
TAXCALC_DEFAULTS[defaults_key] = default_policy(
start_year,
use_puf_not_cps=True
)
(widgets, labels,
update_fields) = PolicyBrainForm.set_form(
TAXCALC_DEFAULTS[defaults_key]
)
def has_field_errors(form, include_parse_errors=False):
"""
This allows us to see if we have field_errors, as opposed to only having
form.non_field_errors. I would prefer to put this in a template tag, but
getting that working with a conditional statement in a template was very
challenging.
"""
if not form.errors:
return False
for field in form:
if field.errors:
if include_parse_errors:
if "Unrecognized value" in field.errors[0]:
return True
else:
continue
else:
if "Unrecognized value" in field.errors[0]:
continue
else:
return True
return False