Merge branch 'master' into research-mode-implementation

This commit is contained in:
Martyn Inglis
2016-06-02 09:28:35 +01:00
5 changed files with 93 additions and 47 deletions

View File

@@ -37,7 +37,7 @@ from app.dao.notifications_dao import (
dao_update_notification,
delete_notifications_created_more_than_a_week_ago,
dao_get_notification_statistics_for_service_and_day,
update_notification_reference_by_id
update_notification_after_sent_to_provider
)
from app.dao.jobs_dao import (
@@ -268,6 +268,12 @@ def send_sms(service_id, notification_id, encrypted_notification, created_at):
reference=str(notification_id)
)
update_notification_after_sent_to_provider(
notification_id,
'sms',
provider.get_name()
)
except SmsClientException as e:
current_app.logger.error(
"SMS notification {} failed".format(notification_id)
@@ -343,7 +349,12 @@ def send_email(service_id, notification_id, from_address, encrypted_notification
reply_to_addresses=reply_to_addresses,
)
update_notification_reference_by_id(notification_id, reference)
update_notification_after_sent_to_provider(
notification_id,
'email',
provider.get_name(),
reference=reference
)
except EmailClientException as e:
current_app.logger.exception(e)

View File

@@ -176,22 +176,6 @@ def dao_create_notification(notification, notification_type, provider_identifier
service_id=notification.service_id)
db.session.add(template_stats)
update_count = db.session.query(ProviderStatistics).filter_by(
day=date.today(),
service_id=notification.service_id,
provider_id=provider.id
).update({'unit_count': ProviderStatistics.unit_count + (
1 if notification_type == TEMPLATE_TYPE_EMAIL else get_sms_fragment_count(notification.content_char_count))})
if update_count == 0:
provider_stats = ProviderStatistics(
day=notification.created_at.date(),
service_id=notification.service_id,
provider_id=provider.id,
unit_count=1 if notification_type == TEMPLATE_TYPE_EMAIL else get_sms_fragment_count(
notification.content_char_count))
db.session.add(provider_stats)
db.session.add(notification)
@@ -293,14 +277,36 @@ def dao_update_notification(notification):
db.session.commit()
def update_notification_reference_by_id(id, reference):
count = db.session.query(Notification).filter_by(
id=id
).update({
Notification.reference: reference
})
db.session.commit()
return count
@transactional
def update_notification_after_sent_to_provider(id_, notification_type, provider_name, reference=None):
provider = ProviderDetails.query.filter_by(identifier=provider_name).one()
notification = Notification.query.filter(Notification.id == id_).one()
def unit_count():
if notification_type == TEMPLATE_TYPE_EMAIL:
return 1
else:
return get_sms_fragment_count(notification.content_char_count)
update_count = db.session.query(ProviderStatistics).filter_by(
day=date.today(),
service_id=notification.service_id,
provider_id=provider.id
).update({'unit_count': ProviderStatistics.unit_count + unit_count()})
if update_count == 0:
provider_stats = ProviderStatistics(
day=notification.created_at.date(),
service_id=notification.service_id,
provider_id=provider.id,
unit_count=unit_count()
)
db.session.add(provider_stats)
if reference:
notification.reference = reference
db.session.add(notification)
def get_notification_for_job(service_id, job_id, notification_id):