Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Testing] Test page for template categories #1928

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion app/main/views/styleguide.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
from flask import abort, current_app, render_template
from flask import abort, current_app, flash, render_template, redirect, request, url_for

Check notice

Code scanning / CodeQL

Unused import Note

Import of 'redirect' is not used.
Import of 'request' is not used.
Import of 'url_for' is not used.
from flask_wtf import FlaskForm as Form
from flask_babel import _
from notifications_utils.template import Template
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 EmailTemplateFormWithCategory
from app.models.enum.template_categories import DefaultTemplateCategories



@main.route("/_styleguide")
Expand All @@ -27,3 +32,38 @@
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)

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, _("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():
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,
)
60 changes: 60 additions & 0 deletions app/templates/views/categories.html
Original file line number Diff line number Diff line change
@@ -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() %}

<div class="grid-row contain-floats">
<div class="md:w-2/3 px-gutterHalf">

{% if config["FF_TEMPLATE_CATEGORY"] %}
<h2 class="heading-medium">{{ _('Template category') }}</h2>
{% 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')) }}
</div>
</div>
{% endcall %}
{% endblock %}

{% block page_script %}
{{ super() }}
<script type="text/javascript" nonce="{{ request_nonce }}">
const loadContent = templateId => {
const callback = `/services/templates/${templateId}/get-data`;

// $("#template_content").val("{{ _('loading...')}}");
const loading_text = '{{ _("loading...") }}';
$("#template_content").val(loading_text);

$.ajax({
url: callback,
type: "post",
headers: {
"X-CSRFToken": "{{ csrf_token() }}"
},
dataType: "json",
success: function (data) {
$("#template_content").val(data.result.content);
}
});
};
</script>

{% endblock %}
1 change: 1 addition & 0 deletions app/translations/csv/fr.csv
Original file line number Diff line number Diff line change
Expand Up @@ -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é"
Loading