replace abort with raise InvalidRequest

it's slightly clearer that it will immediately raise an exception and
back out of the current function
This commit is contained in:
Leo Hemsted
2020-07-27 15:54:31 +01:00
parent 6302c74565
commit fb2910fb08

View File

@@ -1,8 +1,7 @@
from datetime import datetime from datetime import datetime
import iso8601 import iso8601
from flask import Blueprint, jsonify, request, current_app, abort from flask import Blueprint, jsonify, request, current_app
from app.config import QueueNames from app.config import QueueNames
from app.dao.templates_dao import dao_get_template_by_id_and_service_id from app.dao.templates_dao import dao_get_template_by_id_and_service_id
from app.dao.users_dao import get_user_by_id from app.dao.users_dao import get_user_by_id
@@ -13,7 +12,7 @@ from app.dao.broadcast_message_dao import (
dao_update_broadcast_message, dao_update_broadcast_message,
) )
from app.dao.services_dao import dao_fetch_service_by_id from app.dao.services_dao import dao_fetch_service_by_id
from app.errors import register_errors from app.errors import register_errors, InvalidRequest
from app.models import BroadcastMessage, BroadcastStatusType from app.models import BroadcastMessage, BroadcastStatusType
from app.celery.broadcast_message_tasks import send_broadcast_message from app.celery.broadcast_message_tasks import send_broadcast_message
from app.broadcast_message.broadcast_message_schema import ( from app.broadcast_message.broadcast_message_schema import (
@@ -39,23 +38,23 @@ def _parse_nullable_datetime(dt):
def _update_broadcast_message(broadcast_message, new_status, updating_user): def _update_broadcast_message(broadcast_message, new_status, updating_user):
if updating_user not in broadcast_message.service.users: if updating_user not in broadcast_message.service.users:
abort( raise InvalidRequest(
400, f'User {updating_user.id} cannot approve broadcast_message {broadcast_message.id} from other service',
f'User {updating_user.id} cannot approve broadcast_message {broadcast_message.id} from other service' status_code=400
) )
if new_status not in BroadcastStatusType.ALLOWED_STATUS_TRANSITIONS[broadcast_message.status]: if new_status not in BroadcastStatusType.ALLOWED_STATUS_TRANSITIONS[broadcast_message.status]:
abort( raise InvalidRequest(
400, f'Cannot move broadcast_message {broadcast_message.id} from {broadcast_message.status} to {new_status}',
f'Cannot move broadcast_message {broadcast_message.id} from {broadcast_message.status} to {new_status}' status_code=400
) )
if new_status == BroadcastStatusType.BROADCASTING: if new_status == BroadcastStatusType.BROADCASTING:
# TODO: Remove this platform admin shortcut when the feature goes live # TODO: Remove this platform admin shortcut when the feature goes live
if updating_user == broadcast_message.created_by and not updating_user.platform_admin: if updating_user == broadcast_message.created_by and not updating_user.platform_admin:
abort( raise InvalidRequest(
400, f'User {updating_user.id} cannot approve their own broadcast_message {broadcast_message.id}',
f'User {updating_user.id} cannot approve their own broadcast_message {broadcast_message.id}' status_code=400
) )
else: else:
broadcast_message.approved_at = datetime.utcnow() broadcast_message.approved_at = datetime.utcnow()
@@ -120,9 +119,9 @@ def update_broadcast_message(service_id, broadcast_message_id):
broadcast_message = dao_get_broadcast_message_by_id_and_service_id(broadcast_message_id, service_id) broadcast_message = dao_get_broadcast_message_by_id_and_service_id(broadcast_message_id, service_id)
if broadcast_message.status not in BroadcastStatusType.PRE_BROADCAST_STATUSES: if broadcast_message.status not in BroadcastStatusType.PRE_BROADCAST_STATUSES:
abort( raise InvalidRequest(
400, f'Cannot update broadcast_message {broadcast_message.id} while it has status {broadcast_message.status}',
f'Cannot update broadcast_message {broadcast_message.id} while it has status {broadcast_message.status}' status_code=400
) )
if 'personalisation' in data: if 'personalisation' in data: