mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-25 16:53:42 -04:00
add actual_template relationship to notification
also renamed the function to make it apparent that it'll join and grab personalisation
This commit is contained in:
@@ -8,6 +8,7 @@ from datetime import (
|
||||
from flask import current_app
|
||||
from werkzeug.datastructures import MultiDict
|
||||
from sqlalchemy import (desc, func, Integer, or_, and_, asc)
|
||||
from sqlalchemy.orm import joinedload
|
||||
from sqlalchemy.sql.expression import cast
|
||||
from notifications_utils.template import get_sms_fragment_count
|
||||
|
||||
@@ -351,12 +352,12 @@ def get_notifications_for_job(service_id, job_id, filter_dict=None, page=1, page
|
||||
|
||||
|
||||
@statsd(namespace="dao")
|
||||
def get_notification(service_id, notification_id, key_type=None):
|
||||
def get_notification_with_personalisation(service_id, notification_id, key_type):
|
||||
filter_dict = {'service_id': service_id, 'id': notification_id}
|
||||
if key_type:
|
||||
filter_dict['key_type'] = key_type
|
||||
|
||||
return Notification.query.filter_by(**filter_dict).one()
|
||||
return Notification.query.filter_by(**filter_dict).options(joinedload('actual_template')).one()
|
||||
|
||||
|
||||
@statsd(namespace="dao")
|
||||
@@ -374,7 +375,8 @@ def get_notifications_for_service(service_id,
|
||||
page=1,
|
||||
page_size=None,
|
||||
limit_days=None,
|
||||
key_type=None):
|
||||
key_type=None,
|
||||
personalisation=False):
|
||||
if page_size is None:
|
||||
page_size = current_app.config['PAGE_SIZE']
|
||||
filters = [Notification.service_id == service_id]
|
||||
@@ -388,6 +390,10 @@ def get_notifications_for_service(service_id,
|
||||
|
||||
query = Notification.query.filter(*filters)
|
||||
query = _filter_query(query, filter_dict)
|
||||
if personalisation:
|
||||
query.options(
|
||||
joinedload('actual_template')
|
||||
)
|
||||
return query.order_by(desc(Notification.created_at)).paginate(
|
||||
page=page,
|
||||
per_page=page_size
|
||||
|
||||
@@ -5,7 +5,8 @@ from sqlalchemy.dialects.postgresql import (
|
||||
UUID,
|
||||
JSON
|
||||
)
|
||||
from sqlalchemy import UniqueConstraint, text
|
||||
from sqlalchemy import UniqueConstraint, text, ForeignKeyConstraint, and_
|
||||
from sqlalchemy.orm import foreign, remote
|
||||
|
||||
from app.encryption import (
|
||||
hashpw,
|
||||
@@ -447,6 +448,14 @@ 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)
|
||||
))
|
||||
|
||||
@property
|
||||
def personalisation(self):
|
||||
if self._personalisation:
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
from datetime import datetime
|
||||
import itertools
|
||||
|
||||
from flask import (
|
||||
Blueprint,
|
||||
jsonify,
|
||||
@@ -7,6 +8,7 @@ from flask import (
|
||||
current_app,
|
||||
json
|
||||
)
|
||||
|
||||
from notifications_utils.recipients import allowed_to_send_to, first_column_heading
|
||||
from notifications_utils.template import Template
|
||||
from notifications_utils.renderers import PassThrough
|
||||
@@ -170,10 +172,10 @@ def process_firetext_response():
|
||||
|
||||
|
||||
@notifications.route('/notifications/<uuid:notification_id>', methods=['GET'])
|
||||
def get_notifications(notification_id):
|
||||
notification = notifications_dao.get_notification(str(api_user.service_id),
|
||||
notification_id,
|
||||
key_type=api_user.key_type)
|
||||
def get_notification_by_id(notification_id):
|
||||
notification = notifications_dao.get_notification_with_personalisation(str(api_user.service_id),
|
||||
notification_id,
|
||||
key_type=api_user.key_type)
|
||||
return jsonify(data={"notification": notification_with_personalisation_schema.dump(notification).data}), 200
|
||||
|
||||
|
||||
@@ -186,6 +188,7 @@ def get_all_notifications():
|
||||
|
||||
pagination = notifications_dao.get_notifications_for_service(
|
||||
str(api_user.service_id),
|
||||
personalisation=True,
|
||||
filter_dict=data,
|
||||
page=page,
|
||||
page_size=page_size,
|
||||
|
||||
Reference in New Issue
Block a user