Skip to content

Commit

Permalink
Add error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
NarenderRajuB committed Sep 30, 2024
1 parent 3713153 commit 4c3d3c7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
18 changes: 11 additions & 7 deletions app/blueprints/templates/routes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import json
import os

from flask import Blueprint
from flask import redirect
Expand All @@ -10,6 +9,7 @@
from app.blueprints.fund_builder.forms.templates import TemplateUploadForm
from app.db.queries.application import get_all_template_forms
from app.db.queries.application import get_all_template_sections
from app.db.queries.application import get_form_by_template_name

# Blueprint for routes used by FAB PoC to manage templates
template_bp = Blueprint(
Expand All @@ -19,8 +19,6 @@
template_folder="templates",
)

file_upload_path = os.path.join(os.path.dirname(__file__), "uplaoded_files")


def json_import(data, template_name):
from app.import_config.load_form_json import load_json_from_file
Expand All @@ -36,15 +34,21 @@ def view_templates():
if form.validate_on_submit():
template_name = form.template_name.data
file = form.file.data
if get_form_by_template_name(template_name):
form.error = "Template name already exists"
return render_template("view_templates.html", sections=sections, forms=forms, uploadform=form)

if file:
secure_filename(file.filename)
file_data = file.read().decode("utf-8")
form = json.loads(file_data)
try:
json_import(data=form, template_name=template_name)
secure_filename(file.filename)
file_data = file.read().decode("utf-8")
form_data = json.loads(file_data)
json_import(data=form_data, template_name=template_name)
except Exception as e:
print(e)
form.error = "Invalid file: Please upload valid JSON file"
return render_template("view_templates.html", sections=sections, forms=forms, uploadform=form)

return redirect(url_for("template_bp.view_templates"))

return render_template("view_templates.html", sections=sections, forms=forms, uploadform=form)
8 changes: 8 additions & 0 deletions app/blueprints/templates/templates/view_templates.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,17 @@ <h2 class="govuk-heading-m">Forms</h2>
</div>
<div class="govuk-grid-row">
<div class="govuk-form-group">
<div>
{% if uploadform.error %}
<span class ="govuk-error-message">
<span class="govuk-visually-hidden">Error:</span>{{ uploadform.error }}
</span>
{% endif %}
</div>
<fieldset class="govuk-fieldset">
<form method="POST" enctype="multipart/form-data">
{{ uploadform.hidden_tag()}}

{{ input(uploadform.template_name)}}
{{ govukFileUpload(uploadform.file) }}
{{ govukButton({"text": "Add"}) }}
Expand Down
5 changes: 5 additions & 0 deletions app/db/queries/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ def get_form_by_id(form_id: str) -> Form:
return form


def get_form_by_template_name(template_name: str) -> Form:
form = db.session.query(Form).where(Form.template_name == template_name).one_or_none()
return form


def get_component_by_id(component_id: str) -> Component:
component = db.session.query(Component).where(Component.component_id == component_id).one_or_none()
return component
Expand Down

0 comments on commit 4c3d3c7

Please sign in to comment.