mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-11 09:27:56 -04:00
Merge branch 'master' into split-sms-and-retry
This commit is contained in:
@@ -182,16 +182,10 @@ def process_job(job_id):
|
||||
)
|
||||
|
||||
if template.template_type == 'email':
|
||||
from_email = '"{}" <{}@{}>'.format(
|
||||
service.name,
|
||||
service.email_from,
|
||||
current_app.config['NOTIFY_EMAIL_DOMAIN']
|
||||
)
|
||||
|
||||
send_email.apply_async((
|
||||
str(job.service_id),
|
||||
create_uuid(),
|
||||
from_email.encode('ascii', 'ignore').decode('ascii'),
|
||||
'',
|
||||
encrypted,
|
||||
datetime.utcnow().strftime(DATETIME_FORMAT)),
|
||||
{'reply_to_addresses': service.reply_to_email_address},
|
||||
@@ -307,6 +301,9 @@ def send_email(service_id, notification_id, from_address, encrypted_notification
|
||||
(provider.get_name(), str(reference), notification['to']), queue='research-mode'
|
||||
)
|
||||
else:
|
||||
# First step setting the from_address here rather than the method creating the task
|
||||
from_address = '"{}" <{}@{}>'.format(service.name, service.email_from, current_app.config[
|
||||
'NOTIFY_EMAIL_DOMAIN']) if from_address == "" else from_address
|
||||
reference = provider.send_email(
|
||||
from_address,
|
||||
notification['to'],
|
||||
|
||||
@@ -6,14 +6,36 @@ from sqlalchemy.exc import SQLAlchemyError, DataError
|
||||
from sqlalchemy.orm.exc import NoResultFound
|
||||
|
||||
|
||||
class InvalidData(Exception):
|
||||
|
||||
def __init__(self, message, status_code):
|
||||
super().__init__()
|
||||
self.message = message
|
||||
self.status_code = status_code
|
||||
|
||||
def to_dict(self):
|
||||
return {'result': 'error', 'message': self.message}
|
||||
|
||||
def __str__(self):
|
||||
return str(self.to_dict())
|
||||
|
||||
|
||||
def register_errors(blueprint):
|
||||
|
||||
@blueprint.app_errorhandler(InvalidData)
|
||||
def invalid_data(error):
|
||||
response = jsonify(error.to_dict())
|
||||
response.status_code = error.status_code
|
||||
current_app.logger.error(error)
|
||||
return response
|
||||
|
||||
@blueprint.app_errorhandler(400)
|
||||
def bad_request(e):
|
||||
if isinstance(e, str):
|
||||
msg = e
|
||||
else:
|
||||
msg = e.description or "Invalid request parameters"
|
||||
current_app.logger.exception(msg)
|
||||
return jsonify(result='error', message=str(msg)), 400
|
||||
|
||||
@blueprint.app_errorhandler(401)
|
||||
@@ -32,18 +54,17 @@ def register_errors(blueprint):
|
||||
msg = e
|
||||
else:
|
||||
msg = e.description or "Not found"
|
||||
current_app.logger.exception(msg)
|
||||
return jsonify(result='error', message=msg), 404
|
||||
|
||||
@blueprint.app_errorhandler(429)
|
||||
def limit_exceeded(e):
|
||||
current_app.logger.exception(e)
|
||||
return jsonify(result='error', message=str(e.description)), 429
|
||||
|
||||
@blueprint.app_errorhandler(500)
|
||||
def internal_server_error(e):
|
||||
if isinstance(e, str):
|
||||
current_app.logger.exception(e)
|
||||
elif isinstance(e, Exception):
|
||||
current_app.logger.exception(e)
|
||||
current_app.logger.exception(e)
|
||||
return jsonify(result='error', message="Internal server error"), 500
|
||||
|
||||
@blueprint.app_errorhandler(NoResultFound)
|
||||
|
||||
@@ -387,7 +387,7 @@ def send_notification(notification_type):
|
||||
send_email.apply_async((
|
||||
service_id,
|
||||
notification_id,
|
||||
'"{}" <{}@{}>'.format(service.name, service.email_from, current_app.config['NOTIFY_EMAIL_DOMAIN']),
|
||||
'',
|
||||
encryption.encrypt(notification),
|
||||
datetime.utcnow().strftime(DATETIME_FORMAT)
|
||||
), queue='email')
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
from flask import (
|
||||
Blueprint,
|
||||
jsonify,
|
||||
request,
|
||||
current_app
|
||||
request
|
||||
)
|
||||
|
||||
from app.dao.notifications_dao import (
|
||||
@@ -16,7 +15,7 @@ template_statistics = Blueprint('template-statistics',
|
||||
__name__,
|
||||
url_prefix='/service/<service_id>/template-statistics')
|
||||
|
||||
from app.errors import register_errors
|
||||
from app.errors import register_errors, InvalidData
|
||||
|
||||
register_errors(template_statistics)
|
||||
|
||||
@@ -28,14 +27,14 @@ def get_template_statistics_for_service(service_id):
|
||||
limit_days = int(request.args['limit_days'])
|
||||
except ValueError as e:
|
||||
error = '{} is not an integer'.format(request.args['limit_days'])
|
||||
current_app.logger.error(error)
|
||||
return jsonify(result="error", message={'limit_days': [error]}), 400
|
||||
message = {'limit_days': [error]}
|
||||
raise InvalidData(message, status_code=400)
|
||||
else:
|
||||
limit_days = None
|
||||
stats = dao_get_template_statistics_for_service(service_id, limit_days=limit_days)
|
||||
data, errors = template_statistics_schema.dump(stats, many=True)
|
||||
if errors:
|
||||
return jsonify(result="error", message=errors), 400
|
||||
raise InvalidData(errors, status_code=400)
|
||||
return jsonify(data=data)
|
||||
|
||||
|
||||
@@ -44,5 +43,5 @@ def get_template_statistics_for_template_id(service_id, template_id):
|
||||
stats = dao_get_template_statistics_for_template(template_id)
|
||||
data, errors = template_statistics_schema.dump(stats, many=True)
|
||||
if errors:
|
||||
return jsonify(result="error", message=errors), 400
|
||||
raise InvalidData(errors, status_code=400)
|
||||
return jsonify(data=data)
|
||||
|
||||
@@ -28,8 +28,8 @@ from app.schemas import (
|
||||
from app.celery.tasks import (
|
||||
send_sms,
|
||||
email_reset_password,
|
||||
email_registration_verification
|
||||
)
|
||||
email_registration_verification,
|
||||
send_email)
|
||||
|
||||
from app.errors import register_errors
|
||||
|
||||
@@ -157,13 +157,23 @@ def send_user_email_verification(user_id):
|
||||
secret_code = create_secret_code()
|
||||
create_user_code(user_to_send_to, secret_code, 'email')
|
||||
|
||||
email = user_to_send_to.email_address
|
||||
verification_message = {'to': email,
|
||||
'name': user_to_send_to.name,
|
||||
'url': _create_verification_url(user_to_send_to, secret_code)}
|
||||
|
||||
email_registration_verification.apply_async([encryption.encrypt(verification_message)],
|
||||
queue='email-registration-verification')
|
||||
template = dao_get_template_by_id(current_app.config['EMAIL_VERIFY_CODE_TEMPLATE_ID'])
|
||||
message = {
|
||||
'template': str(template.id),
|
||||
'template_version': template.version,
|
||||
'to': user_to_send_to.email_address,
|
||||
'personalisation': {
|
||||
'name': user_to_send_to.name,
|
||||
'url': _create_verification_url(user_to_send_to, secret_code)
|
||||
}
|
||||
}
|
||||
send_email.apply_async((
|
||||
current_app.config['NOTIFY_SERVICE_ID'],
|
||||
str(uuid.uuid4()),
|
||||
'',
|
||||
encryption.encrypt(message),
|
||||
datetime.utcnow().strftime(DATETIME_FORMAT)
|
||||
), queue='email-registration-verification')
|
||||
|
||||
return jsonify({}), 204
|
||||
|
||||
|
||||
Reference in New Issue
Block a user