mirror of
https://github.com/GSA/notifications-api.git
synced 2026-09-11 10:28:55 -04:00
Merge pull request #481 from alphagov/add-notification-type
Add notification type
This commit is contained in:
@@ -23,6 +23,8 @@ from notifications_utils.template import (
|
|||||||
unlink_govuk_escaped
|
unlink_govuk_escaped
|
||||||
)
|
)
|
||||||
|
|
||||||
|
from app.models import SMS_TYPE
|
||||||
|
|
||||||
|
|
||||||
def retry_iteration_to_delay(retry=0):
|
def retry_iteration_to_delay(retry=0):
|
||||||
"""
|
"""
|
||||||
@@ -75,7 +77,7 @@ def send_sms_to_provider(self, service_id, notification_id, encrypted_notificati
|
|||||||
|
|
||||||
update_provider_stats(
|
update_provider_stats(
|
||||||
notification_id,
|
notification_id,
|
||||||
'sms',
|
SMS_TYPE,
|
||||||
provider.get_name(),
|
provider.get_name(),
|
||||||
content_char_count=template.replaced_content_count
|
content_char_count=template.replaced_content_count
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ from flask import current_app
|
|||||||
from app import notify_celery
|
from app import notify_celery
|
||||||
from requests import request, RequestException, HTTPError
|
from requests import request, RequestException, HTTPError
|
||||||
|
|
||||||
|
from app.models import SMS_TYPE
|
||||||
|
|
||||||
temp_fail = "07833333333"
|
temp_fail = "07833333333"
|
||||||
perm_fail = "07822222222"
|
perm_fail = "07822222222"
|
||||||
delivered = "07811111111"
|
delivered = "07811111111"
|
||||||
@@ -21,7 +23,7 @@ def send_sms_response(provider, reference, to):
|
|||||||
else:
|
else:
|
||||||
headers = {"Content-type": "application/x-www-form-urlencoded"}
|
headers = {"Content-type": "application/x-www-form-urlencoded"}
|
||||||
body = firetext_callback(reference, to)
|
body = firetext_callback(reference, to)
|
||||||
make_request('sms', provider, body, headers)
|
make_request(SMS_TYPE, provider, body, headers)
|
||||||
|
|
||||||
|
|
||||||
@notify_celery.task(name="send-ses-response")
|
@notify_celery.task(name="send-ses-response")
|
||||||
|
|||||||
+10
-8
@@ -37,8 +37,8 @@ from app.dao.services_dao import dao_fetch_service_by_id
|
|||||||
from app.dao.templates_dao import dao_get_template_by_id
|
from app.dao.templates_dao import dao_get_template_by_id
|
||||||
from app.models import (
|
from app.models import (
|
||||||
Notification,
|
Notification,
|
||||||
TEMPLATE_TYPE_EMAIL,
|
EMAIL_TYPE,
|
||||||
TEMPLATE_TYPE_SMS
|
SMS_TYPE
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -94,7 +94,7 @@ def process_job(job_id):
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
if template.template_type == 'sms':
|
if template.template_type == SMS_TYPE:
|
||||||
send_sms.apply_async((
|
send_sms.apply_async((
|
||||||
str(job.service_id),
|
str(job.service_id),
|
||||||
create_uuid(),
|
create_uuid(),
|
||||||
@@ -103,7 +103,7 @@ def process_job(job_id):
|
|||||||
queue='bulk-sms'
|
queue='bulk-sms'
|
||||||
)
|
)
|
||||||
|
|
||||||
if template.template_type == 'email':
|
if template.template_type == EMAIL_TYPE:
|
||||||
send_email.apply_async((
|
send_email.apply_async((
|
||||||
str(job.service_id),
|
str(job.service_id),
|
||||||
create_uuid(),
|
create_uuid(),
|
||||||
@@ -157,9 +157,10 @@ def send_sms(self, service_id, notification_id, encrypted_notification, created_
|
|||||||
job_row_number=notification.get('row_number', None),
|
job_row_number=notification.get('row_number', None),
|
||||||
status='created',
|
status='created',
|
||||||
created_at=datetime.strptime(created_at, DATETIME_FORMAT),
|
created_at=datetime.strptime(created_at, DATETIME_FORMAT),
|
||||||
personalisation=notification.get('personalisation')
|
personalisation=notification.get('personalisation'),
|
||||||
|
notification_type=SMS_TYPE
|
||||||
)
|
)
|
||||||
dao_create_notification(notification_db_object, TEMPLATE_TYPE_SMS)
|
dao_create_notification(notification_db_object, SMS_TYPE)
|
||||||
|
|
||||||
send_sms_to_provider.apply_async((service_id, notification_id), queue='sms')
|
send_sms_to_provider.apply_async((service_id, notification_id), queue='sms')
|
||||||
|
|
||||||
@@ -202,10 +203,11 @@ def send_email(service_id, notification_id, encrypted_notification, created_at,
|
|||||||
created_at=datetime.strptime(created_at, DATETIME_FORMAT),
|
created_at=datetime.strptime(created_at, DATETIME_FORMAT),
|
||||||
sent_at=sent_at,
|
sent_at=sent_at,
|
||||||
sent_by=provider.get_name(),
|
sent_by=provider.get_name(),
|
||||||
personalisation=notification.get('personalisation')
|
personalisation=notification.get('personalisation'),
|
||||||
|
notification_type='email'
|
||||||
)
|
)
|
||||||
|
|
||||||
dao_create_notification(notification_db_object, TEMPLATE_TYPE_EMAIL)
|
dao_create_notification(notification_db_object, EMAIL_TYPE)
|
||||||
statsd_client.timing_with_dates(
|
statsd_client.timing_with_dates(
|
||||||
"notifications.tasks.send-email.queued-for",
|
"notifications.tasks.send-email.queued-for",
|
||||||
sent_at,
|
sent_at,
|
||||||
|
|||||||
@@ -18,8 +18,8 @@ from app.models import (
|
|||||||
Job,
|
Job,
|
||||||
NotificationStatistics,
|
NotificationStatistics,
|
||||||
TemplateStatistics,
|
TemplateStatistics,
|
||||||
TEMPLATE_TYPE_SMS,
|
SMS_TYPE,
|
||||||
TEMPLATE_TYPE_EMAIL,
|
EMAIL_TYPE,
|
||||||
Template,
|
Template,
|
||||||
ProviderStatistics,
|
ProviderStatistics,
|
||||||
ProviderDetails)
|
ProviderDetails)
|
||||||
@@ -166,8 +166,8 @@ def dao_create_notification(notification, notification_type):
|
|||||||
stats = NotificationStatistics(
|
stats = NotificationStatistics(
|
||||||
day=notification.created_at.date(),
|
day=notification.created_at.date(),
|
||||||
service_id=notification.service_id,
|
service_id=notification.service_id,
|
||||||
sms_requested=1 if notification_type == TEMPLATE_TYPE_SMS else 0,
|
sms_requested=1 if notification_type == SMS_TYPE else 0,
|
||||||
emails_requested=1 if notification_type == TEMPLATE_TYPE_EMAIL else 0
|
emails_requested=1 if notification_type == EMAIL_TYPE else 0
|
||||||
)
|
)
|
||||||
db.session.add(stats)
|
db.session.add(stats)
|
||||||
|
|
||||||
@@ -187,12 +187,12 @@ def dao_create_notification(notification, notification_type):
|
|||||||
|
|
||||||
def _update_notification_stats_query(notification_type, status):
|
def _update_notification_stats_query(notification_type, status):
|
||||||
mapping = {
|
mapping = {
|
||||||
TEMPLATE_TYPE_SMS: {
|
SMS_TYPE: {
|
||||||
STATISTICS_REQUESTED: NotificationStatistics.sms_requested,
|
STATISTICS_REQUESTED: NotificationStatistics.sms_requested,
|
||||||
STATISTICS_DELIVERED: NotificationStatistics.sms_delivered,
|
STATISTICS_DELIVERED: NotificationStatistics.sms_delivered,
|
||||||
STATISTICS_FAILURE: NotificationStatistics.sms_failed
|
STATISTICS_FAILURE: NotificationStatistics.sms_failed
|
||||||
},
|
},
|
||||||
TEMPLATE_TYPE_EMAIL: {
|
EMAIL_TYPE: {
|
||||||
STATISTICS_REQUESTED: NotificationStatistics.emails_requested,
|
STATISTICS_REQUESTED: NotificationStatistics.emails_requested,
|
||||||
STATISTICS_DELIVERED: NotificationStatistics.emails_delivered,
|
STATISTICS_DELIVERED: NotificationStatistics.emails_delivered,
|
||||||
STATISTICS_FAILURE: NotificationStatistics.emails_failed
|
STATISTICS_FAILURE: NotificationStatistics.emails_failed
|
||||||
@@ -213,7 +213,7 @@ def _update_statistics(notification, notification_statistics_status):
|
|||||||
day=notification.created_at.date(),
|
day=notification.created_at.date(),
|
||||||
service_id=notification.service_id
|
service_id=notification.service_id
|
||||||
).update(
|
).update(
|
||||||
_update_notification_stats_query(notification.template.template_type, notification_statistics_status)
|
_update_notification_stats_query(notification.notification_type, notification_statistics_status)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -294,7 +294,7 @@ def update_provider_stats(
|
|||||||
provider = ProviderDetails.query.filter_by(identifier=provider_name).one()
|
provider = ProviderDetails.query.filter_by(identifier=provider_name).one()
|
||||||
|
|
||||||
def unit_count():
|
def unit_count():
|
||||||
if notification_type == TEMPLATE_TYPE_EMAIL:
|
if notification_type == EMAIL_TYPE:
|
||||||
return 1
|
return 1
|
||||||
else:
|
else:
|
||||||
if (content_char_count):
|
if (content_char_count):
|
||||||
|
|||||||
+11
-7
@@ -165,11 +165,13 @@ class NotificationStatistics(db.Model):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
TEMPLATE_TYPE_SMS = 'sms'
|
SMS_TYPE = 'sms'
|
||||||
TEMPLATE_TYPE_EMAIL = 'email'
|
EMAIL_TYPE = 'email'
|
||||||
TEMPLATE_TYPE_LETTER = 'letter'
|
LETTER_TYPE = 'letter'
|
||||||
|
|
||||||
TEMPLATE_TYPES = [TEMPLATE_TYPE_SMS, TEMPLATE_TYPE_EMAIL, TEMPLATE_TYPE_LETTER]
|
TEMPLATE_TYPES = [SMS_TYPE, EMAIL_TYPE, LETTER_TYPE]
|
||||||
|
|
||||||
|
template_types = db.Enum(*TEMPLATE_TYPES, name='template_type')
|
||||||
|
|
||||||
|
|
||||||
class Template(db.Model, Versioned):
|
class Template(db.Model, Versioned):
|
||||||
@@ -177,7 +179,7 @@ class Template(db.Model, Versioned):
|
|||||||
|
|
||||||
id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||||
name = db.Column(db.String(255), nullable=False)
|
name = db.Column(db.String(255), nullable=False)
|
||||||
template_type = db.Column(db.Enum(*TEMPLATE_TYPES, name='template_type'), nullable=False)
|
template_type = db.Column(template_types, nullable=False)
|
||||||
created_at = db.Column(
|
created_at = db.Column(
|
||||||
db.DateTime,
|
db.DateTime,
|
||||||
index=False,
|
index=False,
|
||||||
@@ -207,7 +209,8 @@ SMS_PROVIDERS = [MMG_PROVIDER, FIRETEXT_PROVIDER]
|
|||||||
EMAIL_PROVIDERS = [SES_PROVIDER]
|
EMAIL_PROVIDERS = [SES_PROVIDER]
|
||||||
PROVIDERS = SMS_PROVIDERS + EMAIL_PROVIDERS
|
PROVIDERS = SMS_PROVIDERS + EMAIL_PROVIDERS
|
||||||
|
|
||||||
NOTIFICATION_TYPE = ['email', 'sms', 'letter']
|
NOTIFICATION_TYPE = [EMAIL_TYPE, SMS_TYPE, LETTER_TYPE]
|
||||||
|
notification_types = db.Enum(*NOTIFICATION_TYPE, name='notification_type')
|
||||||
|
|
||||||
|
|
||||||
class ProviderStatistics(db.Model):
|
class ProviderStatistics(db.Model):
|
||||||
@@ -241,7 +244,7 @@ class ProviderDetails(db.Model):
|
|||||||
display_name = db.Column(db.String, nullable=False)
|
display_name = db.Column(db.String, nullable=False)
|
||||||
identifier = db.Column(db.String, nullable=False)
|
identifier = db.Column(db.String, nullable=False)
|
||||||
priority = db.Column(db.Integer, nullable=False)
|
priority = db.Column(db.Integer, nullable=False)
|
||||||
notification_type = db.Column(db.Enum(*NOTIFICATION_TYPE, name='notification_type'), nullable=False)
|
notification_type = db.Column(notification_types, nullable=False)
|
||||||
active = db.Column(db.Boolean, default=False)
|
active = db.Column(db.Boolean, default=False)
|
||||||
|
|
||||||
|
|
||||||
@@ -345,6 +348,7 @@ class Notification(db.Model):
|
|||||||
api_key = db.relationship('ApiKey')
|
api_key = db.relationship('ApiKey')
|
||||||
key_type = db.Column(db.String, db.ForeignKey('key_types.name'), index=True, unique=False)
|
key_type = db.Column(db.String, db.ForeignKey('key_types.name'), index=True, unique=False)
|
||||||
content_char_count = db.Column(db.Integer, nullable=True)
|
content_char_count = db.Column(db.Integer, nullable=True)
|
||||||
|
notification_type = db.Column(notification_types, nullable=False)
|
||||||
created_at = db.Column(
|
created_at = db.Column(
|
||||||
db.DateTime,
|
db.DateTime,
|
||||||
index=False,
|
index=False,
|
||||||
|
|||||||
@@ -0,0 +1,27 @@
|
|||||||
|
"""empty message
|
||||||
|
|
||||||
|
Revision ID: 0035_notification_type
|
||||||
|
Revises: 0034_pwd_changed_at_not_null
|
||||||
|
Create Date: 2016-06-29 10:48:55.955317
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = '0035_notification_type'
|
||||||
|
down_revision = '0034_pwd_changed_at_not_null'
|
||||||
|
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
notification_types = sa.Enum('email', 'sms', 'letter', name='notification_type')
|
||||||
|
|
||||||
|
op.add_column('notifications', sa.Column('notification_type', notification_types, nullable=True))
|
||||||
|
op.execute('update notifications set notification_type = (select CAST(CAST(template_type as text) as notification_type) '
|
||||||
|
'from templates where templates.id = notifications.template_id)')
|
||||||
|
op.alter_column('notifications', 'notification_type', nullable=False)
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
### commands auto generated by Alembic - please adjust! ###
|
||||||
|
op.drop_column('notifications', 'notification_type')
|
||||||
|
### end Alembic commands ###
|
||||||
@@ -46,6 +46,7 @@ def _notification_json(template, to, personalisation=None, job_id=None, row_numb
|
|||||||
"template": str(template.id),
|
"template": str(template.id),
|
||||||
"template_version": template.version,
|
"template_version": template.version,
|
||||||
"to": to,
|
"to": to,
|
||||||
|
"notification_type": template.template_type
|
||||||
}
|
}
|
||||||
if personalisation:
|
if personalisation:
|
||||||
notification.update({"personalisation": personalisation})
|
notification.update({"personalisation": personalisation})
|
||||||
|
|||||||
@@ -351,7 +351,8 @@ def sample_notification(notify_db,
|
|||||||
'reference': reference,
|
'reference': reference,
|
||||||
'created_at': created_at,
|
'created_at': created_at,
|
||||||
'content_char_count': content_char_count,
|
'content_char_count': content_char_count,
|
||||||
'personalisation': personalisation
|
'personalisation': personalisation,
|
||||||
|
'notification_type': template.template_type
|
||||||
}
|
}
|
||||||
if job_row_number:
|
if job_row_number:
|
||||||
data['job_row_number'] = job_row_number
|
data['job_row_number'] = job_row_number
|
||||||
|
|||||||
@@ -976,6 +976,7 @@ def _notification_json(sample_template, job_id=None, id=None, status=None):
|
|||||||
'template_version': sample_template.version,
|
'template_version': sample_template.version,
|
||||||
'created_at': datetime.utcnow(),
|
'created_at': datetime.utcnow(),
|
||||||
'content_char_count': 160,
|
'content_char_count': 160,
|
||||||
|
'notification_type': sample_template.template_type
|
||||||
}
|
}
|
||||||
if job_id:
|
if job_id:
|
||||||
data.update({'job_id': job_id})
|
data.update({'job_id': job_id})
|
||||||
|
|||||||
Reference in New Issue
Block a user