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))