Merge branch 'master' into celery-spike

Conflicts:
	app/notifications/rest.py
This commit is contained in:
Martyn Inglis
2016-02-15 11:12:07 +00:00
6 changed files with 146 additions and 86 deletions

View File

@@ -10,11 +10,12 @@ def add_notification_to_queue(service_id, template_id, type_, notification):
).create_queue(QueueName="{}_{}".format(
current_app.config['NOTIFICATION_QUEUE_PREFIX'],
str(service_id)))
message_id = str(uuid.uuid4())
notification_id = str(uuid.uuid4())
serializer = URLSafeSerializer(current_app.config.get('SECRET_KEY'))
encrypted = serializer.dumps(notification, current_app.config.get('DANGEROUS_SALT'))
q.send_message(MessageBody=encrypted,
MessageAttributes={'type': {'StringValue': type_, 'DataType': 'String'},
'message_id': {'StringValue': message_id, 'DataType': 'String'},
'notification_id': {'StringValue': notification_id, 'DataType': 'String'},
'service_id': {'StringValue': str(service_id), 'DataType': 'String'},
'template_id': {'StringValue': str(template_id), 'DataType': 'String'}})
return notification_id

View File

@@ -39,9 +39,8 @@ def create_sms_notification():
return jsonify(result="error", message=errors), 400
send_sms.delay()
add_notification_to_queue(api_user['client'], notification['template'], 'sms', notification)
# TODO data to be returned
return jsonify({}), 204
notification_id = add_notification_to_queue(api_user['client'], notification['template'], 'sms', notification)
return jsonify({'notification_id': notification_id}), 201
@notifications.route('/email', methods=['POST'])
@@ -50,9 +49,8 @@ def create_email_notification():
notification, errors = email_notification_schema.load(resp_json)
if errors:
return jsonify(result="error", message=errors), 400
add_notification_to_queue(api_user['client'], "admin", 'email', notification)
# TODO data to be returned
return jsonify({}), 204
notification_id = add_notification_to_queue(api_user['client'], "admin", 'email', notification)
return jsonify({'notification_id': notification_id}), 201
@notifications.route('/sms/service/<service_id>', methods=['POST'])
@@ -76,6 +74,5 @@ def create_sms_for_service(service_id):
message = "Invalid template: id {} for service id: {}".format(template.id, service_id)
return jsonify(result="error", message=message), 400
add_notification_to_queue(service_id, template_id, 'sms', notification)
# TODO data to be returned
return jsonify({}), 204
notification_id = add_notification_to_queue(service_id, template_id, 'sms', notification)
return jsonify({'notification_id': notification_id}), 201

View File

@@ -1,4 +1,5 @@
import re
from flask import current_app
from flask_marshmallow.fields import fields
from . import ma
from . import models
@@ -102,12 +103,15 @@ class SmsTemplateNotificationSchema(SmsNotificationSchema):
@validates_schema
def validate_schema(self, data):
"""
Validate the to field is valid for this template
Validate the to field is valid for this notification
"""
from app import api_user
template_id = data.get('template', None)
template = models.Template.query.filter_by(id=template_id).first()
if template:
service = template.service
# Validate restricted service,
# restricted services can only send to one of its users.
if service.restricted:
valid = False
for usr in service.users:
@@ -116,6 +120,11 @@ class SmsTemplateNotificationSchema(SmsNotificationSchema):
break
if not valid:
raise ValidationError('Invalid phone number for restricted service', 'restricted')
# Assert the template is valid for the service which made the request.
service = api_user['client']
if (service != current_app.config.get('ADMIN_CLIENT_USER_NAME') and
template.service != models.Service.query.filter_by(id=service).first()):
raise ValidationError('Invalid template', 'restricted')
class SmsAdminNotificationSchema(SmsNotificationSchema):