diff --git a/app/blueprints/template/routes.py b/app/blueprints/template/routes.py index 82e45284..c0584d21 100644 --- a/app/blueprints/template/routes.py +++ b/app/blueprints/template/routes.py @@ -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, @@ -15,43 +15,15 @@ ) 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": "{form.template_name}" - }, - {"text": form.name_in_apply_json["en"]}, - {"text": form.runner_publish_name}, - { - "html": "Delete  " - "Rename" - }, - ] - 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() @@ -59,7 +31,7 @@ def view_templates(): 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")}, @@ -92,44 +64,41 @@ def view_templates(): return render_template("view_templates.html", **params, error=error) -@template_bp.route("/forms/", methods=["GET", "POST"]) -def edit_form_template(form_id): +@template_bp.route("/", 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("//delete", methods=["GET"]) +def delete_template(form_id): + delete_form(form_id=form_id, cascade=True) return redirect(url_for("template_bp.view_templates")) diff --git a/app/blueprints/template/services.py b/app/blueprints/template/services.py new file mode 100644 index 00000000..a9e6f8d6 --- /dev/null +++ b/app/blueprints/template/services.py @@ -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": "{form.template_name}" + }, + {"text": form.name_in_apply_json["en"]}, + {"text": form.runner_publish_name}, + { + "html": "Edit  " + "Delete" + }, + ] + rows.append(row) + return rows diff --git a/app/blueprints/template/templates/edit_form_template.html b/app/blueprints/template/templates/edit_form_template.html index aa287370..09557704 100644 --- a/app/blueprints/template/templates/edit_form_template.html +++ b/app/blueprints/template/templates/edit_form_template.html @@ -6,7 +6,7 @@ {% extends "base.html" %} {% set pageHeading %} -Rename Template +Edit Template {% endset %} {% block content %}