Merge branch 'master' into delete-expired-things

Conflicts:
	app/celery/tasks.py
	tests/app/celery/test_tasks.py
	tests/app/dao/test_notification_dao.py
This commit is contained in:
Martyn Inglis
2016-03-10 09:48:29 +00:00
12 changed files with 370 additions and 28 deletions

View File

@@ -14,6 +14,7 @@ from app.clients.email.aws_ses import AwsSesClient
from app.encryption import Encryption
DATETIME_FORMAT = "%Y-%m-%dT%H:%M:%S.%f"
DATE_FORMAT = "%Y-%m-%d"
db = SQLAlchemy()
ma = Marshmallow()

View File

@@ -1,4 +1,4 @@
from app import create_uuid, DATETIME_FORMAT
from app import create_uuid, DATETIME_FORMAT, DATE_FORMAT
from app import notify_celery, encryption, firetext_client, aws_ses_client
from app.clients.email.aws_ses import AwsSesClientException
from app.clients.sms.firetext import FiretextClientException
@@ -8,7 +8,8 @@ from app.dao.notifications_dao import (
dao_create_notification,
dao_update_notification,
delete_failed_notifications_created_more_than_a_week_ago,
delete_successful_notifications_created_more_than_a_day_ago
delete_successful_notifications_created_more_than_a_day_ago,
dao_get_notification_statistics_for_service_and_day
)
from app.dao.jobs_dao import dao_update_job, dao_get_job_by_id
from app.dao.users_dao import delete_codes_older_created_more_than_a_day_ago
@@ -90,6 +91,27 @@ def delete_invitations():
def process_job(job_id):
start = datetime.utcnow()
job = dao_get_job_by_id(job_id)
service = job.service
stats = dao_get_notification_statistics_for_service_and_day(
service_id=service.id,
day=job.created_at.strftime(DATE_FORMAT)
)
total_sent = 0
if stats:
total_sent = stats.emails_requested + stats.sms_requested
if total_sent + job.notification_count > service.limit:
job.status = 'sending limits exceeded'
job.processing_finished = datetime.utcnow()
dao_update_job(job)
current_app.logger.info(
"Job {} size {} error. Sending limits {} exceeded".format(job_id, job.notification_count, service.limit)
)
return
job.status = 'in progress'
dao_update_job(job)

View File

@@ -11,6 +11,13 @@ def dao_get_notification_statistics_for_service(service_id):
).order_by(desc(NotificationStatistics.day)).all()
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()
def dao_create_notification(notification, notification_type):
try:
if notification.job_id:

View File

@@ -157,7 +157,7 @@ class Template(db.Model):
subject = db.Column(db.Text, index=False, unique=True, nullable=True)
JOB_STATUS_TYPES = ['pending', 'in progress', 'finished']
JOB_STATUS_TYPES = ['pending', 'in progress', 'finished', 'sending limits exceeded']
class Job(db.Model):

View File

@@ -8,9 +8,9 @@ from flask import (
url_for
)
from utils.template import Template, NeededByTemplateError, NoPlaceholderForDataError
from utils.template import Template
from app import api_user, encryption, create_uuid, DATETIME_FORMAT
from app import api_user, encryption, create_uuid, DATETIME_FORMAT, DATE_FORMAT
from app.authentication.auth import require_admin
from app.dao import (
templates_dao,
@@ -127,6 +127,21 @@ def send_notification(notification_type):
assert False
service_id = api_user['client']
service = services_dao.dao_fetch_service_by_id(api_user['client'])
service_stats = notifications_dao.dao_get_notification_statistics_for_service_and_day(
service_id,
datetime.utcnow().strftime(DATE_FORMAT)
)
print(service_stats)
if service_stats:
total_sms_count = service_stats.sms_requested
total_email_count = service_stats.emails_requested
if total_email_count + total_sms_count >= service.limit:
return jsonify(result="error", message='Exceeded send limits ({}) for today'.format(service.limit)), 429
notification, errors = (
sms_template_notification_schema if notification_type == 'sms' else email_notification_schema
@@ -167,7 +182,6 @@ def send_notification(notification_type):
}
), 400
service = services_dao.dao_fetch_service_by_id(api_user['client'])
notification_id = create_uuid()
if notification_type == 'sms':

View File

@@ -28,9 +28,7 @@ from app.models import ApiKey
from app.schemas import (
service_schema,
api_key_schema,
user_schema,
permission_schema,
invited_user_schema
user_schema
)
from app.errors import register_errors