Email invitation to an invited user.

New celery task to send the email.
This commit is contained in:
Rebecca Law
2016-02-29 13:21:12 +00:00
parent df61e0366e
commit df278a8e6e
9 changed files with 118 additions and 13 deletions

View File

@@ -156,3 +156,33 @@ def send_email_code(encrypted_verification_message):
verification_message['secret_code'])
except AwsSesClientException as e:
current_app.logger.error(e)
# TODO: when placeholders in templates work, this will be a real template
def invitation_template(user_name, service_name, url, expiry_date):
from string import Template
t = Template('You are invited to use GOV.UK Notify by $user_name for service $service_name.'
' The url to join is $url. This url will expire on $expiry_date')
return t.substitute(user_name=user_name, service_name=service_name, url=url, expiry_date=expiry_date)
def invited_user_url(base_url, token):
return '{0}/invitation/{1}'.format(base_url, token)
@notify_celery.task(name='email-invited-user')
def email_invited_user(encrypted_invitation):
invitation = encryption.decrypt(encrypted_invitation)
url = invited_user_url(current_app.config['ADMIN_BASE_URL'],
invitation['token'])
invitation_content = invitation_template(invitation['user_name'],
invitation['service_name'],
url,
invitation['expiry_date'])
try:
aws_ses_client.send_email(current_app.config['VERIFY_CODE_FROM_EMAIL_ADDRESS'],
invitation['to'],
'Invitation to GOV.UK Notify',
invitation_content)
except AwsSesClientException as e:
current_app.logger.error(e)

View File

@@ -1,8 +1,10 @@
from datetime import timedelta
from flask import (
Blueprint,
request,
jsonify
)
jsonify,
current_app)
from app.dao.invited_user_dao import (
save_invited_user,
@@ -11,6 +13,7 @@ from app.dao.invited_user_dao import (
)
from app.schemas import invited_user_schema
from app.celery.tasks import email_invited_user
invite = Blueprint('invite', __name__, url_prefix='/service/<service_id>/invite')
@@ -24,6 +27,8 @@ def create_invited_user(service_id):
if errors:
return jsonify(result="error", message=errors), 400
save_invited_user(invited_user)
invitation = _create_invitation(invited_user)
email_invited_user.apply_async(encrypted_invitation=invitation, queue_name='email-invited-user')
return jsonify(data=invited_user_schema.dump(invited_user).data), 201
@@ -41,3 +46,21 @@ def get_invited_user_by_service_and_id(service_id, invited_user_id):
invited_user_id)
return jsonify(result='error', message=message), 404
return jsonify(data=invited_user_schema.dump(invited_user).data), 200
def _create_invitation(invited_user):
from utils.url_safe_token import generate_token
token = generate_token(str(invited_user.id), current_app.config['SECRET_KEY'], current_app.config['DANGEROUS_SALT'])
# TODO: confirm what we want to do for this - the idea is that we say expires tomorrow at midnight
# and give 48 hours as the max_age
expiration_date = (invited_user.created_at + timedelta(days=current_app.config['INVITATION_EXPIRATION_DAYS'])) \
.replace(hour=0, minute=0, second=0, microsecond=0)
invitation = {'to': invited_user.email_address,
'user_name': invited_user.from_user.name,
'service_id': str(invited_user.service_id),
'service_name': invited_user.service.name,
'token': token,
'expiry_date': expiration_date
}
return invitation

View File

@@ -93,7 +93,7 @@ def update_service(service_id):
current_data = dict(service_schema.dump(fetched_service).data.items())
current_data.update(request.get_json())
print(current_data)
update_dict, errors = service_schema.load(current_data)
if errors:
return jsonify(result="error", message=errors), 400