Skip to content

Commit 139f263

Browse files
committed
Refactor to use same sorting method as other models
Other models sort based on their `__sort_attribute__`. This commit moves the calculation of each files position in the template into the `TemplateEmailFile` object, so that its sorting can also work in this way.
1 parent 8ca2b02 commit 139f263

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

app/models/template_email_file.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class TemplateEmailFile(JSONModel):
2828
validate_users_email: bool
2929
pending: bool
3030

31-
__sort_attribute__ = "filename"
31+
__sort_attribute__ = "position_in_template"
3232

3333
@staticmethod
3434
def create(*, filename, file_contents, template_id, pending=True):
@@ -110,6 +110,10 @@ def file_contents(self):
110110
f"{self.service_id}/{self.id}",
111111
)
112112

113+
@property
114+
def position_in_template(self):
115+
return self.template.index_of_placeholder(self.filename)
116+
113117

114118
class TemplateEmailFiles(SerialisedModelCollection):
115119
model = TemplateEmailFile
@@ -123,9 +127,6 @@ def __init__(self, template):
123127
email_files = template._template.get("email_files", [])
124128
super().__init__(email_files)
125129

126-
position_in_template = (template.index_of_placeholder(email_file.filename) for email_file in self)
127-
self.items = sorted(email_files, key=lambda _: next(position_in_template))
128-
129130
def __getitem__(self, index):
130131
return self.model(self.items[index] | {"service_id": self.service_id, "template": self.template})
131132

app/templates/views/templates/email-template-files/files-list.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
{% set files = [] %}
2222
{% if template.email_files %}
23-
{% for item in template.email_files %}
23+
{% for item in template.email_files | sort %}
2424
{% do files.append(
2525
{
2626
"key": {

0 commit comments

Comments
 (0)