2016-02-08 14:54:15 +00:00
|
|
|
import uuid
|
|
|
|
|
|
2016-01-19 11:23:09 +00:00
|
|
|
from flask import (
|
|
|
|
|
Blueprint,
|
|
|
|
|
jsonify,
|
2016-02-22 17:17:29 +00:00
|
|
|
request,
|
|
|
|
|
current_app
|
2016-02-16 11:22:44 +00:00
|
|
|
)
|
2016-02-04 12:07:26 +00:00
|
|
|
|
2016-02-16 15:28:30 +00:00
|
|
|
from app import api_user, encryption
|
2016-02-22 17:17:29 +00:00
|
|
|
from app.dao import (
|
|
|
|
|
templates_dao,
|
|
|
|
|
services_dao,
|
2016-02-23 17:30:50 +00:00
|
|
|
notifications_dao,
|
|
|
|
|
jobs_dao
|
2016-02-22 17:17:29 +00:00
|
|
|
)
|
2016-02-03 13:16:19 +00:00
|
|
|
from app.schemas import (
|
2016-02-16 11:22:44 +00:00
|
|
|
email_notification_schema,
|
|
|
|
|
sms_template_notification_schema,
|
2016-02-23 17:30:50 +00:00
|
|
|
notification_status_schema,
|
|
|
|
|
job_sms_template_notification_schema
|
2016-02-16 11:22:44 +00:00
|
|
|
)
|
2016-02-22 17:17:29 +00:00
|
|
|
from app.celery.tasks import send_sms, send_email
|
2016-02-16 11:22:44 +00:00
|
|
|
from sqlalchemy.orm.exc import NoResultFound
|
2016-01-19 11:23:09 +00:00
|
|
|
|
|
|
|
|
notifications = Blueprint('notifications', __name__)
|
|
|
|
|
|
2016-02-17 17:04:50 +00:00
|
|
|
from app.errors import register_errors
|
2016-02-22 17:17:29 +00:00
|
|
|
|
2016-02-17 17:04:50 +00:00
|
|
|
register_errors(notifications)
|
|
|
|
|
|
2016-01-19 11:23:09 +00:00
|
|
|
|
2016-02-15 16:01:14 +00:00
|
|
|
def create_notification_id():
|
|
|
|
|
return str(uuid.uuid4())
|
|
|
|
|
|
|
|
|
|
|
2016-02-16 11:22:44 +00:00
|
|
|
@notifications.route('/<string:notification_id>', methods=['GET'])
|
2016-01-19 13:45:57 +00:00
|
|
|
def get_notifications(notification_id):
|
2016-02-16 11:22:44 +00:00
|
|
|
try:
|
|
|
|
|
notification = notifications_dao.get_notification(api_user['client'], notification_id)
|
|
|
|
|
return jsonify({'notification': notification_status_schema.dump(notification).data}), 200
|
|
|
|
|
except NoResultFound:
|
|
|
|
|
return jsonify(result="error", message="not found"), 404
|
2016-01-19 11:23:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@notifications.route('/sms', methods=['POST'])
|
|
|
|
|
def create_sms_notification():
|
2016-02-24 09:23:21 +00:00
|
|
|
return base_create_sms_notification(expects_job=False)
|
2016-01-27 17:42:05 +00:00
|
|
|
|
2016-02-23 12:35:28 +00:00
|
|
|
|
2016-02-24 09:23:21 +00:00
|
|
|
@notifications.route('/sms/service/<service_id>', methods=['POST'])
|
|
|
|
|
def create_sms_for_service(service_id):
|
|
|
|
|
return base_create_sms_notification(service_id, expects_job=True)
|
2016-02-16 14:06:56 +00:00
|
|
|
|
2016-02-22 17:17:29 +00:00
|
|
|
|
2016-02-24 09:23:21 +00:00
|
|
|
def base_create_sms_notification(service_id=None, expects_job=False):
|
|
|
|
|
if not service_id:
|
|
|
|
|
service_id = api_user['client']
|
2016-02-22 17:17:29 +00:00
|
|
|
|
2016-02-24 09:23:21 +00:00
|
|
|
if expects_job:
|
|
|
|
|
schema = job_sms_template_notification_schema
|
|
|
|
|
else:
|
|
|
|
|
schema = sms_template_notification_schema
|
2016-02-15 16:01:14 +00:00
|
|
|
|
2016-02-24 09:23:21 +00:00
|
|
|
notification, errors = schema.load(request.get_json())
|
2016-02-23 17:30:50 +00:00
|
|
|
if errors:
|
|
|
|
|
return jsonify(result="error", message=errors), 400
|
|
|
|
|
|
|
|
|
|
template = templates_dao.dao_get_template_by_id_and_service_id(
|
|
|
|
|
template_id=notification['template'],
|
|
|
|
|
service_id=service_id
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if not template:
|
2016-02-24 09:23:21 +00:00
|
|
|
return jsonify(
|
|
|
|
|
result="error",
|
|
|
|
|
message={
|
|
|
|
|
'template': ['Template {} not found for service {}'.format(notification['template'], service_id)]
|
|
|
|
|
}
|
|
|
|
|
), 400
|
|
|
|
|
|
|
|
|
|
if expects_job:
|
|
|
|
|
job = jobs_dao.get_job(service_id, notification['job'])
|
|
|
|
|
|
|
|
|
|
if not job:
|
|
|
|
|
return jsonify(result="error", message={'job': ['Job {} not found'.format(notification['job'])]}), 400
|
2016-02-23 17:30:50 +00:00
|
|
|
|
|
|
|
|
service = services_dao.dao_fetch_service_by_id(service_id)
|
|
|
|
|
|
|
|
|
|
if service.restricted:
|
|
|
|
|
if notification['to'] not in [user.email_address for user in service.users]:
|
|
|
|
|
return jsonify(result="error", message={'to': ['Invalid phone number for restricted service']}), 400
|
|
|
|
|
|
|
|
|
|
notification_id = create_notification_id()
|
|
|
|
|
|
|
|
|
|
send_sms.apply_async((
|
|
|
|
|
api_user['client'],
|
|
|
|
|
notification_id,
|
2016-02-23 17:39:08 +00:00
|
|
|
encryption.encrypt(notification)),
|
2016-02-23 17:30:50 +00:00
|
|
|
queue='sms')
|
|
|
|
|
return jsonify({'notification_id': notification_id}), 201
|
|
|
|
|
|
|
|
|
|
|
2016-01-19 11:23:09 +00:00
|
|
|
@notifications.route('/email', methods=['POST'])
|
|
|
|
|
def create_email_notification():
|
2016-02-22 17:17:29 +00:00
|
|
|
notification, errors = email_notification_schema.load(request.get_json())
|
2016-01-20 13:14:23 +00:00
|
|
|
if errors:
|
|
|
|
|
return jsonify(result="error", message=errors), 400
|
2016-02-22 17:17:29 +00:00
|
|
|
|
|
|
|
|
template = templates_dao.dao_get_template_by_id_and_service_id(
|
|
|
|
|
template_id=notification['template'],
|
|
|
|
|
service_id=api_user['client']
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if not template:
|
|
|
|
|
return jsonify(result="error", message={'template': ['Template not found']}), 400
|
|
|
|
|
|
|
|
|
|
service = services_dao.dao_fetch_service_by_id(api_user['client'])
|
|
|
|
|
|
|
|
|
|
if service.restricted:
|
|
|
|
|
if notification['to'] not in [user.email_address for user in service.users]:
|
|
|
|
|
return jsonify(result="error", message={'to': ['Email address not permitted for restricted service']}), 400
|
|
|
|
|
|
|
|
|
|
notification_id = create_notification_id()
|
|
|
|
|
|
|
|
|
|
send_email.apply_async((
|
|
|
|
|
api_user['client'],
|
|
|
|
|
notification_id,
|
|
|
|
|
template.subject,
|
|
|
|
|
"{}@{}".format(service.email_from, current_app.config['NOTIFY_EMAIL_DOMAIN']),
|
|
|
|
|
encryption.encrypt(notification)),
|
|
|
|
|
queue='email')
|
2016-02-10 11:15:41 +00:00
|
|
|
return jsonify({'notification_id': notification_id}), 201
|