Update limit to message_limit.

Further db changes and updates.

Remove traceback print out.

Fix bug in passing template id to a task.
This commit is contained in:
Nicholas Staples
2016-04-08 16:13:10 +01:00
parent c4b316bde6
commit 90f0505a3d
19 changed files with 116 additions and 116 deletions

View File

@@ -70,7 +70,7 @@ def delete_verify_codes():
def delete_successful_notifications():
try:
start = datetime.utcnow()
deleted = delete_notifications_created_more_than_a_week_ago('sent')
deleted = delete_notifications_created_more_than_a_week_ago('sending')
current_app.logger.info(
"Delete job started {} finished {} deleted {} successful notifications".format(
start,
@@ -129,12 +129,13 @@ def process_job(job_id):
if stats:
total_sent = stats.emails_requested + stats.sms_requested
if total_sent + job.notification_count > service.limit:
if total_sent + job.notification_count > service.message_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)
"Job {} size {} error. Sending limits {} exceeded".format(
job_id, job.notification_count, service.message_limit)
)
return
@@ -152,7 +153,7 @@ def process_job(job_id):
).recipients_and_personalisation:
encrypted = encryption.encrypt({
'template': template.id,
'template': str(template.id),
'job': str(job.id),
'to': recipient,
'personalisation': personalisation
@@ -217,7 +218,7 @@ def send_sms(service_id, notification_id, encrypted_notification, created_at):
to=notification['to'],
service_id=service_id,
job_id=notification.get('job', None),
status='failed' if restricted else 'sent',
status='failed' if restricted else 'sending',
created_at=datetime.strptime(created_at, DATETIME_FORMAT),
sent_at=sent_at,
sent_by=client.get_name()
@@ -277,7 +278,7 @@ def send_email(service_id, notification_id, subject, from_address, encrypted_not
to=notification['to'],
service_id=service_id,
job_id=notification.get('job', None),
status='failed' if restricted else 'sent',
status='failed' if restricted else 'sending',
created_at=datetime.strptime(created_at, DATETIME_FORMAT),
sent_at=sent_at,
sent_by=client.get_name()

View File

@@ -8,7 +8,7 @@ ses_response_map = {
'Bounce': {
"message": 'Bounced',
"success": False,
"notification_status": 'bounce',
"notification_status": 'failed',
"notification_statistics_status": STATISTICS_FAILURE
},
'Delivery': {
@@ -19,9 +19,9 @@ ses_response_map = {
},
'Complaint': {
"message": 'Complaint',
"success": False,
"notification_status": 'complaint',
"notification_statistics_status": STATISTICS_FAILURE
"success": True,
"notification_status": 'delivered',
"notification_statistics_status": STATISTICS_DELIVERED
}
}

View File

@@ -25,9 +25,9 @@ firetext_responses = {
},
'2': {
"message": 'Undelivered (Pending with Network)',
"success": False,
"notification_statistics_status": None,
"notification_status": 'sent'
"success": True,
"notification_statistics_status": STATISTICS_DELIVERED,
"notification_status": 'delivered'
}
}

View File

@@ -44,8 +44,10 @@ class TwilioClient(SmsClient):
def status(self, message_id):
try:
response = self.client.messages.get(message_id)
if response.status in ('delivered', 'undelivered', 'failed'):
if response.status in ('delivered', 'failed'):
return response.status
elif response.status == 'undelivered':
return 'sending'
return None
except TwilioRestException as e:
current_app.logger.exception(e)

View File

@@ -112,12 +112,12 @@ def update_query(notification_type, status):
TEMPLATE_TYPE_SMS: {
STATISTICS_REQUESTED: NotificationStatistics.sms_requested,
STATISTICS_DELIVERED: NotificationStatistics.sms_delivered,
STATISTICS_FAILURE: NotificationStatistics.sms_error
STATISTICS_FAILURE: NotificationStatistics.sms_failed
},
TEMPLATE_TYPE_EMAIL: {
STATISTICS_REQUESTED: NotificationStatistics.emails_requested,
STATISTICS_DELIVERED: NotificationStatistics.emails_delivered,
STATISTICS_FAILURE: NotificationStatistics.emails_error
STATISTICS_FAILURE: NotificationStatistics.emails_failed
}
}
return {

View File

@@ -88,7 +88,7 @@ class Service(db.Model):
nullable=True,
onupdate=datetime.datetime.now)
active = db.Column(db.Boolean, index=False, unique=False, nullable=False)
limit = db.Column(db.BigInteger, index=False, unique=False, nullable=False)
message_limit = db.Column(db.BigInteger, index=False, unique=False, nullable=False)
users = db.relationship(
'User',
secondary=user_to_service,
@@ -121,10 +121,10 @@ class NotificationStatistics(db.Model):
service = db.relationship('Service', backref=db.backref('service_notification_stats', lazy='dynamic'))
emails_requested = db.Column(db.BigInteger, index=False, unique=False, nullable=False, default=0)
emails_delivered = db.Column(db.BigInteger, index=False, unique=False, nullable=False, default=0)
emails_error = db.Column(db.BigInteger, index=False, unique=False, nullable=False, default=0)
emails_failed = db.Column(db.BigInteger, index=False, unique=False, nullable=False, default=0)
sms_requested = db.Column(db.BigInteger, index=False, unique=False, nullable=False, default=0)
sms_delivered = db.Column(db.BigInteger, index=False, unique=False, nullable=False, default=0)
sms_error = db.Column(db.BigInteger, index=False, unique=False, nullable=False, default=0)
sms_failed = db.Column(db.BigInteger, index=False, unique=False, nullable=False, default=0)
__table_args__ = (
UniqueConstraint('service_id', 'day', name='uix_service_to_day'),
@@ -234,7 +234,7 @@ class VerifyCode(db.Model):
return check_hash(cde, self._code)
NOTIFICATION_STATUS_TYPES = ['sent', 'delivered', 'failed', 'complaint', 'bounce']
NOTIFICATION_STATUS_TYPES = ['sending', 'delivered', 'failed']
class Notification(db.Model):
@@ -267,7 +267,7 @@ class Notification(db.Model):
nullable=True,
onupdate=datetime.datetime.utcnow)
status = db.Column(
db.Enum(*NOTIFICATION_STATUS_TYPES, name='notification_status_types'), nullable=False, default='sent')
db.Enum(*NOTIFICATION_STATUS_TYPES, name='notification_status_types'), nullable=False, default='sending')
reference = db.Column(db.String, nullable=True, index=True)

View File

@@ -287,8 +287,9 @@ def send_notification(notification_type):
total_sms_count = service_stats.sms_requested
total_email_count = service_stats.emails_requested
if (total_email_count + total_sms_count >= service.limit) and service.restricted:
return jsonify(result="error", message='Exceeded send limits ({}) for today'.format(service.limit)), 429
if (total_email_count + total_sms_count >= service.message_limit) and service.restricted:
return jsonify(result="error", message='Exceeded send limits ({}) for today'.format(
service.message_limit)), 429
notification, errors = (
sms_template_notification_schema if notification_type == 'sms' else email_notification_schema