mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-17 18:52:30 -05:00
Refactor to abstract out post notification errors
This commit is contained in:
@@ -16,6 +16,7 @@ from app.service.utils import service_allowed_to_send_to
|
||||
from app.v2.errors import TooManyRequestsError, BadRequestError, RateLimitError
|
||||
from app import redis_store
|
||||
from app.notifications.process_notifications import create_content_for_notification
|
||||
from app.utils import get_public_notify_type_text
|
||||
|
||||
|
||||
def check_service_over_api_rate_limit(service, api_key):
|
||||
@@ -77,6 +78,18 @@ def service_has_permission(notify_type, permissions):
|
||||
return notify_type in [p.permission for p in permissions]
|
||||
|
||||
|
||||
def check_service_has_permission(notify_type, permissions):
|
||||
if not service_has_permission(notify_type, permissions):
|
||||
raise BadRequestError(message="Cannot send {}".format(
|
||||
get_public_notify_type_text(notify_type, plural=True)))
|
||||
|
||||
|
||||
def check_service_can_schedule_notification(permissions, scheduled_for):
|
||||
if scheduled_for:
|
||||
if not service_has_permission(SCHEDULE_NOTIFICATIONS, permissions):
|
||||
raise BadRequestError(message="Cannot schedule notifications (this feature is invite-only)")
|
||||
|
||||
|
||||
def validate_and_format_recipient(send_to, key_type, service, notification_type):
|
||||
service_can_send_to_recipient(send_to, key_type, service)
|
||||
|
||||
|
||||
@@ -12,6 +12,8 @@ from app.notifications.validators import (
|
||||
validate_and_format_recipient,
|
||||
check_rate_limiting,
|
||||
service_has_permission,
|
||||
check_service_can_schedule_notification,
|
||||
check_service_has_permission,
|
||||
validate_template
|
||||
)
|
||||
from app.schema_validation import validate
|
||||
@@ -32,14 +34,10 @@ def post_notification(notification_type):
|
||||
else:
|
||||
form = validate(request.get_json(), post_sms_request)
|
||||
|
||||
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)))
|
||||
check_service_has_permission(notification_type, authenticated_service.permissions)
|
||||
|
||||
scheduled_for = form.get("scheduled_for", None)
|
||||
if scheduled_for:
|
||||
if not service_has_permission(SCHEDULE_NOTIFICATIONS, authenticated_service.permissions):
|
||||
raise BadRequestError(message="Cannot schedule notifications (this feature is invite-only)")
|
||||
check_service_can_schedule_notification(authenticated_service.permissions, scheduled_for)
|
||||
|
||||
check_rate_limiting(authenticated_service, api_user)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user