mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-03 18:01:08 -05:00
reformat
This commit is contained in:
@@ -24,24 +24,28 @@ from app.organization.organization_schema import (
|
||||
)
|
||||
from app.schema_validation import validate
|
||||
|
||||
organization_invite_blueprint = Blueprint('organization_invite', __name__)
|
||||
organization_invite_blueprint = Blueprint("organization_invite", __name__)
|
||||
|
||||
register_errors(organization_invite_blueprint)
|
||||
|
||||
|
||||
@organization_invite_blueprint.route('/organization/<uuid:organization_id>/invite', methods=['POST'])
|
||||
@organization_invite_blueprint.route(
|
||||
"/organization/<uuid:organization_id>/invite", methods=["POST"]
|
||||
)
|
||||
def invite_user_to_org(organization_id):
|
||||
data = request.get_json()
|
||||
validate(data, post_create_invited_org_user_status_schema)
|
||||
|
||||
invited_org_user = InvitedOrganizationUser(
|
||||
email_address=data['email_address'],
|
||||
invited_by_id=data['invited_by'],
|
||||
organization_id=organization_id
|
||||
email_address=data["email_address"],
|
||||
invited_by_id=data["invited_by"],
|
||||
organization_id=organization_id,
|
||||
)
|
||||
save_invited_org_user(invited_org_user)
|
||||
|
||||
template = dao_get_template_by_id(current_app.config['ORGANIZATION_INVITATION_EMAIL_TEMPLATE_ID'])
|
||||
template = dao_get_template_by_id(
|
||||
current_app.config["ORGANIZATION_INVITATION_EMAIL_TEMPLATE_ID"]
|
||||
)
|
||||
|
||||
saved_notification = persist_notification(
|
||||
template_id=template.id,
|
||||
@@ -49,21 +53,21 @@ def invite_user_to_org(organization_id):
|
||||
recipient=invited_org_user.email_address,
|
||||
service=template.service,
|
||||
personalisation={
|
||||
'user_name': (
|
||||
'The GOV.UK Notify team'
|
||||
"user_name": (
|
||||
"The GOV.UK Notify team"
|
||||
if invited_org_user.invited_by.platform_admin
|
||||
else invited_org_user.invited_by.name
|
||||
),
|
||||
'organization_name': invited_org_user.organization.name,
|
||||
'url': invited_org_user_url(
|
||||
"organization_name": invited_org_user.organization.name,
|
||||
"url": invited_org_user_url(
|
||||
invited_org_user.id,
|
||||
data.get('invite_link_host'),
|
||||
data.get("invite_link_host"),
|
||||
),
|
||||
},
|
||||
notification_type=EMAIL_TYPE,
|
||||
api_key_id=None,
|
||||
key_type=KEY_TYPE_NORMAL,
|
||||
reply_to_text=invited_org_user.invited_by.email_address
|
||||
reply_to_text=invited_org_user.invited_by.email_address,
|
||||
)
|
||||
|
||||
send_notification_to_queue(saved_notification, queue=QueueNames.NOTIFY)
|
||||
@@ -71,15 +75,16 @@ def invite_user_to_org(organization_id):
|
||||
return jsonify(data=invited_org_user.serialize()), 201
|
||||
|
||||
|
||||
@organization_invite_blueprint.route('/organization/<uuid:organization_id>/invite', methods=['GET'])
|
||||
@organization_invite_blueprint.route(
|
||||
"/organization/<uuid:organization_id>/invite", methods=["GET"]
|
||||
)
|
||||
def get_invited_org_users_by_organization(organization_id):
|
||||
invited_org_users = get_invited_org_users_for_organization(organization_id)
|
||||
return jsonify(data=[x.serialize() for x in invited_org_users]), 200
|
||||
|
||||
|
||||
@organization_invite_blueprint.route(
|
||||
'/organization/<uuid:organization_id>/invite/<invited_org_user_id>',
|
||||
methods=['GET']
|
||||
"/organization/<uuid:organization_id>/invite/<invited_org_user_id>", methods=["GET"]
|
||||
)
|
||||
def get_invited_org_user_by_organization(organization_id, invited_org_user_id):
|
||||
invited_org_user = dao_get_invited_org_user(organization_id, invited_org_user_id)
|
||||
@@ -87,16 +92,18 @@ def get_invited_org_user_by_organization(organization_id, invited_org_user_id):
|
||||
|
||||
|
||||
@organization_invite_blueprint.route(
|
||||
'/organization/<uuid:organization_id>/invite/<invited_org_user_id>',
|
||||
methods=['POST']
|
||||
"/organization/<uuid:organization_id>/invite/<invited_org_user_id>",
|
||||
methods=["POST"],
|
||||
)
|
||||
def update_org_invite_status(organization_id, invited_org_user_id):
|
||||
fetched = dao_get_invited_org_user(organization_id=organization_id, invited_org_user_id=invited_org_user_id)
|
||||
fetched = dao_get_invited_org_user(
|
||||
organization_id=organization_id, invited_org_user_id=invited_org_user_id
|
||||
)
|
||||
|
||||
data = request.get_json()
|
||||
validate(data, post_update_invited_org_user_status_schema)
|
||||
|
||||
fetched.status = data['status']
|
||||
fetched.status = data["status"]
|
||||
save_invited_org_user(fetched)
|
||||
|
||||
return jsonify(data=fetched.serialize()), 200
|
||||
@@ -105,40 +112,48 @@ def update_org_invite_status(organization_id, invited_org_user_id):
|
||||
def invited_org_user_url(invited_org_user_id, invite_link_host=None):
|
||||
token = generate_token(
|
||||
str(invited_org_user_id),
|
||||
current_app.config['SECRET_KEY'],
|
||||
current_app.config['DANGEROUS_SALT']
|
||||
current_app.config["SECRET_KEY"],
|
||||
current_app.config["DANGEROUS_SALT"],
|
||||
)
|
||||
|
||||
if invite_link_host is None:
|
||||
invite_link_host = current_app.config['ADMIN_BASE_URL']
|
||||
invite_link_host = current_app.config["ADMIN_BASE_URL"]
|
||||
|
||||
return '{0}/organization-invitation/{1}'.format(invite_link_host, token)
|
||||
return "{0}/organization-invitation/{1}".format(invite_link_host, token)
|
||||
|
||||
|
||||
@organization_invite_blueprint.route('/invite/organization/<uuid:invited_org_user_id>', methods=['GET'])
|
||||
@organization_invite_blueprint.route(
|
||||
"/invite/organization/<uuid:invited_org_user_id>", methods=["GET"]
|
||||
)
|
||||
def get_invited_org_user(invited_org_user_id):
|
||||
invited_user = get_invited_org_user_by_id(invited_org_user_id)
|
||||
return jsonify(data=invited_user.serialize()), 200
|
||||
|
||||
|
||||
@organization_invite_blueprint.route('/invite/organization/<token>', methods=['GET'])
|
||||
@organization_invite_blueprint.route('/invite/organization/check/<token>', methods=['GET'])
|
||||
@organization_invite_blueprint.route("/invite/organization/<token>", methods=["GET"])
|
||||
@organization_invite_blueprint.route(
|
||||
"/invite/organization/check/<token>", methods=["GET"]
|
||||
)
|
||||
def validate_invitation_token(token):
|
||||
|
||||
max_age_seconds = 60 * 60 * 24 * current_app.config['INVITATION_EXPIRATION_DAYS']
|
||||
max_age_seconds = 60 * 60 * 24 * current_app.config["INVITATION_EXPIRATION_DAYS"]
|
||||
|
||||
try:
|
||||
invited_user_id = check_token(token,
|
||||
current_app.config['SECRET_KEY'],
|
||||
current_app.config['DANGEROUS_SALT'],
|
||||
max_age_seconds)
|
||||
invited_user_id = check_token(
|
||||
token,
|
||||
current_app.config["SECRET_KEY"],
|
||||
current_app.config["DANGEROUS_SALT"],
|
||||
max_age_seconds,
|
||||
)
|
||||
except SignatureExpired:
|
||||
errors = {'invitation':
|
||||
'Your invitation to GOV.UK Notify has expired. '
|
||||
'Please ask the person that invited you to send you another one'}
|
||||
errors = {
|
||||
"invitation": "Your invitation to GOV.UK Notify has expired. "
|
||||
"Please ask the person that invited you to send you another one"
|
||||
}
|
||||
raise InvalidRequest(errors, status_code=400)
|
||||
except BadData:
|
||||
errors = {'invitation': 'Something’s wrong with this link. Make sure you’ve copied the whole thing.'}
|
||||
errors = {
|
||||
"invitation": "Something’s wrong with this link. Make sure you’ve copied the whole thing."
|
||||
}
|
||||
raise InvalidRequest(errors, status_code=400)
|
||||
|
||||
invited_user = get_invited_org_user_by_id(invited_user_id)
|
||||
|
||||
@@ -10,7 +10,7 @@ post_create_organization_schema = {
|
||||
"active": {"type": ["boolean", "null"]},
|
||||
"organization_type": {"enum": ORGANIZATION_TYPES},
|
||||
},
|
||||
"required": ["name", "organization_type"]
|
||||
"required": ["name", "organization_type"],
|
||||
}
|
||||
|
||||
post_update_organization_schema = {
|
||||
@@ -22,17 +22,15 @@ post_update_organization_schema = {
|
||||
"active": {"type": ["boolean", "null"]},
|
||||
"organization_type": {"enum": ORGANIZATION_TYPES},
|
||||
},
|
||||
"required": []
|
||||
"required": [],
|
||||
}
|
||||
|
||||
post_link_service_to_organization_schema = {
|
||||
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||
"description": "POST link service to organization schema",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"service_id": uuid
|
||||
},
|
||||
"required": ["service_id"]
|
||||
"properties": {"service_id": uuid},
|
||||
"required": ["service_id"],
|
||||
}
|
||||
|
||||
|
||||
@@ -43,9 +41,9 @@ post_create_invited_org_user_status_schema = {
|
||||
"properties": {
|
||||
"email_address": {"type": "string", "format": "email_address"},
|
||||
"invited_by": uuid,
|
||||
"invite_link_host": {"type": "string"}
|
||||
"invite_link_host": {"type": "string"},
|
||||
},
|
||||
"required": ["email_address", "invited_by"]
|
||||
"required": ["email_address", "invited_by"],
|
||||
}
|
||||
|
||||
|
||||
@@ -53,8 +51,6 @@ post_update_invited_org_user_status_schema = {
|
||||
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||
"description": "POST update organization invite schema",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"status": {"enum": INVITED_USER_STATUS_TYPES}
|
||||
},
|
||||
"required": ["status"]
|
||||
"properties": {"status": {"enum": INVITED_USER_STATUS_TYPES}},
|
||||
"required": ["status"],
|
||||
}
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
|
||||
from flask import Blueprint, abort, current_app, jsonify, request
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
|
||||
@@ -34,7 +33,7 @@ from app.organization.organization_schema import (
|
||||
)
|
||||
from app.schema_validation import validate
|
||||
|
||||
organization_blueprint = Blueprint('organization', __name__)
|
||||
organization_blueprint = Blueprint("organization", __name__)
|
||||
register_errors(organization_blueprint)
|
||||
|
||||
|
||||
@@ -45,41 +44,36 @@ def handle_integrity_error(exc):
|
||||
"""
|
||||
print(exc)
|
||||
current_app.logger.exception(exc)
|
||||
if 'ix_organization_name' in str(exc):
|
||||
return jsonify(result="error",
|
||||
message="Organization name already exists"), 400
|
||||
if "ix_organization_name" in str(exc):
|
||||
return jsonify(result="error", message="Organization name already exists"), 400
|
||||
if 'duplicate key value violates unique constraint "domain_pkey"' in str(exc):
|
||||
return jsonify(result='error',
|
||||
message='Domain already exists'), 400
|
||||
return jsonify(result="error", message="Domain already exists"), 400
|
||||
|
||||
return jsonify(result='error', message="Internal server error"), 500
|
||||
return jsonify(result="error", message="Internal server error"), 500
|
||||
|
||||
|
||||
@organization_blueprint.route('', methods=['GET'])
|
||||
@organization_blueprint.route("", methods=["GET"])
|
||||
def get_organizations():
|
||||
organizations = [
|
||||
org.serialize_for_list() for org in dao_get_organizations()
|
||||
]
|
||||
organizations = [org.serialize_for_list() for org in dao_get_organizations()]
|
||||
|
||||
return jsonify(organizations)
|
||||
|
||||
|
||||
@organization_blueprint.route('/<uuid:organization_id>', methods=['GET'])
|
||||
@organization_blueprint.route("/<uuid:organization_id>", methods=["GET"])
|
||||
def get_organization_by_id(organization_id):
|
||||
organization = dao_get_organization_by_id(organization_id)
|
||||
return jsonify(organization.serialize())
|
||||
|
||||
|
||||
@organization_blueprint.route('/by-domain', methods=['GET'])
|
||||
@organization_blueprint.route("/by-domain", methods=["GET"])
|
||||
def get_organization_by_domain():
|
||||
domain = request.args.get("domain")
|
||||
|
||||
domain = request.args.get('domain')
|
||||
|
||||
if not domain or '@' in domain:
|
||||
if not domain or "@" in domain:
|
||||
abort(400)
|
||||
|
||||
organization = dao_get_organization_by_email_address(
|
||||
'example@{}'.format(request.args.get('domain'))
|
||||
"example@{}".format(request.args.get("domain"))
|
||||
)
|
||||
|
||||
if not organization:
|
||||
@@ -88,7 +82,7 @@ def get_organization_by_domain():
|
||||
return jsonify(organization.serialize())
|
||||
|
||||
|
||||
@organization_blueprint.route('', methods=['POST'])
|
||||
@organization_blueprint.route("", methods=["POST"])
|
||||
def create_organization():
|
||||
data = request.get_json()
|
||||
validate(data, post_create_organization_schema)
|
||||
@@ -98,70 +92,78 @@ def create_organization():
|
||||
return jsonify(organization.serialize()), 201
|
||||
|
||||
|
||||
@organization_blueprint.route('/<uuid:organization_id>', methods=['POST'])
|
||||
@organization_blueprint.route("/<uuid:organization_id>", methods=["POST"])
|
||||
def update_organization(organization_id):
|
||||
data = request.get_json()
|
||||
validate(data, post_update_organization_schema)
|
||||
|
||||
result = dao_update_organization(organization_id, **data)
|
||||
|
||||
if data.get('agreement_signed') is True:
|
||||
if data.get("agreement_signed") is True:
|
||||
# if a platform admin has manually adjusted the organization, don't tell people
|
||||
if data.get('agreement_signed_by_id'):
|
||||
if data.get("agreement_signed_by_id"):
|
||||
send_notifications_on_mou_signed(organization_id)
|
||||
|
||||
if result:
|
||||
return '', 204
|
||||
return "", 204
|
||||
else:
|
||||
raise InvalidRequest("Organization not found", 404)
|
||||
|
||||
|
||||
@organization_blueprint.route('/<uuid:organization_id>/service', methods=['POST'])
|
||||
@organization_blueprint.route("/<uuid:organization_id>/service", methods=["POST"])
|
||||
def link_service_to_organization(organization_id):
|
||||
data = request.get_json()
|
||||
validate(data, post_link_service_to_organization_schema)
|
||||
service = dao_fetch_service_by_id(data['service_id'])
|
||||
service = dao_fetch_service_by_id(data["service_id"])
|
||||
service.organization = None
|
||||
|
||||
with transaction():
|
||||
dao_add_service_to_organization(service, organization_id)
|
||||
set_default_free_allowance_for_service(service, year_start=None)
|
||||
|
||||
return '', 204
|
||||
return "", 204
|
||||
|
||||
|
||||
@organization_blueprint.route('/<uuid:organization_id>/services', methods=['GET'])
|
||||
@organization_blueprint.route("/<uuid:organization_id>/services", methods=["GET"])
|
||||
def get_organization_services(organization_id):
|
||||
services = dao_get_organization_services(organization_id)
|
||||
sorted_services = sorted(services, key=lambda s: (-s.active, s.name))
|
||||
return jsonify([s.serialize_for_org_dashboard() for s in sorted_services])
|
||||
|
||||
|
||||
@organization_blueprint.route('/<uuid:organization_id>/services-with-usage', methods=['GET'])
|
||||
@organization_blueprint.route(
|
||||
"/<uuid:organization_id>/services-with-usage", methods=["GET"]
|
||||
)
|
||||
def get_organization_services_usage(organization_id):
|
||||
try:
|
||||
year = int(request.args.get('year', 'none'))
|
||||
year = int(request.args.get("year", "none"))
|
||||
except ValueError:
|
||||
return jsonify(result='error', message='No valid year provided'), 400
|
||||
return jsonify(result="error", message="No valid year provided"), 400
|
||||
services = fetch_usage_year_for_organization(organization_id, year)
|
||||
list_services = services.values()
|
||||
sorted_services = sorted(list_services, key=lambda s: (-s['active'], s['service_name'].lower()))
|
||||
sorted_services = sorted(
|
||||
list_services, key=lambda s: (-s["active"], s["service_name"].lower())
|
||||
)
|
||||
return jsonify(services=sorted_services)
|
||||
|
||||
|
||||
@organization_blueprint.route('/<uuid:organization_id>/users/<uuid:user_id>', methods=['POST'])
|
||||
@organization_blueprint.route(
|
||||
"/<uuid:organization_id>/users/<uuid:user_id>", methods=["POST"]
|
||||
)
|
||||
def add_user_to_organization(organization_id, user_id):
|
||||
new_org_user = dao_add_user_to_organization(organization_id, user_id)
|
||||
return jsonify(data=new_org_user.serialize())
|
||||
|
||||
|
||||
@organization_blueprint.route('/<uuid:organization_id>/users/<uuid:user_id>', methods=['DELETE'])
|
||||
@organization_blueprint.route(
|
||||
"/<uuid:organization_id>/users/<uuid:user_id>", methods=["DELETE"]
|
||||
)
|
||||
def remove_user_from_organization(organization_id, user_id):
|
||||
organization = dao_get_organization_by_id(organization_id)
|
||||
user = get_user_by_id(user_id=user_id)
|
||||
|
||||
if user not in organization.users:
|
||||
error = 'User not found'
|
||||
error = "User not found"
|
||||
raise InvalidRequest(error, status_code=404)
|
||||
|
||||
dao_remove_user_from_organization(organization, user)
|
||||
@@ -169,20 +171,20 @@ def remove_user_from_organization(organization_id, user_id):
|
||||
return {}, 204
|
||||
|
||||
|
||||
@organization_blueprint.route('/<uuid:organization_id>/users', methods=['GET'])
|
||||
@organization_blueprint.route("/<uuid:organization_id>/users", methods=["GET"])
|
||||
def get_organization_users(organization_id):
|
||||
org_users = dao_get_users_for_organization(organization_id)
|
||||
return jsonify(data=[x.serialize() for x in org_users])
|
||||
|
||||
|
||||
def check_request_args(request):
|
||||
org_id = request.args.get('org_id')
|
||||
name = request.args.get('name', None)
|
||||
org_id = request.args.get("org_id")
|
||||
name = request.args.get("name", None)
|
||||
errors = []
|
||||
if not org_id:
|
||||
errors.append({'org_id': ["Can't be empty"]})
|
||||
errors.append({"org_id": ["Can't be empty"]})
|
||||
if not name:
|
||||
errors.append({'name': ["Can't be empty"]})
|
||||
errors.append({"name": ["Can't be empty"]})
|
||||
if errors:
|
||||
raise InvalidRequest(errors, status_code=400)
|
||||
return org_id, name
|
||||
@@ -190,7 +192,7 @@ def check_request_args(request):
|
||||
|
||||
def send_notifications_on_mou_signed(organization_id):
|
||||
organization = dao_get_organization_by_id(organization_id)
|
||||
notify_service = dao_fetch_service_by_id(current_app.config['NOTIFY_SERVICE_ID'])
|
||||
notify_service = dao_fetch_service_by_id(current_app.config["NOTIFY_SERVICE_ID"])
|
||||
|
||||
def _send_notification(template_id, recipient, personalisation):
|
||||
template = dao_get_template_by_id(template_id)
|
||||
@@ -204,38 +206,37 @@ def send_notifications_on_mou_signed(organization_id):
|
||||
notification_type=template.template_type,
|
||||
api_key_id=None,
|
||||
key_type=KEY_TYPE_NORMAL,
|
||||
reply_to_text=notify_service.get_default_reply_to_email_address()
|
||||
reply_to_text=notify_service.get_default_reply_to_email_address(),
|
||||
)
|
||||
send_notification_to_queue(saved_notification, queue=QueueNames.NOTIFY)
|
||||
|
||||
personalisation = {
|
||||
'mou_link': '{}/agreement/agreement.pdf'.format(
|
||||
current_app.config['ADMIN_BASE_URL']
|
||||
"mou_link": "{}/agreement/agreement.pdf".format(
|
||||
current_app.config["ADMIN_BASE_URL"]
|
||||
),
|
||||
'org_name': organization.name,
|
||||
'org_dashboard_link': '{}/organizations/{}'.format(
|
||||
current_app.config['ADMIN_BASE_URL'],
|
||||
organization.id
|
||||
"org_name": organization.name,
|
||||
"org_dashboard_link": "{}/organizations/{}".format(
|
||||
current_app.config["ADMIN_BASE_URL"], organization.id
|
||||
),
|
||||
'signed_by_name': organization.agreement_signed_by.name,
|
||||
'on_behalf_of_name': organization.agreement_signed_on_behalf_of_name
|
||||
"signed_by_name": organization.agreement_signed_by.name,
|
||||
"on_behalf_of_name": organization.agreement_signed_on_behalf_of_name,
|
||||
}
|
||||
|
||||
if not organization.agreement_signed_on_behalf_of_email_address:
|
||||
signer_template_id = 'MOU_SIGNER_RECEIPT_TEMPLATE_ID'
|
||||
signer_template_id = "MOU_SIGNER_RECEIPT_TEMPLATE_ID"
|
||||
else:
|
||||
signer_template_id = 'MOU_SIGNED_ON_BEHALF_SIGNER_RECEIPT_TEMPLATE_ID'
|
||||
signer_template_id = "MOU_SIGNED_ON_BEHALF_SIGNER_RECEIPT_TEMPLATE_ID"
|
||||
|
||||
# let the person who has been signed on behalf of know.
|
||||
_send_notification(
|
||||
current_app.config['MOU_SIGNED_ON_BEHALF_ON_BEHALF_RECEIPT_TEMPLATE_ID'],
|
||||
current_app.config["MOU_SIGNED_ON_BEHALF_ON_BEHALF_RECEIPT_TEMPLATE_ID"],
|
||||
organization.agreement_signed_on_behalf_of_email_address,
|
||||
personalisation
|
||||
personalisation,
|
||||
)
|
||||
|
||||
# let the person who signed know - the template is different depending on if they signed on behalf of someone
|
||||
_send_notification(
|
||||
current_app.config[signer_template_id],
|
||||
organization.agreement_signed_by.email_address,
|
||||
personalisation
|
||||
personalisation,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user