Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions website/admin/data_health/checks/media_integrity.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import os

from django.conf import settings
from django.urls import reverse

from website.admin.data_health.registry import HealthCheck, register_check
from website.models import Poster, Publication, Talk
Expand Down Expand Up @@ -53,6 +54,20 @@ def get_rows(self):
rows.extend(self._orphan_files(model))
return rows

def row_link(self, row):
"""Deep-link a ``missing-file`` row to its artifact's admin edit page so
the editor can re-upload the file or clear the dead reference right
there (mirrors the action buttons on the other checks).

``orphan-file`` rows have no DB object to open — they're files on disk
that ``delete_unused_files`` would remove — so they get no link.
"""
if row.get('status') != 'missing-file' or not row.get('id'):
return None
url = reverse(f"admin:website_{row['type'].lower()}_change",
args=[row['id']])
return ('Open →', url)

def _missing_files(self, model):
"""DB rows whose file field is set but the file is gone from disk."""
rows = []
Expand Down
27 changes: 27 additions & 0 deletions website/tests/test_data_health.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,33 @@ def test_flags_missing_file(self):
]
self.assertTrue(hits)

def test_missing_file_row_links_to_admin_edit(self):
"""A missing-file row gets an 'Open →' action to the artifact's admin
edit page; orphan-file rows (no DB object) get no link."""
pub = self.make_publication(title="Vanishing Paper")
path = pub.pdf_file.path
if os.path.exists(path):
os.remove(path)

check = get_check("media-integrity")
missing = next(
r
for r in check.get_rows()
if r["type"] == "Publication"
and r["id"] == pub.pk
and r["status"] == "missing-file"
)
label, url = check.row_link(missing)
self.assertEqual(label, "Open →")
self.assertEqual(
url, reverse("admin:website_publication_change", args=[pub.pk])
)

# Orphan-file rows carry no id and must not produce a link.
self.assertIsNone(
check.row_link({"type": "Publication", "id": "", "status": "orphan-file"})
)


class DataHealthReadOnlyTests(DatabaseTestCase):
def test_get_rows_does_not_mutate_db(self):
Expand Down
Loading