add front end for download all users

This commit is contained in:
Kenneth Kehl
2024-08-06 11:31:05 -07:00
parent a48f4fa476
commit 7b03d02447
4 changed files with 33 additions and 1 deletions

View File

@@ -3,7 +3,7 @@ import json
from collections import OrderedDict
from datetime import datetime
from flask import abort, flash, render_template, request, url_for
from flask import Response, abort, flash, render_template, request, send_file, url_for
from notifications_python_client.errors import HTTPError
from app import (
@@ -70,6 +70,29 @@ def platform_admin():
)
@main.route("/platform-admin/download-all-users")
@user_is_platform_admin
def download_all_users():
# Create a CSV string from the user data
csv_data = "Name,Email Address,Mobile Number,Service\n"
users = user_api_client.get_all_users_detailed()
if len(users) == 0:
return "No data to download."
users = json.loads(users)
for user in users:
if user["name"].startswith("e2e"):
continue
csv_data += f"{user['name']},{user['email_address']},{user['mobile_number']},{user['service']}\n"
# Create a direct download response with the CSV data and appropriate headers
response = Response(csv_data, content_type="text/csv")
response.headers["Content-Disposition"] = "attachment; filename=users.csv"
return response
def is_over_threshold(number, total, threshold):
percentage = number / total * 100 if total else 0
return percentage > threshold

View File

@@ -123,6 +123,7 @@ class HeaderNavigation(Navigation):
"get_billing_report",
"get_users_report",
"get_daily_volumes",
"download_all_users",
"get_daily_sms_provider_volumes",
"get_volumes_by_service",
"organizations",

View File

@@ -157,6 +157,10 @@ class UserApiClient(NotifyAdminAPIClient):
endpoint = "/user"
return self.get(endpoint)["data"]
def get_all_users_detailed(self):
endpoint = "/user/report-all-users"
return self.get(endpoint)["data"]
@cache.delete("service-{service_id}")
@cache.delete("service-{service_id}-template-folders")
@cache.delete("user-{user_id}")

View File

@@ -31,4 +31,8 @@
<p>
<a class="usa-link" href="{{ url_for('main.get_users_report') }}">Users Report</a>
</p>
<p>
<a class="usa-link" href="{{ url_for('main.download_all_users') }}">Download All Users</a>
</p>
{% endblock %}