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 @@