Skip to content

Commit

Permalink
Merge branch 'main' into jskinne3-upgrade-and-protect-demo-modules
Browse files Browse the repository at this point in the history
  • Loading branch information
jskinne3 authored Jun 18, 2024
2 parents a580368 + 833ebfa commit 7929b75
Show file tree
Hide file tree
Showing 50 changed files with 169 additions and 4,993 deletions.
29 changes: 0 additions & 29 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ def create_app(application):
document_download_client.init_app(application)

register_blueprint(application)
register_v2_blueprints(application)

# avoid circular imports by importing this file later
from app.commands import setup_commands
Expand Down Expand Up @@ -252,34 +251,6 @@ def register_blueprint(application):
application.register_blueprint(upload_blueprint)


def register_v2_blueprints(application):
from app.authentication.auth import requires_auth
from app.v2.inbound_sms.get_inbound_sms import v2_inbound_sms_blueprint
from app.v2.notifications import ( # noqa
get_notifications,
post_notifications,
v2_notification_blueprint,
)
from app.v2.template import ( # noqa
get_template,
post_template,
v2_template_blueprint,
)
from app.v2.templates.get_templates import v2_templates_blueprint

v2_notification_blueprint.before_request(requires_auth)
application.register_blueprint(v2_notification_blueprint)

v2_templates_blueprint.before_request(requires_auth)
application.register_blueprint(v2_templates_blueprint)

v2_template_blueprint.before_request(requires_auth)
application.register_blueprint(v2_template_blueprint)

v2_inbound_sms_blueprint.before_request(requires_auth)
application.register_blueprint(v2_inbound_sms_blueprint)


def init_app(app):
@app.before_request
def record_request_details():
Expand Down
12 changes: 9 additions & 3 deletions app/celery/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
from app.dao.service_sms_sender_dao import dao_get_service_sms_senders_by_id
from app.dao.templates_dao import dao_get_template_by_id
from app.enums import JobStatus, KeyType, NotificationType
from app.errors import TotalRequestsError
from app.notifications.process_notifications import persist_notification
from app.notifications.validators import check_service_over_total_message_limit
from app.serialised_models import SerialisedService, SerialisedTemplate
from app.service.utils import service_allowed_to_send_to
from app.utils import DATETIME_FORMAT, hilite, scrub, utc_now
from app.v2.errors import TotalRequestsError
from notifications_utils.recipients import RecipientCSV


Expand Down Expand Up @@ -189,7 +189,11 @@ def save_sms(self, service_id, notification_id, encrypted_notification, sender_i
# Return False when trial mode services try sending notifications
# to non-team and non-simulated recipients.
if not service_allowed_to_send_to(notification["to"], service, KeyType.NORMAL):
current_app.logger.info(hilite(scrub(f"service not allowed to send to {notification['to']}, aborting")))
current_app.logger.info(
hilite(
scrub(f"service not allowed to send to {notification['to']}, aborting")
)
)
current_app.logger.debug(
"SMS {} failed as restricted service".format(notification_id)
)
Expand Down Expand Up @@ -220,7 +224,9 @@ def save_sms(self, service_id, notification_id, encrypted_notification, sender_i
)

# Kick off sns process in provider_tasks.py
current_app.logger.info(hilite(scrub(f"Going to deliver sms for recipient: {notification['to']}")))
current_app.logger.info(
hilite(scrub(f"Going to deliver sms for recipient: {notification['to']}"))
)
provider_tasks.deliver_sms.apply_async(
[str(saved_notification.id)], queue=QueueNames.SEND_SMS
)
Expand Down
43 changes: 43 additions & 0 deletions app/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from sqlalchemy.orm.exc import NoResultFound

from app.authentication.auth import AuthError
from app.enums import KeyType
from app.exceptions import ArchiveValidationError
from notifications_utils.recipients import InvalidEmailError

Expand Down Expand Up @@ -113,3 +114,45 @@ def internal_server_error(e):
e = getattr(e, "original_exception", e)
current_app.logger.exception(e)
return jsonify(result="error", message="Internal server error"), 500


class TooManyRequestsError(InvalidRequest):
status_code = 429
message_template = "Exceeded send limits ({}) for today"

def __init__(self, sending_limit):
self.message = self.message_template.format(sending_limit)


class TotalRequestsError(InvalidRequest):
status_code = 429
message_template = "Exceeded total application limits ({}) for today"

def __init__(self, sending_limit):
self.message = self.message_template.format(sending_limit)


class RateLimitError(InvalidRequest):
status_code = 429
message_template = (
"Exceeded rate limit for key type {} of {} requests per {} seconds"
)

def __init__(self, sending_limit, interval, key_type):
# normal keys are spoken of as "live" in the documentation
# so using this in the error messaging
if key_type == KeyType.NORMAL:
key_type = "live"

self.message = self.message_template.format(
key_type.upper(), sending_limit, interval
)


class BadRequestError(InvalidRequest):
message = "An error occurred"

def __init__(self, fields=None, message=None, status_code=400):
self.status_code = status_code
self.fields = fields or []
self.message = message if message else self.message
4 changes: 2 additions & 2 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1215,7 +1215,6 @@ class Template(TemplateBase):
)

def get_link(self):
# TODO: use "/v2/" route once available
return url_for(
"template.get_template_by_id_and_service_id",
service_id=self.service_id,
Expand Down Expand Up @@ -1284,8 +1283,9 @@ def template_redacted(cls):

def get_link(self):
return url_for(
"v2_template.get_template_by_id",
"template.get_template_by_id_and_service_id",
template_id=self.id,
service_id=self.service.id,
version=self.version,
_external=True,
)
Expand Down
8 changes: 6 additions & 2 deletions app/notifications/process_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
dao_delete_notifications_by_id,
)
from app.enums import KeyType, NotificationStatus, NotificationType
from app.errors import BadRequestError
from app.models import Notification
from app.utils import hilite, scrub, utc_now
from app.v2.errors import BadRequestError
from notifications_utils.recipients import (
format_email_address,
get_international_phone_info,
Expand Down Expand Up @@ -110,7 +110,11 @@ def persist_notification(
formatted_recipient = validate_and_format_phone_number(
recipient, international=True
)
current_app.logger.info(hilite(scrub(f"Persisting notification with recipient {formatted_recipient}")))
current_app.logger.info(
hilite(
scrub(f"Persisting notification with recipient {formatted_recipient}")
)
)
recipient_info = get_international_phone_info(formatted_recipient)
notification.normalised_to = formatted_recipient
notification.international = recipient_info.international
Expand Down
2 changes: 1 addition & 1 deletion app/notifications/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
from app.dao.service_email_reply_to_dao import dao_get_reply_to_by_id
from app.dao.service_sms_sender_dao import dao_get_service_sms_senders_by_id
from app.enums import KeyType, NotificationType, ServicePermissionType, TemplateType
from app.errors import BadRequestError, RateLimitError, TotalRequestsError
from app.models import ServicePermission
from app.notifications.process_notifications import create_content_for_notification
from app.serialised_models import SerialisedTemplate
from app.service.utils import service_allowed_to_send_to
from app.utils import get_public_notify_type_text
from app.v2.errors import BadRequestError, RateLimitError, TotalRequestsError
from notifications_utils import SMS_CHAR_COUNT_LIMIT
from notifications_utils.clients.redis import (
rate_limit_cache_key,
Expand Down
2 changes: 1 addition & 1 deletion app/service/send_notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
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.enums import KeyType, NotificationType, TemplateProcessType
from app.errors import BadRequestError
from app.notifications.process_notifications import (
persist_notification,
send_notification_to_queue,
Expand All @@ -16,7 +17,6 @@
validate_and_format_recipient,
validate_template,
)
from app.v2.errors import BadRequestError


def validate_created_by(service, created_by_id):
Expand Down
Empty file removed app/v2/__init__.py
Empty file.
125 changes: 0 additions & 125 deletions app/v2/errors.py

This file was deleted.

9 changes: 0 additions & 9 deletions app/v2/inbound_sms/__init__.py

This file was deleted.

46 changes: 0 additions & 46 deletions app/v2/inbound_sms/get_inbound_sms.py

This file was deleted.

Loading

0 comments on commit 7929b75

Please sign in to comment.