mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-05-05 16:38:59 -04:00
Make send the send flow generic
This commit parameterises all methods in the send view so that they can send either emails or SMS messages. It works out what kind of message it is sending from the `template_type` property of the template object. This means that the `Template` util class needs to know about these properties, which means that this commit depends on: https://github.com/alphagov/notifications-utils/pull/2 This commit does _not_ add tests for sending emails. The existing tests for sending SMS still pass, but actually sending emails is outside the scope of this story.
This commit is contained in:
20
app/utils.py
20
app/utils.py
@@ -1,3 +1,5 @@
|
||||
import re
|
||||
|
||||
from functools import wraps
|
||||
from flask import abort
|
||||
|
||||
@@ -28,6 +30,11 @@ class BrowsableItem(object):
|
||||
pass
|
||||
|
||||
|
||||
class InvalidEmailError(Exception):
|
||||
def __init__(self, message):
|
||||
self.message = message
|
||||
|
||||
|
||||
class InvalidPhoneError(Exception):
|
||||
def __init__(self, message):
|
||||
self.message = message
|
||||
@@ -74,6 +81,19 @@ def format_phone_number(number):
|
||||
return '+447{}{}{}'.format(*re.findall('...', number))
|
||||
|
||||
|
||||
def validate_email_address(email_address):
|
||||
if re.match(r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)", email_address):
|
||||
return
|
||||
raise InvalidEmailError('Not a valid email address')
|
||||
|
||||
|
||||
def validate_recipient(recipient, template_type):
|
||||
return {
|
||||
'email': validate_email_address,
|
||||
'sms': validate_phone_number
|
||||
}[template_type](recipient)
|
||||
|
||||
|
||||
def user_has_permissions(*permissions):
|
||||
def wrap(func):
|
||||
@wraps(func)
|
||||
|
||||
Reference in New Issue
Block a user