Skip to content

Commit

Permalink
FS-4919 - Restructuring and rerouting template routes for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
wjrm500 committed Dec 27, 2024
1 parent f13442f commit e25672f
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 65 deletions.
97 changes: 33 additions & 64 deletions app/blueprints/template/routes.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import json

from flask import Blueprint, redirect, render_template, request, url_for
from flask import Blueprint, redirect, render_template, url_for
from werkzeug.utils import secure_filename

from app.blueprints.template.forms import TemplateFormForm, TemplateUploadForm
from app.db.models.application_config import Form
from app.blueprints.template.services import build_rows, json_import
from app.db.queries.application import (
delete_form,
get_all_template_forms,
Expand All @@ -15,51 +15,23 @@
)
from app.shared.helpers import error_formatter

# Blueprint for routes used by FAB PoC to manage templates
template_bp = Blueprint(
"template_bp",
__name__,
url_prefix="/template",
url_prefix="/templates",
template_folder="templates",
)


def json_import(data, template_name, filename):
from app.import_config.load_form_json import load_json_from_file

load_json_from_file(data=data, template_name=template_name, filename=filename)


def _build_rows(forms: list[Form]) -> list[dict]:
rows = []
for form in forms:
row = [
{
"html": "<a class='govuk-link--no-visited-state' "
f"href='{url_for('index_bp.preview_form', form_id=form.form_id)}'>{form.template_name}</a>"
},
{"text": form.name_in_apply_json["en"]},
{"text": form.runner_publish_name},
{
"html": "<a class='govuk-link--no-visited-state' href='"
f"{url_for('template_bp.edit_form_template', form_id=form.form_id, action='remove')}'>Delete</a> &nbsp;"
"<a class='govuk-link--no-visited-state' href='"
f"{url_for('template_bp.edit_form_template', form_id=form.form_id, action='edit')}'>Rename</a>"
},
]
rows.append(row)
return rows


@template_bp.route("/all", methods=["GET", "POST"])
@template_bp.route("", methods=["GET", "POST"])
def view_templates():
sections = get_all_template_sections()
forms = get_all_template_forms()
form = TemplateUploadForm()
params = {
"sections": sections,
"forms": forms,
"form_template_rows": _build_rows(forms),
"form_template_rows": build_rows(forms),
"uploadform": form,
"breadcrumb_items": [
{"text": "Home", "href": url_for("index_bp.dashboard")},
Expand Down Expand Up @@ -92,44 +64,41 @@ def view_templates():
return render_template("view_templates.html", **params, error=error)


@template_bp.route("/forms/<form_id>", methods=["GET", "POST"])
def edit_form_template(form_id):
@template_bp.route("/<form_id>", methods=["GET", "POST"])
def edit_template(form_id):
template_form = TemplateFormForm()
if template_form.validate_on_submit():
update_form(
form_id=form_id,
new_form_config={
"runner_publish_name": template_form.url_path.data,
"name_in_apply_json": {"en": template_form.tasklist_name.data},
"template_name": template_form.template_name.data,
},
)
return redirect(url_for("template_bp.view_templates"))
existing_form = get_form_by_id(form_id=form_id)
template_form.form_id.data = form_id
template_form.template_name.data = existing_form.template_name
template_form.tasklist_name.data = existing_form.name_in_apply_json["en"]
template_form.url_path.data = existing_form.runner_publish_name
params = {
"breadcrumb_items": [
{"text": "Home", "href": url_for("index_bp.dashboard")},
{"text": "Manage Templates", "href": url_for("template_bp.view_templates")},
{"text": "Rename Template", "href": "#"},
{"text": "Edit Template", "href": "#"},
],
"template_form": template_form,
}
error = error_formatter(template_form)
return render_template(
"edit_form_template.html",
**params,
error=error,
)

if request.method == "POST":
if template_form.validate_on_submit():
update_form(
form_id=form_id,
new_form_config={
"runner_publish_name": template_form.url_path.data,
"name_in_apply_json": {"en": template_form.tasklist_name.data},
"template_name": template_form.template_name.data,
},
)
return redirect(url_for("template_bp.view_templates"))
params["template_form"] = template_form
error = None
if "template_form" in params:
error = error_formatter(params["template_form"])
return render_template("edit_form_template.html", **params, error=error)

if request.args.get("action") == "remove":
delete_form(form_id=form_id, cascade=True)
if request.args.get("action") == "edit":
existing_form = get_form_by_id(form_id=form_id)
template_form = TemplateFormForm()
template_form.form_id.data = form_id
template_form.template_name.data = existing_form.template_name
template_form.tasklist_name.data = existing_form.name_in_apply_json["en"]
template_form.url_path.data = existing_form.runner_publish_name
params["template_form"] = template_form
return render_template("edit_form_template.html", **params)

@template_bp.route("/<form_id>/delete", methods=["GET"])
def delete_template(form_id):
delete_form(form_id=form_id, cascade=True)
return redirect(url_for("template_bp.view_templates"))
30 changes: 30 additions & 0 deletions app/blueprints/template/services.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from flask import url_for

from app.db.models.application_config import Form


def json_import(data, template_name, filename):
from app.import_config.load_form_json import load_json_from_file

load_json_from_file(data, template_name, filename)


def build_rows(forms: list[Form]) -> list[dict]:
rows = []
for form in forms:
row = [
{
"html": "<a class='govuk-link--no-visited-state' "
f"href='{url_for('index_bp.preview_form', form_id=form.form_id)}'>{form.template_name}</a>"
},
{"text": form.name_in_apply_json["en"]},
{"text": form.runner_publish_name},
{
"html": "<a class='govuk-link--no-visited-state' href='"
f"{url_for('template_bp.edit_template', form_id=form.form_id)}'>Edit</a> &nbsp;"
"<a class='govuk-link--no-visited-state' href='"
f"{url_for('template_bp.delete_template', form_id=form.form_id)}'>Delete</a>"
},
]
rows.append(row)
return rows
2 changes: 1 addition & 1 deletion app/blueprints/template/templates/edit_form_template.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

{% extends "base.html" %}
{% set pageHeading %}
Rename Template
Edit Template
{% endset %}
{% block content %}
<div class="govuk-grid-row">
Expand Down

0 comments on commit e25672f

Please sign in to comment.