From f8e1fbe3e60d67ab50807b49bb364f5d81fdf87d Mon Sep 17 00:00:00 2001 From: Alexey Bezhan Date: Tue, 21 Nov 2017 14:51:12 +0000 Subject: [PATCH] Add reply_to fields to template schemas We're hiding the `service_letter_contact_id` column since it should only be readable and writable using the `.reply_to` wrapper. Schemas are defined using `fields.Method` since the fields are represented by a property on the Template model that requires template type to be set. When creating a template, if `reply_to` is defined using `fields.String` it gets assigned at the same time as `template_type` (or the order of assignments is not defined), so the schema loader attempts to set `.reply_to` on a Template object with a `None` `template_type`, which raises an exception. Using `fields.Method` seems to delay `.reply_to` assignment until the Template object is created, which means it already has a valid type. --- app/schemas.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/app/schemas.py b/app/schemas.py index 5e020ddf1..b4d34ab62 100644 --- a/app/schemas.py +++ b/app/schemas.py @@ -314,9 +314,14 @@ class NotificationModelSchema(BaseSchema): class BaseTemplateSchema(BaseSchema): + reply_to = fields.Method("get_reply_to", allow_none=True) + + def get_reply_to(self, template): + return template.reply_to + class Meta: model = models.Template - exclude = ("service_id", "jobs") + exclude = ("service_id", "jobs", "service_letter_contact_id") strict = True @@ -339,9 +344,14 @@ class TemplateSchema(BaseTemplateSchema): class TemplateHistorySchema(BaseSchema): + reply_to = fields.Method("get_reply_to", allow_none=True) + created_by = fields.Nested(UserSchema, only=['id', 'name', 'email_address'], dump_only=True) created_at = field_for(models.Template, 'created_at', format='%Y-%m-%d %H:%M:%S.%f') + def get_reply_to(self, template): + return template.reply_to + class Meta: model = models.TemplateHistory