Update notification_history table on insert/update of notifications

triggered via calls in dao_create_notification and dao_update_notification - if you don't use those functions (eg update in bulk) you'll have to update the history table yourself!
This commit is contained in:
Leo Hemsted
2016-07-08 16:19:34 +01:00
parent 47ef63adbe
commit 722699a72a
5 changed files with 54 additions and 2 deletions

View File

@@ -15,6 +15,7 @@ from app.dao import days_ago
from app.models import (
Service,
Notification,
NotificationHistory,
Job,
NotificationStatistics,
TemplateStatistics,
@@ -182,7 +183,10 @@ def dao_create_notification(notification, notification_type):
service_id=notification.service_id)
db.session.add(template_stats)
notification_history = NotificationHistory.from_notification(notification)
db.session.add(notification)
db.session.add(notification_history)
def _update_notification_stats_query(notification_type, status):
@@ -280,6 +284,8 @@ def update_notification_status_by_reference(reference, status, notification_stat
def dao_update_notification(notification):
notification.updated_at = datetime.utcnow()
notification_history = NotificationHistory.query.get(notification.id)
notification_history.update_from_notification(notification)
db.session.add(notification)
db.session.commit()

View File

@@ -409,6 +409,16 @@ class NotificationHistory(db.Model):
status = db.Column(NOTIFICATION_STATUS_TYPES_ENUM, nullable=False, default='created')
reference = db.Column(db.String, nullable=True, index=True)
@classmethod
def from_notification(cls, notification):
from app.schemas import notification_status_schema
return cls(notification_status_schema.dump(notification))
def update_from_notification(self, notification):
from app.schemas import notification_status_schema
new_notification_schema = cls(notification_status_schema.dump(notification))
INVITED_USER_STATUS_TYPES = ['pending', 'accepted', 'cancelled']

View File

@@ -288,6 +288,12 @@ class NotificationStatusSchema(BaseSchema):
return in_data
class NotificationHistorySchema(BaseSchema):
class Meta:
model = models.NotificationHistory
strict = True
class InvitedUserSchema(BaseSchema):
class Meta:
@@ -492,6 +498,7 @@ email_notification_schema = EmailNotificationSchema()
job_email_template_notification_schema = JobEmailTemplateNotificationSchema()
notification_status_schema = NotificationStatusSchema()
notification_status_schema_load_json = NotificationStatusSchema(load_json=True)
notification_history_schema = NotificationHistorySchema()
invited_user_schema = InvitedUserSchema()
permission_schema = PermissionSchema()
email_data_request_schema = EmailDataSchema()