Skip to content

Commit 8ca2b02

Browse files
committed
Pass reference to template object down to individual files
This is a form of dependency injection. It will be needed so the files can know their own position in the template.
1 parent 60904e8 commit 8ca2b02

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

app/main/views/template_email_files.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def manage_a_template_email_file(service_id, template_id, template_email_file_id
8282
)
8383
delete = bool(request.args.get("delete"))
8484
template_email_file = TemplateEmailFile.get_by_id(
85-
template_email_file_id=template_email_file_id, service_id=service_id, template_id=template_id
85+
template_email_file_id=template_email_file_id, service_id=service_id, template=template
8686
)
8787
if request.method == "POST" and delete:
8888
new_content = PlainTextField(
@@ -121,7 +121,7 @@ def make_file_live(service_id, template_id, template_email_file_id):
121121
must_be_of_type="email",
122122
)
123123
template_email_file = TemplateEmailFile.get_by_id(
124-
template_email_file_id=template_email_file_id, service_id=service_id, template_id=template_id
124+
template_email_file_id=template_email_file_id, service_id=service_id, template=template
125125
)
126126
if template_email_file.filename not in InsensitiveSet(template.placeholders):
127127
new_content = template.content + f"\n\n(({template_email_file.filename}))"
@@ -185,7 +185,7 @@ def change_link_text(service_id, template_id, template_email_file_id):
185185
must_be_of_type="email",
186186
)
187187
template_email_file = TemplateEmailFile.get_by_id(
188-
template_email_file_id=template_email_file_id, service_id=service_id, template_id=template_id
188+
template_email_file_id=template_email_file_id, service_id=service_id, template=template
189189
)
190190
form = TemplateEmailFileLinkTextForm(link_text=template_email_file.link_text)
191191

@@ -221,7 +221,7 @@ def change_data_retention_period(service_id, template_id, template_email_file_id
221221
must_be_of_type="email",
222222
)
223223
template_email_file = TemplateEmailFile.get_by_id(
224-
template_email_file_id=template_email_file_id, service_id=service_id, template_id=template_id
224+
template_email_file_id=template_email_file_id, service_id=service_id, template=template
225225
)
226226
form = TemplateEmailFileRetentionPeriodForm(retention_period=template_email_file.retention_period)
227227

@@ -258,7 +258,7 @@ def change_email_validation(service_id, template_id, template_email_file_id):
258258
must_be_of_type="email",
259259
)
260260
template_email_file = TemplateEmailFile.get_by_id(
261-
template_email_file_id=template_email_file_id, service_id=service_id, template_id=template_id
261+
template_email_file_id=template_email_file_id, service_id=service_id, template=template
262262
)
263263
form = OnOffSettingForm(
264264
"Ask recipient for their email address",

app/models/template_email_file.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from notifications_utils.base64_uuid import uuid_to_base64
99
from notifications_utils.s3 import s3download
1010
from notifications_utils.serialised_model import SerialisedModelCollection
11+
from notifications_utils.template import Template
1112

1213
from app.models import JSONModel
1314
from app.s3_client.s3_template_email_file_upload_client import upload_template_email_file_to_s3
@@ -20,7 +21,7 @@ def _get_file_location(file_id: uuid, service_id: uuid) -> str:
2021
class TemplateEmailFile(JSONModel):
2122
id: Any
2223
service_id: Any
23-
template_id: Any
24+
template: Any
2425
filename: str
2526
link_text: str
2627
retention_period: int
@@ -57,15 +58,16 @@ def update(self, **kwargs):
5758
} | kwargs
5859

5960
return template_email_file_client.update_file(
60-
service_id=self.service_id, template_id=self.template_id, file_id=self.id, **data
61+
service_id=self.service_id, template_id=self.template.id, file_id=self.id, **data
6162
)
6263

6364
@classmethod
64-
def get_by_id(cls, template_email_file_id: str, service_id: str, template_id: str):
65+
def get_by_id(cls, template_email_file_id: str, service_id: str, template: Template):
6566
from app import template_email_file_client
6667

67-
template_email_file = template_email_file_client.get_file_by_id(template_email_file_id, service_id, template_id)
68+
template_email_file = template_email_file_client.get_file_by_id(template_email_file_id, service_id, template.id)
6869
template_email_file["data"]["service_id"] = service_id
70+
template_email_file["data"]["template"] = template
6971
return cls(template_email_file.get("data"))
7072

7173
@property
@@ -74,7 +76,7 @@ def link_as_markdown(self):
7476
"main.document_download_landing",
7577
service_id=self.service_id,
7678
document_id=self.id,
77-
key=uuid_to_base64(self.template_id),
79+
key=uuid_to_base64(self.template.id),
7880
_external=True,
7981
)
8082
if self.link_text:
@@ -116,7 +118,7 @@ def __init__(self, template):
116118
from app import current_service
117119

118120
self.service_id = current_service.id
119-
self.template_id = template.id
121+
self.template = template
120122

121123
email_files = template._template.get("email_files", [])
122124
super().__init__(email_files)
@@ -125,7 +127,7 @@ def __init__(self, template):
125127
self.items = sorted(email_files, key=lambda _: next(position_in_template))
126128

127129
def __getitem__(self, index):
128-
return self.model(self.items[index] | {"service_id": self.service_id, "template_id": self.template_id})
130+
return self.model(self.items[index] | {"service_id": self.service_id, "template": self.template})
129131

130132
@property
131133
def as_personalisation(self):

0 commit comments

Comments
 (0)