From ae0032bbb1f298ea63c67c52bed09abdbc2fea39 Mon Sep 17 00:00:00 2001 From: Philippe Caron Date: Thu, 15 Aug 2024 15:07:09 +0000 Subject: [PATCH 1/2] Test page for tempalte categories --- app/main/views/styleguide.py | 61 ++++++++++++++++++++++++++++- app/templates/views/categories.html | 60 ++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+), 2 deletions(-) create mode 100644 app/templates/views/categories.html diff --git a/app/main/views/styleguide.py b/app/main/views/styleguide.py index 92fdda53de..f482bd783a 100644 --- a/app/main/views/styleguide.py +++ b/app/main/views/styleguide.py @@ -1,9 +1,17 @@ -from flask import abort, current_app, render_template +from flask import abort, current_app, render_template, redirect, url_for from flask_wtf import FlaskForm as Form +from flask_babel import lazy_gettext as _l from notifications_utils.template import Template -from wtforms import FileField, PasswordField, StringField, TextAreaField, validators +from wtforms import FileField, PasswordField, RadioField, StringField, TextAreaField, validators +from app import (get_current_locale, template_category_api_client) from app.main import main +from app.main.forms import ( + TC_PRIORITY_VALUE, + EmailTemplateFormWithCategory, +) +from app.models.enum.template_categories import DefaultTemplateCategories + @main.route("/_styleguide") @@ -27,3 +35,52 @@ class FormExamples(Form): template = Template({"content": sms}) return render_template("views/styleguide.html", form=form, template=template) + + +@main.route("/_categories", methods=["GET", "POST"]) +def categories(): + if not current_app.config["SHOW_STYLEGUIDE"]: + abort(404) + + class FormExamples(Form): + template_categories = RadioField(_l("Select category"), validators=[validators.DataRequired(message=_l("This cannot be empty"))], choices=[ + ("alert", "Alert"), + ("authentication", "Authentication"), + ("automatic_reply", "Automatic reply"), + ("decision", "Decision"), + ("information_blast", "Information blast"), + ("reminder", "Reminder"), + ("request", "Request"), + ("status_update", "Status update"), + ("test", "Test"), + ("other", "Other"), + ]) + template_category_hints = { + "alert": "System checks and monitoring", + "authentication": "Password resets and two factor verification", + "automatic_reply": "No-reply and confirmation messages", + "decision": "Permits, documents and results", + "information_blast": "Newsletters, surveys and general information", + "reminder": "Appointments and deadlines", + "request": "Follow up and next steps", + "status_update": "Changes and progress", + "test": "Practice messages" + } + + form = EmailTemplateFormWithCategory() + categories = template_category_api_client.get_all_template_categories() + + other_category = {DefaultTemplateCategories.LOW.value: form.template_category_other} + + name_col = "name_en" if get_current_locale(current_app) == "en" else "name_fr" + desc_col = "description_en" if get_current_locale(current_app) == "en" else "description_fr" + categories = sorted(categories, key=lambda x: x[name_col]) + form.template_category_id.choices = [(cat["id"], cat[name_col]) for cat in categories if not cat.get("hidden", False)] + + form.template_category_id.choices.append((DefaultTemplateCategories.LOW.value, _l("Other"))) + template_category_hints = {cat["id"]: cat[desc_col] for cat in categories} + + if form.validate_on_submit(): + return redirect(url_for(".categories")) + + return render_template("views/categories.html", form=form, template_category_hints=template_category_hints, other_category=other_category) diff --git a/app/templates/views/categories.html b/app/templates/views/categories.html new file mode 100644 index 0000000000..2c66d9a7ae --- /dev/null +++ b/app/templates/views/categories.html @@ -0,0 +1,60 @@ +{% extends "admin_template.html" %} +{% from "components/textbox.html" import textbox %} +{% from "components/page-header.html" import page_header %} +{% from "components/page-footer.html" import sticky_page_footer %} +{% from "components/radios.html" import radios %} +{% from "components/select-input.html" import select %} +{% from "components/form.html" import form_wrapper %} +{% from "components/task-shortcut.html" import task_shortcut %} +{% from "components/template-category.html" import template_category %} + +{% block service_page_title %} +{{ heading }} +{% endblock %} + +{% block maincolumn_content %} +{{ page_header(heading) }} +{% call form_wrapper() %} + +
+
+ + {% if config["FF_TEMPLATE_CATEGORY"] %} +

{{ _('Template category') }}

+ {% call template_category(form.template_category_id, true if template_category_mode == 'expand' else false) %} + {{ select(form.template_category_id, hint=_('Template categories help improve delivery of your messages'), + option_hints=template_category_hints, option_conditionals=other_category, testid="template-categories", + use_aria_labelledby=false) }} + {% endcall %} + {% endif %} + {{ sticky_page_footer(_('Save')) }} +
+
+{% endcall %} +{% endblock %} + +{% block page_script %} +{{ super() }} + + +{% endblock %} \ No newline at end of file From 2319486d1fb7aca20be16c4c0155e1225d724e1b Mon Sep 17 00:00:00 2001 From: Philippe Caron Date: Fri, 16 Aug 2024 13:16:46 +0000 Subject: [PATCH 2/2] Added flash message for confirmation --- app/main/views/styleguide.py | 55 +++++++++++++----------------------- app/translations/csv/fr.csv | 1 + 2 files changed, 20 insertions(+), 36 deletions(-) diff --git a/app/main/views/styleguide.py b/app/main/views/styleguide.py index f482bd783a..3b710ba9b1 100644 --- a/app/main/views/styleguide.py +++ b/app/main/views/styleguide.py @@ -1,15 +1,12 @@ -from flask import abort, current_app, render_template, redirect, url_for +from flask import abort, current_app, flash, render_template, redirect, request, url_for from flask_wtf import FlaskForm as Form -from flask_babel import lazy_gettext as _l +from flask_babel import _ from notifications_utils.template import Template -from wtforms import FileField, PasswordField, RadioField, StringField, TextAreaField, validators +from wtforms import FileField, PasswordField, StringField, TextAreaField, validators from app import (get_current_locale, template_category_api_client) from app.main import main -from app.main.forms import ( - TC_PRIORITY_VALUE, - EmailTemplateFormWithCategory, -) +from app.main.forms import EmailTemplateFormWithCategory from app.models.enum.template_categories import DefaultTemplateCategories @@ -42,31 +39,6 @@ def categories(): if not current_app.config["SHOW_STYLEGUIDE"]: abort(404) - class FormExamples(Form): - template_categories = RadioField(_l("Select category"), validators=[validators.DataRequired(message=_l("This cannot be empty"))], choices=[ - ("alert", "Alert"), - ("authentication", "Authentication"), - ("automatic_reply", "Automatic reply"), - ("decision", "Decision"), - ("information_blast", "Information blast"), - ("reminder", "Reminder"), - ("request", "Request"), - ("status_update", "Status update"), - ("test", "Test"), - ("other", "Other"), - ]) - template_category_hints = { - "alert": "System checks and monitoring", - "authentication": "Password resets and two factor verification", - "automatic_reply": "No-reply and confirmation messages", - "decision": "Permits, documents and results", - "information_blast": "Newsletters, surveys and general information", - "reminder": "Appointments and deadlines", - "request": "Follow up and next steps", - "status_update": "Changes and progress", - "test": "Practice messages" - } - form = EmailTemplateFormWithCategory() categories = template_category_api_client.get_all_template_categories() @@ -77,10 +49,21 @@ class FormExamples(Form): categories = sorted(categories, key=lambda x: x[name_col]) form.template_category_id.choices = [(cat["id"], cat[name_col]) for cat in categories if not cat.get("hidden", False)] - form.template_category_id.choices.append((DefaultTemplateCategories.LOW.value, _l("Other"))) + form.template_category_id.choices.append((DefaultTemplateCategories.LOW.value, _("Other"))) template_category_hints = {cat["id"]: cat[desc_col] for cat in categories} + + form.name.data = "name" + form.subject.data = "subject" + form.template_content.data = "content" if form.validate_on_submit(): - return redirect(url_for(".categories")) - - return render_template("views/categories.html", form=form, template_category_hints=template_category_hints, other_category=other_category) + flash(_("Category saved"), "default_with_tick") + + return render_template( + "views/categories.html", + form=form, + template_category_hints=template_category_hints, + other_category=other_category, + heading=_("Category"), + template_category_mode=None, + ) diff --git a/app/translations/csv/fr.csv b/app/translations/csv/fr.csv index 460d263830..3e3d1b6364 100644 --- a/app/translations/csv/fr.csv +++ b/app/translations/csv/fr.csv @@ -1972,5 +1972,6 @@ "Alternative text","Texte de remplacement" "Provide an accessible description of your logo","Fournissez une description accessible de votre logo" "Category","Catégorie" +"Category saved","Catégorie enregistrée" "Does the category still apply?","La catégorie s'applique-t-elle toujours?" "Review your activity","Examinez votre activité" \ No newline at end of file