Add template.service_letter_contact_id and reply_to wrapper property

Adds a relationship between Template models and service letter contact
blocks.

Depending on template type, we can have a reference to either a letter
contact block, email reply-to address or SMS sender record. This means
that in order to enforce foreign key constraints we need to define three
separate foreign key columns on the template model.

To hide this implementation detail and make it easier to access the
sender/reply-to information we define a wrapper property that returns
the value from the correct column.

The relationship and the property are only defined for letter templates
at the moment.

The setter raises an error when trying to assign a reply_to value for
non-letter templates. The exception isn't raised if the value being
assigned is `None` since it can get assigned by marshmallow schemas
and as it matches the value returned for other template types it
doesn't need to be written anywhere.
This commit is contained in:
Alexey Bezhan
2017-11-21 14:46:08 +00:00
parent 4c253bf3b9
commit cbce610098

View File

@@ -559,6 +559,26 @@ class TemplateBase(db.Model):
redact_personalisation = association_proxy('template_redacted', 'redact_personalisation')
@declared_attr
def service_letter_contact_id(cls):
return db.Column(UUID(as_uuid=True), db.ForeignKey('service_letter_contacts.id'), nullable=True)
@property
def reply_to(self):
if self.template_type == LETTER_TYPE:
return self.service_letter_contact_id
else:
return None
@reply_to.setter
def reply_to(self, value):
if self.template_type == LETTER_TYPE:
self.service_letter_contact_id = value
elif value is None:
pass
else:
raise ValueError('Unable to set sender for {} template'.format(self.template_type))
def _as_utils_template(self):
if self.template_type == EMAIL_TYPE:
return PlainTextEmailTemplate(