diff --git a/app/blueprints/fund_builder/forms/templates.py b/app/blueprints/fund_builder/forms/templates.py new file mode 100644 index 00000000..3a3285ce --- /dev/null +++ b/app/blueprints/fund_builder/forms/templates.py @@ -0,0 +1,9 @@ +from flask_wtf import FlaskForm +from wtforms import FileField +from wtforms import StringField +from wtforms.validators import DataRequired + + +class TemplateUploadForm(FlaskForm): + template_name = StringField('Template Name',validators=[DataRequired()]) + file = FileField('Upload File',validators=[DataRequired()]) \ No newline at end of file diff --git a/app/blueprints/templates/routes.py b/app/blueprints/templates/routes.py index 33438363..63a7c6c5 100644 --- a/app/blueprints/templates/routes.py +++ b/app/blueprints/templates/routes.py @@ -1,8 +1,14 @@ from flask import Blueprint +from flask import redirect +from flask import url_for from flask import render_template - +from wtforms import ValidationError from app.db.queries.application import get_all_template_forms from app.db.queries.application import get_all_template_sections +from app.blueprints.fund_builder.forms.templates import TemplateUploadForm +import os +from werkzeug.utils import secure_filename + # Blueprint for routes used by FAB PoC to manage templates template_bp = Blueprint( @@ -12,9 +18,32 @@ template_folder="templates", ) +file_upload_path = os.path.join(os.path.dirname(__file__), "uplaoded_files") + +def json_import(file_path, template_name): + from app.import_config.load_form_json import load_json_from_file -@template_bp.route("/view") + load_json_from_file(file_path=file_path, template_name=template_name) + + +@template_bp.route("/view", methods=["GET", "POST"]) def view_templates(): sections = get_all_template_sections() forms = get_all_template_forms() - return render_template("view_templates.html", sections=sections, forms=forms) + form = TemplateUploadForm() + if form.validate_on_submit(): + template_name = form.template_name.data + file = form.file.data + if not os.path.exists(file_upload_path): + os.makedirs(file_upload_path) + if file: + uploaded_file = secure_filename(file.filename) + file_path = os.path.join(file_upload_path, uploaded_file) + file.save(file_path) + try: + json_import(file_path=file_path, template_name=template_name) + except Exception as e: + print(e) + 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 cfee63d7..2651ddb3 100644 --- a/app/blueprints/templates/templates/view_templates.html +++ b/app/blueprints/templates/templates/view_templates.html @@ -1,3 +1,7 @@ +{% from "macros/wtfToGovUk.html" import input %} +{%- from "govuk_frontend_jinja/components/file-upload/macro.html" import govukFileUpload -%} +{%- from "govuk_frontend_jinja/components/button/macro.html" import govukButton -%} + {% extends "base.html" %} {% set pageHeading %} View All Templates @@ -17,7 +21,19 @@