update notification_history table from notification_dao create/update functions

please ensure that any changes to notifications table happen through either dao_create_notification or dao_update_notification.
changed the notification status update triggered by the provider callbacks to ensure that sets updated_by and can update the history table.
also re-added the character_count so we can reconstruct billing data if needed.
This commit is contained in:
Leo Hemsted
2016-07-11 16:48:32 +01:00
parent 722699a72a
commit 4ef084464d
7 changed files with 140 additions and 117 deletions

View File

@@ -1,6 +1,4 @@
from sqlalchemy import (desc, func, Integer, or_, and_, asc)
from sqlalchemy.sql.expression import cast
import uuid
from datetime import (
datetime,
timedelta,
@@ -9,6 +7,9 @@ from datetime import (
from flask import current_app
from werkzeug.datastructures import MultiDict
from sqlalchemy import (desc, func, Integer, or_, and_, asc)
from sqlalchemy.sql.expression import cast
from notifications_utils.template import get_sms_fragment_count
from app import db
from app.dao import days_ago
@@ -24,15 +25,11 @@ from app.models import (
Template,
ProviderStatistics,
ProviderDetails)
from notifications_utils.template import get_sms_fragment_count
from app.clients import (
STATISTICS_FAILURE,
STATISTICS_DELIVERED,
STATISTICS_REQUESTED
)
from app.dao.dao_utils import transactional
@@ -183,6 +180,12 @@ def dao_create_notification(notification, notification_type):
service_id=notification.service_id)
db.session.add(template_stats)
if not notification.id:
# need to populate defaulted fields before we create the notification history object
notification.id = uuid.uuid4()
if not notification.status:
notification.status = 'created'
notification_history = NotificationHistory.from_notification(notification)
db.session.add(notification)
@@ -244,7 +247,8 @@ def _update_notification_status(notification, status, notification_statistics_st
if notification_statistics_status:
_update_statistics(notification, notification_statistics_status)
db.session.query(Notification).filter(Notification.id == notification.id).update({Notification.status: status})
notification.status = status
dao_update_notification(notification)
return True

View File

@@ -17,6 +17,7 @@ from app.models import (
ApiKey,
Template,
Job,
NotificationHistory,
Notification,
Permission,
User,
@@ -97,6 +98,7 @@ def delete_service_and_all_associated_db_objects(service):
_delete_commit(Permission.query.filter_by(service=service))
_delete_commit(ApiKey.query.filter_by(service=service))
_delete_commit(ApiKey.get_history_model().query.filter_by(service_id=service.id))
_delete_commit(NotificationHistory.query.filter_by(service=service))
_delete_commit(Notification.query.filter_by(service=service))
_delete_commit(Job.query.filter_by(service=service))
_delete_commit(Template.query.filter_by(service=service))

View File

@@ -334,6 +334,7 @@ NOTIFICATION_STATUS_TYPES = ['created', 'sending', 'delivered', 'pending', 'fail
'technical-failure', 'temporary-failure', 'permanent-failure']
NOTIFICATION_STATUS_TYPES_ENUM = db.Enum(*NOTIFICATION_STATUS_TYPES, name='notify_status_type')
class Notification(db.Model):
__tablename__ = 'notifications'
@@ -401,6 +402,7 @@ class NotificationHistory(db.Model):
api_key_id = db.Column(UUID(as_uuid=True), db.ForeignKey('api_keys.id'), index=True, unique=False)
api_key = db.relationship('ApiKey')
key_type = db.Column(db.String, db.ForeignKey('key_types.name'), index=True, unique=False, nullable=False)
content_char_count = db.Column(db.Integer, nullable=True)
notification_type = db.Column(notification_types, nullable=False)
created_at = db.Column(db.DateTime, index=False, unique=False, nullable=False)
sent_at = db.Column(db.DateTime, index=False, unique=False, nullable=True)
@@ -411,13 +413,11 @@ class NotificationHistory(db.Model):
@classmethod
def from_notification(cls, notification):
from app.schemas import notification_status_schema
return cls(notification_status_schema.dump(notification))
return cls(**{c.name: getattr(notification, c.name) for c in cls.__table__.columns})
def update_from_notification(self, notification):
from app.schemas import notification_status_schema
new_notification_schema = cls(notification_status_schema.dump(notification))
for c in self.__table__.columns:
setattr(self, c.name, getattr(notification, c.name))
INVITED_USER_STATUS_TYPES = ['pending', 'accepted', 'cancelled']

View File

@@ -288,12 +288,6 @@ class NotificationStatusSchema(BaseSchema):
return in_data
class NotificationHistorySchema(BaseSchema):
class Meta:
model = models.NotificationHistory
strict = True
class InvitedUserSchema(BaseSchema):
class Meta:
@@ -498,7 +492,6 @@ 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()