mirror of
https://github.com/GSA/notifications-api.git
synced 2026-09-09 17:09:49 -04:00
Remove all DAO get service calls, replaced with reference to new service object in context.
This commit is contained in:
@@ -5,20 +5,23 @@ from flask import (
|
|||||||
current_app
|
current_app
|
||||||
)
|
)
|
||||||
|
|
||||||
from app import api_user
|
from app import api_user, authenticated_service
|
||||||
from app.dao import (
|
from app.dao import (
|
||||||
templates_dao,
|
templates_dao,
|
||||||
services_dao,
|
|
||||||
notifications_dao
|
notifications_dao
|
||||||
)
|
)
|
||||||
from app.models import KEY_TYPE_TEAM, PRIORITY
|
from app.models import KEY_TYPE_TEAM, PRIORITY
|
||||||
from app.models import SMS_TYPE
|
from app.models import SMS_TYPE
|
||||||
from app.notifications.process_notifications import (persist_notification,
|
from app.notifications.process_notifications import (
|
||||||
send_notification_to_queue,
|
persist_notification,
|
||||||
simulated_recipient)
|
send_notification_to_queue,
|
||||||
from app.notifications.validators import (check_service_over_daily_message_limit,
|
simulated_recipient
|
||||||
check_template_is_for_notification_type,
|
)
|
||||||
check_template_is_active, check_rate_limiting)
|
from app.notifications.validators import (
|
||||||
|
check_template_is_for_notification_type,
|
||||||
|
check_template_is_active,
|
||||||
|
check_rate_limiting
|
||||||
|
)
|
||||||
from app.schemas import (
|
from app.schemas import (
|
||||||
email_notification_schema,
|
email_notification_schema,
|
||||||
sms_template_notification_schema,
|
sms_template_notification_schema,
|
||||||
@@ -45,9 +48,10 @@ register_errors(notifications)
|
|||||||
|
|
||||||
@notifications.route('/notifications/<uuid:notification_id>', methods=['GET'])
|
@notifications.route('/notifications/<uuid:notification_id>', methods=['GET'])
|
||||||
def get_notification_by_id(notification_id):
|
def get_notification_by_id(notification_id):
|
||||||
notification = notifications_dao.get_notification_with_personalisation(str(api_user.service_id),
|
notification = notifications_dao.get_notification_with_personalisation(
|
||||||
notification_id,
|
str(authenticated_service.id),
|
||||||
key_type=None)
|
notification_id,
|
||||||
|
key_type=None)
|
||||||
return jsonify(data={"notification": notification_with_personalisation_schema.dump(notification).data}), 200
|
return jsonify(data={"notification": notification_with_personalisation_schema.dump(notification).data}), 200
|
||||||
|
|
||||||
|
|
||||||
@@ -60,7 +64,7 @@ def get_all_notifications():
|
|||||||
limit_days = data.get('limit_days')
|
limit_days = data.get('limit_days')
|
||||||
|
|
||||||
pagination = notifications_dao.get_notifications_for_service(
|
pagination = notifications_dao.get_notifications_for_service(
|
||||||
str(api_user.service_id),
|
str(authenticated_service.id),
|
||||||
personalisation=True,
|
personalisation=True,
|
||||||
filter_dict=data,
|
filter_dict=data,
|
||||||
page=page,
|
page=page,
|
||||||
@@ -96,8 +100,6 @@ def send_notification(notification_type):
|
|||||||
if notification_type not in ['sms', 'email']:
|
if notification_type not in ['sms', 'email']:
|
||||||
assert False
|
assert False
|
||||||
|
|
||||||
service = services_dao.dao_fetch_service_by_id(api_user.service_id)
|
|
||||||
|
|
||||||
notification_form, errors = (
|
notification_form, errors = (
|
||||||
sms_template_notification_schema if notification_type == SMS_TYPE else email_notification_schema
|
sms_template_notification_schema if notification_type == SMS_TYPE else email_notification_schema
|
||||||
).load(request.get_json())
|
).load(request.get_json())
|
||||||
@@ -105,27 +107,27 @@ def send_notification(notification_type):
|
|||||||
if errors:
|
if errors:
|
||||||
raise InvalidRequest(errors, status_code=400)
|
raise InvalidRequest(errors, status_code=400)
|
||||||
|
|
||||||
check_rate_limiting(service, api_user)
|
check_rate_limiting(authenticated_service, api_user)
|
||||||
|
|
||||||
template = templates_dao.dao_get_template_by_id_and_service_id(
|
template = templates_dao.dao_get_template_by_id_and_service_id(
|
||||||
template_id=notification_form['template'],
|
template_id=notification_form['template'],
|
||||||
service_id=service.id)
|
service_id=authenticated_service.id)
|
||||||
|
|
||||||
check_template_is_for_notification_type(notification_type, template.template_type)
|
check_template_is_for_notification_type(notification_type, template.template_type)
|
||||||
check_template_is_active(template)
|
check_template_is_active(template)
|
||||||
|
|
||||||
template_object = create_template_object_for_notification(template, notification_form.get('personalisation', {}))
|
template_object = create_template_object_for_notification(template, notification_form.get('personalisation', {}))
|
||||||
|
|
||||||
_service_allowed_to_send_to(notification_form, service)
|
_service_allowed_to_send_to(notification_form, authenticated_service)
|
||||||
if notification_type == SMS_TYPE:
|
if notification_type == SMS_TYPE:
|
||||||
_service_can_send_internationally(service, notification_form['to'])
|
_service_can_send_internationally(authenticated_service, notification_form['to'])
|
||||||
|
|
||||||
# Do not persist or send notification to the queue if it is a simulated recipient
|
# Do not persist or send notification to the queue if it is a simulated recipient
|
||||||
simulated = simulated_recipient(notification_form['to'], notification_type)
|
simulated = simulated_recipient(notification_form['to'], notification_type)
|
||||||
notification_model = persist_notification(template_id=template.id,
|
notification_model = persist_notification(template_id=template.id,
|
||||||
template_version=template.version,
|
template_version=template.version,
|
||||||
recipient=request.get_json()['to'],
|
recipient=request.get_json()['to'],
|
||||||
service=service,
|
service=authenticated_service,
|
||||||
personalisation=notification_form.get('personalisation', None),
|
personalisation=notification_form.get('personalisation', None),
|
||||||
notification_type=notification_type,
|
notification_type=notification_type,
|
||||||
api_key_id=api_user.id,
|
api_key_id=api_user.id,
|
||||||
@@ -134,7 +136,7 @@ def send_notification(notification_type):
|
|||||||
if not simulated:
|
if not simulated:
|
||||||
queue_name = 'priority' if template.process_type == PRIORITY else None
|
queue_name = 'priority' if template.process_type == PRIORITY else None
|
||||||
send_notification_to_queue(notification=notification_model,
|
send_notification_to_queue(notification=notification_model,
|
||||||
research_mode=service.research_mode,
|
research_mode=authenticated_service.research_mode,
|
||||||
queue=queue_name)
|
queue=queue_name)
|
||||||
else:
|
else:
|
||||||
current_app.logger.info("POST simulated notification for id: {}".format(notification_model.id))
|
current_app.logger.info("POST simulated notification for id: {}".format(notification_model.id))
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import uuid
|
|||||||
from flask import jsonify, request, url_for, current_app
|
from flask import jsonify, request, url_for, current_app
|
||||||
from werkzeug.exceptions import abort
|
from werkzeug.exceptions import abort
|
||||||
|
|
||||||
from app import api_user
|
from app import api_user, authenticated_service
|
||||||
from app.dao import notifications_dao
|
from app.dao import notifications_dao
|
||||||
from app.schema_validation import validate
|
from app.schema_validation import validate
|
||||||
from app.v2.notifications import v2_notification_blueprint
|
from app.v2.notifications import v2_notification_blueprint
|
||||||
@@ -17,7 +17,7 @@ def get_notification_by_id(id):
|
|||||||
except ValueError or AttributeError:
|
except ValueError or AttributeError:
|
||||||
abort(404)
|
abort(404)
|
||||||
notification = notifications_dao.get_notification_with_personalisation(
|
notification = notifications_dao.get_notification_with_personalisation(
|
||||||
api_user.service_id, casted_id, key_type=None
|
authenticated_service.id, casted_id, key_type=None
|
||||||
)
|
)
|
||||||
|
|
||||||
return jsonify(notification.serialize()), 200
|
return jsonify(notification.serialize()), 200
|
||||||
@@ -38,7 +38,7 @@ def get_notifications():
|
|||||||
data = validate(_data, get_notifications_request)
|
data = validate(_data, get_notifications_request)
|
||||||
|
|
||||||
paginated_notifications = notifications_dao.get_notifications_for_service(
|
paginated_notifications = notifications_dao.get_notifications_for_service(
|
||||||
str(api_user.service_id),
|
str(authenticated_service.id),
|
||||||
filter_dict=data,
|
filter_dict=data,
|
||||||
key_type=api_user.key_type,
|
key_type=api_user.key_type,
|
||||||
personalisation=True,
|
personalisation=True,
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
from flask import request, jsonify, current_app
|
from flask import request, jsonify, current_app
|
||||||
from sqlalchemy.orm.exc import NoResultFound
|
from sqlalchemy.orm.exc import NoResultFound
|
||||||
|
|
||||||
from app import api_user
|
from app import api_user, authenticated_service
|
||||||
from app.dao import services_dao, templates_dao
|
from app.dao import services_dao, templates_dao
|
||||||
from app.models import SMS_TYPE, EMAIL_TYPE, PRIORITY
|
from app.models import SMS_TYPE, EMAIL_TYPE, PRIORITY
|
||||||
from app.notifications.process_notifications import (
|
from app.notifications.process_notifications import (
|
||||||
@@ -32,17 +32,15 @@ def post_notification(notification_type):
|
|||||||
else:
|
else:
|
||||||
form = validate(request.get_json(), post_sms_request)
|
form = validate(request.get_json(), post_sms_request)
|
||||||
|
|
||||||
service = services_dao.dao_fetch_service_by_id(api_user.service_id)
|
check_rate_limiting(authenticated_service, api_user)
|
||||||
|
|
||||||
check_rate_limiting(service, api_user)
|
|
||||||
|
|
||||||
form_send_to = form['phone_number'] if notification_type == SMS_TYPE else form['email_address']
|
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,
|
send_to = validate_and_format_recipient(send_to=form_send_to,
|
||||||
key_type=api_user.key_type,
|
key_type=api_user.key_type,
|
||||||
service=service,
|
service=authenticated_service,
|
||||||
notification_type=notification_type)
|
notification_type=notification_type)
|
||||||
|
|
||||||
template, template_with_content = __validate_template(form, service, notification_type)
|
template, template_with_content = __validate_template(form, authenticated_service, notification_type)
|
||||||
|
|
||||||
# Do not persist or send notification to the queue if it is a simulated recipient
|
# Do not persist or send notification to the queue if it is a simulated recipient
|
||||||
simulated = simulated_recipient(send_to, notification_type)
|
simulated = simulated_recipient(send_to, notification_type)
|
||||||
@@ -50,7 +48,7 @@ def post_notification(notification_type):
|
|||||||
notification = persist_notification(template_id=template.id,
|
notification = persist_notification(template_id=template.id,
|
||||||
template_version=template.version,
|
template_version=template.version,
|
||||||
recipient=form_send_to,
|
recipient=form_send_to,
|
||||||
service=service,
|
service=authenticated_service,
|
||||||
personalisation=form.get('personalisation', None),
|
personalisation=form.get('personalisation', None),
|
||||||
notification_type=notification_type,
|
notification_type=notification_type,
|
||||||
api_key_id=api_user.id,
|
api_key_id=api_user.id,
|
||||||
@@ -60,23 +58,32 @@ def post_notification(notification_type):
|
|||||||
|
|
||||||
if not simulated:
|
if not simulated:
|
||||||
queue_name = 'priority' if template.process_type == PRIORITY else None
|
queue_name = 'priority' if template.process_type == PRIORITY else None
|
||||||
send_notification_to_queue(notification=notification, research_mode=service.research_mode, queue=queue_name)
|
send_notification_to_queue(
|
||||||
|
notification=notification,
|
||||||
|
research_mode=authenticated_service.research_mode,
|
||||||
|
queue=queue_name
|
||||||
|
)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
current_app.logger.info("POST simulated notification for id: {}".format(notification.id))
|
current_app.logger.info("POST simulated notification for id: {}".format(notification.id))
|
||||||
if notification_type == SMS_TYPE:
|
if notification_type == SMS_TYPE:
|
||||||
sms_sender = service.sms_sender if service.sms_sender else current_app.config.get('FROM_NUMBER')
|
sms_sender = (authenticated_service.sms_sender
|
||||||
|
if authenticated_service.sms_sender
|
||||||
|
else current_app.config.get('FROM_NUMBER')
|
||||||
|
)
|
||||||
resp = create_post_sms_response_from_notification(notification=notification,
|
resp = create_post_sms_response_from_notification(notification=notification,
|
||||||
body=str(template_with_content),
|
body=str(template_with_content),
|
||||||
from_number=sms_sender,
|
from_number=sms_sender,
|
||||||
url_root=request.url_root,
|
url_root=request.url_root,
|
||||||
service_id=service.id)
|
service_id=authenticated_service.id)
|
||||||
else:
|
else:
|
||||||
resp = create_post_email_response_from_notification(notification=notification,
|
resp = create_post_email_response_from_notification(notification=notification,
|
||||||
content=str(template_with_content),
|
content=str(template_with_content),
|
||||||
subject=template_with_content.subject,
|
subject=template_with_content.subject,
|
||||||
email_from=service.email_from,
|
email_from=authenticated_service.email_from,
|
||||||
url_root=request.url_root,
|
url_root=request.url_root,
|
||||||
service_id=service.id)
|
service_id=authenticated_service.id)
|
||||||
|
|
||||||
return jsonify(resp), 201
|
return jsonify(resp), 201
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
from flask import jsonify
|
from flask import jsonify
|
||||||
from jsonschema.exceptions import ValidationError
|
|
||||||
|
|
||||||
from app import api_user
|
from app import authenticated_service
|
||||||
from app.dao import templates_dao
|
from app.dao import templates_dao
|
||||||
from app.schema_validation import validate
|
from app.schema_validation import validate
|
||||||
from app.v2.template import v2_template_blueprint
|
from app.v2.template import v2_template_blueprint
|
||||||
@@ -18,6 +17,6 @@ def get_template_by_id(template_id, version=None):
|
|||||||
data = validate(_data, get_template_by_id_request)
|
data = validate(_data, get_template_by_id_request)
|
||||||
|
|
||||||
template = templates_dao.dao_get_template_by_id_and_service_id(
|
template = templates_dao.dao_get_template_by_id_and_service_id(
|
||||||
template_id, api_user.service_id, data.get('version'))
|
template_id, authenticated_service.id, data.get('version'))
|
||||||
|
|
||||||
return jsonify(template.serialize()), 200
|
return jsonify(template.serialize()), 200
|
||||||
|
|||||||
@@ -1,9 +1,7 @@
|
|||||||
from flask import jsonify, request
|
from flask import jsonify, request
|
||||||
from jsonschema.exceptions import ValidationError
|
|
||||||
|
|
||||||
from app import api_user
|
from app import authenticated_service
|
||||||
from app.dao import templates_dao
|
from app.dao import templates_dao
|
||||||
from app.models import SMS_TYPE
|
|
||||||
from app.schema_validation import validate
|
from app.schema_validation import validate
|
||||||
from app.utils import get_template_instance
|
from app.utils import get_template_instance
|
||||||
from app.v2.errors import BadRequestError
|
from app.v2.errors import BadRequestError
|
||||||
@@ -22,7 +20,7 @@ def post_template_preview(template_id):
|
|||||||
data = validate(_data, post_template_preview_request)
|
data = validate(_data, post_template_preview_request)
|
||||||
|
|
||||||
template = templates_dao.dao_get_template_by_id_and_service_id(
|
template = templates_dao.dao_get_template_by_id_and_service_id(
|
||||||
template_id, api_user.service_id)
|
template_id, authenticated_service.id)
|
||||||
|
|
||||||
template_object = get_template_instance(
|
template_object = get_template_instance(
|
||||||
template.__dict__, values=data.get('personalisation'))
|
template.__dict__, values=data.get('personalisation'))
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
from flask import jsonify, request
|
from flask import jsonify, request
|
||||||
from jsonschema.exceptions import ValidationError
|
|
||||||
|
|
||||||
from app import api_user
|
from app import authenticated_service
|
||||||
from app.dao import templates_dao
|
from app.dao import templates_dao
|
||||||
from app.schema_validation import validate
|
from app.schema_validation import validate
|
||||||
from app.v2.templates import v2_templates_blueprint
|
from app.v2.templates import v2_templates_blueprint
|
||||||
@@ -12,7 +11,7 @@ from app.v2.templates.templates_schemas import get_all_template_request
|
|||||||
def get_templates():
|
def get_templates():
|
||||||
data = validate(request.args.to_dict(), get_all_template_request)
|
data = validate(request.args.to_dict(), get_all_template_request)
|
||||||
|
|
||||||
templates = templates_dao.dao_get_all_templates_for_service(api_user.service_id, data.get('type'))
|
templates = templates_dao.dao_get_all_templates_for_service(authenticated_service.id, data.get('type'))
|
||||||
|
|
||||||
return jsonify(
|
return jsonify(
|
||||||
templates=[template.serialize() for template in templates]
|
templates=[template.serialize() for template in templates]
|
||||||
|
|||||||
Reference in New Issue
Block a user