2025-11-05 13:20:21 -08:00
|
|
|
import re
|
2025-10-20 12:11:01 -07: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
|
|
|
|
2025-10-30 14:14:17 -07:00
|
|
|
from flask import (
|
2025-11-05 13:20:21 -08:00
|
|
|
Response,
|
2025-10-30 14:14:17 -07:00
|
|
|
current_app,
|
|
|
|
|
flash,
|
|
|
|
|
redirect,
|
|
|
|
|
render_template,
|
|
|
|
|
request,
|
|
|
|
|
session,
|
|
|
|
|
url_for,
|
|
|
|
|
)
|
2019-07-01 15:22:08 +01:00
|
|
|
from flask_login import current_user
|
2025-11-05 15:27:05 -08:00
|
|
|
from markupsafe import escape
|
2018-02-19 16:53:29 +00:00
|
|
|
|
2025-11-11 12:18:16 -08:00
|
|
|
from app import (
|
|
|
|
|
current_organization,
|
|
|
|
|
org_invite_api_client,
|
|
|
|
|
organizations_client,
|
|
|
|
|
service_api_client,
|
|
|
|
|
)
|
2025-10-30 10:00:52 -07:00
|
|
|
from app.enums import OrganizationType
|
2025-11-11 12:18:16 -08:00
|
|
|
from app.event_handlers import create_archive_service_event
|
2025-10-30 10:00:52 -07:00
|
|
|
from app.formatters import email_safe
|
2018-02-20 11:22:17 +00:00
|
|
|
from app.main import main
|
2018-02-19 16:53:29 +00:00
|
|
|
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,
|
2025-10-30 10:00:52 -07:00
|
|
|
CreateServiceForm,
|
2018-02-19 16:53:29 +00:00
|
|
|
InviteOrgUserForm,
|
2023-07-12 12:09:44 -04:00
|
|
|
OrganizationOrganizationTypeForm,
|
|
|
|
|
RenameOrganizationForm,
|
2019-02-19 17:26:16 +00:00
|
|
|
SearchByNameForm,
|
2018-02-20 11:22:17 +00:00
|
|
|
SearchUsersForm,
|
2018-02-19 16:53:29 +00:00
|
|
|
)
|
2025-10-30 10:00:52 -07:00
|
|
|
from app.main.views.add_service import _create_service
|
2020-02-28 12:32:47 +00:00
|
|
|
from app.main.views.dashboard import (
|
|
|
|
|
get_tuples_of_financial_years,
|
|
|
|
|
requested_and_current_financial_year,
|
|
|
|
|
)
|
2023-07-12 12:09:44 -04:00
|
|
|
from app.models.organization import AllOrganizations, Organization
|
2025-11-05 13:20:21 -08:00
|
|
|
from app.models.service import Service
|
Make user API client return JSON, not a model
The data flow of other bits of our application looks like this:
```
API (returns JSON)
⬇
API client (returns a built in type, usually `dict`)
⬇
Model (returns an instance, eg of type `Service`)
⬇
View (returns HTML)
```
The user API client was architected weirdly, in that it returned a model
directly, like this:
```
API (returns JSON)
⬇
API client (returns a model, of type `User`, `InvitedUser`, etc)
⬇
View (returns HTML)
```
This mixing of different layers of the application is bad because it
makes it hard to write model code that doesn’t have circular
dependencies. As our application gets more complicated we will be
relying more on models to manage this complexity, so we should make it
easy, not hard to write them.
It also means that most of our mocking was of the User model, not just
the underlying JSON. So it would have been easy to introduce subtle bugs
to the user model, because it wasn’t being comprehensively tested. A lot
of the changed lines of code in this commit mean changing the tests to
mock only the JSON, which means that the model layer gets implicitly
tested.
For those reasons this commit changes the user API client to return
JSON, not an instance of `User` or other models.
2019-05-23 15:27:35 +01:00
|
|
|
from app.models.user import InvitedOrgUser, User
|
2025-11-05 13:20:21 -08:00
|
|
|
from app.notify_client import cache
|
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
|
2025-06-10 11:40:14 -07:00
|
|
|
from notifications_python_client.errors import HTTPError
|
2018-02-08 12:19:21 +00:00
|
|
|
|
2025-11-06 11:16:44 -08:00
|
|
|
EMAIL_REGEX = re.compile(r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$")
|
2025-11-05 13:20:21 -08:00
|
|
|
|
2018-02-08 12:19:21 +00:00
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
@main.route("/organizations", methods=["GET"])
|
2018-02-27 16:45:20 +00:00
|
|
|
@user_is_platform_admin
|
2023-07-12 12:09:44 -04:00
|
|
|
def organizations():
|
2018-02-08 12:19:21 +00:00
|
|
|
return render_template(
|
2023-08-25 09:12:23 -07:00
|
|
|
"views/organizations/index.html",
|
2023-07-12 12:09:44 -04:00
|
|
|
organizations=AllOrganizations(),
|
2019-03-21 11:22:53 +00:00
|
|
|
search_form=SearchByNameForm(),
|
2018-02-08 12:19:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
@main.route("/organizations/add", methods=["GET", "POST"])
|
2018-02-27 16:45:20 +00:00
|
|
|
@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:
|
2023-08-25 09:12:23 -07:00
|
|
|
return redirect(
|
|
|
|
|
url_for(
|
|
|
|
|
".organization_settings",
|
|
|
|
|
org_id=Organization.create_from_form(form).id,
|
|
|
|
|
)
|
|
|
|
|
)
|
2021-02-11 12:02:42 +00:00
|
|
|
except HTTPError as e:
|
2023-08-25 09:12:23 -07: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
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
return render_template("views/organizations/add-organization.html", form=form)
|
2018-02-14 13:08:44 +00:00
|
|
|
|
|
|
|
|
|
2025-12-08 09:12:19 -08:00
|
|
|
def get_organization_messages_sent(org_id):
|
2025-10-16 14:39:01 -07:00
|
|
|
try:
|
2025-10-20 12:26:07 -07:00
|
|
|
message_usage = organizations_client.get_organization_message_usage(org_id)
|
2025-10-16 14:39:01 -07:00
|
|
|
except Exception as e:
|
|
|
|
|
current_app.logger.error(f"Error fetching organization message usage: {e}")
|
|
|
|
|
message_usage = {}
|
|
|
|
|
|
2025-12-08 09:12:19 -08:00
|
|
|
return message_usage.get("messages_sent", 0)
|
2025-10-16 14:44:45 -07:00
|
|
|
|
|
|
|
|
|
2025-11-05 13:20:21 -08:00
|
|
|
def _handle_create_service(org_id):
|
|
|
|
|
create_service_form = CreateServiceForm(
|
|
|
|
|
organization_type=current_user.default_organization_type
|
|
|
|
|
or OrganizationType.FEDERAL
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if request.method == "POST" and create_service_form.validate_on_submit():
|
|
|
|
|
service_name = create_service_form.name.data
|
|
|
|
|
service_id, error = _create_service(
|
|
|
|
|
service_name,
|
|
|
|
|
create_service_form.organization_type.data,
|
|
|
|
|
email_safe(service_name),
|
|
|
|
|
create_service_form,
|
|
|
|
|
)
|
|
|
|
|
if not error:
|
|
|
|
|
current_organization.associate_service(service_id)
|
|
|
|
|
flash(f"Service '{service_name}' has been created", "default_with_tick")
|
|
|
|
|
session["new_service_id"] = service_id
|
|
|
|
|
return redirect(url_for(".organization_dashboard", org_id=org_id))
|
|
|
|
|
else:
|
|
|
|
|
flash("Error creating service", "error")
|
|
|
|
|
|
|
|
|
|
return create_service_form
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _handle_invite_user(org_id):
|
|
|
|
|
invite_user_form = InviteOrgUserForm(
|
|
|
|
|
inviter_email_address=current_user.email_address
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if request.method == "POST" and invite_user_form.validate_on_submit():
|
|
|
|
|
try:
|
|
|
|
|
invited_org_user = InvitedOrgUser.create(
|
|
|
|
|
current_user.id, org_id, invite_user_form.email_address.data
|
|
|
|
|
)
|
|
|
|
|
flash(
|
|
|
|
|
f"Invite sent to {invited_org_user.email_address}",
|
|
|
|
|
"default_with_tick",
|
|
|
|
|
)
|
|
|
|
|
return redirect(url_for(".organization_dashboard", org_id=org_id))
|
|
|
|
|
except Exception:
|
|
|
|
|
flash("Error sending invitation", "error")
|
|
|
|
|
|
|
|
|
|
return invite_user_form
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _handle_edit_service(org_id, service_id):
|
|
|
|
|
service = Service.from_id(service_id)
|
|
|
|
|
|
|
|
|
|
if request.method == "POST":
|
|
|
|
|
service_name = request.form.get("service_name", "").strip()
|
|
|
|
|
primary_contact = request.form.get("primary_contact", "").strip()
|
|
|
|
|
new_status = request.form.get("status")
|
|
|
|
|
|
|
|
|
|
if not service_name:
|
|
|
|
|
flash("Service name is required", "error")
|
2025-11-06 11:16:44 -08:00
|
|
|
elif primary_contact and not EMAIL_REGEX.match(primary_contact):
|
2025-11-05 13:20:21 -08:00
|
|
|
flash("Please enter a valid email address", "error")
|
|
|
|
|
else:
|
|
|
|
|
if service_name != service.name:
|
|
|
|
|
service.update(name=service_name)
|
|
|
|
|
|
|
|
|
|
if primary_contact != (service.billing_contact_email_addresses or ""):
|
|
|
|
|
service.update(billing_contact_email_addresses=primary_contact)
|
|
|
|
|
|
|
|
|
|
current_status = "trial" if service.trial_mode else "live"
|
|
|
|
|
if new_status != current_status:
|
|
|
|
|
service.update_status(live=(new_status == "live"))
|
|
|
|
|
cache.redis_client.delete("organizations")
|
|
|
|
|
|
|
|
|
|
flash("Service updated successfully", "default_with_tick")
|
|
|
|
|
session["updated_service_id"] = str(service_id)
|
|
|
|
|
return redirect(url_for(".organization_dashboard", org_id=org_id))
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
"id": service.id,
|
|
|
|
|
"name": (
|
2025-11-05 15:27:05 -08:00
|
|
|
escape(request.form.get("service_name", "").strip())
|
2025-11-05 13:20:21 -08:00
|
|
|
if request.method == "POST"
|
|
|
|
|
else service.name
|
|
|
|
|
),
|
|
|
|
|
"primary_contact": (
|
2025-11-05 15:27:05 -08:00
|
|
|
escape(request.form.get("primary_contact", "").strip())
|
2025-11-05 13:20:21 -08:00
|
|
|
if request.method == "POST"
|
|
|
|
|
else (service.billing_contact_email_addresses or "")
|
|
|
|
|
),
|
|
|
|
|
"status": "trial" if service.trial_mode else "live",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2025-11-11 12:18:16 -08:00
|
|
|
def _handle_delete_service(org_id, service_id):
|
|
|
|
|
if request.method != "POST":
|
|
|
|
|
flash("Invalid request method", "error")
|
|
|
|
|
return redirect(url_for(".organization_dashboard", org_id=org_id))
|
|
|
|
|
|
|
|
|
|
service = Service.from_id(service_id)
|
|
|
|
|
|
|
|
|
|
if not service.active or not (service.trial_mode or current_user.platform_admin):
|
|
|
|
|
flash("You don't have permission to delete this service", "error")
|
|
|
|
|
return redirect(url_for(".organization_dashboard", org_id=org_id))
|
|
|
|
|
|
|
|
|
|
confirm = request.form.get("confirm_delete")
|
|
|
|
|
if confirm != "delete":
|
|
|
|
|
flash("Delete confirmation was not provided", "error")
|
|
|
|
|
return redirect(url_for(".organization_dashboard", org_id=org_id))
|
|
|
|
|
|
|
|
|
|
cached_service_user_ids = [user.id for user in service.active_users]
|
|
|
|
|
|
|
|
|
|
service_api_client.archive_service(service_id, cached_service_user_ids)
|
2025-11-19 19:55:24 -08:00
|
|
|
create_archive_service_event(service_id=service_id, archived_by_id=current_user.id)
|
2025-11-11 12:18:16 -08:00
|
|
|
|
|
|
|
|
cache.redis_client.delete("organizations")
|
|
|
|
|
|
|
|
|
|
flash(f"'{service.name}' was deleted", "default_with_tick")
|
|
|
|
|
return redirect(url_for(".organization_dashboard", org_id=org_id))
|
|
|
|
|
|
|
|
|
|
|
2025-10-27 17:28:31 -07:00
|
|
|
def get_services_dashboard_data(organization, year):
|
2025-10-21 12:26:56 -07:00
|
|
|
try:
|
2025-10-30 17:30:20 -07:00
|
|
|
dashboard_data = organizations_client.get_organization_dashboard(
|
|
|
|
|
organization.id, year
|
|
|
|
|
)
|
2025-10-24 14:40:17 -07:00
|
|
|
services = dashboard_data.get("services", [])
|
2025-10-21 12:26:56 -07:00
|
|
|
except Exception as e:
|
2025-10-24 14:40:17 -07:00
|
|
|
current_app.logger.error(f"Error fetching dashboard data: {e}")
|
2025-10-21 12:26:56 -07:00
|
|
|
return []
|
|
|
|
|
|
2025-10-24 14:40:17 -07:00
|
|
|
for service in services:
|
2025-10-21 12:26:56 -07:00
|
|
|
service["id"] = service.get("service_id")
|
|
|
|
|
service["name"] = service.get("service_name")
|
2025-10-27 17:28:31 -07:00
|
|
|
service["recent_template"] = service.get("recent_sms_template_name") or "N/A"
|
|
|
|
|
service["primary_contact"] = service.get("primary_contact") or "N/A"
|
2025-10-21 12:26:56 -07:00
|
|
|
|
|
|
|
|
emails_sent = service.get("emails_sent", 0)
|
|
|
|
|
sms_sent = service.get("sms_billable_units", 0)
|
|
|
|
|
sms_remainder = service.get("sms_remainder", 0)
|
|
|
|
|
sms_cost = service.get("sms_cost", 0)
|
|
|
|
|
|
|
|
|
|
usage_parts = []
|
|
|
|
|
if emails_sent > 0:
|
|
|
|
|
usage_parts.append(f"{emails_sent:,} emails")
|
|
|
|
|
if sms_sent > 0 or sms_remainder > 0:
|
|
|
|
|
if sms_cost > 0:
|
2025-10-30 17:30:20 -07:00
|
|
|
usage_parts.append(
|
2025-12-08 12:26:00 -08:00
|
|
|
f"{sms_sent:,} sms ({sms_remainder:,} message parts remaining, ${sms_cost:,.2f})"
|
2025-10-30 17:30:20 -07:00
|
|
|
)
|
2025-10-21 12:26:56 -07:00
|
|
|
else:
|
2025-12-08 12:26:00 -08:00
|
|
|
usage_parts.append(f"{sms_sent:,} sms ({sms_remainder:,} message parts remaining)")
|
2025-10-21 12:26:56 -07:00
|
|
|
|
|
|
|
|
service["usage"] = ", ".join(usage_parts) if usage_parts else "No usage"
|
|
|
|
|
|
|
|
|
|
return services
|
|
|
|
|
|
|
|
|
|
|
2025-10-30 10:00:52 -07:00
|
|
|
@main.route("/organizations/<uuid:org_id>", methods=["GET", "POST"])
|
2025-10-16 14:44:45 -07:00
|
|
|
@user_has_permissions()
|
|
|
|
|
def organization_dashboard(org_id):
|
|
|
|
|
if not current_app.config.get("ORGANIZATION_DASHBOARD_ENABLED", False):
|
|
|
|
|
return redirect(url_for(".organization_usage", org_id=org_id))
|
|
|
|
|
|
|
|
|
|
year = requested_and_current_financial_year(request)[0]
|
2025-10-30 10:00:52 -07:00
|
|
|
action = request.args.get("action")
|
2025-11-05 13:20:21 -08:00
|
|
|
service_id = request.args.get("service_id")
|
2025-10-30 10:00:52 -07:00
|
|
|
|
|
|
|
|
create_service_form = None
|
|
|
|
|
invite_user_form = None
|
2025-11-05 13:20:21 -08:00
|
|
|
edit_service_data = None
|
|
|
|
|
|
|
|
|
|
if action == "create-service":
|
|
|
|
|
result = _handle_create_service(org_id)
|
|
|
|
|
if isinstance(result, Response):
|
|
|
|
|
return result
|
|
|
|
|
create_service_form = result
|
|
|
|
|
|
|
|
|
|
elif action == "invite-user":
|
|
|
|
|
result = _handle_invite_user(org_id)
|
|
|
|
|
if isinstance(result, Response):
|
|
|
|
|
return result
|
|
|
|
|
invite_user_form = result
|
|
|
|
|
|
|
|
|
|
elif action == "edit-service" and service_id:
|
|
|
|
|
result = _handle_edit_service(org_id, service_id)
|
|
|
|
|
if isinstance(result, Response):
|
|
|
|
|
return result
|
|
|
|
|
edit_service_data = result
|
2025-10-16 14:39:01 -07:00
|
|
|
|
2025-11-11 12:18:16 -08:00
|
|
|
elif action == "delete-service" and service_id:
|
|
|
|
|
return _handle_delete_service(org_id, service_id)
|
|
|
|
|
|
2025-12-08 09:12:19 -08:00
|
|
|
messages_sent = get_organization_messages_sent(org_id)
|
2025-10-16 14:39:01 -07:00
|
|
|
|
2018-02-08 16:18:18 +00:00
|
|
|
return render_template(
|
2023-08-25 09:12:23 -07:00
|
|
|
"views/organizations/organization/index.html",
|
2020-02-28 12:32:47 +00:00
|
|
|
selected_year=year,
|
2025-11-05 13:20:21 -08:00
|
|
|
services=get_services_dashboard_data(current_organization, year),
|
2025-10-20 12:11:01 -07:00
|
|
|
live_services=len(current_organization.live_services),
|
|
|
|
|
trial_services=len(current_organization.trial_services),
|
|
|
|
|
suspended_services=len(current_organization.suspended_services),
|
|
|
|
|
total_services=len(current_organization.services),
|
2025-10-30 10:00:52 -07:00
|
|
|
create_service_form=create_service_form,
|
|
|
|
|
invite_user_form=invite_user_form,
|
2025-11-05 13:20:21 -08:00
|
|
|
edit_service_data=edit_service_data,
|
|
|
|
|
new_service_id=session.pop("new_service_id", None),
|
|
|
|
|
updated_service_id=session.pop("updated_service_id", None),
|
2025-12-08 09:12:19 -08:00
|
|
|
messages_sent=messages_sent,
|
2025-10-14 12:27:15 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@main.route("/organizations/<uuid:org_id>/usage", methods=["GET"])
|
|
|
|
|
@user_has_permissions()
|
|
|
|
|
def organization_usage(org_id):
|
|
|
|
|
year, current_financial_year = requested_and_current_financial_year(request)
|
|
|
|
|
services = current_organization.services_and_usage(financial_year=year)["services"]
|
|
|
|
|
|
|
|
|
|
return render_template(
|
|
|
|
|
"views/organizations/organization/usage.html",
|
|
|
|
|
services=services,
|
|
|
|
|
years=get_tuples_of_financial_years(
|
|
|
|
|
partial(url_for, ".organization_usage", org_id=current_organization.id),
|
|
|
|
|
start=current_financial_year - 2,
|
|
|
|
|
end=current_financial_year,
|
|
|
|
|
),
|
|
|
|
|
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
|
|
|
**{
|
2023-08-25 09:12:23 -07:00
|
|
|
f"total_{key}": sum(service[key] for service in services)
|
|
|
|
|
for key in ("emails_sent", "sms_cost")
|
2021-11-12 14:35:16 +00:00
|
|
|
},
|
|
|
|
|
download_link=url_for(
|
2023-08-25 09:12:23 -07:00
|
|
|
".download_organization_usage_report", org_id=org_id, selected_year=year
|
|
|
|
|
),
|
2018-02-08 16:18:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07: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):
|
2025-10-14 13:37:04 -07:00
|
|
|
# Validate and sanitize selected_year to prevent header injection
|
|
|
|
|
selected_year_input = request.args.get("selected_year", "")
|
|
|
|
|
if selected_year_input.isdigit() and len(selected_year_input) == 4:
|
|
|
|
|
selected_year = str(int(selected_year_input))
|
2025-09-18 16:42:39 -04:00
|
|
|
else:
|
|
|
|
|
selected_year = str(datetime.now().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
|
2023-08-25 09:12:23 -07:00
|
|
|
)["services"]
|
2021-11-12 18:32:35 +00:00
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
unit_column_names = OrderedDict(
|
|
|
|
|
[
|
|
|
|
|
("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
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
monetary_column_names = OrderedDict(
|
|
|
|
|
[
|
|
|
|
|
("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 = [
|
2021-11-25 10:15:50 +00:00
|
|
|
list(unit_column_names.values()) + list(monetary_column_names.values())
|
2021-11-25 10:07:53 +00:00
|
|
|
] + [
|
2023-08-25 09:12:23 -07: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
|
|
|
|
2025-09-18 16:42:39 -04:00
|
|
|
# Sanitize organization name for filename to prevent header injection
|
2025-09-26 06:57:18 -04:00
|
|
|
safe_org_name = re.sub(r"[^\w\s-]", "", current_organization.name).strip()
|
|
|
|
|
safe_org_name = re.sub(r"[-\s]+", "-", safe_org_name)
|
2025-09-18 16:42:39 -04:00
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
return (
|
|
|
|
|
Spreadsheet.from_rows(org_usage_data).as_csv_data,
|
|
|
|
|
200,
|
|
|
|
|
{
|
|
|
|
|
"Content-Type": "text/csv; charset=utf-8",
|
|
|
|
|
"Content-Disposition": (
|
2025-10-14 13:32:11 -07:00
|
|
|
f'inline;filename="{safe_org_name} organization usage report for year {selected_year}'
|
|
|
|
|
f' - generated on {datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%fZ")}.csv"'
|
2023-08-25 09:12:23 -07:00
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
)
|
2021-11-12 14:35:16 +00:00
|
|
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
@main.route("/organizations/<uuid:org_id>/trial-services", methods=["GET"])
|
2019-05-21 14:56:15 +01:00
|
|
|
@user_is_platform_admin
|
2023-07-12 12:09:44 -04:00
|
|
|
def organization_trial_mode_services(org_id):
|
2019-05-21 14:56:15 +01:00
|
|
|
return render_template(
|
2023-08-25 09:12:23 -07:00
|
|
|
"views/organizations/organization/trial-mode-services.html",
|
2019-05-21 14:56:15 +01:00
|
|
|
search_form=SearchByNameForm(),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
@main.route("/organizations/<uuid:org_id>/users", methods=["GET"])
|
2018-03-05 14:00:47 +00:00
|
|
|
@user_has_permissions()
|
2018-02-19 16:53:29 +00:00
|
|
|
def manage_org_users(org_id):
|
2018-02-08 12:19:21 +00:00
|
|
|
return render_template(
|
2023-08-25 09:12:23 -07:00
|
|
|
"views/organizations/organization/users/index.html",
|
2023-07-12 12:09:44 -04:00
|
|
|
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-08 12:19:21 +00:00
|
|
|
)
|
2018-02-19 16:53:29 +00:00
|
|
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
@main.route("/organizations/<uuid:org_id>/users/invite", methods=["GET", "POST"])
|
2018-03-05 14:00:47 +00:00
|
|
|
@user_has_permissions()
|
2018-02-19 16:53:29 +00:00
|
|
|
def invite_org_user(org_id):
|
2023-08-25 09:12:23 -07:00
|
|
|
form = InviteOrgUserForm(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
|
2023-08-25 09:12:23 -07:00
|
|
|
invited_org_user = InvitedOrgUser.create(current_user.id, org_id, email_address)
|
2018-02-19 16:53:29 +00:00
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
flash(
|
|
|
|
|
"Invite sent to {}".format(invited_org_user.email_address),
|
|
|
|
|
"default_with_tick",
|
|
|
|
|
)
|
|
|
|
|
return redirect(url_for(".manage_org_users", org_id=org_id))
|
2018-02-19 16:53:29 +00:00
|
|
|
|
|
|
|
|
return render_template(
|
2023-08-25 09:12:23 -07:00
|
|
|
"views/organizations/organization/users/invite-org-user.html", form=form
|
2018-02-19 16:53:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
@main.route("/organizations/<uuid:org_id>/users/<uuid:user_id>", methods=["GET"])
|
2018-03-05 14:00:47 +00:00
|
|
|
@user_has_permissions()
|
2023-07-12 12:09:44 -04:00
|
|
|
def edit_organization_user(org_id, user_id):
|
2022-01-11 14:16:37 +00:00
|
|
|
# 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-08-25 09:12:23 -07:00
|
|
|
"views/organizations/organization/users/index.html",
|
2023-07-12 12:09:44 -04:00
|
|
|
users=current_organization.team_members,
|
|
|
|
|
show_search_box=(len(current_organization.team_members) > 7),
|
2022-01-11 14:16:37 +00:00
|
|
|
form=SearchUsersForm(),
|
2023-08-25 09:12:23 -07:00
|
|
|
user_to_remove=User.from_id(user_id),
|
2018-02-19 16:53:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
@main.route(
|
|
|
|
|
"/organizations/<uuid:org_id>/users/<uuid:user_id>/delete", methods=["POST"]
|
|
|
|
|
)
|
2018-03-05 14:00:47 +00:00
|
|
|
@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
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
return redirect(url_for(".show_accounts_or_dashboard"))
|
2018-02-19 16:53:29 +00:00
|
|
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
@main.route(
|
|
|
|
|
"/organizations/<uuid:org_id>/cancel-invited-user/<uuid:invited_user_id>",
|
|
|
|
|
methods=["GET"],
|
|
|
|
|
)
|
2018-03-05 14:00:47 +00:00
|
|
|
@user_has_permissions()
|
2018-02-19 16:53:29 +00:00
|
|
|
def cancel_invited_org_user(org_id, invited_user_id):
|
2023-08-25 09:12:23 -07:00
|
|
|
org_invite_api_client.cancel_invited_user(
|
|
|
|
|
org_id=org_id, invited_user_id=invited_user_id
|
|
|
|
|
)
|
2018-02-19 16:53:29 +00:00
|
|
|
|
2020-08-17 14:30:09 +01:00
|
|
|
invited_org_user = InvitedOrgUser.by_id_and_org_id(org_id, invited_user_id)
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
flash(
|
|
|
|
|
f"Invitation cancelled for {invited_org_user.email_address}",
|
|
|
|
|
"default_with_tick",
|
|
|
|
|
)
|
|
|
|
|
return redirect(url_for("main.manage_org_users", org_id=org_id))
|
2018-03-06 17:12:31 +00:00
|
|
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
@main.route("/organizations/<uuid:org_id>/settings/", methods=["GET"])
|
2019-06-03 13:29:28 +01:00
|
|
|
@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-08-25 09:12:23 -07:00
|
|
|
"views/organizations/organization/settings/index.html",
|
2018-03-06 17:12:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
@main.route("/organizations/<uuid:org_id>/settings/edit-name", methods=["GET", "POST"])
|
2019-06-03 13:29:28 +01:00
|
|
|
@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():
|
2022-01-11 14:24:25 +00:00
|
|
|
try:
|
2023-07-12 12:09:44 -04:00
|
|
|
current_organization.update(name=form.name.data)
|
2022-01-11 14:24:25 +00:00
|
|
|
except HTTPError as http_error:
|
2023-08-25 09:12:23 -07:00
|
|
|
error_msg = "Organization name already exists"
|
2022-01-11 14:24:25 +00:00
|
|
|
if http_error.status_code == 400 and error_msg in http_error.message:
|
2023-08-25 09:12:23 -07:00
|
|
|
form.name.errors.append("This organization name is already in use")
|
2022-01-11 14:24:25 +00:00
|
|
|
else:
|
|
|
|
|
raise http_error
|
|
|
|
|
else:
|
2023-08-25 09:12:23 -07:00
|
|
|
return redirect(url_for(".organization_settings", org_id=org_id))
|
2018-03-06 17:12:31 +00:00
|
|
|
|
|
|
|
|
return render_template(
|
2023-08-25 09:12:23 -07:00
|
|
|
"views/organizations/organization/settings/edit-name.html",
|
2018-03-06 17:12:31 +00:00
|
|
|
form=form,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
@main.route("/organizations/<uuid:org_id>/settings/edit-type", methods=["GET", "POST"])
|
2019-02-19 17:26:16 +00:00
|
|
|
@user_is_platform_admin
|
2023-07-12 12:09:44 -04:00
|
|
|
def edit_organization_type(org_id):
|
|
|
|
|
form = OrganizationOrganizationTypeForm(
|
|
|
|
|
organization_type=current_organization.organization_type
|
2019-02-19 17:26:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if form.validate_on_submit():
|
2023-07-12 12:09:44 -04:00
|
|
|
current_organization.update(
|
|
|
|
|
organization_type=form.organization_type.data,
|
2019-10-03 11:57:24 +01:00
|
|
|
delete_services_cache=True,
|
2019-02-19 17:26:16 +00:00
|
|
|
)
|
2023-08-25 09:12:23 -07:00
|
|
|
return redirect(url_for(".organization_settings", org_id=org_id))
|
2019-02-19 17:26:16 +00:00
|
|
|
|
|
|
|
|
return render_template(
|
2023-08-25 09:12:23 -07:00
|
|
|
"views/organizations/organization/settings/edit-type.html",
|
2019-02-19 17:26:16 +00:00
|
|
|
form=form,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
@main.route(
|
|
|
|
|
"/organizations/<uuid:org_id>/settings/edit-organization-domains",
|
|
|
|
|
methods=["GET", "POST"],
|
|
|
|
|
)
|
2019-02-19 17:26:16 +00:00
|
|
|
@user_is_platform_admin
|
2023-07-12 12:09:44 -04:00
|
|
|
def edit_organization_domains(org_id):
|
|
|
|
|
form = AdminOrganizationDomainsForm()
|
2019-02-19 17:26:16 +00:00
|
|
|
|
|
|
|
|
if form.validate_on_submit():
|
2020-05-15 17:50:30 +01:00
|
|
|
try:
|
2023-07-12 12:09:44 -04:00
|
|
|
organizations_client.update_organization(
|
2020-05-15 17:50:30 +01:00
|
|
|
org_id,
|
2023-08-25 09:12:23 -07:00
|
|
|
domains=list(
|
|
|
|
|
OrderedDict.fromkeys(
|
|
|
|
|
domain.lower() for domain in filter(None, form.domains.data)
|
|
|
|
|
)
|
|
|
|
|
),
|
2020-05-15 17:50:30 +01:00
|
|
|
)
|
|
|
|
|
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-08-25 09:12:23 -07:00
|
|
|
"views/organizations/organization/settings/edit-domains.html",
|
2020-05-15 17:50:30 +01:00
|
|
|
form=form,
|
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
raise e
|
2023-08-25 09:12:23 -07:00
|
|
|
return redirect(url_for(".organization_settings", org_id=org_id))
|
2019-02-19 17:26:16 +00:00
|
|
|
|
2023-07-12 12:09:44 -04:00
|
|
|
form.populate(current_organization.domains)
|
2019-02-19 17:26:16 +00:00
|
|
|
|
|
|
|
|
return render_template(
|
2023-08-25 09:12:23 -07:00
|
|
|
"views/organizations/organization/settings/edit-domains.html",
|
2019-02-19 17:26:16 +00:00
|
|
|
form=form,
|
2019-05-13 14:50:40 +01:00
|
|
|
)
|
2021-02-05 10:52:08 +00:00
|
|
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
@main.route("/organizations/<uuid:org_id>/settings/notes", methods=["GET", "POST"])
|
2021-02-05 10:52:08 +00:00
|
|
|
@user_is_platform_admin
|
2023-07-12 12:09:44 -04:00
|
|
|
def edit_organization_notes(org_id):
|
|
|
|
|
form = AdminNotesForm(notes=current_organization.notes)
|
2021-02-05 10:52:08 +00:00
|
|
|
|
|
|
|
|
if form.validate_on_submit():
|
2023-07-12 12:09:44 -04:00
|
|
|
if form.notes.data == current_organization.notes:
|
2023-08-25 09:12:23 -07:00
|
|
|
return redirect(url_for(".organization_settings", org_id=org_id))
|
2021-02-05 10:52:08 +00:00
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
current_organization.update(notes=form.notes.data)
|
|
|
|
|
return redirect(url_for(".organization_settings", org_id=org_id))
|
2021-02-05 10:52:08 +00:00
|
|
|
|
|
|
|
|
return render_template(
|
2023-08-25 09:12:23 -07:00
|
|
|
"views/organizations/organization/settings/edit-organization-notes.html",
|
2021-02-05 10:52:08 +00:00
|
|
|
form=form,
|
|
|
|
|
)
|
2021-02-04 18:19:54 +00:00
|
|
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
@main.route(
|
|
|
|
|
"/organizations/<uuid:org_id>/settings/edit-billing-details",
|
|
|
|
|
methods=["GET", "POST"],
|
|
|
|
|
)
|
2021-02-04 18:19:54 +00:00
|
|
|
@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-08-25 09:12:23 -07:00
|
|
|
return redirect(url_for(".organization_settings", org_id=org_id))
|
2021-02-05 11:56:05 +00:00
|
|
|
|
|
|
|
|
return render_template(
|
2023-08-25 09:12:23 -07:00
|
|
|
"views/organizations/organization/settings/edit-organization-billing-details.html",
|
2021-02-05 11:56:05 +00:00
|
|
|
form=form,
|
|
|
|
|
)
|
Add new 'Billing' page for organisations
We want organisation team members to be able to see the MOU details for
their organisation. This change creates a new page called billing, which
contains these details. It's only visible to platform admin users now -
the plan is to add more information to this page, then to make it visible
to all organisation users.
The page showing the MOU covers the case of when agreement_signed is
True, when an agreement_signed is False, and when agreement_signed is
None. The case when an agreement_signed is None is very rare - it
signifies that the agreement is not signed but that we have some
service-specific agreements in place. We only have a few organisations
in this state, so it's unlikely that the content for this scenario will
be seen.
When an organisation has signed the agreement we may know the full
details (signing date, version signed, the person who signed it or who it
was signed on behalf of), or we may only have the name of the person who
signed the agreement. We show the more detailed content if possible, and
a less detailed version of the content if not.
There's a new route for downloading the agreement which is almost
identical to the existing `.service_download_agreement` route (plus the
test is almost the same), except that it takes an organisation ID
instead of a service ID.
2021-12-07 13:42:44 +00:00
|
|
|
|
|
|
|
|
|
2023-07-12 12:09:44 -04:00
|
|
|
@main.route("/organizations/<uuid:org_id>/billing")
|
Add new 'Billing' page for organisations
We want organisation team members to be able to see the MOU details for
their organisation. This change creates a new page called billing, which
contains these details. It's only visible to platform admin users now -
the plan is to add more information to this page, then to make it visible
to all organisation users.
The page showing the MOU covers the case of when agreement_signed is
True, when an agreement_signed is False, and when agreement_signed is
None. The case when an agreement_signed is None is very rare - it
signifies that the agreement is not signed but that we have some
service-specific agreements in place. We only have a few organisations
in this state, so it's unlikely that the content for this scenario will
be seen.
When an organisation has signed the agreement we may know the full
details (signing date, version signed, the person who signed it or who it
was signed on behalf of), or we may only have the name of the person who
signed the agreement. We show the more detailed content if possible, and
a less detailed version of the content if not.
There's a new route for downloading the agreement which is almost
identical to the existing `.service_download_agreement` route (plus the
test is almost the same), except that it takes an organisation ID
instead of a service ID.
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):
|
2023-08-25 09:12:23 -07:00
|
|
|
return render_template("views/organizations/organization/billing.html")
|