Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GUI-generated JSON reform file to static output page #901

Merged
merged 12 commits into from
Jun 18, 2018
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ bower_components

src/django-flatblocks
src/dropq
src/pip-*
webapp_test.sh

puf.csv.gz
Expand All @@ -17,6 +18,7 @@ logs/

dump.rdb

.pytest_cache
.idea
.cache
.ipynb_checkpoints
Expand Down
2 changes: 1 addition & 1 deletion PARAMETERS.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ and edit the parameters that still exist and for the parameters that do not,
display an error message at the top of the page for each missing parameter that
says:

Field {parameter name} has been deprecated. Refer to the Tax-Caclulator
Field {parameter name} has been deprecated. Refer to the Tax-Calculator
documentation for a sensible replacement.
12 changes: 12 additions & 0 deletions RELEASES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@ Go
[here](https://github.com/OpenSourcePolicyCenter/PolicyBrain/pulls?q=is%3Apr+is%3Aclosed)
for a complete commit history.

Release 1.6.1 on 2018-06-??
----------------------------
**Major Changes**
- None

**Minor Changes**
- [#901](https://github.com/OpenSourcePolicyCenter/PolicyBrain/pull/901) - Display the JSON reform and assumption parameters generated by the GUI on the output page
- [#905](https://github.com/OpenSourcePolicyCenter/PolicyBrain/pull/905) - Allow the TaxBrain model to be run without any reforms

**Bug Fixes**
- [#904](https://github.com/OpenSourcePolicyCenter/PolicyBrain/pull/904) - Fix formatting of the first line in CSV output for better compatibility

Release 1.6.0 on 2018-06-13
----------------------------
**Major Changes**
Expand Down
2 changes: 1 addition & 1 deletion templates/taxbrain/not_avail.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ <h3>

<h3>
The Tax-Calculator package may have more options for re-creating results with the exact version of
Tax-Caclulator used in this simulation. For more details, please <a href="https://www.ospc.org/hello/"> contact the OSPC team. </a>
Tax-Calculator used in this simulation. For more details, please <a href="https://www.ospc.org/hello/"> contact the OSPC team. </a>
</h3>

</div>
Expand Down
26 changes: 19 additions & 7 deletions templates/taxbrain/results.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ <h1>Partial Equilibrium Results</h1>
{% endif %}
<div class="result-table">
<div class="result-table-controls">
{% if is_micro and not reform_file_contents %}
{% if is_micro and not is_from_file %}
<a href="/taxbrain/edit/{{ unique_url.pk }}/?start_year={{ first_year }}" class="text-white btn btn-secondary">Edit Parameters</a>
{% endif %}
{% if is_behavior and not reform_file_contents %}
{% if is_behavior and not is_from_file %}
<a href="/dynamic/behavioral/edit/{{ unique_url.pk }}/?start_year={{ first_year }}" class="text-white btn btn-secondary">Edit Parameters</a>
{% endif %}
{% if is_micro %}
Expand All @@ -70,16 +70,28 @@ <h1>Partial Equilibrium Results</h1>
{% endif %}
{% endif %}
</div>
{% if reform_file_contents %}
<h2> The following reform file(s) were uploaded for this simulation: </h2>
{% if reform_file_contents and assump_file_contents %}
{% if is_from_file %}
<h2>The following reform file(s) were uploaded for this simulation: </h2>
{% else %}
<h2>The following parameters were used for this simulation: </h2>
{% endif %}
<div class="file-contents">
<h3>Reform</h3>
{%autoescape off %}
{{ raw_reform_text | nbsp | linebreaks | safe}}
{{ reform_file_contents | nbsp | linebreaks | safe}}
{%endautoescape %}
{% if dynamic_file_contents %}
<h3>Dynamic Simulation Parameters</h3>
{% autoescape off %}
{{ dynamic_file_contents | nbsp | linebreaks | safe}}
{% endautoescape %}
{% endif %}
<h3>Assumptions</h3>
{%autoescape off %}
{{ raw_assumption_text | nbsp | linebreaks | safe}}
{{ assump_file_contents | nbsp | linebreaks | safe}}
{%endautoescape %}
</div>
</div>
{% endif %}
</div>
</div>
Expand Down
9 changes: 6 additions & 3 deletions webapp/apps/dynamic/tests/test_behavioral.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,15 @@ def test_get_not_avail_page_renders(self, start_year, start_year_is_none):
UrlModel=DynamicBehaviorOutputUrl)

model = unique_url.unique_inputs
model.raw_input_fields = None
model.input_fields = None
model.deprecated_fields = None
model.tax_result = "unrenderable"
if start_year_is_none:
model.first_year = None
micro_sim_fields = get_post_data(start_year,
_ID_BenefitSurtax_Switches=False)
micro_sim_fields['first_year'] = start_year
model.micro_sim = get_taxbrain_model(micro_sim_fields,
taxcalc_vers="0.14.2",
webapp_vers="1.4.0")
model.save()
unique_url.unique_inputs = model
unique_url.save()
Expand Down
38 changes: 28 additions & 10 deletions webapp/apps/dynamic/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -582,8 +582,7 @@ def dynamic_finished(request):
dsi.save()

params = dynamic_params_from_model(dsi)
hostname = os.environ.get('BASE_IRI', 'http://www.ospc.org')
microsim_url = hostname + "/taxbrain/" + str(dsi.micro_sim.pk)
microsim_url = "/taxbrain/" + str(dsi.micro_sim.pk)
#Create a new output model instance
if status == "SUCCESS":
unique_url = DynamicOutputUrl()
Expand All @@ -593,8 +592,7 @@ def dynamic_finished(request):
unique_url.unique_inputs = dsi
unique_url.model_pk = dsi.pk
unique_url.save()
result_url = "{host}/dynamic/results/{pk}".format(host=hostname,
pk=unique_url.pk)
result_url = "/dynamic/results/{pk}".format(pk=unique_url.pk)
text = success_text()
text = text.format(url=result_url, microsim_url=microsim_url,
job_id=job_id, params=params)
Expand Down Expand Up @@ -653,8 +651,7 @@ def elastic_results(request, pk):
first_year = model.first_year
created_on = model.creation_date
tables = elast_results_to_tables(output, first_year)
hostname = os.environ.get('BASE_IRI', 'http://www.ospc.org')
microsim_url = hostname + "/taxbrain/" + str(url.unique_inputs.micro_sim.pk)
microsim_url = "/taxbrain/" + str(url.unique_inputs.micro_sim.pk)

context = {
'locals':locals(),
Expand Down Expand Up @@ -749,8 +746,7 @@ def ogusa_results(request, pk):
first_year = url.unique_inputs.first_year
created_on = url.unique_inputs.creation_date
tables = ogusa_results_to_tables(output, first_year)
hostname = os.environ.get('BASE_IRI', 'http://www.ospc.org')
microsim_url = hostname + "/taxbrain/" + str(url.unique_inputs.micro_sim.pk)
microsim_url = "/taxbrain/" + str(url.unique_inputs.micro_sim.pk)

ogusa_vers_disp = get_version(url, 'ogusa_vers', OGUSA_VERSION)
taxcalc_vers_disp = get_version(url, 'taxcalc_vers', TAXCALC_VERSION)
Expand Down Expand Up @@ -788,6 +784,26 @@ def behavior_results(request, pk):
'webapp_version': webapp_vers_disp}

model = url.unique_inputs

first_year = model.first_year or int(START_YEAR)
micro = model.micro_sim.unique_inputs
if (micro.json_text is not None and (micro.json_text.raw_reform_text or
micro.json_text.raw_assumption_text)):
reform_file_contents = micro.json_text.raw_reform_text
reform_file_contents = reform_file_contents.replace(" ", "&nbsp;")
assump_file_contents = micro.json_text.raw_assumption_text
assump_file_contents = assump_file_contents.replace(" ", "&nbsp;")
elif micro.input_fields is not None:
reform = to_json_reform(first_year, micro.input_fields)
reform_file_contents = json.dumps(reform, indent=4)
assump_file_contents = '{}'
else:
reform_file_contents = ''
assump_file_contents = ''

dynamic_parameters = to_json_reform(first_year, model.input_fields)
dynamic_file_contents = json.dumps(dynamic_parameters, indent=4)

if model.tax_result:
# try to render table; if failure render not available page
try:
Expand All @@ -813,8 +829,7 @@ def behavior_results(request, pk):
'deciles': INCOME_DECILES_TOOLTIP
}
is_registered = True if request.user.is_authenticated() else False
hostname = os.environ.get('BASE_IRI', 'http://www.ospc.org')
microsim_url = hostname + "/taxbrain/" + str(model.micro_sim.pk)
microsim_url = "/taxbrain/" + str(model.micro_sim.pk)

# TODO: Fix the java script mapping problem. There exists somewhere in
# the taxbrain javascript code a mapping to the old table names. As
Expand Down Expand Up @@ -855,6 +870,9 @@ def behavior_results(request, pk):
'is_registered': is_registered,
'is_behavior': True,
'microsim_url': microsim_url,
'reform_file_contents': reform_file_contents,
'assump_file_contents': assump_file_contents,
'dynamic_file_contents': dynamic_file_contents,
'results_type': "behavioral"
}
context.update(context_vers_disp)
Expand Down
7 changes: 4 additions & 3 deletions webapp/apps/taxbrain/param_formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,18 +351,19 @@ def get_reform_from_gui(start_year, taxbrain_fields=None, behavior_fields=None,

policy_dict_json = {"policy": policy_dict_json}

policy_dict_json = json.dumps(policy_dict_json)
policy_dict_json = json.dumps(policy_dict_json, indent=4)

assumptions_dict_json = {"behavior": assumptions_dict_json,
"growdiff_response": {},
"consumption": {},
"growdiff_baseline": {},
"growmodel": {}}
assumptions_dict_json = json.dumps(assumptions_dict_json)
assumptions_dict_json = json.dumps(assumptions_dict_json, indent=4)

(reform_dict, assumptions_dict,
errors_warnings) = read_json_reform(policy_dict_json,
assumptions_dict_json,
use_puf_not_cps=use_puf_not_cps)

return (reform_dict, assumptions_dict, "", "", errors_warnings)
return (reform_dict, assumptions_dict, policy_dict_json,
assumptions_dict_json, errors_warnings)
5 changes: 1 addition & 4 deletions webapp/apps/taxbrain/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@ def test_taxbrain_old_data_gives_deprecation_errors(self):

assert response.context['has_errors'] is False

msg = ('Field {} has been deprecated. Refer to the Tax-Caclulator '
msg = ('Field {} has been deprecated. Refer to the Tax-Calculator '
'documentation for a sensible replacement.')

for param in ["ALD_Alimony_hc", "PT_exclusion_rt",
Expand All @@ -798,9 +798,6 @@ def test_get_not_avail_page_renders(self, start_year, start_year_is_none):
taxcalc_vers="0.14.2",
webapp_vers="1.3.0")
model = unique_url.unique_inputs
model.raw_input_fields = None
model.input_fields = None
model.deprecated_fields = None
model.tax_result = "unrenderable"
if start_year_is_none:
model.first_year = None
Expand Down
20 changes: 12 additions & 8 deletions webapp/apps/taxbrain/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ def edit_personal_results(request, pk):
start_year = model.first_year
model.set_fields()

msg = ('Field {} has been deprecated. Refer to the Tax-Caclulator '
msg = ('Field {} has been deprecated. Refer to the Tax-Calculator '
'documentation for a sensible replacement.')
form_personal_exemp = TaxBrainForm(
first_year=start_year,
Expand Down Expand Up @@ -690,15 +690,21 @@ def get_result_context(model, request, url):
'fiscal_change': FISCAL_CHANGE,
}

is_from_file = not model.raw_input_fields

if (model.json_text is not None and (model.json_text.raw_reform_text or
model.json_text.raw_assumption_text)):
reform_file_contents = model.json_text.raw_reform_text
reform_file_contents = reform_file_contents.replace(" ", "&nbsp;")
assump_file_contents = model.json_text.raw_assumption_text
assump_file_contents = assump_file_contents.replace(" ", "&nbsp;")
elif model.input_fields is not None:
reform = to_json_reform(first_year, model.input_fields)
reform_file_contents = json.dumps(reform, indent=4)
assump_file_contents = '{}'
else:
reform_file_contents = False
assump_file_contents = False
reform_file_contents = None
assump_file_contents = None

is_registered = hasattr(request, 'user') and request.user.is_authenticated()

Expand Down Expand Up @@ -734,7 +740,9 @@ def get_result_context(model, request, url):
'is_micro': True,
'reform_file_contents': reform_file_contents,
'assump_file_contents': assump_file_contents,
'allow_dyn_links': True if not assump_file_contents else False,
'dynamic_file_contents': None,
'is_from_file': is_from_file,
'allow_dyn_links': not is_from_file,
'results_type': "static"
}
return context
Expand Down Expand Up @@ -776,10 +784,6 @@ def output_detail(request, pk):
return render(request, 'taxbrain/not_avail.html', not_avail_context)

context.update(context_vers_disp)
context["raw_reform_text"] = (model.json_text.raw_reform_text
if model.json_text else "")
context["raw_assumption_text"] = (model.json_text.raw_assumption_text
if model.json_text else "")
return render(request, 'taxbrain/results.html', context)
elif model.error_text:
return render(request, 'taxbrain/failed.html',
Expand Down