mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-01 07:35:34 -05:00
Merge pull request #778 from alphagov/refactor-notify-user-endpoints
Refactor notify user endpoints
This commit is contained in:
@@ -1,20 +1,18 @@
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
from flask import (
|
||||
Blueprint,
|
||||
request,
|
||||
jsonify,
|
||||
current_app)
|
||||
|
||||
from app import encryption, DATETIME_FORMAT
|
||||
from app.dao.invited_user_dao import (
|
||||
save_invited_user,
|
||||
get_invited_user,
|
||||
get_invited_users_for_service
|
||||
)
|
||||
from app.dao.templates_dao import dao_get_template_by_id
|
||||
from app.models import EMAIL_TYPE, KEY_TYPE_NORMAL
|
||||
from app.notifications.process_notifications import persist_notification, send_notification_to_queue
|
||||
from app.schemas import invited_user_schema
|
||||
from app.celery.tasks import (send_email)
|
||||
|
||||
invite = Blueprint('invite', __name__, url_prefix='/service/<service_id>/invite')
|
||||
|
||||
@@ -29,22 +27,23 @@ def create_invited_user(service_id):
|
||||
save_invited_user(invited_user)
|
||||
|
||||
template = dao_get_template_by_id(current_app.config['INVITATION_EMAIL_TEMPLATE_ID'])
|
||||
message = {
|
||||
'template': str(template.id),
|
||||
'template_version': template.version,
|
||||
'to': invited_user.email_address,
|
||||
'personalisation': {
|
||||
|
||||
saved_notification = persist_notification(
|
||||
template_id=template.id,
|
||||
template_version=template.version,
|
||||
recipient=invited_user.email_address,
|
||||
service_id=current_app.config['NOTIFY_SERVICE_ID'],
|
||||
personalisation={
|
||||
'user_name': invited_user.from_user.name,
|
||||
'service_name': invited_user.service.name,
|
||||
'url': invited_user_url(invited_user.id)
|
||||
}
|
||||
}
|
||||
send_email.apply_async((
|
||||
current_app.config['NOTIFY_SERVICE_ID'],
|
||||
str(uuid.uuid4()),
|
||||
encryption.encrypt(message),
|
||||
datetime.utcnow().strftime(DATETIME_FORMAT)
|
||||
), queue="notify")
|
||||
},
|
||||
notification_type=EMAIL_TYPE,
|
||||
api_key_id=None,
|
||||
key_type=KEY_TYPE_NORMAL
|
||||
)
|
||||
|
||||
send_notification_to_queue(saved_notification, False, queue="notify")
|
||||
|
||||
return jsonify(data=invited_user_schema.dump(invited_user).data), 201
|
||||
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
import json
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
from flask import (jsonify, request, Blueprint, current_app)
|
||||
from app import encryption, DATETIME_FORMAT
|
||||
from app.dao.users_dao import (
|
||||
get_user_by_id,
|
||||
save_model_user,
|
||||
@@ -32,10 +30,6 @@ from app.schemas import (
|
||||
user_update_schema_load_json
|
||||
)
|
||||
|
||||
from app.celery.tasks import (
|
||||
send_email
|
||||
)
|
||||
|
||||
from app.errors import (
|
||||
register_errors,
|
||||
InvalidRequest
|
||||
@@ -87,7 +81,6 @@ def update_user_attribute(user_id):
|
||||
def verify_user_password(user_id):
|
||||
user_to_verify = get_user_by_id(user_id=user_id)
|
||||
|
||||
txt_pwd = None
|
||||
try:
|
||||
txt_pwd = request.get_json()['password']
|
||||
except KeyError:
|
||||
@@ -227,22 +220,22 @@ def send_already_registered_email(user_id):
|
||||
to, errors = email_data_request_schema.load(request.get_json())
|
||||
template = dao_get_template_by_id(current_app.config['ALREADY_REGISTERED_EMAIL_TEMPLATE_ID'])
|
||||
|
||||
message = {
|
||||
'template': str(template.id),
|
||||
'template_version': template.version,
|
||||
'to': to['email'],
|
||||
'personalisation': {
|
||||
saved_notification = persist_notification(
|
||||
template_id=template.id,
|
||||
template_version=template.version,
|
||||
recipient=to['email'],
|
||||
service_id=current_app.config['NOTIFY_SERVICE_ID'],
|
||||
personalisation={
|
||||
'signin_url': current_app.config['ADMIN_BASE_URL'] + '/sign-in',
|
||||
'forgot_password_url': current_app.config['ADMIN_BASE_URL'] + '/forgot-password',
|
||||
'feedback_url': current_app.config['ADMIN_BASE_URL'] + '/feedback'
|
||||
}
|
||||
}
|
||||
send_email.apply_async((
|
||||
current_app.config['NOTIFY_SERVICE_ID'],
|
||||
str(uuid.uuid4()),
|
||||
encryption.encrypt(message),
|
||||
datetime.utcnow().strftime(DATETIME_FORMAT)
|
||||
), queue='notify')
|
||||
},
|
||||
notification_type=EMAIL_TYPE,
|
||||
api_key_id=None,
|
||||
key_type=KEY_TYPE_NORMAL
|
||||
)
|
||||
|
||||
send_notification_to_queue(saved_notification, False, queue="notify")
|
||||
|
||||
return jsonify({}), 204
|
||||
|
||||
@@ -289,19 +282,22 @@ def send_user_reset_password():
|
||||
user_to_send_to = get_user_by_email(email['email'])
|
||||
|
||||
template = dao_get_template_by_id(current_app.config['PASSWORD_RESET_TEMPLATE_ID'])
|
||||
message = {
|
||||
'template': str(template.id),
|
||||
'template_version': template.version,
|
||||
'to': user_to_send_to.email_address,
|
||||
'personalisation': {
|
||||
|
||||
saved_notification = persist_notification(
|
||||
template_id=template.id,
|
||||
template_version=template.version,
|
||||
recipient=email['email'],
|
||||
service_id=current_app.config['NOTIFY_SERVICE_ID'],
|
||||
personalisation={
|
||||
'user_name': user_to_send_to.name,
|
||||
'url': _create_reset_password_url(user_to_send_to.email_address)
|
||||
}
|
||||
}
|
||||
send_email.apply_async([current_app.config['NOTIFY_SERVICE_ID'],
|
||||
str(uuid.uuid4()),
|
||||
encryption.encrypt(message),
|
||||
datetime.utcnow().strftime(DATETIME_FORMAT)], queue='notify')
|
||||
},
|
||||
notification_type=EMAIL_TYPE,
|
||||
api_key_id=None,
|
||||
key_type=KEY_TYPE_NORMAL
|
||||
)
|
||||
|
||||
send_notification_to_queue(saved_notification, False, queue="notify")
|
||||
|
||||
return jsonify({}), 204
|
||||
|
||||
|
||||
Reference in New Issue
Block a user