diff --git a/app/blueprints/templates/routes.py b/app/blueprints/templates/routes.py index 1efd3564..244bbde8 100644 --- a/app/blueprints/templates/routes.py +++ b/app/blueprints/templates/routes.py @@ -1,5 +1,4 @@ import json -import os from flask import Blueprint from flask import redirect @@ -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( @@ -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 @@ -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) diff --git a/app/blueprints/templates/templates/view_templates.html b/app/blueprints/templates/templates/view_templates.html index 2651ddb3..7884be8b 100644 --- a/app/blueprints/templates/templates/view_templates.html +++ b/app/blueprints/templates/templates/view_templates.html @@ -26,9 +26,17 @@

Forms

+
+ {% if uploadform.error %} + + Error:{{ uploadform.error }} + + {% endif %} +
{{ uploadform.hidden_tag()}} + {{ input(uploadform.template_name)}} {{ govukFileUpload(uploadform.file) }} {{ govukButton({"text": "Add"}) }} diff --git a/app/db/queries/application.py b/app/db/queries/application.py index 78f37a72..3bcfde6e 100644 --- a/app/db/queries/application.py +++ b/app/db/queries/application.py @@ -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