2016-11-01 10:33:34 +00:00
|
|
|
from flask import request, jsonify, current_app
|
2016-10-31 12:22:26 +00:00
|
|
|
from sqlalchemy.orm.exc import NoResultFound
|
|
|
|
|
|
2016-10-27 11:46:37 +01:00
|
|
|
from app import api_user
|
|
|
|
|
from app.dao import services_dao, templates_dao
|
|
|
|
|
from app.models import SMS_TYPE
|
2016-10-28 17:10:00 +01:00
|
|
|
from app.notifications.process_notifications import (create_content_for_notification,
|
|
|
|
|
persist_notification,
|
|
|
|
|
send_notification_to_queue)
|
2016-10-27 11:46:37 +01:00
|
|
|
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)
|
|
|
|
|
from app.schema_validation import validate
|
2016-10-31 12:22:26 +00:00
|
|
|
from app.v2.errors import BadRequestError
|
2016-10-25 18:04:03 +01:00
|
|
|
from app.v2.notifications import notification_blueprint
|
2016-10-27 17:34:54 +01:00
|
|
|
from app.v2.notifications.notification_schemas import (post_sms_request,
|
|
|
|
|
create_post_sms_response_from_notification)
|
2016-10-25 18:04:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@notification_blueprint.route('/sms', methods=['POST'])
|
|
|
|
|
def post_sms_notification():
|
2016-10-27 11:46:37 +01:00
|
|
|
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)
|
2016-10-28 17:10:00 +01:00
|
|
|
service_can_send_to_recipient(form['phone_number'], api_user.key_type, service)
|
2016-10-27 11:46:37 +01:00
|
|
|
|
2016-10-28 17:10:00 +01:00
|
|
|
template, content = __validate_template(form, service)
|
2016-10-27 11:46:37 +01:00
|
|
|
|
2016-10-27 17:34:54 +01:00
|
|
|
notification = persist_notification(template_id=template.id,
|
|
|
|
|
template_version=template.version,
|
|
|
|
|
recipient=form['phone_number'],
|
|
|
|
|
service_id=service.id,
|
|
|
|
|
personalisation=form.get('personalisation', None),
|
|
|
|
|
notification_type=SMS_TYPE,
|
|
|
|
|
api_key_id=api_user.id,
|
|
|
|
|
key_type=api_user.key_type)
|
|
|
|
|
send_notification_to_queue(notification, service.research_mode)
|
2016-11-01 10:33:34 +00:00
|
|
|
|
|
|
|
|
resp = create_post_sms_response_from_notification(notification, content, service.sms_sender, request.url_root)
|
2016-10-27 17:34:54 +01:00
|
|
|
return jsonify(resp), 201
|
2016-10-25 18:04:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@notification_blueprint.route('/email', methods=['POST'])
|
|
|
|
|
def post_email_notification():
|
|
|
|
|
# validate post form against post_email_request schema
|
|
|
|
|
# validate service
|
|
|
|
|
# validate template
|
|
|
|
|
# persist notification
|
|
|
|
|
# send notification to queue
|
|
|
|
|
# create content
|
|
|
|
|
# return post_email_response schema
|
|
|
|
|
pass
|
2016-10-28 17:10:00 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def __validate_template(form, service):
|
2016-10-31 12:22:26 +00:00
|
|
|
try:
|
|
|
|
|
template = templates_dao.dao_get_template_by_id_and_service_id(template_id=form['template_id'],
|
|
|
|
|
service_id=service.id)
|
|
|
|
|
except NoResultFound:
|
|
|
|
|
message = 'Template not found'
|
|
|
|
|
raise BadRequestError(message=message,
|
|
|
|
|
fields=[{'template': message}])
|
|
|
|
|
|
2016-10-28 17:10:00 +01:00
|
|
|
check_template_is_for_notification_type(SMS_TYPE, template.template_type)
|
|
|
|
|
check_template_is_active(template)
|
|
|
|
|
template_with_content = create_content_for_notification(template, form.get('personalisation', {}))
|
|
|
|
|
check_sms_content_char_count(template_with_content.replaced_content_count)
|
2016-11-01 10:33:34 +00:00
|
|
|
return template, template_with_content.content
|