Skip to content

Commit

Permalink
#2108: IIIF - check for orphaned files
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderWatzinger committed Nov 22, 2023
1 parent 5003c66 commit 8596ef6
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 9 deletions.
13 changes: 7 additions & 6 deletions openatlas/display/tab.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
_('unlinked')
_('missing files')
_('orphaned files')
_('orphaned IIIF files')
_('orphaned subunits')
_('circular dependencies')

Expand Down Expand Up @@ -119,12 +120,12 @@ def set_buttons(self, name: str, entity: Optional[Entity] = None) -> None:
g.classes[item].label,
url_for('insert', class_=item, origin_id=id_)))
elif name == 'artifact':
if entity and entity.class_.name in \
['place',
'artifact',
'human_remains',
'feature',
'stratigraphic_unit']:
if entity and entity.class_.name in [
'place',
'artifact',
'human_remains',
'feature',
'stratigraphic_unit']:
self.buttons.append(
button(_('add subunit'),
url_for('add_subunit', super_id=id_)))
Expand Down
39 changes: 38 additions & 1 deletion openatlas/views/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import math
import os
import shutil
from pathlib import Path
from subprocess import run
from typing import Any, Optional, Union

Expand Down Expand Up @@ -492,6 +493,9 @@ def admin_orphans() -> str:
'orphaned_files': Tab(
'orphaned_files',
table=Table(['name', 'size', 'date', 'ext'])),
'orphaned_iiif_files': Tab(
'orphaned_iiif_files',
table=Table(['name', 'size', 'date', 'ext'])),
'orphaned_subunits': Tab(
'orphaned_subunits',
table=Table([
Expand Down Expand Up @@ -544,10 +548,31 @@ def admin_orphans() -> str:
url_for('download_file', filename=file.name)),
link(
_('delete'),
url_for('admin_file_delete', filename=file.name),
url_for('admin_file_iiif_delete', filename=file.name),
js=f"return confirm('{confirm}')")
if is_authorized('editor') else ''])

# Orphaned IIIF files with no corresponding entity
if g.settings['iiif'] and g.settings['iiif_path']:
for file in Path(g.settings['iiif_path']).iterdir():
confirm = _('Delete %(name)s?', name=file.name.replace("'", ''))
if file.name != '.gitignore' \
and os.path.isfile(file) \
and file.stem.isdigit() \
and int(file.stem) not in entity_file_ids:
tabs['orphaned_iiif_files'].table.rows.append([
file.stem,
convert_size(file.stat().st_size),
format_date(
datetime.datetime.utcfromtimestamp(
file.stat().st_ctime)),
file.suffix,
link(
_('delete'),
url_for('admin_file_iiif_delete', filename=file.name),
js=f"return confirm('{confirm}')")
if is_authorized('editor') else ''])

# Orphaned subunits (without connection to a P46 super)
for entity in Entity.get_orphaned_subunits():
tabs['orphaned_subunits'].table.rows.append([
Expand Down Expand Up @@ -606,6 +631,18 @@ def admin_file_delete(filename: str) -> Response:
f"{url_for('admin_orphans')}#tab-orphaned-files") # pragma: no cover


@app.route('/admin/file/iiif/delete/<filename>')
@required_group('editor')
def admin_file_iiif_delete(filename: str) -> Response:
try:
(Path(g.settings['iiif_path']) / filename).unlink()
flash(f"{filename} {_('was deleted')}", 'info')
except Exception as e:
g.logger.log('error', 'file', f'deletion of IIIF {filename} failed', e)
flash(_('error file delete'), 'error')
return redirect(f"{url_for('admin_orphans')}#tab-orphaned-iiif-files")


@app.route('/admin/logo/')
@app.route('/admin/logo/<int:id_>')
@required_group('manager')
Expand Down
4 changes: 3 additions & 1 deletion openatlas/views/changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ def index_changelog() -> str:


versions = {
'7.18.0': ['TBA', {}],
'7.18.0': ['TBA', {
'feature': {
'2108': 'IIIF - check for orphaned files'}}],
'7.17.4': ['2023-11-17', {
'fix': {
'2124': 'No modifications at preceding event'}}],
Expand Down
14 changes: 13 additions & 1 deletion tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ def test_admin(self) -> None:
assert b'Invalid linked entity' in rv.data

file_ = 'Test77.txt'
with open(Path(app.config['UPLOAD_PATH'] / file_), 'w') as _file:
with open(Path(app.config['UPLOAD_PATH'] / file_), 'w') as _:
pass
with open(Path(Path(g.settings['iiif_path']) / file_), 'w') as _:
pass

rv = self.app.get(
Expand All @@ -57,6 +59,16 @@ def test_admin(self) -> None:
follow_redirects=True)
assert b'An error occurred when trying to delete' in rv.data

rv = self.app.get(
url_for('admin_file_iiif_delete', filename=file_),
follow_redirects=True)
assert b'Test77.txt was deleted' in rv.data

rv = self.app.get(
url_for('admin_file_iiif_delete', filename=file_),
follow_redirects=True)
assert b'An error occurred when trying to delete' in rv.data

with app.test_request_context():
app.preprocess_request() # type: ignore
event = insert('acquisition', 'Event Horizon')
Expand Down

0 comments on commit 8596ef6

Please sign in to comment.