mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-17 04:58:50 -04:00
update schema to use template_history for accurate template details
only in the public notification endpoint so far for fear of breaking things - in an ideal world i'd remove the template relationship from models entirely and replace that with actual_template
This commit is contained in:
@@ -391,7 +391,7 @@ def get_notifications_for_service(service_id,
|
||||
query = Notification.query.filter(*filters)
|
||||
query = _filter_query(query, filter_dict)
|
||||
if personalisation:
|
||||
query.options(
|
||||
query = query.options(
|
||||
joinedload('actual_template')
|
||||
)
|
||||
return query.order_by(desc(Notification.created_at)).paginate(
|
||||
|
||||
@@ -448,9 +448,6 @@ class Notification(db.Model):
|
||||
reference = db.Column(db.String, nullable=True, index=True)
|
||||
_personalisation = db.Column(db.String, nullable=True)
|
||||
|
||||
# __table_args__ = (
|
||||
# ForeignKeyConstraint(['template_id', 'template_version'], ['template_history.id', 'template_history.version']),
|
||||
# )
|
||||
actual_template = db.relationship('TemplateHistory', primaryjoin=and_(
|
||||
foreign(template_id) == remote(TemplateHistory.id),
|
||||
foreign(template_version) == remote(TemplateHistory.version)
|
||||
|
||||
@@ -282,12 +282,21 @@ class NotificationWithTemplateSchema(BaseSchema):
|
||||
strict = True
|
||||
exclude = ('_personalisation',)
|
||||
|
||||
template = fields.Nested(TemplateSchema, only=["id", "name", "template_type", "content", "subject"], dump_only=True)
|
||||
template = fields.Nested(
|
||||
TemplateSchema,
|
||||
only=['id', 'version', 'name', 'template_type', 'content', 'subject'],
|
||||
dump_only=True
|
||||
)
|
||||
job = fields.Nested(JobSchema, only=["id", "original_file_name"], dump_only=True)
|
||||
personalisation = fields.Dict(required=False)
|
||||
|
||||
|
||||
class NotificationWithPersonalisationSchema(NotificationWithTemplateSchema):
|
||||
template = None
|
||||
actual_template = fields.Nested(TemplateHistorySchema,
|
||||
only=['id', 'name', 'template_type', 'content', 'subject', 'version'],
|
||||
dump_only=True)
|
||||
|
||||
@pre_dump
|
||||
def handle_personalisation_property(self, in_data):
|
||||
self.personalisation = in_data.personalisation
|
||||
@@ -295,6 +304,7 @@ class NotificationWithPersonalisationSchema(NotificationWithTemplateSchema):
|
||||
|
||||
@post_dump
|
||||
def handle_template_merge(self, in_data):
|
||||
in_data['template'] = in_data.pop('actual_template')
|
||||
from notifications_utils.template import Template
|
||||
template = Template(
|
||||
in_data['template'],
|
||||
|
||||
Reference in New Issue
Block a user