Skip to content

Commit fc374e5

Browse files
authored
Merge pull request #5831 from alphagov/add-banner-delete-file
Add and improve banners shown when deleting files
2 parents 8d27058 + a6f6da2 commit fc374e5

File tree

4 files changed

+53
-32
lines changed

4 files changed

+53
-32
lines changed

app/main/views/template_email_files.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from flask import abort, redirect, render_template, request, url_for
1+
from flask import abort, flash, redirect, render_template, request, url_for
22
from notifications_utils.field import PlainTextField
33
from notifications_utils.insensitive_dict import InsensitiveSet
44

@@ -81,32 +81,30 @@ def manage_a_template_email_file(service_id, template_id, template_email_file_id
8181
must_be_of_type="email",
8282
)
8383
delete = bool(request.args.get("delete"))
84-
pending = bool(request.args.get("pending", False))
8584
template_email_file = TemplateEmailFile.get_by_id(
8685
template_email_file_id=template_email_file_id, service_id=service_id, template_id=template_id
8786
)
88-
if request.method == "POST":
89-
if delete:
90-
new_content = PlainTextField(
91-
template.content,
92-
{template_email_file.filename: ""},
93-
html="passthrough",
94-
)
95-
service_api_client.update_service_template(
96-
service_id=service_id,
97-
template_id=template_id,
98-
content=str(new_content).strip(),
99-
archive_email_file_ids=[template_email_file.id],
100-
)
101-
return redirect(url_for("main.view_template", service_id=current_service.id, template_id=template.id))
87+
if request.method == "POST" and delete:
88+
new_content = PlainTextField(
89+
template.content,
90+
{template_email_file.filename: ""},
91+
html="passthrough",
92+
)
93+
service_api_client.update_service_template(
94+
service_id=service_id,
95+
template_id=template_id,
96+
content=str(new_content).strip(),
97+
archive_email_file_ids=[template_email_file.id],
98+
)
99+
flash(f"‘{template_email_file.filename}’ has been removed", "default_with_tick")
100+
return redirect(url_for("main.view_template", service_id=current_service.id, template_id=template.id))
102101

103102
return render_template(
104103
"views/templates/email-template-files/email_file.html",
105104
template_email_file=template_email_file,
106105
service_id=service_id,
107106
template_id=template_id,
108107
delete=delete,
109-
pending=pending,
110108
)
111109

112110

app/main/views/templates.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -775,8 +775,10 @@ def edit_service_template(service_id, template_id, language=None): # noqa
775775
template.template_type == "letter" and template.welsh_page_count and language != "welsh"
776776
)
777777
if template_change.email_files_removed:
778+
multiple_files_removed = len(template_change.email_filenames_removed) > 1
778779
flash(
779-
f"Files for {formatted_list(template_change.email_filenames_removed)} have been removed from the template.", # noqa
780+
f"{formatted_list(template_change.email_filenames_removed)} "
781+
f"{'have' if multiple_files_removed else 'has'} been removed",
780782
"default_with_tick",
781783
)
782784
return redirect(

tests/app/main/views/test_template_email_files.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,20 +224,22 @@ def test_post_delete_to_manage_a_template_email_file_updates_and_redirects(
224224
mock_update_service_template = mocker.patch(
225225
"app.notify_client.service_api_client.ServiceAPIClient.update_service_template"
226226
)
227-
client_request.post(
227+
page = client_request.post(
228228
"main.manage_a_template_email_file",
229229
service_id=service_one["id"],
230230
template_id=fake_uuid,
231231
template_email_file_id=test_data_for_a_template_email_file["id"],
232232
delete=True,
233-
_expected_redirect=url_for("main.view_template", service_id=service_one["id"], template_id=fake_uuid),
233+
_follow_redirects=True,
234234
)
235235
mock_update_service_template.assert_called_once_with(
236236
service_id=service_one["id"],
237237
template_id=fake_uuid,
238238
content="This template contains an email file",
239239
archive_email_file_ids=[test_data_for_a_template_email_file["id"]],
240240
)
241+
assert normalize_spaces(page.select_one("h1.folder-heading")) == "sample template"
242+
assert normalize_spaces(page.select_one(".banner-default-with-tick")) == "‘test_file_1.csv’ has been removed"
241243

242244

243245
def test_manage_a_template_email_file_raises_404_for_invalid_template_email_file_id(

tests/app/main/views/test_templates.py

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3751,7 +3751,34 @@ def test_should_redirect_when_saving_a_template_email(
37513751
)
37523752

37533753

3754-
def test_edit_service_template_archives_email_files(client_request, fake_uuid, mocker):
3754+
@pytest.mark.parametrize(
3755+
"new_content, expected_file_ids_to_archive, expected_banner_text",
3756+
(
3757+
(
3758+
"For the appointment, you will just need ((map.pdf))",
3759+
[
3760+
"00000000-0000-4000-8000-000000000001",
3761+
"00000000-0000-4000-8000-000000000002",
3762+
],
3763+
"‘invite.pdf’ and ‘form.pdf’ have been removed",
3764+
),
3765+
(
3766+
"For the appointment, you will just need ((form.pdf)) and ((map.pdf))",
3767+
[
3768+
"00000000-0000-4000-8000-000000000001",
3769+
],
3770+
"‘invite.pdf’ has been removed",
3771+
),
3772+
),
3773+
)
3774+
def test_edit_service_template_archives_email_files(
3775+
client_request,
3776+
fake_uuid,
3777+
mocker,
3778+
new_content,
3779+
expected_file_ids_to_archive,
3780+
expected_banner_text,
3781+
):
37553782
# mock out template with email files
37563783
email_template = create_template(
37573784
template_id=fake_uuid,
@@ -3784,8 +3811,6 @@ def test_edit_service_template_archives_email_files(client_request, fake_uuid, m
37843811
)
37853812
mocker.patch("app.service_api_client.get_service_template", return_value={"data": email_template})
37863813

3787-
new_content = "For the appointment, you will just need ((map.pdf))"
3788-
37893814
mock_update_service_template = mocker.patch("notifications_python_client.base.BaseAPIClient.request")
37903815

37913816
page = client_request.post(
@@ -3806,21 +3831,15 @@ def test_edit_service_template_archives_email_files(client_request, fake_uuid, m
38063831
"/service/596364a0-858e-42c8-9062-a8fe822260eb/template/6ce466d0-fd6a-11e5-82f5-e0accb9d11a6",
38073832
data={
38083833
"created_by": "6ce466d0-fd6a-11e5-82f5-e0accb9d11a6",
3809-
"content": "For the appointment, you will just need ((map.pdf))",
3834+
"content": new_content,
38103835
"subject": "Your ((thing)) is due soon",
38113836
"name": "sample template",
38123837
"has_unsubscribe_link": False,
3813-
"archive_email_file_ids": [
3814-
"00000000-0000-4000-8000-000000000001",
3815-
"00000000-0000-4000-8000-000000000002",
3816-
],
3838+
"archive_email_file_ids": expected_file_ids_to_archive,
38173839
},
38183840
)
38193841

3820-
assert (
3821-
normalize_spaces(page.select(".banner-default-with-tick")[0].text)
3822-
== "Files for ‘invite.pdf’ and ‘form.pdf’ have been removed from the template."
3823-
)
3842+
assert normalize_spaces(page.select(".banner-default-with-tick")[0].text) == expected_banner_text
38243843

38253844

38263845
def test_should_redirect_when_saving_a_template_letter(

0 commit comments

Comments
 (0)