diff --git a/app/main/forms.py b/app/main/forms.py index 5683149..85240fa 100644 --- a/app/main/forms.py +++ b/app/main/forms.py @@ -1,7 +1,7 @@ from flask_wtf import FlaskForm -from govuk_frontend_wtf.wtforms_widgets import GovRadioInput, GovSelect, GovSubmitInput +from govuk_frontend_wtf.wtforms_widgets import GovRadioInput, GovSubmitInput from wtforms.fields import RadioField, SelectField, SubmitField -from wtforms.validators import AnyOf, InputRequired +from wtforms.validators import InputRequired class CookiesForm(FlaskForm): @@ -24,16 +24,15 @@ class CookiesForm(FlaskForm): class DownloadForm(FlaskForm): file_format = SelectField( - "File type", - widget=GovSelect(), - validators=[AnyOf(["json", "xlsx"])], + "Which format do you need?", + widget=GovRadioInput(), choices=[ - ("xlsx", "XSLX (Excel)"), + ("xlsx", "XLSX (Microsoft Excel)"), ("json", "JSON"), ], default=None, ) - download = SubmitField("Download", widget=GovSubmitInput()) + download = SubmitField("Confirm and request data", widget=GovSubmitInput()) class RetrieveForm(FlaskForm): diff --git a/app/main/routes.py b/app/main/routes.py index d46ca73..4636d67 100644 --- a/app/main/routes.py +++ b/app/main/routes.py @@ -69,65 +69,76 @@ def download(): ) if request.method == "POST": - file_format = form.file_format.data - if file_format not in ["json", "xlsx"]: - current_app.logger.error( - "Unexpected file format requested from /download: {file_format}", - extra=dict(file_format=file_format), + if form.validate_on_submit(): + file_format = form.file_format.data + orgs = request.form.getlist(FormNames.ORGS) + regions = request.form.getlist(FormNames.REGIONS) + funds = request.form.getlist(FormNames.FUNDS) + outcome_categories = request.form.getlist(FormNames.OUTCOMES) + from_quarter = request.form.get("from-quarter") + from_year = request.form.get("from-year") + to_quarter = request.form.get("to-quarter") + to_year = request.form.get("to-year") + + reporting_period_start = ( + financial_quarter_from_mapping(quarter=from_quarter, year=from_year) if to_quarter and to_year else None ) - return abort(500), f"Unknown file format: {file_format}" - - orgs = request.form.getlist(FormNames.ORGS) - regions = request.form.getlist(FormNames.REGIONS) - funds = request.form.getlist(FormNames.FUNDS) - outcome_categories = request.form.getlist(FormNames.OUTCOMES) - from_quarter = request.form.get("from-quarter") - from_year = request.form.get("from-year") - to_quarter = request.form.get("to-quarter") - to_year = request.form.get("to-year") - - reporting_period_start = ( - financial_quarter_from_mapping(quarter=from_quarter, year=from_year) if to_quarter and to_year else None - ) - reporting_period_end = ( - financial_quarter_to_mapping(quarter=to_quarter, year=to_year) if to_quarter and to_year else None - ) + reporting_period_end = ( + financial_quarter_to_mapping(quarter=to_quarter, year=to_year) if to_quarter and to_year else None + ) + + query_params = {"email_address": g.user.email, "file_format": file_format} + if orgs: + query_params["organisations"] = orgs + if regions: + query_params["regions"] = regions + if funds: + query_params["funds"] = funds + if outcome_categories: + query_params["outcome_categories"] = outcome_categories + if reporting_period_start: + query_params["rp_start"] = reporting_period_start + if reporting_period_end: + query_params["rp_end"] = reporting_period_end + + status_code = process_async_download(query_params) + + current_app.logger.info( + "Request for download by {user_id} with {query_params}", + extra={ + "user_id": g.account_id, + "email": g.user.email, + "query_params": query_params, + "request_type": "download", + }, + ) - query_params = {"email_address": g.user.email, "file_format": file_format} - if orgs: - query_params["organisations"] = orgs - if regions: - query_params["regions"] = regions - if funds: - query_params["funds"] = funds - if outcome_categories: - query_params["outcome_categories"] = outcome_categories - if reporting_period_start: - query_params["rp_start"] = reporting_period_start - if reporting_period_end: - query_params["rp_end"] = reporting_period_end - - status_code = process_async_download(query_params) - - current_app.logger.info( - "Request for download by {user_id} with {query_params}", - extra={ - "user_id": g.account_id, - "email": g.user.email, - "query_params": query_params, - "request_type": "download", - }, + if status_code == 204: + return redirect(url_for("main.request_received")) + else: + current_app.logger.error( + "Response status code from data-store/trigger_async_download: {content_type}", + extra=dict(content_type=status_code), + ) + return render_template("500.html") + + current_app.logger.error( + "Unexpected file format requested from /download: {file_format}", + extra=dict(file_format=form.file_format), ) + if form.file_format.errors: + form.file_format.errors = ["Select a file format"] - if status_code == 204: - return redirect(url_for("main.request_received")) - else: - current_app.logger.error( - "Response status code from data-store/trigger_async_download: {content_type}", - extra=dict(content_type=status_code), - ) - return render_template("500.html") + return render_template( + "download.html", + form=form, + funds=get_fund_checkboxes(), + regions=get_region_checkboxes(), + orgs=get_org_checkboxes(), + outcomes=get_outcome_checkboxes(), + returnsParams=get_returns(), + ) @bp.route("/request-received", methods=["GET"]) diff --git a/app/templates/main/download.html b/app/templates/main/download.html index d8f4933..9d4613e 100644 --- a/app/templates/main/download.html +++ b/app/templates/main/download.html @@ -5,6 +5,7 @@ {%- from 'govuk_frontend_jinja/components/select/macro.html' import govukSelect -%} {%- from 'govuk_frontend_jinja/components/button/macro.html' import govukButton -%} {%- from 'govuk_frontend_jinja/components/label/macro.html' import govukLabel -%} +{%- from 'govuk_frontend_jinja/components/error-summary/macro.html' import govukErrorSummary -%} {%- from "outcomeCheckboxes.html" import outcomeCheckboxItems -%} {%- from "checkboxes.html" import checkboxItems -%} {%- from "select.html" import selectItems -%} @@ -14,74 +15,102 @@ {% endblock beforeContent %} {% block content %} -
Use the following filters to select the projects you need.
+ ] + }) + }} -The filters you select will be applied at project level. You will be able to download this as a file to your desktop.
+ {% endif %} +Use the filters to select the data you need.
+You will get all of the data from all projects and programmes if you do not select any filters.
+ + +