File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1616import os
1717
1818from django .conf import settings
19+ from django .urls import reverse
1920
2021from website .admin .data_health .registry import HealthCheck , register_check
2122from website .models import Poster , Publication , Talk
@@ -53,6 +54,20 @@ def get_rows(self):
5354 rows .extend (self ._orphan_files (model ))
5455 return rows
5556
57+ def row_link (self , row ):
58+ """Deep-link a ``missing-file`` row to its artifact's admin edit page so
59+ the editor can re-upload the file or clear the dead reference right
60+ there (mirrors the action buttons on the other checks).
61+
62+ ``orphan-file`` rows have no DB object to open — they're files on disk
63+ that ``delete_unused_files`` would remove — so they get no link.
64+ """
65+ if row .get ('status' ) != 'missing-file' or not row .get ('id' ):
66+ return None
67+ url = reverse (f"admin:website_{ row ['type' ].lower ()} _change" ,
68+ args = [row ['id' ]])
69+ return ('Open →' , url )
70+
5671 def _missing_files (self , model ):
5772 """DB rows whose file field is set but the file is gone from disk."""
5873 rows = []
Original file line number Diff line number Diff line change @@ -303,6 +303,33 @@ def test_flags_missing_file(self):
303303 ]
304304 self .assertTrue (hits )
305305
306+ def test_missing_file_row_links_to_admin_edit (self ):
307+ """A missing-file row gets an 'Open →' action to the artifact's admin
308+ edit page; orphan-file rows (no DB object) get no link."""
309+ pub = self .make_publication (title = "Vanishing Paper" )
310+ path = pub .pdf_file .path
311+ if os .path .exists (path ):
312+ os .remove (path )
313+
314+ check = get_check ("media-integrity" )
315+ missing = next (
316+ r
317+ for r in check .get_rows ()
318+ if r ["type" ] == "Publication"
319+ and r ["id" ] == pub .pk
320+ and r ["status" ] == "missing-file"
321+ )
322+ label , url = check .row_link (missing )
323+ self .assertEqual (label , "Open →" )
324+ self .assertEqual (
325+ url , reverse ("admin:website_publication_change" , args = [pub .pk ])
326+ )
327+
328+ # Orphan-file rows carry no id and must not produce a link.
329+ self .assertIsNone (
330+ check .row_link ({"type" : "Publication" , "id" : "" , "status" : "orphan-file" })
331+ )
332+
306333
307334class DataHealthReadOnlyTests (DatabaseTestCase ):
308335 def test_get_rows_does_not_mutate_db (self ):
You can’t perform that action at this time.
0 commit comments