2016-03-01 13:30:10 +00:00
|
|
|
from flask import current_app
|
2016-02-09 12:01:17 +00:00
|
|
|
from app import db
|
2016-03-08 16:34:03 +00:00
|
|
|
from app.models import Notification, Job, NotificationStatistics, TEMPLATE_TYPE_SMS, TEMPLATE_TYPE_EMAIL
|
2016-03-01 14:58:27 +00:00
|
|
|
from sqlalchemy import desc
|
2016-03-09 17:46:01 +00:00
|
|
|
from datetime import datetime, timedelta
|
2016-02-09 12:01:17 +00:00
|
|
|
|
|
|
|
|
|
2016-03-08 16:34:03 +00:00
|
|
|
def dao_get_notification_statistics_for_service(service_id):
|
|
|
|
|
return NotificationStatistics.query.filter_by(
|
|
|
|
|
service_id=service_id
|
|
|
|
|
).order_by(desc(NotificationStatistics.day)).all()
|
|
|
|
|
|
|
|
|
|
|
2016-03-09 11:06:37 +00:00
|
|
|
def dao_get_notification_statistics_for_service_and_day(service_id, day):
|
|
|
|
|
return NotificationStatistics.query.filter_by(
|
|
|
|
|
service_id=service_id,
|
|
|
|
|
day=day
|
|
|
|
|
).order_by(desc(NotificationStatistics.day)).first()
|
|
|
|
|
|
|
|
|
|
|
2016-03-08 15:23:19 +00:00
|
|
|
def dao_create_notification(notification, notification_type):
|
|
|
|
|
try:
|
|
|
|
|
if notification.job_id:
|
|
|
|
|
update_job_sent_count(notification)
|
|
|
|
|
|
2016-03-08 16:34:03 +00:00
|
|
|
if update_notification_stats(notification, notification_type) == 0:
|
|
|
|
|
stats = NotificationStatistics(
|
|
|
|
|
day=notification.created_at.strftime('%Y-%m-%d'),
|
2016-03-08 15:23:19 +00:00
|
|
|
service_id=notification.service_id,
|
|
|
|
|
sms_requested=1 if notification_type == TEMPLATE_TYPE_SMS else 0,
|
|
|
|
|
emails_requested=1 if notification_type == TEMPLATE_TYPE_EMAIL else 0
|
|
|
|
|
)
|
|
|
|
|
db.session.add(stats)
|
|
|
|
|
db.session.add(notification)
|
|
|
|
|
db.session.commit()
|
|
|
|
|
except:
|
|
|
|
|
db.session.rollback()
|
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
2016-03-08 16:34:03 +00:00
|
|
|
def update_notification_stats(notification, notification_type):
|
|
|
|
|
if notification_type == TEMPLATE_TYPE_SMS:
|
|
|
|
|
update = {
|
|
|
|
|
NotificationStatistics.sms_requested: NotificationStatistics.sms_requested + 1
|
|
|
|
|
}
|
|
|
|
|
else:
|
|
|
|
|
update = {
|
|
|
|
|
NotificationStatistics.emails_requested: NotificationStatistics.emails_requested + 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return db.session.query(NotificationStatistics).filter_by(
|
|
|
|
|
day=notification.created_at.strftime('%Y-%m-%d'),
|
|
|
|
|
service_id=notification.service_id
|
|
|
|
|
).update(update)
|
|
|
|
|
|
|
|
|
|
|
2016-03-08 15:23:19 +00:00
|
|
|
def update_job_sent_count(notification):
|
|
|
|
|
db.session.query(Job).filter_by(
|
|
|
|
|
id=notification.job_id
|
|
|
|
|
).update({
|
|
|
|
|
Job.notifications_sent: Job.notifications_sent + 1,
|
|
|
|
|
Job.updated_at: datetime.utcnow()
|
|
|
|
|
})
|
2016-02-25 09:59:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def dao_update_notification(notification):
|
|
|
|
|
db.session.add(notification)
|
2016-02-09 12:01:17 +00:00
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
|
|
|
2016-02-16 11:22:44 +00:00
|
|
|
def get_notification_for_job(service_id, job_id, notification_id):
|
2016-02-09 14:17:42 +00:00
|
|
|
return Notification.query.filter_by(service_id=service_id, job_id=job_id, id=notification_id).one()
|
2016-02-09 12:01:17 +00:00
|
|
|
|
|
|
|
|
|
2016-03-01 13:30:10 +00:00
|
|
|
def get_notifications_for_job(service_id, job_id, page=1):
|
2016-03-04 14:25:28 +00:00
|
|
|
query = Notification.query.filter_by(service_id=service_id, job_id=job_id) \
|
|
|
|
|
.order_by(desc(Notification.created_at)) \
|
2016-03-01 13:30:10 +00:00
|
|
|
.paginate(
|
2016-03-04 14:25:28 +00:00
|
|
|
page=page,
|
|
|
|
|
per_page=current_app.config['PAGE_SIZE']
|
|
|
|
|
)
|
2016-03-01 13:30:10 +00:00
|
|
|
return query
|
2016-02-16 11:22:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_notification(service_id, notification_id):
|
|
|
|
|
return Notification.query.filter_by(service_id=service_id, id=notification_id).one()
|
2016-03-01 13:30:10 +00:00
|
|
|
|
|
|
|
|
|
2016-03-10 15:40:41 +00:00
|
|
|
def get_notification_by_id(notification_id):
|
|
|
|
|
return Notification.query.filter_by(id=notification_id).first()
|
|
|
|
|
|
|
|
|
|
|
2016-03-01 13:30:10 +00:00
|
|
|
def get_notifications_for_service(service_id, page=1):
|
2016-03-01 14:58:27 +00:00
|
|
|
query = Notification.query.filter_by(service_id=service_id).order_by(desc(Notification.created_at)).paginate(
|
2016-03-01 13:30:10 +00:00
|
|
|
page=page,
|
|
|
|
|
per_page=current_app.config['PAGE_SIZE']
|
|
|
|
|
)
|
|
|
|
|
return query
|
2016-03-09 17:46:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def delete_successful_notifications_created_more_than_a_day_ago():
|
|
|
|
|
deleted = db.session.query(Notification).filter(
|
2016-03-10 09:34:27 +00:00
|
|
|
Notification.created_at < datetime.utcnow() - timedelta(days=1),
|
2016-03-09 17:46:01 +00:00
|
|
|
Notification.status == 'sent'
|
|
|
|
|
).delete()
|
|
|
|
|
db.session.commit()
|
|
|
|
|
return deleted
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def delete_failed_notifications_created_more_than_a_week_ago():
|
|
|
|
|
deleted = db.session.query(Notification).filter(
|
2016-03-10 09:34:27 +00:00
|
|
|
Notification.created_at < datetime.utcnow() - timedelta(days=7),
|
2016-03-09 17:46:01 +00:00
|
|
|
Notification.status == 'failed'
|
|
|
|
|
).delete()
|
|
|
|
|
db.session.commit()
|
|
|
|
|
return deleted
|