2016-10-27 11:46:37 +01:00
|
|
|
|
from flask import current_app
|
2017-01-17 12:08:24 +00:00
|
|
|
|
from notifications_utils.recipients import validate_and_format_phone_number, validate_and_format_email_address
|
2016-10-27 11:46:37 +01:00
|
|
|
|
|
2016-10-25 18:04:03 +01:00
|
|
|
|
from app.dao import services_dao
|
2017-01-17 12:08:24 +00:00
|
|
|
|
from app.models import KEY_TYPE_TEST, KEY_TYPE_TEAM, SMS_TYPE
|
2016-10-27 11:46:37 +01:00
|
|
|
|
from app.service.utils import service_allowed_to_send_to
|
|
|
|
|
|
from app.v2.errors import TooManyRequestsError, BadRequestError
|
2016-11-11 16:47:52 +00:00
|
|
|
|
from app import redis_store
|
2016-12-01 17:20:05 +00:00
|
|
|
|
from notifications_utils.clients import redis
|
2016-10-25 18:04:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def check_service_message_limit(key_type, service):
|
2016-11-11 16:47:52 +00:00
|
|
|
|
if key_type != KEY_TYPE_TEST:
|
2016-11-22 12:53:20 +00:00
|
|
|
|
cache_key = redis.daily_limit_cache_key(service.id)
|
2016-11-11 16:47:52 +00:00
|
|
|
|
service_stats = redis_store.get(cache_key)
|
|
|
|
|
|
if not service_stats:
|
|
|
|
|
|
service_stats = services_dao.fetch_todays_total_message_count(service.id)
|
|
|
|
|
|
redis_store.set(cache_key, service_stats, ex=3600)
|
2016-11-12 15:37:57 +00:00
|
|
|
|
if int(service_stats) >= service.message_limit:
|
2016-10-27 11:46:37 +01:00
|
|
|
|
raise TooManyRequestsError(service.message_limit)
|
2016-10-25 18:04:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def check_template_is_for_notification_type(notification_type, template_type):
|
|
|
|
|
|
if notification_type != template_type:
|
2016-10-31 12:22:26 +00:00
|
|
|
|
message = "{0} template is not suitable for {1} notification".format(template_type,
|
|
|
|
|
|
notification_type)
|
|
|
|
|
|
raise BadRequestError(fields=[{'template': message}], message=message)
|
2016-10-25 18:04:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def check_template_is_active(template):
|
|
|
|
|
|
if template.archived:
|
2016-10-31 12:22:26 +00:00
|
|
|
|
raise BadRequestError(fields=[{'template': 'Template has been deleted'}],
|
|
|
|
|
|
message="Template has been deleted")
|
2016-10-27 11:46:37 +01:00
|
|
|
|
|
|
|
|
|
|
|
2016-10-28 17:10:00 +01:00
|
|
|
|
def service_can_send_to_recipient(send_to, key_type, service):
|
2016-10-27 11:46:37 +01:00
|
|
|
|
if not service_allowed_to_send_to(send_to, service, key_type):
|
|
|
|
|
|
if key_type == KEY_TYPE_TEAM:
|
|
|
|
|
|
message = 'Can’t send to this recipient using a team-only API key'
|
|
|
|
|
|
else:
|
|
|
|
|
|
message = (
|
|
|
|
|
|
'Can’t send to this recipient when service is in trial mode '
|
|
|
|
|
|
'– see https://www.notifications.service.gov.uk/trial-mode'
|
|
|
|
|
|
)
|
2016-10-28 17:10:00 +01:00
|
|
|
|
raise BadRequestError(message=message)
|
2016-10-27 11:46:37 +01:00
|
|
|
|
|
|
|
|
|
|
|
2017-01-17 12:08:24 +00:00
|
|
|
|
def validate_and_format_recipient(send_to, key_type, service, notification_type):
|
|
|
|
|
|
service_can_send_to_recipient(send_to, key_type, service)
|
|
|
|
|
|
if notification_type == SMS_TYPE:
|
|
|
|
|
|
return validate_and_format_phone_number(number=send_to)
|
|
|
|
|
|
else:
|
|
|
|
|
|
return validate_and_format_email_address(email_address=send_to)
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-10-27 11:46:37 +01:00
|
|
|
|
def check_sms_content_char_count(content_count):
|
|
|
|
|
|
char_count_limit = current_app.config.get('SMS_CHAR_COUNT_LIMIT')
|
|
|
|
|
|
if (
|
|
|
|
|
|
content_count > char_count_limit
|
|
|
|
|
|
):
|
2016-10-28 17:10:00 +01:00
|
|
|
|
message = 'Content for template has a character count greater than the limit of {}'.format(char_count_limit)
|
|
|
|
|
|
raise BadRequestError(message=message)
|