Skip to content

Commit d8c20b4

Browse files
authored
refactor(admin): convert the last two tables and drop DataTables (#21035)
The account activity and API token tables on the user detail page were the last on DataTables. Both are now configured by `tabulator-*` attributes, so their DataTables setup in `warehouse.js` goes and nothing replaces it. Columns DataTables hid by default (IP address, hashed IP address, deprecated permissions) are marked `tabulator-visible="false"` and reached through the header menu, which also replaces the copy, CSV and column-visibility buttons. Neither needs a view change: events and API tokens already arrive newest-first. Headers use tristate sorting, so a third click restores that order. A token that was never used now shows a blank Last Used rather than "None", so it sorts after the used ones in either direction. With no tables left on it, DataTables goes, along with its stylesheets and the sidebar-toggle handler that recalculated its column widths. The production admin JavaScript bundle drops from 1.48 MiB to 1.20 MiB. The `datatables.net` entries under `overrides` in package.json stay. They pin transitive versions still pulled in by admin-lte, so removing them is a separate dependency change. Signed-off-by: Mike Fiedler <miketheman@gmail.com>
1 parent 80e3195 commit d8c20b4

8 files changed

Lines changed: 75 additions & 79 deletions

File tree

‎tests/functional/admin/test_users.py‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from http import HTTPStatus
44

55
from tests.common.db.accounts import UserFactory
6+
from tests.common.db.macaroons import MacaroonFactory
67

78

89
class TestUserList:
@@ -60,3 +61,40 @@ def test_moderator_sees_no_button_and_is_denied(self, webtest, login_admin):
6061
f"/admin/users/{target.username}/export/",
6162
status=HTTPStatus.FORBIDDEN,
6263
)
64+
65+
66+
class TestUserDetailTables:
67+
def test_account_activity_keeps_hidden_address_columns(self, webtest, login_admin):
68+
"""
69+
The address columns render but start hidden, so the column menu is
70+
what an admin uses to reach them.
71+
"""
72+
admin = login_admin()
73+
74+
page = webtest.get(f"/admin/users/{admin.username}/", status=HTTPStatus.OK)
75+
76+
table = page.html.find(id="account-activity")
77+
assert table.has_attr("data-tabulator-column-menu")
78+
headers = {
79+
th.get_text(strip=True): th for th in table.find("thead").find_all("th")
80+
}
81+
assert headers["IP address"]["tabulator-visible"] == "false"
82+
assert headers["Hashed IP address"]["tabulator-visible"] == "false"
83+
# The login itself is an event, so the table is not empty.
84+
assert table.find("tbody").find_all("tr")
85+
86+
def test_api_tokens_keeps_hidden_permissions_column(self, webtest, login_admin):
87+
admin = login_admin()
88+
MacaroonFactory.create(user_id=admin.id, description="a token")
89+
90+
page = webtest.get(f"/admin/users/{admin.username}/", status=HTTPStatus.OK)
91+
92+
table = page.html.find(id="api-tokens")
93+
assert table.has_attr("data-tabulator-column-menu")
94+
permissions = next(
95+
th
96+
for th in table.find("thead").find_all("th")
97+
if "Permissions" in th.get_text()
98+
)
99+
assert permissions["tabulator-visible"] == "false"
100+
assert "a token" in table.get_text()

‎warehouse/admin/static/css/admin.css‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,12 @@
33
@charset "utf-8";
44
@import "@fortawesome/fontawesome-free/css/all.min.css";
55

6-
@import "admin-lte/plugins/datatables-bs4/css/dataTables.bootstrap4.css";
7-
@import "admin-lte/plugins/datatables-responsive/css/responsive.bootstrap4.css";
8-
@import "admin-lte/plugins/datatables-buttons/css/buttons.bootstrap4.css";
9-
106
@import "tabulator-tables/dist/css/tabulator_bootstrap4.css";
117

128
@import "admin-lte/dist/css/adminlte.css";
139

1410
/* Admin styles for the Warehouse project (PyPI) */
1511

16-
/* Force DataTables to fill their container instead of using a fixed pixel width */
17-
table.dataTable {
18-
width: 100% !important;
19-
}
20-
2112
.table td,
2213
.table th {
2314
max-width: 30em;

‎warehouse/admin/static/js/warehouse.js‎

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,6 @@
33
import "admin-lte/plugins/jquery/jquery";
44
import "admin-lte/plugins/bootstrap/js/bootstrap.bundle";
55

6-
// Import DataTables JS
7-
import "admin-lte/plugins/datatables/jquery.dataTables";
8-
import "admin-lte/plugins/datatables-bs4/js/dataTables.bootstrap4";
9-
import "admin-lte/plugins/datatables-responsive/js/dataTables.responsive";
10-
import "admin-lte/plugins/datatables-responsive/js/responsive.bootstrap4";
11-
import "admin-lte/plugins/datatables-buttons/js/dataTables.buttons";
12-
import "admin-lte/plugins/datatables-buttons/js/buttons.bootstrap4";
13-
import "admin-lte/plugins/datatables-buttons/js/buttons.html5";
14-
import "admin-lte/plugins/datatables-buttons/js/buttons.colVis";
15-
166
// Import Chart JS
177
import "admin-lte/plugins/chart.js/Chart";
188

@@ -38,13 +28,6 @@ $(document).ready(function() {
3828
timeAgo();
3929
});
4030

41-
// Recalculate DataTables columns after sidebar toggle animation completes
42-
$(document).on("collapsed-done.lte.pushmenu shown.lte.pushmenu", function() {
43-
setTimeout(function() {
44-
$($.fn.dataTable.tables({ visible: true })).DataTable().columns.adjust();
45-
}, 350);
46-
});
47-
4831
document.querySelectorAll("a[data-form-submit]").forEach(function (element) {
4932
element.addEventListener("click", function(event) {
5033
// We're turning this element into a form submission, so instead of the
@@ -132,38 +115,6 @@ document.querySelectorAll(".copy-text").forEach(function (element) {
132115
});
133116
});
134117

135-
// Activate Datatables https://datatables.net/
136-
// Guard each one to not break execution if the table isn't present
137-
138-
// User Account Activity
139-
let accountActivityTable = $("#account-activity");
140-
if (accountActivityTable.length) {
141-
let table = accountActivityTable.DataTable({
142-
responsive: true,
143-
lengthChange: false,
144-
});
145-
// sort by time
146-
table.column(".time").order("desc").draw();
147-
// Hide some columns we don't need to see all the time
148-
table.columns([".ip_address", ".hashed_ip"]).visible(false);
149-
// add column visibility button
150-
new $.fn.dataTable.Buttons(table, {buttons: ["copy", "csv", "colvis"]});
151-
table.buttons().container().appendTo($(".col-md-6:eq(0)", table.table().container()));
152-
}
153-
154-
// User API Tokens
155-
let tokenTable = $("#api-tokens");
156-
if (tokenTable.length) {
157-
let table = tokenTable.DataTable({
158-
responsive: true,
159-
lengthChange: false,
160-
});
161-
table.columns([".last_used", ".created"]).order([1, "desc"]).draw();
162-
table.columns([".permissions_caveat"]).visible(false);
163-
new $.fn.dataTable.Buttons(table, {buttons: ["colvis"]});
164-
table.buttons().container().appendTo($(".col-md-6:eq(0)", table.table().container()));
165-
}
166-
167118
let organizationApplicationTurboModeSwitch = document.getElementById("organizationApplicationTurboMode");
168119
if (organizationApplicationTurboModeSwitch !== null) {
169120
let organizationApplicationTurboModeEnabled = JSON.parse(localStorage.getItem("organizationApplicationTurboModeEnabled") || false);

‎warehouse/admin/templates/admin/projects/detail.html‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ <h3 class="card-title">Project Observations</h3>
588588

589589
<div class="card-body">
590590
{% if observations %}
591-
{# TODO: Make this dataTables-enabled for sorting, filtering, etc. See Account Activity for an example. #}
591+
{# TODO: Make this a Tabulator table for sorting and filtering: add `data-tabulator` and `tabulator-*` attributes. See Account Activity for an example. #}
592592
<div class="table-responsive p-0">
593593
<table class="table table-hover table-striped">
594594
<thead>

‎warehouse/admin/templates/admin/projects/project_observations_list.html‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<div class="card">
1717
<div class="card-body">
1818
{% if observations %}
19-
{# TODO: Make this dataTables-enabled for sorting, filtering, etc. See Account Activity for an example. #}
19+
{# TODO: Make this a Tabulator table for sorting and filtering: add `data-tabulator` and `tabulator-*` attributes. See Account Activity for an example. #}
2020
<div class="table-responsive p-0">
2121
<table class="table table-hover table-striped">
2222
<thead>

‎warehouse/admin/templates/admin/projects/release_detail.html‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ <h3>Observations</h3>
251251
</div>
252252
<div class="card-body">
253253
{% if observations %}
254-
{# TODO: Make this dataTables-enabled for sorting, filtering, etc. See Account Activity for an example. #}
254+
{# TODO: Make this a Tabulator table for sorting and filtering: add `data-tabulator` and `tabulator-*` attributes. See Account Activity for an example. #}
255255
<div class="table-responsive p-0">
256256
<table class="table table-hover table-striped">
257257
<thead>

‎warehouse/admin/templates/admin/users/detail.html‎

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,15 +1011,23 @@ <h3 class="card-title">API Tokens</h3>
10111011

10121012
<div class="card-body">
10131013
{% if user.macaroons %}
1014-
<table id="api-tokens" class="table table-hover">
1014+
<table id="api-tokens" class="table table-hover"
1015+
data-tabulator
1016+
data-tabulator-column-menu
1017+
tabulator-layout="fitDataFill"
1018+
tabulator-responsiveLayout="collapse"
1019+
tabulator-headerSortTristate="true"
1020+
tabulator-pagination="true"
1021+
tabulator-paginationSize="25"
1022+
tabulator-placeholder="No API tokens configured">
10151023
<thead>
10161024
<tr>
1017-
<th scope="col" class="token">Token</th>
1018-
<th scope="col" class="description">Description</th>
1019-
<th scope="col" class="created">Created</th>
1020-
<th scope="col" class="last_used">Last Used</th>
1021-
<th scope="col" class="caveats">Caveats (as created)</th>
1022-
<th scope="col" class="permissions_caveat">Permissions (deprecated)</th>
1025+
<th scope="col">Token</th>
1026+
<th scope="col">Description</th>
1027+
<th scope="col" tabulator-responsive="3">Created</th>
1028+
<th scope="col" tabulator-responsive="2">Last Used</th>
1029+
<th scope="col" tabulator-responsive="4">Caveats (as created)</th>
1030+
<th scope="col" tabulator-visible="false">Permissions (deprecated)</th>
10231031
</tr>
10241032
</thead>
10251033
<tbody>
@@ -1032,7 +1040,7 @@ <h3 class="card-title">API Tokens</h3>
10321040
</td>
10331041
<td>{{ token.description }}</td>
10341042
<td>{{ token.created }}</td>
1035-
<td>{{ token.last_used }}</td>
1043+
<td>{{ token.last_used or '' }}</td>
10361044
<td>{{ token.caveats }}</td>
10371045
<td>{{ token.permissions_caveat }}</td>
10381046
</tr>
@@ -1230,16 +1238,24 @@ <h3 class="card-title">Unique logins</h3>
12301238
<h3 class="card-title">Account activity</h3>
12311239
</div>
12321240
<div class="card-body">
1233-
<table id="account-activity" class="table table-striped table-hover">
1241+
<table id="account-activity" class="table table-striped table-hover"
1242+
data-tabulator
1243+
data-tabulator-column-menu
1244+
tabulator-layout="fitDataFill"
1245+
tabulator-responsiveLayout="collapse"
1246+
tabulator-headerSortTristate="true"
1247+
tabulator-pagination="true"
1248+
tabulator-paginationSize="25"
1249+
tabulator-placeholder="No account activity">
12341250
<thead>
12351251
<tr>
1236-
<th scope="col" class="event">Event</th>
1237-
<th scope="col" class="time">Time</th>
1238-
<th scope="col" class="ip_address">IP address</th>
1239-
<th scope="col" class="hashed_ip">Hashed IP address</th>
1240-
<th scope="col" class="location_info">Location info</th>
1241-
<th scope="col" class="user_agent">User-Agent</th>
1242-
<th scope="col" class="additional_info">Additional information</th>
1252+
<th scope="col">Event</th>
1253+
<th scope="col">Time</th>
1254+
<th scope="col" tabulator-visible="false">IP address</th>
1255+
<th scope="col" tabulator-visible="false">Hashed IP address</th>
1256+
<th scope="col" tabulator-responsive="2">Location info</th>
1257+
<th scope="col" tabulator-responsive="3">User-Agent</th>
1258+
<th scope="col" tabulator-responsive="4">Additional information</th>
12431259
</tr>
12441260
</thead>
12451261
<tbody>

‎warehouse/admin/views/projects.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,8 +368,8 @@ def project_observations_list(project, request):
368368
.order_by(project.Observation.created.desc())
369369
)
370370

371-
# TODO: When implementing DataTables, consider removing server-side pagination.
372-
# DataTables can handle up to about 50,000 rows.
371+
# TODO: Consider dropping server-side pagination in favor of a Tabulator
372+
# table fed by a JSON endpoint, as admin.views.journals does.
373373
observations = SQLAlchemyORMPage(
374374
observations_query,
375375
page=page_num,

0 commit comments

Comments
 (0)