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

Sections

Forms

+
+
+
+
+ {{ uploadform.hidden_tag()}} + {{ input(uploadform.template_name)}} + {{ govukFileUpload(uploadform.file) }} + {{ govukButton({"text": "Add"}) }} +
+
+
+
{% endblock %} diff --git a/app/import_config/files_to_import/applicant-information-cof.json b/app/import_config/files_to_import/applicant-information-cof.json deleted file mode 100644 index ab6d3b62..00000000 --- a/app/import_config/files_to_import/applicant-information-cof.json +++ /dev/null @@ -1,90 +0,0 @@ -{ - "metadata": {}, - "startPage": "/applicant-information", - "pages": [ - { - "title": "Applicant information", - "path": "/applicant-information", - "components": [ - { - "name": "UtVqvP", - "options": {}, - "type": "Para", - "content": "\n\n" - } - ], - "next": [ - { - "path": "/lead-contact-details" - } - ], - "controller": "./pages/start.js" - }, - { - "path": "/lead-contact-details", - "title": "Lead contact details", - "components": [ - { - "name": "SnLGJE", - "options": { - "classes": "govuk-!-width-full" - }, - "type": "TextField", - "title": "Name of lead contact", - "hint": "They will receive all the information about this application." - }, - { - "name": "qRDTUc", - "options": { - "classes": "govuk-!-width-full" - }, - "type": "TextField", - "title": "Lead contact job title" - }, - { - "name": "NlHSBg", - "options": { - "classes": "govuk-!-width-full" - }, - "type": "EmailAddressField", - "title": "Lead contact email address" - }, - { - "name": "FhBkJQ", - "options": { - "classes": "govuk-!-width-full" - }, - "type": "TelephoneNumberField", - "title": "Lead contact telephone number" - } - ], - "next": [ - { - "path": "/summary" - } - ], - "section": "ZuHuGk" - }, - { - "title": "Check your answers", - "path": "/summary", - "controller": "./pages/summary.js", - "components": [], - "next": [], - "section": "ZuHuGk" - } - ], - "lists": [], - "sections": [ - { - "name": "ZuHuGk", - "title": "Applicant information" - } - ], - "conditions": [], - "fees": [], - "outputs": [], - "version": 2, - "skipSummary": false, - "name": "Apply for funding to save an asset in your community" -} diff --git a/app/import_config/load_form_json.py b/app/import_config/load_form_json.py index 6ab164c6..88718a86 100644 --- a/app/import_config/load_form_json.py +++ b/app/import_config/load_form_json.py @@ -227,6 +227,23 @@ def load_form_jsons(override_fund_config=None): raise e +def load_json_from_file(file_path, template_name=None): + db = app.extensions["sqlalchemy"] + try: + if os.path.basename(file_path).endswith(".json"): + with open(file_path, "r") as json_file: + form = json.load(json_file) + form["filename"] = template_name if template_name else os.path.basename(file_path) + inserted_form = insert_form_as_template(form) + db.session.flush() # flush to get the form id + insert_form_config(form, inserted_form.form_id) + db.session.commit() + except Exception as e: + print(e) + db.session.rollback() + raise e + + if __name__ == "__main__": with app.app_context(): load_form_jsons()