mirror of
https://github.com/GSA/notifications-api.git
synced 2026-07-30 00:18:21 -04:00
Merge pull request #794 from alphagov/priority-by-template
Priority by template
This commit is contained in:
@@ -146,8 +146,8 @@ class Config(object):
|
||||
SENDING_NOTIFICATIONS_TIMEOUT_PERIOD = 259200 # 3 days
|
||||
|
||||
SIMULATED_EMAIL_ADDRESSES = ('simulate-delivered@notifications.service.gov.uk',
|
||||
'simulate-permanent-failure@notifications.service.gov.uk',
|
||||
'simulate-temporary-failure@notifications.service.gov.uk',
|
||||
'simulate-delivered-2@notifications.service.gov.uk',
|
||||
'simulate-delivered-3@notifications.service.gov.uk',
|
||||
)
|
||||
|
||||
SIMULATED_SMS_NUMBERS = ('+447700900000', '+447700900111', '+447700900222')
|
||||
|
||||
@@ -305,7 +305,7 @@ class Template(db.Model):
|
||||
process_type = db.Column(db.String(255),
|
||||
db.ForeignKey('template_process_type.name'),
|
||||
index=True,
|
||||
nullable=True,
|
||||
nullable=False,
|
||||
default=NORMAL)
|
||||
|
||||
def get_link(self):
|
||||
@@ -337,7 +337,7 @@ class TemplateHistory(db.Model):
|
||||
process_type = db.Column(db.String(255),
|
||||
db.ForeignKey('template_process_type.name'),
|
||||
index=True,
|
||||
nullable=True,
|
||||
nullable=False,
|
||||
default=NORMAL)
|
||||
|
||||
MMG_PROVIDER = "mmg"
|
||||
|
||||
@@ -41,7 +41,9 @@ def persist_notification(template_id,
|
||||
job_id=None,
|
||||
job_row_number=None,
|
||||
reference=None,
|
||||
notification_id=None):
|
||||
notification_id=None,
|
||||
simulated=False):
|
||||
# if simulated create a Notification model to return but do not persist the Notification to the dB
|
||||
notification = Notification(
|
||||
id=notification_id,
|
||||
template_id=template_id,
|
||||
@@ -58,8 +60,12 @@ def persist_notification(template_id,
|
||||
job_row_number=job_row_number,
|
||||
client_reference=reference
|
||||
)
|
||||
dao_create_notification(notification)
|
||||
redis_store.incr(redis.daily_limit_cache_key(service.id))
|
||||
if not simulated:
|
||||
dao_create_notification(notification)
|
||||
redis_store.incr(redis.daily_limit_cache_key(service.id))
|
||||
current_app.logger.info(
|
||||
"{} {} created at {}".format(notification.notification_type, notification.id, notification.created_at)
|
||||
)
|
||||
return notification
|
||||
|
||||
|
||||
@@ -85,5 +91,12 @@ def send_notification_to_queue(notification, research_mode, queue=None):
|
||||
raise SendNotificationToQueueError()
|
||||
|
||||
current_app.logger.info(
|
||||
"{} {} created at {}".format(notification.notification_type, notification.id, notification.created_at)
|
||||
)
|
||||
"{} {} sent to the {} queue for delivery".format(notification.notification_type,
|
||||
notification.id,
|
||||
queue))
|
||||
|
||||
|
||||
def simulated_recipient(to_address, notification_type):
|
||||
return (to_address in current_app.config['SIMULATED_SMS_NUMBERS']
|
||||
if notification_type == SMS_TYPE
|
||||
else to_address in current_app.config['SIMULATED_EMAIL_ADDRESSES'])
|
||||
|
||||
@@ -8,22 +8,25 @@ from flask import (
|
||||
json
|
||||
)
|
||||
|
||||
from app import api_user, create_uuid, statsd_client
|
||||
from app import api_user, statsd_client
|
||||
from app.clients.email.aws_ses import get_aws_responses
|
||||
from app.dao import (
|
||||
templates_dao,
|
||||
services_dao,
|
||||
notifications_dao
|
||||
)
|
||||
from app.models import KEY_TYPE_TEAM
|
||||
from app.models import KEY_TYPE_TEAM, PRIORITY
|
||||
from app.models import SMS_TYPE
|
||||
from app.notifications.process_client_response import (
|
||||
validate_callback_data,
|
||||
process_sms_client_response
|
||||
)
|
||||
from app.notifications.process_notifications import persist_notification, send_notification_to_queue
|
||||
from app.notifications.validators import check_service_message_limit, check_template_is_for_notification_type, \
|
||||
check_template_is_active
|
||||
from app.notifications.process_notifications import (persist_notification,
|
||||
send_notification_to_queue,
|
||||
simulated_recipient)
|
||||
from app.notifications.validators import (check_service_message_limit,
|
||||
check_template_is_for_notification_type,
|
||||
check_template_is_active)
|
||||
from app.schemas import (
|
||||
email_notification_schema,
|
||||
sms_template_notification_schema,
|
||||
@@ -212,10 +215,9 @@ def send_notification(notification_type):
|
||||
if notification_type not in ['sms', 'email']:
|
||||
assert False
|
||||
|
||||
service_id = str(api_user.service_id)
|
||||
service = services_dao.dao_fetch_service_by_id(service_id)
|
||||
service = services_dao.dao_fetch_service_by_id(api_user.service_id)
|
||||
|
||||
notification, errors = (
|
||||
notification_form, errors = (
|
||||
sms_template_notification_schema if notification_type == SMS_TYPE else email_notification_schema
|
||||
).load(request.get_json())
|
||||
if errors:
|
||||
@@ -223,34 +225,40 @@ def send_notification(notification_type):
|
||||
|
||||
check_service_message_limit(api_user.key_type, service)
|
||||
|
||||
template = templates_dao.dao_get_template_by_id_and_service_id(template_id=notification['template'],
|
||||
service_id=service_id)
|
||||
template = templates_dao.dao_get_template_by_id_and_service_id(template_id=notification_form['template'],
|
||||
service_id=service.id)
|
||||
|
||||
check_template_is_for_notification_type(notification_type, template.template_type)
|
||||
check_template_is_active(template)
|
||||
|
||||
template_object = create_template_object_for_notification(template, notification.get('personalisation', {}))
|
||||
template_object = create_template_object_for_notification(template, notification_form.get('personalisation', {}))
|
||||
|
||||
_service_allowed_to_send_to(notification, service)
|
||||
_service_allowed_to_send_to(notification_form, service)
|
||||
|
||||
saved_notification = None
|
||||
if not _simulated_recipient(notification['to'], notification_type):
|
||||
saved_notification = persist_notification(template_id=template.id,
|
||||
template_version=template.version,
|
||||
recipient=notification['to'],
|
||||
service=service,
|
||||
personalisation=notification.get('personalisation', None),
|
||||
notification_type=notification_type,
|
||||
api_key_id=api_user.id,
|
||||
key_type=api_user.key_type)
|
||||
send_notification_to_queue(saved_notification, service.research_mode)
|
||||
# Do not persist or send notification to the queue if it is a simulated recipient
|
||||
simulated = simulated_recipient(notification_form['to'], notification_type)
|
||||
notification_model = persist_notification(template_id=template.id,
|
||||
template_version=template.version,
|
||||
recipient=notification_form['to'],
|
||||
service=service,
|
||||
personalisation=notification_form.get('personalisation', None),
|
||||
notification_type=notification_type,
|
||||
api_key_id=api_user.id,
|
||||
key_type=api_user.key_type,
|
||||
simulated=simulated)
|
||||
if not simulated:
|
||||
queue_name = 'notify' if template.process_type == PRIORITY else None
|
||||
send_notification_to_queue(notification=notification_model,
|
||||
research_mode=service.research_mode,
|
||||
queue=queue_name)
|
||||
else:
|
||||
current_app.logger.info("POST simulated notification for id: {}".format(notification_model.id))
|
||||
notification_form.update({"template_version": template.version})
|
||||
|
||||
notification_id = create_uuid() if saved_notification is None else saved_notification.id
|
||||
notification.update({"template_version": template.version})
|
||||
return jsonify(
|
||||
data=get_notification_return_data(
|
||||
notification_id,
|
||||
notification,
|
||||
notification_model.id,
|
||||
notification_form,
|
||||
template_object)
|
||||
), 201
|
||||
|
||||
@@ -268,12 +276,6 @@ def get_notification_return_data(notification_id, notification, template):
|
||||
return output
|
||||
|
||||
|
||||
def _simulated_recipient(to_address, notification_type):
|
||||
return (to_address in current_app.config['SIMULATED_SMS_NUMBERS']
|
||||
if notification_type == SMS_TYPE
|
||||
else to_address in current_app.config['SIMULATED_EMAIL_ADDRESSES'])
|
||||
|
||||
|
||||
def _service_allowed_to_send_to(notification, service):
|
||||
if not service_allowed_to_send_to(notification['to'], service, api_user.key_type):
|
||||
if api_user.key_type == KEY_TYPE_TEAM:
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
from flask import current_app
|
||||
from notifications_utils.recipients import validate_and_format_phone_number, validate_and_format_email_address
|
||||
|
||||
from app.dao import services_dao
|
||||
from app.models import KEY_TYPE_TEST, KEY_TYPE_TEAM
|
||||
from app.models import KEY_TYPE_TEST, KEY_TYPE_TEAM, SMS_TYPE
|
||||
from app.service.utils import service_allowed_to_send_to
|
||||
from app.v2.errors import TooManyRequestsError, BadRequestError
|
||||
from app import redis_store
|
||||
@@ -44,6 +45,14 @@ def service_can_send_to_recipient(send_to, key_type, service):
|
||||
raise BadRequestError(message=message)
|
||||
|
||||
|
||||
def validate_and_format_recipient(send_to, key_type, service, notification_type):
|
||||
service_can_send_to_recipient(send_to, key_type, service)
|
||||
if notification_type == SMS_TYPE:
|
||||
return validate_and_format_phone_number(number=send_to)
|
||||
else:
|
||||
return validate_and_format_email_address(email_address=send_to)
|
||||
|
||||
|
||||
def check_sms_content_char_count(content_count):
|
||||
char_count_limit = current_app.config.get('SMS_CHAR_COUNT_LIMIT')
|
||||
if (
|
||||
|
||||
@@ -208,6 +208,7 @@ class BaseTemplateSchema(BaseSchema):
|
||||
class TemplateSchema(BaseTemplateSchema):
|
||||
|
||||
created_by = field_for(models.Template, 'created_by', required=True)
|
||||
process_type = field_for(models.Template, 'process_type')
|
||||
|
||||
@validates_schema
|
||||
def validate_type(self, data):
|
||||
|
||||
@@ -144,5 +144,5 @@ def _strip_html(content):
|
||||
def _template_has_not_changed(current_data, updated_template):
|
||||
return all(
|
||||
current_data[key] == updated_template[key]
|
||||
for key in ('name', 'content', 'subject', 'archived')
|
||||
for key in ('name', 'content', 'subject', 'archived', 'process_type')
|
||||
)
|
||||
|
||||
@@ -1,4 +1,8 @@
|
||||
import uuid
|
||||
|
||||
from flask import jsonify, request, url_for
|
||||
from werkzeug.exceptions import abort
|
||||
|
||||
from app import api_user
|
||||
from app.dao import notifications_dao
|
||||
from app.schema_validation import validate
|
||||
@@ -6,10 +10,14 @@ from app.v2.notifications import notification_blueprint
|
||||
from app.v2.notifications.notification_schemas import get_notifications_request
|
||||
|
||||
|
||||
@notification_blueprint.route("/<uuid:id>", methods=['GET'])
|
||||
@notification_blueprint.route("/<id>", methods=['GET'])
|
||||
def get_notification_by_id(id):
|
||||
try:
|
||||
casted_id = uuid.UUID(id)
|
||||
except ValueError or AttributeError:
|
||||
abort(404)
|
||||
notification = notifications_dao.get_notification_with_personalisation(
|
||||
str(api_user.service_id), id, key_type=None
|
||||
api_user.service_id, casted_id, key_type=None
|
||||
)
|
||||
|
||||
return jsonify(notification.serialize()), 200
|
||||
|
||||
@@ -3,15 +3,16 @@ from sqlalchemy.orm.exc import NoResultFound
|
||||
|
||||
from app import api_user
|
||||
from app.dao import services_dao, templates_dao
|
||||
from app.models import SMS_TYPE, EMAIL_TYPE
|
||||
from app.models import SMS_TYPE, EMAIL_TYPE, PRIORITY
|
||||
from app.notifications.process_notifications import (create_content_for_notification,
|
||||
persist_notification,
|
||||
send_notification_to_queue)
|
||||
send_notification_to_queue,
|
||||
simulated_recipient)
|
||||
from app.notifications.validators import (check_service_message_limit,
|
||||
check_template_is_for_notification_type,
|
||||
check_template_is_active,
|
||||
service_can_send_to_recipient,
|
||||
check_sms_content_char_count)
|
||||
check_sms_content_char_count,
|
||||
validate_and_format_recipient)
|
||||
from app.schema_validation import validate
|
||||
from app.v2.errors import BadRequestError
|
||||
from app.v2.notifications import notification_blueprint
|
||||
@@ -20,62 +21,53 @@ from app.v2.notifications.notification_schemas import (post_sms_request,
|
||||
create_post_email_response_from_notification)
|
||||
|
||||
|
||||
@notification_blueprint.route('/sms', methods=['POST'])
|
||||
def post_sms_notification():
|
||||
form = validate(request.get_json(), post_sms_request)
|
||||
@notification_blueprint.route('/<notification_type>', methods=['POST'])
|
||||
def post_notification(notification_type):
|
||||
if notification_type == EMAIL_TYPE:
|
||||
form = validate(request.get_json(), post_email_request)
|
||||
else:
|
||||
form = validate(request.get_json(), post_sms_request)
|
||||
service = services_dao.dao_fetch_service_by_id(api_user.service_id)
|
||||
|
||||
check_service_message_limit(api_user.key_type, service)
|
||||
service_can_send_to_recipient(form['phone_number'], api_user.key_type, service)
|
||||
form_send_to = form['phone_number'] if notification_type == SMS_TYPE else form['email_address']
|
||||
send_to = validate_and_format_recipient(send_to=form_send_to,
|
||||
key_type=api_user.key_type,
|
||||
service=service,
|
||||
notification_type=notification_type)
|
||||
|
||||
template, template_with_content = __validate_template(form, service, SMS_TYPE)
|
||||
template, template_with_content = __validate_template(form, service, notification_type)
|
||||
|
||||
# Do not persist or send notification to the queue if it is a simulated recipient
|
||||
simulated = simulated_recipient(send_to, notification_type)
|
||||
notification = persist_notification(template_id=template.id,
|
||||
template_version=template.version,
|
||||
recipient=form['phone_number'],
|
||||
recipient=send_to,
|
||||
service=service,
|
||||
personalisation=form.get('personalisation', None),
|
||||
notification_type=SMS_TYPE,
|
||||
notification_type=notification_type,
|
||||
api_key_id=api_user.id,
|
||||
key_type=api_user.key_type,
|
||||
reference=form.get('reference'))
|
||||
send_notification_to_queue(notification, service.research_mode)
|
||||
sms_sender = service.sms_sender if service.sms_sender else current_app.config.get('FROM_NUMBER')
|
||||
resp = create_post_sms_response_from_notification(notification=notification,
|
||||
body=str(template_with_content),
|
||||
from_number=sms_sender,
|
||||
url_root=request.url_root,
|
||||
service_id=service.id)
|
||||
return jsonify(resp), 201
|
||||
|
||||
|
||||
@notification_blueprint.route('/email', methods=['POST'])
|
||||
def post_email_notification():
|
||||
form = validate(request.get_json(), post_email_request)
|
||||
service = services_dao.dao_fetch_service_by_id(api_user.service_id)
|
||||
|
||||
check_service_message_limit(api_user.key_type, service)
|
||||
service_can_send_to_recipient(form['email_address'], api_user.key_type, service)
|
||||
|
||||
template, template_with_content = __validate_template(form, service, EMAIL_TYPE)
|
||||
notification = persist_notification(template_id=template.id,
|
||||
template_version=template.version,
|
||||
recipient=form['email_address'],
|
||||
service=service,
|
||||
personalisation=form.get('personalisation', None),
|
||||
notification_type=EMAIL_TYPE,
|
||||
api_key_id=api_user.id,
|
||||
key_type=api_user.key_type,
|
||||
reference=form.get('reference'))
|
||||
|
||||
send_notification_to_queue(notification, service.research_mode)
|
||||
|
||||
resp = create_post_email_response_from_notification(notification=notification,
|
||||
content=str(template_with_content),
|
||||
subject=template_with_content.subject,
|
||||
email_from=service.email_from,
|
||||
url_root=request.url_root,
|
||||
service_id=service.id)
|
||||
reference=form.get('reference', None),
|
||||
simulated=simulated)
|
||||
if not simulated:
|
||||
queue_name = 'notify' if template.process_type == PRIORITY else None
|
||||
send_notification_to_queue(notification=notification, research_mode=service.research_mode, queue=queue_name)
|
||||
else:
|
||||
current_app.logger.info("POST simulated notification for id: {}".format(notification.id))
|
||||
if notification_type == SMS_TYPE:
|
||||
sms_sender = service.sms_sender if service.sms_sender else current_app.config.get('FROM_NUMBER')
|
||||
resp = create_post_sms_response_from_notification(notification=notification,
|
||||
body=str(template_with_content),
|
||||
from_number=sms_sender,
|
||||
url_root=request.url_root,
|
||||
service_id=service.id)
|
||||
else:
|
||||
resp = create_post_email_response_from_notification(notification=notification,
|
||||
content=str(template_with_content),
|
||||
subject=template_with_content.subject,
|
||||
email_from=service.email_from,
|
||||
url_root=request.url_root,
|
||||
service_id=service.id)
|
||||
return jsonify(resp), 201
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user