Return placeholders when getting a template

> Currently when retrieving a template via one of the clients, we do
> not return the personalisation fields that are required for that
> template.
>
> This is useful for services who want to perform template validation on
> their own systems. A service user has also requested this.

– https://www.pivotaltracker.com/story/show/150674476

This commit adds an extra attribute to the JSON response containing an
array of the placeholder names. This key is called "personalisation",
to match the argument that developers use to pass in the values of
placeholders.
This commit is contained in:
Chris Hill-Scott
2017-09-20 10:27:18 +01:00
parent 8360b9a122
commit fa8e4b29f2
3 changed files with 79 additions and 2 deletions

View File

@@ -18,6 +18,11 @@ from notifications_utils.recipients import (
InvalidEmailError
)
from notifications_utils.letter_timings import get_letter_timings
from notifications_utils.template import (
PlainTextEmailTemplate,
SMSMessageTemplate,
LetterDVLATemplate,
)
from app.encryption import (
hashpw,
@@ -516,6 +521,22 @@ class Template(db.Model):
_external=True
)
def _as_utils_template(self):
if self.template_type == EMAIL_TYPE:
return PlainTextEmailTemplate(
{'content': self.content, 'subject': self.subject}
)
if self.template_type == SMS_TYPE:
return SMSMessageTemplate(
{'content': self.content}
)
if self.template_type == LETTER_TYPE:
return LetterDVLATemplate(
{'content': self.content, 'subject': self.subject},
notification_reference=1,
contact_block=self.service.letter_contact_block,
)
def serialize(self):
serialized = {
"id": str(self.id),
@@ -527,6 +548,7 @@ class Template(db.Model):
"body": self.content,
"subject": self.subject if self.template_type != SMS_TYPE else None,
"name": self.name,
"personalisation": list(self._as_utils_template().placeholders),
}
return serialized
@@ -567,6 +589,9 @@ class TemplateHistory(db.Model):
nullable=False,
default=NORMAL)
def _as_utils_template(self):
return Template._as_utils_template(self)
def serialize(self):
return Template.serialize(self)