Further refactoring

This commit is contained in:
Ken Tsang
2017-06-30 15:00:44 +01:00
committed by venusbb
parent 0b3277b8a4
commit 23618a186c
5 changed files with 27 additions and 30 deletions

View File

@@ -39,7 +39,7 @@ from app.schemas import (
day_schema
)
from app.service.utils import service_allowed_to_send_to
from app.utils import pagination_links, get_template_instance
from app.utils import pagination_links, get_template_instance, get_public_notify_type_text
from notifications_utils.recipients import get_international_phone_info
@@ -121,20 +121,14 @@ def send_notification(notification_type):
template_object = create_template_object_for_notification(template, notification_form.get('personalisation', {}))
_service_allowed_to_send_to(notification_form, authenticated_service)
if notification_type == SMS_TYPE:
if service_has_permission(SMS_TYPE, authenticated_service.permissions) is False:
raise InvalidRequest(
{'to': ["Cannot send text messages"]},
status_code=400
)
if not service_has_permission(notification_type, authenticated_service.permissions):
raise InvalidRequest(
{'service': ["Cannot send {}".format(get_public_notify_type_text(notification_type, plural=True))]},
status_code=400
)
if notification_type == SMS_TYPE:
_service_can_send_internationally(authenticated_service, notification_form['to'])
elif notification_type == EMAIL_TYPE:
if service_has_permission(EMAIL_TYPE, authenticated_service.permissions) is False:
raise InvalidRequest(
{'to': ["Cannot send emails"]},
status_code=400
)
# Do not persist or send notification to the queue if it is a simulated recipient
simulated = simulated_recipient(notification_form['to'], notification_type)

View File

@@ -45,10 +45,11 @@ def create_template(service_id):
permissions = fetched_service.permissions
new_template = template_schema.load(request.get_json()).data
if service_has_permission(new_template.template_type, permissions) is False:
raise InvalidRequest(
"Creating {} templates is not allowed".format(
get_public_notify_type_text(new_template.template_type)), 403)
if not service_has_permission(new_template.template_type, permissions):
message = "Creating {} templates is not allowed".format(
get_public_notify_type_text(new_template.template_type))
errors = {'template_type': [message]}
raise InvalidRequest(errors, 403)
new_template.service = fetched_service
over_limit = _content_count_greater_than_limit(new_template.content, new_template.template_type)
@@ -66,10 +67,12 @@ def create_template(service_id):
def update_template(service_id, template_id):
fetched_template = dao_get_template_by_id_and_service_id(template_id=template_id, service_id=service_id)
if service_has_permission(fetched_template.template_type, fetched_template.service.permissions) is False:
raise InvalidRequest(
"Updating {} templates is not allowed".format(
get_public_notify_type_text(fetched_template.template_type)), 403)
if not service_has_permission(fetched_template.template_type, fetched_template.service.permissions):
message = "Updating {} templates is not allowed".format(
get_public_notify_type_text(fetched_template.template_type))
errors = {'template_type': [message]}
raise InvalidRequest(errors, 403)
data = request.get_json()

View File

@@ -32,13 +32,13 @@ def post_notification(notification_type):
else:
form = validate(request.get_json(), post_sms_request)
if service_has_permission(notification_type, authenticated_service.permissions) is False:
if not service_has_permission(notification_type, authenticated_service.permissions):
raise BadRequestError(message="Cannot send {}".format(
get_public_notify_type_text(notification_type, plural=True)))
scheduled_for = form.get("scheduled_for", None)
if scheduled_for:
if service_has_permission(SCHEDULE_NOTIFICATIONS, authenticated_service.permissions) is False:
if not service_has_permission(SCHEDULE_NOTIFICATIONS, authenticated_service.permissions):
raise BadRequestError(message="Cannot schedule notifications (this feature is invite-only)")
check_rate_limiting(authenticated_service, api_user)