Add new schema, TemplateSchemaNested

When we nest the `TemplateSchema` as a field on the
`NotificationWithTemplateSchema`, we want to include the
`is_precompiled_letter` field. However, we don't want the
`is_precompiled_letter` in any of the other places that we use the
`TemplateSchema`.

The way we had the code was giving errors in version 3 of Marshmallow
since `is_precompiled_letter` was not defined on the `TemplateSchema`.
Instead of writing complicated logic around when the field should be
included or excluded, this adds a new schema which we can use in the one
place where we do want to include `is_precompiled_letter`.
This commit is contained in:
Katie Smith
2022-05-10 11:08:07 +01:00
parent cfb6c9abc0
commit 57788e5da1

View File

@@ -359,6 +359,16 @@ class TemplateSchema(BaseTemplateSchema, UUIDsAsStringsMixin):
raise ValidationError('Invalid template subject', 'subject')
class TemplateSchemaNested(TemplateSchema):
"""
Contains extra 'is_precompiled_letter' field for use with NotificationWithTemplateSchema
"""
is_precompiled_letter = fields.Method('get_is_precompiled_letter')
def get_is_precompiled_letter(self, template):
return template.is_precompiled_letter
class TemplateSchemaNoDetail(TemplateSchema):
class Meta(TemplateSchema.Meta):
exclude = TemplateSchema.Meta.exclude + (
@@ -502,7 +512,7 @@ class NotificationWithTemplateSchema(BaseSchema):
exclude = ('_personalisation',)
template = fields.Nested(
TemplateSchema,
TemplateSchemaNested,
only=[
'id',
'version',