Restored code to share sms creation logic between:

- sms for API calls
- sms for Jobs
This commit is contained in:
Martyn Inglis
2016-02-24 09:23:21 +00:00
parent 201c2d01ba
commit 0007c69972
2 changed files with 38 additions and 41 deletions

View File

@@ -45,37 +45,24 @@ def get_notifications(notification_id):
@notifications.route('/sms', methods=['POST'])
def create_sms_notification():
notification, errors = sms_template_notification_schema.load(request.get_json())
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=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': ['Invalid phone number for restricted service']}), 400
notification_id = create_notification_id()
send_sms.apply_async((
api_user['client'],
notification_id,
encryption.encrypt(notification)),
queue='sms')
return jsonify({'notification_id': notification_id}), 201
return base_create_sms_notification(expects_job=False)
@notifications.route('/sms/service/<service_id>', methods=['POST'])
def create_sms_for_service(service_id):
notification, errors = job_sms_template_notification_schema.load(request.get_json())
return base_create_sms_notification(service_id, expects_job=True)
def base_create_sms_notification(service_id=None, expects_job=False):
if not service_id:
service_id = api_user['client']
if expects_job:
schema = job_sms_template_notification_schema
else:
schema = sms_template_notification_schema
notification, errors = schema.load(request.get_json())
if errors:
return jsonify(result="error", message=errors), 400
@@ -85,14 +72,18 @@ def create_sms_for_service(service_id):
)
if not template:
return jsonify(result="error", message={'template': ['Template or service not found']}), 400
return jsonify(
result="error",
message={
'template': ['Template {} not found for service {}'.format(notification['template'], service_id)]
}
), 400
job_id = notification['job']
if expects_job:
job = jobs_dao.get_job(service_id, notification['job'])
job = jobs_dao.get_job(service_id, job_id)
if not job:
return jsonify(result="error", message={'job': ['Job not found']}), 400
if not job:
return jsonify(result="error", message={'job': ['Job {} not found'.format(notification['job'])]}), 400
service = services_dao.dao_fetch_service_by_id(service_id)