Skip to content

Commit 188414b

Browse files
authored
feat(admin): exclude orphaned malware records (pypi#15908)
1 parent fe5303d commit 188414b

File tree

7 files changed

+29
-14
lines changed

7 files changed

+29
-14
lines changed

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
"scripts": {
66
"build": "webpack",
77
"watch": "webpack --watch",
8-
"lint": "eslint 'warehouse/static/js/**' 'tests/frontend/**' --ignore-pattern 'warehouse/static/js/vendor/**'",
9-
"lint:fix": "eslint 'warehouse/static/js/**' 'tests/frontend/**' --ignore-pattern 'warehouse/static/js/vendor/**' --fix",
8+
"lint": "eslint 'warehouse/static/js/**' 'warehouse/admin/static/js/**' 'tests/frontend/**' --ignore-pattern 'warehouse/static/js/vendor/**'",
9+
"lint:fix": "eslint 'warehouse/static/js/**' 'warehouse/admin/static/js/**' 'tests/frontend/**' --ignore-pattern 'warehouse/static/js/vendor/**' --fix",
1010
"stylelint": "stylelint '**/*.scss' --cache",
1111
"stylelint:fix": "stylelint '**/*.scss' --cache --fix",
1212
"test": "jest --coverage"
@@ -71,7 +71,8 @@
7171
"env": {
7272
"browser": true,
7373
"es6": true,
74-
"amd": true
74+
"amd": true,
75+
"jquery": true
7576
},
7677
"extends": "eslint:recommended",
7778
"parser": "@babel/eslint-parser",

tests/unit/admin/views/test_core.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,11 @@ def test_dashboard(self, pyramid_request):
3030
]
3131

3232
def test_dashboard_with_permission_and_observation(self, db_request):
33+
"""Test that the dashboard view returns the correct data when the user has the
34+
required permission and there are multiple Observations in the database."""
3335
ProjectObservationFactory.create(kind="is_malware")
3436
ProjectObservationFactory.create(kind="is_malware", actions={"foo": "bar"})
37+
ProjectObservationFactory.create(kind="is_malware", related=None)
3538
ProjectObservationFactory.create(kind="something_else")
3639
db_request.user = pretend.stub()
3740
db_request.has_permission = pretend.call_recorder(lambda perm: True)

tests/unit/admin/views/test_malware_reports.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ def test_malware_reports_list(self, db_request):
2222

2323
def test_malware_reports_list_with_observations(self, db_request):
2424
ProjectObservationFactory.create(kind="is_spam")
25+
ProjectObservationFactory.create(kind="is_malware", actions={"foo": "bar"})
26+
ProjectObservationFactory.create(kind="is_malware", related=None)
2527
malware = ProjectObservationFactory.create_batch(size=3, kind="is_malware")
2628

2729
assert views.malware_reports_list(db_request) == {"malware_reports": malware}

warehouse/admin/static/js/warehouse.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ import "admin-lte/plugins/datatables-rowgroup/js/rowGroup.bootstrap4";
2929
// Import AdminLTE JS
3030
import "admin-lte/build/js/AdminLTE";
3131

32+
// Get our timeago function
33+
import timeAgo from "warehouse/utils/timeago";
34+
35+
// Human-readable timestamps
36+
$(document).ready(function() {
37+
timeAgo();
38+
});
3239

3340
document.querySelectorAll("a[data-form-submit]").forEach(function (element) {
3441
element.addEventListener("click", function(event) {
@@ -116,7 +123,7 @@ document.querySelectorAll(".copy-text").forEach(function (element) {
116123
// Guard each one to not break execution if the table isn't present
117124

118125
// User Account Activity
119-
let accountActivityTable = $("#account-activity")
126+
let accountActivityTable = $("#account-activity");
120127
if (accountActivityTable.length) {
121128
let table = accountActivityTable.DataTable({
122129
responsive: true,
@@ -132,7 +139,7 @@ if (accountActivityTable.length) {
132139
}
133140

134141
// User API Tokens
135-
let tokenTable = $("#api-tokens")
142+
let tokenTable = $("#api-tokens");
136143
if (tokenTable.length) {
137144
let table = tokenTable.DataTable({
138145
responsive: true,
@@ -145,7 +152,7 @@ if (tokenTable.length) {
145152
}
146153

147154
// Observations
148-
let observationsTable = $("#observations")
155+
let observationsTable = $("#observations");
149156
if (observationsTable.length) {
150157
let table = observationsTable.DataTable({
151158
responsive: true,
@@ -158,7 +165,7 @@ if (observationsTable.length) {
158165
}
159166

160167
// Malware Reports
161-
let malwareReportsTable = $("#malware-reports")
168+
let malwareReportsTable = $("#malware-reports");
162169
if (malwareReportsTable.length) {
163170
let table = malwareReportsTable.DataTable({
164171
displayLength: 25,
@@ -169,7 +176,7 @@ if (malwareReportsTable.length) {
169176
dataSrc: 0,
170177
// display row count in group header
171178
startRender: function (rows, group) {
172-
return group + ' (' + rows.count() + ')';
179+
return group + " (" + rows.count() + ")";
173180
},
174181
},
175182
});

warehouse/admin/views/core.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ def dashboard(request):
2929
malware_reports_count = (
3030
request.db.query(func.count(Observation.id)).filter(
3131
Observation.kind == ObservationKind.IsMalware.value[0],
32+
Observation.related_id.is_not(None), # Project is not removed
3233
Observation.actions == {}, # No actions have been taken
3334
)
3435
).scalar()

warehouse/admin/views/malware_reports.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,11 @@ def malware_reports_list(request):
3636
"""
3737
malware_observations = (
3838
request.db.query(Observation)
39-
.filter(Observation.kind == "is_malware")
40-
# TODO: filter by handled or not, once we have a way to handle them
39+
.filter(
40+
Observation.kind == "is_malware",
41+
Observation.related_id.isnot(None),
42+
Observation.actions == {},
43+
)
4144
.order_by(Observation.created)
4245
.all()
4346
)

warehouse/admin/views/projects.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,7 @@ def add_project_observation(project, request):
218218
)
219219
)
220220

221-
# We allow an empty payload from Admin.
222-
payload = {}
221+
payload = {"origin": "admin"}
223222

224223
project.record_observation(
225224
request=request,
@@ -356,8 +355,7 @@ def add_release_observation(release, request):
356355
)
357356
)
358357

359-
# We allow an empty payload from Admin.
360-
payload = {}
358+
payload = {"origin": "admin"}
361359

362360
release.record_observation(
363361
request=request,

0 commit comments

Comments
 (0)