Skip to content

Commit

Permalink
Encapsulate URL generation in single item
Browse files Browse the repository at this point in the history
  • Loading branch information
quis committed Feb 26, 2025
1 parent bfa924f commit 8e0ca07
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions app/models/template_attachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ def __contains__(self, key):


class TemplateAttachment(JSONModel):
BASE_URL = "https://www.download.example.gov.uk/f/"

file_name: str
weeks_of_retention: int
email_confirmation: bool
Expand All @@ -38,10 +40,14 @@ def __setattr__(self, name, value):
def __bool__(self):
return bool(self.file_name)

@property
def url(self):
if not self.file_name:
return
return f"{self.BASE_URL}{base64.b64encode(self.file_name.encode()).decode()}"


class TemplateAttachments(InsensitiveDict):
BASE_URL = "https://www.download.example.gov.uk/f/"

def __init__(self, template):
self._template = template
super().__init__(json.loads(redis_client.get(self.cache_key) or "{}"))
Expand Down Expand Up @@ -77,12 +83,7 @@ def count(self):

@property
def as_personalisation(self):
return {
placeholder: f"{self.BASE_URL}{base64.b64encode(attachment['file_name'].encode()).decode()}"
if attachment["file_name"]
else None
for placeholder, attachment in self.items()
}
return {placeholder: self[placeholder].url for placeholder in self}

def prune_orphans(self):
for placeholder in self.keys():
Expand Down

0 comments on commit 8e0ca07

Please sign in to comment.