Skip to content

Commit

Permalink
Added frontend to upload a template json
Browse files Browse the repository at this point in the history
  • Loading branch information
NarenderRajuB committed Sep 27, 2024
1 parent 3883a7c commit ff9321a
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 94 deletions.
9 changes: 9 additions & 0 deletions app/blueprints/fund_builder/forms/templates.py
Original file line number Diff line number Diff line change
@@ -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()])
35 changes: 32 additions & 3 deletions app/blueprints/templates/routes.py
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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)
18 changes: 17 additions & 1 deletion app/blueprints/templates/templates/view_templates.html
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -17,7 +21,19 @@ <h2 class="govuk-heading-m">Sections</h2>
<h2 class="govuk-heading-m">Forms</h2>
<ul class="govuk-body">
{% for form in forms %}
<li> {{form.template_name}}</li>
<li class="govuk-body"><a class="govuk-link" href="{{url_for('build_fund_bp.preview_form', form_id=form.form_id)}}">{{form.template_name}}</a></li>
{% endfor %}</ul>
</div>
<div class="govuk-grid-row">
<div class="govuk-form-group">
<fieldset class="govuk-fieldset">
<form method="POST" enctype="multipart/form-data">
{{ uploadform.hidden_tag()}}
{{ input(uploadform.template_name)}}
{{ govukFileUpload(uploadform.file) }}
{{ govukButton({"text": "Add"}) }}
</form>
</fieldset>
</div>
</div>
{% endblock %}
90 changes: 0 additions & 90 deletions app/import_config/files_to_import/applicant-information-cof.json

This file was deleted.

17 changes: 17 additions & 0 deletions app/import_config/load_form_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit ff9321a

Please sign in to comment.