Files
notifications-admin/app/main/views/organizations.py
T

436 lines
15 KiB
Python
Raw Normal View History

2019-03-22 14:46:03 +00:00
from collections import OrderedDict
2021-11-12 18:32:35 +00:00
from datetime import datetime
2020-02-28 12:32:47 +00:00
from functools import partial
2019-03-22 14:46:03 +00:00
from flask import flash, redirect, render_template, request, url_for
from flask_login import current_user
2018-02-19 16:53:29 +00:00
from notifications_python_client.errors import HTTPError
2018-03-06 17:12:31 +00:00
from app import (
2023-07-12 12:09:44 -04:00
current_organization,
email_branding_client,
2018-03-06 17:12:31 +00:00
org_invite_api_client,
2023-07-12 12:09:44 -04:00
organizations_client,
2018-03-06 17:12:31 +00:00
)
2018-02-20 11:22:17 +00:00
from app.main import main
from app.main.forms import (
2022-03-15 10:50:18 +00:00
AdminBillingDetailsForm,
2023-07-12 12:09:44 -04:00
AdminNewOrganizationForm,
2022-03-15 10:50:18 +00:00
AdminNotesForm,
2023-07-12 12:09:44 -04:00
AdminOrganizationDomainsForm,
AdminOrganizationGoLiveNotesForm,
AdminPreviewBrandingForm,
AdminSetEmailBrandingForm,
2018-02-20 11:22:17 +00:00
InviteOrgUserForm,
2023-07-12 12:09:44 -04:00
OrganizationOrganizationTypeForm,
RenameOrganizationForm,
SearchByNameForm,
2018-02-20 11:22:17 +00:00
SearchUsersForm,
)
2020-02-28 12:32:47 +00:00
from app.main.views.dashboard import (
get_tuples_of_financial_years,
requested_and_current_financial_year,
)
from app.main.views.service_settings import get_branding_as_value_and_label
2023-07-12 12:09:44 -04:00
from app.models.organization import AllOrganizations, Organization
from app.models.user import InvitedOrgUser, User
2021-11-12 18:32:35 +00:00
from app.utils.csv import Spreadsheet
2021-06-09 13:19:05 +01:00
from app.utils.user import user_has_permissions, user_is_platform_admin
2018-02-20 11:22:17 +00:00
2023-07-12 12:09:44 -04:00
@main.route("/organizations", methods=['GET'])
@user_is_platform_admin
2023-07-12 12:09:44 -04:00
def organizations():
return render_template(
2023-07-12 12:09:44 -04:00
'views/organizations/index.html',
organizations=AllOrganizations(),
2019-03-21 11:22:53 +00:00
search_form=SearchByNameForm(),
)
2023-07-12 12:09:44 -04:00
@main.route("/organizations/add", methods=['GET', 'POST'])
@user_is_platform_admin
2023-07-12 12:09:44 -04:00
def add_organization():
form = AdminNewOrganizationForm()
2018-02-14 13:08:44 +00:00
if form.validate_on_submit():
2021-02-11 12:02:42 +00:00
try:
return redirect(url_for(
2023-07-12 12:09:44 -04:00
'.organization_settings',
org_id=Organization.create_from_form(form).id,
2021-02-11 12:02:42 +00:00
))
except HTTPError as e:
2022-12-20 09:44:33 -05:00
msg = 'Organization name already exists'
2021-02-11 12:02:42 +00:00
if e.status_code == 400 and msg in e.message:
2022-12-20 09:44:33 -05:00
form.name.errors.append("This organization name is already in use")
2021-02-11 12:02:42 +00:00
else:
raise e
2018-02-14 13:08:44 +00:00
return render_template(
2023-07-12 12:09:44 -04:00
'views/organizations/add-organization.html',
2018-02-14 13:08:44 +00:00
form=form
)
2023-07-12 12:09:44 -04:00
@main.route("/organizations/<uuid:org_id>", methods=['GET'])
@user_has_permissions()
2023-07-12 12:09:44 -04:00
def organization_dashboard(org_id):
2020-02-28 12:32:47 +00:00
year, current_financial_year = requested_and_current_financial_year(request)
2023-07-12 12:09:44 -04:00
services = current_organization.services_and_usage(
2020-02-28 12:32:47 +00:00
financial_year=year
)['services']
return render_template(
2023-07-12 12:09:44 -04:00
'views/organizations/organization/index.html',
2020-02-27 15:21:08 +00:00
services=services,
2020-02-28 12:32:47 +00:00
years=get_tuples_of_financial_years(
2023-07-12 12:09:44 -04:00
partial(url_for, '.organization_dashboard', org_id=current_organization.id),
start=current_financial_year - 2,
end=current_financial_year,
2020-02-28 12:32:47 +00:00
),
selected_year=year,
2020-02-28 16:14:28 +00:00
search_form=SearchByNameForm() if len(services) > 7 else None,
2020-02-27 15:21:08 +00:00
**{
f'total_{key}': sum(service[key] for service in services)
2022-12-05 15:33:44 -05:00
for key in ('emails_sent', 'sms_cost')
2021-11-12 14:35:16 +00:00
},
download_link=url_for(
2023-07-12 12:09:44 -04:00
'.download_organization_usage_report',
2021-11-12 14:35:16 +00:00
org_id=org_id,
2021-11-12 18:32:35 +00:00
selected_year=year
2021-11-12 14:35:16 +00:00
)
)
2023-07-12 12:09:44 -04:00
@main.route("/organizations/<uuid:org_id>/download-usage-report.csv", methods=['GET'])
2021-11-12 14:35:16 +00:00
@user_has_permissions()
2023-07-12 12:09:44 -04:00
def download_organization_usage_report(org_id):
2021-11-12 18:32:35 +00:00
selected_year = request.args.get('selected_year')
2023-07-12 12:09:44 -04:00
services_usage = current_organization.services_and_usage(
2021-11-12 18:32:35 +00:00
financial_year=selected_year
)['services']
2021-11-25 10:04:21 +00:00
unit_column_names = OrderedDict([
2021-11-12 18:32:35 +00:00
('service_id', 'Service ID'),
('service_name', 'Service Name'),
('emails_sent', 'Emails sent'),
('sms_remainder', 'Free text message allowance remaining'),
2021-11-25 10:04:21 +00:00
])
monetary_column_names = OrderedDict([
2022-10-12 20:16:22 +00:00
('sms_cost', 'Spent on text messages ($)'),
2021-11-12 18:32:35 +00:00
])
2021-11-25 10:07:53 +00:00
org_usage_data = [
list(unit_column_names.values()) + list(monetary_column_names.values())
2021-11-25 10:07:53 +00:00
] + [
[
service[attribute] for attribute in unit_column_names.keys()
] + [
'{:,.2f}'.format(service[attribute]) for attribute in monetary_column_names.keys()
2021-11-25 10:07:53 +00:00
]
for service in services_usage
]
2021-11-12 18:32:35 +00:00
return Spreadsheet.from_rows(org_usage_data).as_csv_data, 200, {
'Content-Type': 'text/csv; charset=utf-8',
'Content-Disposition': (
'inline;'
2023-07-12 12:09:44 -04:00
'filename="{} organization usage report for year {}'
2021-11-12 18:32:35 +00:00
' - generated on {}.csv"'.format(
2023-07-12 12:09:44 -04:00
current_organization.name,
2021-11-12 18:32:35 +00:00
selected_year,
datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%fZ")
))
}
2021-11-12 14:35:16 +00:00
2023-07-12 12:09:44 -04:00
@main.route("/organizations/<uuid:org_id>/trial-services", methods=['GET'])
@user_is_platform_admin
2023-07-12 12:09:44 -04:00
def organization_trial_mode_services(org_id):
return render_template(
2023-07-12 12:09:44 -04:00
'views/organizations/organization/trial-mode-services.html',
search_form=SearchByNameForm(),
)
2023-07-12 12:09:44 -04:00
@main.route("/organizations/<uuid:org_id>/users", methods=['GET'])
@user_has_permissions()
2018-02-19 16:53:29 +00:00
def manage_org_users(org_id):
return render_template(
2023-07-12 12:09:44 -04:00
'views/organizations/organization/users/index.html',
users=current_organization.team_members,
show_search_box=(len(current_organization.team_members) > 7),
2018-02-19 16:53:29 +00:00
form=SearchUsersForm(),
)
2018-02-19 16:53:29 +00:00
2023-07-12 12:09:44 -04:00
@main.route("/organizations/<uuid:org_id>/users/invite", methods=['GET', 'POST'])
@user_has_permissions()
2018-02-19 16:53:29 +00:00
def invite_org_user(org_id):
form = InviteOrgUserForm(
2021-07-15 14:21:58 +01:00
inviter_email_address=current_user.email_address
2018-02-19 16:53:29 +00:00
)
if form.validate_on_submit():
email_address = form.email_address.data
invited_org_user = InvitedOrgUser.create(
2018-02-19 16:53:29 +00:00
current_user.id,
org_id,
email_address
)
flash('Invite sent to {}'.format(invited_org_user.email_address), 'default_with_tick')
return redirect(url_for('.manage_org_users', org_id=org_id))
return render_template(
2023-07-12 12:09:44 -04:00
'views/organizations/organization/users/invite-org-user.html',
2018-02-19 16:53:29 +00:00
form=form
)
2023-07-12 12:09:44 -04:00
@main.route("/organizations/<uuid:org_id>/users/<uuid:user_id>", methods=['GET'])
@user_has_permissions()
2023-07-12 12:09:44 -04:00
def edit_organization_user(org_id, user_id):
# The only action that can be done to an org user is to remove them from the org.
# This endpoint is used to get the ID of the user to delete without passing it as a
# query string, but it uses the template for all org team members in order to avoid
# having a page containing a single link.
2018-02-19 16:53:29 +00:00
return render_template(
2023-07-12 12:09:44 -04:00
'views/organizations/organization/users/index.html',
users=current_organization.team_members,
show_search_box=(len(current_organization.team_members) > 7),
form=SearchUsersForm(),
user_to_remove=User.from_id(user_id)
2018-02-19 16:53:29 +00:00
)
2023-07-12 12:09:44 -04:00
@main.route("/organizations/<uuid:org_id>/users/<uuid:user_id>/delete", methods=['POST'])
@user_has_permissions()
2023-07-12 12:09:44 -04:00
def remove_user_from_organization(org_id, user_id):
organizations_client.remove_user_from_organization(org_id, user_id)
2018-02-19 16:53:29 +00:00
return redirect(url_for('.show_accounts_or_dashboard'))
2018-02-19 16:53:29 +00:00
2023-07-12 12:09:44 -04:00
@main.route("/organizations/<uuid:org_id>/cancel-invited-user/<uuid:invited_user_id>", methods=['GET'])
@user_has_permissions()
2018-02-19 16:53:29 +00:00
def cancel_invited_org_user(org_id, invited_user_id):
org_invite_api_client.cancel_invited_user(org_id=org_id, invited_user_id=invited_user_id)
invited_org_user = InvitedOrgUser.by_id_and_org_id(org_id, invited_user_id)
flash(f'Invitation cancelled for {invited_org_user.email_address}', 'default_with_tick')
2018-02-19 16:53:29 +00:00
return redirect(url_for('main.manage_org_users', org_id=org_id))
2018-03-06 17:12:31 +00:00
2023-07-12 12:09:44 -04:00
@main.route("/organizations/<uuid:org_id>/settings/", methods=['GET'])
@user_is_platform_admin
2023-07-12 12:09:44 -04:00
def organization_settings(org_id):
2018-03-06 17:12:31 +00:00
return render_template(
2023-07-12 12:09:44 -04:00
'views/organizations/organization/settings/index.html',
2018-03-06 17:12:31 +00:00
)
2023-07-12 12:09:44 -04:00
@main.route("/organizations/<uuid:org_id>/settings/edit-name", methods=['GET', 'POST'])
@user_is_platform_admin
2023-07-12 12:09:44 -04:00
def edit_organization_name(org_id):
form = RenameOrganizationForm(name=current_organization.name)
2018-03-06 17:12:31 +00:00
if form.validate_on_submit():
try:
2023-07-12 12:09:44 -04:00
current_organization.update(name=form.name.data)
except HTTPError as http_error:
2022-12-20 09:44:33 -05:00
error_msg = 'Organization name already exists'
if http_error.status_code == 400 and error_msg in http_error.message:
2022-12-20 09:44:33 -05:00
form.name.errors.append('This organization name is already in use')
else:
raise http_error
else:
2023-07-12 12:09:44 -04:00
return redirect(url_for('.organization_settings', org_id=org_id))
2018-03-06 17:12:31 +00:00
return render_template(
2023-07-12 12:09:44 -04:00
'views/organizations/organization/settings/edit-name.html',
2018-03-06 17:12:31 +00:00
form=form,
)
2023-07-12 12:09:44 -04:00
@main.route("/organizations/<uuid:org_id>/settings/edit-type", methods=['GET', 'POST'])
@user_is_platform_admin
2023-07-12 12:09:44 -04:00
def edit_organization_type(org_id):
2023-07-12 12:09:44 -04:00
form = OrganizationOrganizationTypeForm(
organization_type=current_organization.organization_type
)
if form.validate_on_submit():
2023-07-12 12:09:44 -04:00
current_organization.update(
organization_type=form.organization_type.data,
delete_services_cache=True,
)
2023-07-12 12:09:44 -04:00
return redirect(url_for('.organization_settings', org_id=org_id))
return render_template(
2023-07-12 12:09:44 -04:00
'views/organizations/organization/settings/edit-type.html',
form=form,
)
2023-07-12 12:09:44 -04:00
@main.route("/organizations/<uuid:org_id>/settings/set-email-branding", methods=['GET', 'POST'])
@user_is_platform_admin
2023-07-12 12:09:44 -04:00
def edit_organization_email_branding(org_id):
email_branding = email_branding_client.get_all_email_branding()
form = AdminSetEmailBrandingForm(
all_branding_options=get_branding_as_value_and_label(email_branding),
2023-07-12 12:09:44 -04:00
current_branding=current_organization.email_branding_id,
)
if form.validate_on_submit():
return redirect(url_for(
2023-07-12 12:09:44 -04:00
'.organization_preview_email_branding',
org_id=org_id,
branding_style=form.branding_style.data,
))
return render_template(
2023-07-12 12:09:44 -04:00
'views/organizations/organization/settings/set-email-branding.html',
form=form,
search_form=SearchByNameForm()
)
2023-07-12 12:09:44 -04:00
@main.route("/organizations/<uuid:org_id>/settings/preview-email-branding", methods=['GET', 'POST'])
@user_is_platform_admin
2023-07-12 12:09:44 -04:00
def organization_preview_email_branding(org_id):
branding_style = request.args.get('branding_style', None)
form = AdminPreviewBrandingForm(branding_style=branding_style)
if form.validate_on_submit():
2023-07-12 12:09:44 -04:00
current_organization.update(
email_branding_id=form.branding_style.data,
delete_services_cache=True,
)
2023-07-12 12:09:44 -04:00
return redirect(url_for('.organization_settings', org_id=org_id))
return render_template(
2023-07-12 12:09:44 -04:00
'views/organizations/organization/settings/preview-email-branding.html',
form=form,
2023-07-12 12:09:44 -04:00
action=url_for('main.organization_preview_email_branding', org_id=org_id),
)
2023-07-12 12:09:44 -04:00
@main.route("/organizations/<uuid:org_id>/settings/edit-organization-domains", methods=['GET', 'POST'])
@user_is_platform_admin
2023-07-12 12:09:44 -04:00
def edit_organization_domains(org_id):
2023-07-12 12:09:44 -04:00
form = AdminOrganizationDomainsForm()
if form.validate_on_submit():
try:
2023-07-12 12:09:44 -04:00
organizations_client.update_organization(
org_id,
domains=list(OrderedDict.fromkeys(
domain.lower()
for domain in filter(None, form.domains.data)
)),
)
except HTTPError as e:
error_message = "Domain already exists"
if e.status_code == 400 and error_message in e.message:
flash("This domain is already in use", "error")
return render_template(
2023-07-12 12:09:44 -04:00
'views/organizations/organization/settings/edit-domains.html',
form=form,
)
else:
raise e
2023-07-12 12:09:44 -04:00
return redirect(url_for('.organization_settings', org_id=org_id))
2023-07-12 12:09:44 -04:00
form.populate(current_organization.domains)
return render_template(
2023-07-12 12:09:44 -04:00
'views/organizations/organization/settings/edit-domains.html',
form=form,
)
2023-07-12 12:09:44 -04:00
@main.route("/organizations/<uuid:org_id>/settings/edit-go-live-notes", methods=['GET', 'POST'])
2019-05-13 14:50:40 +01:00
@user_is_platform_admin
2023-07-12 12:09:44 -04:00
def edit_organization_go_live_notes(org_id):
2019-05-13 14:50:40 +01:00
2023-07-12 12:09:44 -04:00
form = AdminOrganizationGoLiveNotesForm()
2019-05-13 14:50:40 +01:00
if form.validate_on_submit():
2023-07-12 12:09:44 -04:00
organizations_client.update_organization(
2019-05-13 14:50:40 +01:00
org_id,
request_to_go_live_notes=form.request_to_go_live_notes.data
)
2023-07-12 12:09:44 -04:00
return redirect(url_for('.organization_settings', org_id=org_id))
2019-05-13 14:50:40 +01:00
2023-07-12 12:09:44 -04:00
org = organizations_client.get_organization(org_id)
2019-05-13 14:50:40 +01:00
form.request_to_go_live_notes.data = org['request_to_go_live_notes']
return render_template(
2023-07-12 12:09:44 -04:00
'views/organizations/organization/settings/edit-go-live-notes.html',
2019-05-13 14:50:40 +01:00
form=form,
)
2023-07-12 12:09:44 -04:00
@main.route("/organizations/<uuid:org_id>/settings/notes", methods=['GET', 'POST'])
@user_is_platform_admin
2023-07-12 12:09:44 -04:00
def edit_organization_notes(org_id):
form = AdminNotesForm(notes=current_organization.notes)
if form.validate_on_submit():
2023-07-12 12:09:44 -04:00
if form.notes.data == current_organization.notes:
return redirect(url_for('.organization_settings', org_id=org_id))
2023-07-12 12:09:44 -04:00
current_organization.update(
notes=form.notes.data
)
2023-07-12 12:09:44 -04:00
return redirect(url_for('.organization_settings', org_id=org_id))
return render_template(
2023-07-12 12:09:44 -04:00
'views/organizations/organization/settings/edit-organization-notes.html',
form=form,
)
2023-07-12 12:09:44 -04:00
@main.route("/organizations/<uuid:org_id>/settings/edit-billing-details", methods=['GET', 'POST'])
@user_is_platform_admin
2023-07-12 12:09:44 -04:00
def edit_organization_billing_details(org_id):
2022-03-15 10:50:18 +00:00
form = AdminBillingDetailsForm(
2023-07-12 12:09:44 -04:00
billing_contact_email_addresses=current_organization.billing_contact_email_addresses,
billing_contact_names=current_organization.billing_contact_names,
billing_reference=current_organization.billing_reference,
purchase_order_number=current_organization.purchase_order_number,
notes=current_organization.notes,
2021-02-05 11:56:05 +00:00
)
if form.validate_on_submit():
2023-07-12 12:09:44 -04:00
current_organization.update(
2021-02-05 11:56:05 +00:00
billing_contact_email_addresses=form.billing_contact_email_addresses.data,
billing_contact_names=form.billing_contact_names.data,
billing_reference=form.billing_reference.data,
purchase_order_number=form.purchase_order_number.data,
notes=form.notes.data,
)
2023-07-12 12:09:44 -04:00
return redirect(url_for('.organization_settings', org_id=org_id))
2021-02-05 11:56:05 +00:00
return render_template(
2023-07-12 12:09:44 -04:00
'views/organizations/organization/settings/edit-organization-billing-details.html',
2021-02-05 11:56:05 +00:00
form=form,
)
2021-12-07 13:42:44 +00:00
2023-07-12 12:09:44 -04:00
@main.route("/organizations/<uuid:org_id>/billing")
2021-12-07 13:42:44 +00:00
@user_is_platform_admin
2023-07-12 12:09:44 -04:00
def organization_billing(org_id):
2021-12-07 13:42:44 +00:00
return render_template(
2023-07-12 12:09:44 -04:00
'views/organizations/organization/billing.html'
2021-12-07 13:42:44 +00:00
)