mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-15 17:52:26 -05:00
Working code and tests.
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
from flask import (
|
||||
Blueprint,
|
||||
jsonify,
|
||||
request
|
||||
request,
|
||||
current_app
|
||||
)
|
||||
|
||||
from app import notify_alpha_client
|
||||
from app import api_user
|
||||
from app.dao import (templates_dao, services_dao)
|
||||
import re
|
||||
|
||||
mobile_regex = re.compile("^\\+44[\\d]{10}$")
|
||||
@@ -21,19 +24,19 @@ def get_notifications(notification_id):
|
||||
def create_sms_notification():
|
||||
notification = request.get_json()['notification']
|
||||
errors = {}
|
||||
to_errors = validate_to(notification)
|
||||
message_errors = validate_message(notification)
|
||||
|
||||
if to_errors:
|
||||
to, to_errors = validate_to(notification, api_user['client'])
|
||||
print("create sms")
|
||||
print(notification)
|
||||
template, template_errors = validate_template(notification, api_user['client'])
|
||||
if to_errors['to']:
|
||||
errors.update(to_errors)
|
||||
if message_errors:
|
||||
errors.update(message_errors)
|
||||
|
||||
if template_errors['template']:
|
||||
errors.update(template_errors)
|
||||
if errors:
|
||||
return jsonify(result="error", message=errors), 400
|
||||
return jsonify(notify_alpha_client.send_sms(
|
||||
mobile_number=notification['to'],
|
||||
message=notification['message'])), 200
|
||||
mobile_number=to,
|
||||
message=template)), 200
|
||||
|
||||
|
||||
@notifications.route('/email', methods=['POST'])
|
||||
@@ -55,36 +58,45 @@ def create_email_notification():
|
||||
notification['subject']))
|
||||
|
||||
|
||||
def validate_to(json_body):
|
||||
errors = []
|
||||
|
||||
if 'to' not in json_body:
|
||||
errors.append('required')
|
||||
def validate_to(json_body, service_id):
|
||||
errors = {"to": []}
|
||||
mob = json_body.get('to', None)
|
||||
if not mob:
|
||||
errors['to'].append('Required data missing')
|
||||
else:
|
||||
if not mobile_regex.match(json_body['to']):
|
||||
errors.append('invalid phone number, must be of format +441234123123')
|
||||
if errors:
|
||||
return {
|
||||
"to": errors
|
||||
}
|
||||
return None
|
||||
if not mobile_regex.match(mob):
|
||||
errors['to'].append('invalid phone number, must be of format +441234123123')
|
||||
if service_id != current_app.config.get('ADMIN_CLIENT_USER_NAME'):
|
||||
service = services_dao.get_model_services(service_id=service_id)
|
||||
if service.restricted:
|
||||
valid = False
|
||||
for usr in service.users:
|
||||
if mob == usr.mobile_number:
|
||||
valid = True
|
||||
break
|
||||
if not valid:
|
||||
errors['to'].append('Invalid phone number for restricted service')
|
||||
return mob, errors
|
||||
|
||||
|
||||
def validate_message(json_body):
|
||||
errors = []
|
||||
|
||||
if 'message' not in json_body:
|
||||
errors.append('required')
|
||||
def validate_template(json_body, service_id):
|
||||
errors = {"template": []}
|
||||
template_id = json_body.get('template', None)
|
||||
content = ''
|
||||
if not template_id:
|
||||
errors['template'].append('Required data missing')
|
||||
else:
|
||||
message_length = len(json_body['message'])
|
||||
if message_length < 1 or message_length > 160:
|
||||
errors.append('Invalid length. [1 - 160]')
|
||||
|
||||
if errors:
|
||||
return {
|
||||
"message": errors
|
||||
}
|
||||
return None
|
||||
if service_id == current_app.config.get('ADMIN_CLIENT_USER_NAME'):
|
||||
content = json_body['template']
|
||||
else:
|
||||
try:
|
||||
template = templates_dao.get_model_templates(
|
||||
template_id=json_body['template'],
|
||||
service_id=service_id)
|
||||
content = template.content
|
||||
except:
|
||||
errors['template'].append("Unable to load template.")
|
||||
return content, errors
|
||||
|
||||
|
||||
def validate_required_and_something(json_body, field):
|
||||
|
||||
Reference in New Issue
Block a user