mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-25 01:16:18 -05:00
Models like Archive/Video/Doc can be deleted, even if they are tagged. User is shown a warning and must confirm deletion of tagged files.
This commit is contained in:
parent
513ae52a6e
commit
09a967535b
23 changed files with 689 additions and 109 deletions
|
|
@ -36,15 +36,21 @@ async def get_doc(request: Request, file_group_id: int):
|
|||
|
||||
@docs_bp.delete('/<file_group_ids:int>', name='doc_delete_one')
|
||||
@docs_bp.delete('/<file_group_ids:[0-9,]+>', name='doc_delete_many')
|
||||
@openapi.description('Delete Docs by FileGroup IDs')
|
||||
@openapi.description('Delete Docs by FileGroup IDs. Pass ?force=true to delete even tagged FileGroups.')
|
||||
@openapi.response(HTTPStatus.NOT_FOUND, JSONErrorResponse)
|
||||
@wrol_mode_check
|
||||
async def delete_docs(_: Request, file_group_ids: str):
|
||||
async def delete_docs(request: Request, file_group_ids: str):
|
||||
try:
|
||||
file_group_ids = [int(i) for i in str(file_group_ids).split(',')]
|
||||
except ValueError:
|
||||
raise ValidationError('Could not parse file group ids')
|
||||
_delete_docs(*file_group_ids)
|
||||
force = request.args.get('force', '').lower() == 'true'
|
||||
tagged = _delete_docs(*file_group_ids, force=force)
|
||||
if tagged:
|
||||
return json_response(
|
||||
{'code': 'FILE_GROUP_IS_TAGGED', 'file_groups': tagged},
|
||||
HTTPStatus.CONFLICT,
|
||||
)
|
||||
return response.empty()
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -11,7 +11,8 @@ from wrolpi.collections.models import Collection
|
|||
from wrolpi.collections.types import collection_type_registry
|
||||
from wrolpi.db import get_db_session
|
||||
from wrolpi.errors import ValidationError
|
||||
from wrolpi.files.lib import split_path_stem_and_suffix, get_mimetype
|
||||
from wrolpi.files.lib import split_path_stem_and_suffix, get_mimetype, get_tagged_file_groups_by_ids
|
||||
from wrolpi.tags import save_tags_config
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
|
@ -346,13 +347,33 @@ def _get_doc(session, file_group_id: int):
|
|||
return doc
|
||||
|
||||
|
||||
def _delete_docs(*file_group_ids):
|
||||
def _delete_docs(*file_group_ids, force: bool = False) -> Optional[List[dict]]:
|
||||
"""Delete Docs by their FileGroup IDs and all of their files.
|
||||
|
||||
If any FileGroup is tagged and force is False, returns the list of serialized tagged FileGroups
|
||||
without deleting anything. If force is True, deletes even tagged files and re-saves the tags
|
||||
config so `tags.yaml` reflects the removed TagFiles.
|
||||
"""
|
||||
from .models import Doc
|
||||
with get_db_session(commit=True) as session:
|
||||
for file_group_id in file_group_ids:
|
||||
doc = session.query(Doc).filter_by(file_group_id=file_group_id).one_or_none()
|
||||
if doc:
|
||||
session.delete(doc)
|
||||
docs = list(session.query(Doc).filter(Doc.file_group_id.in_(file_group_ids)))
|
||||
if not docs:
|
||||
raise ValidationError(
|
||||
f'Could not find Docs to delete with file_group_ids: {", ".join(map(str, file_group_ids))}'
|
||||
)
|
||||
|
||||
tagged = get_tagged_file_groups_by_ids(session, [d.file_group_id for d in docs])
|
||||
if tagged and not force:
|
||||
return tagged
|
||||
|
||||
# Delete the FileGroup, which removes the files on disk and cascades the Doc row via
|
||||
# the ON DELETE CASCADE foreign key on Doc.file_group_id.
|
||||
for doc in docs:
|
||||
doc.file_group.delete(force=force)
|
||||
|
||||
if tagged:
|
||||
save_tags_config.activate_switch()
|
||||
return None
|
||||
|
||||
|
||||
def _search_docs(search_str=None, author=None, subject=None, language=None, mimetype=None,
|
||||
|
|
|
|||
|
|
@ -77,14 +77,74 @@ async def test_docs_search_by_tag(async_client, test_session, doc_factory, tag_f
|
|||
|
||||
@pytest.mark.asyncio
|
||||
async def test_delete_doc(async_client, test_session, test_directory, example_epub, refresh_files):
|
||||
"""Docs can be deleted."""
|
||||
"""Docs can be deleted, including their on-disk files and FileGroup row."""
|
||||
from wrolpi.files.models import FileGroup
|
||||
await refresh_files()
|
||||
|
||||
doc = test_session.query(Doc).first()
|
||||
assert doc
|
||||
file_group_id = doc.file_group_id
|
||||
paths = list(doc.file_group.my_paths())
|
||||
assert paths and all(p.is_file() for p in paths)
|
||||
|
||||
request, response = await async_client.delete(f'/api/docs/{file_group_id}')
|
||||
assert response.status_code == HTTPStatus.NO_CONTENT
|
||||
|
||||
# Doc, FileGroup, and on-disk files all removed.
|
||||
assert test_session.query(Doc).filter_by(file_group_id=file_group_id).one_or_none() is None
|
||||
assert test_session.query(FileGroup).filter_by(id=file_group_id).one_or_none() is None
|
||||
assert all(not p.exists() for p in paths)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_delete_doc_tagged_requires_force(async_client, test_session, doc_factory, tag_factory,
|
||||
await_switches):
|
||||
"""DELETE /api/docs/<id> returns 409 for tagged docs until ?force=true is passed."""
|
||||
from wrolpi.files.models import FileGroup
|
||||
tag = await tag_factory()
|
||||
doc = doc_factory()
|
||||
doc.file_group.add_tag(test_session, tag.name)
|
||||
test_session.commit()
|
||||
file_group_id = doc.file_group_id
|
||||
paths = list(doc.file_group.my_paths())
|
||||
|
||||
request, response = await async_client.delete(f'/api/docs/{file_group_id}')
|
||||
assert response.status_code == HTTPStatus.CONFLICT
|
||||
assert response.json['code'] == 'FILE_GROUP_IS_TAGGED'
|
||||
assert len(response.json['file_groups']) == 1
|
||||
assert response.json['file_groups'][0]['id'] == file_group_id
|
||||
assert tag.name in response.json['file_groups'][0]['tags']
|
||||
# Nothing was deleted.
|
||||
assert all(p.is_file() for p in paths)
|
||||
assert test_session.query(Doc).count() == 1
|
||||
|
||||
# With force=true, the Doc is deleted.
|
||||
request, response = await async_client.delete(f'/api/docs/{file_group_id}?force=true')
|
||||
assert response.status_code == HTTPStatus.NO_CONTENT
|
||||
assert all(not p.exists() for p in paths)
|
||||
assert test_session.query(Doc).count() == 0
|
||||
assert test_session.query(FileGroup).filter_by(id=file_group_id).one_or_none() is None
|
||||
await await_switches()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_delete_doc_atomic_when_some_tagged(async_client, test_session, doc_factory, tag_factory):
|
||||
"""When deleting multiple Docs and any are tagged, NONE are deleted until ?force=true."""
|
||||
untagged = doc_factory()
|
||||
tagged = doc_factory()
|
||||
tag = await tag_factory()
|
||||
tagged.file_group.add_tag(test_session, tag.name)
|
||||
test_session.commit()
|
||||
untagged_paths = list(untagged.file_group.my_paths())
|
||||
tagged_paths = list(tagged.file_group.my_paths())
|
||||
|
||||
ids = f'{untagged.file_group_id},{tagged.file_group_id}'
|
||||
request, response = await async_client.delete(f'/api/docs/{ids}')
|
||||
assert response.status_code == HTTPStatus.CONFLICT
|
||||
assert response.json['code'] == 'FILE_GROUP_IS_TAGGED'
|
||||
returned_ids = {fg['id'] for fg in response.json['file_groups']}
|
||||
assert returned_ids == {tagged.file_group_id}
|
||||
# Nothing was deleted.
|
||||
assert all(p.is_file() for p in untagged_paths)
|
||||
assert all(p.is_file() for p in tagged_paths)
|
||||
assert test_session.query(Doc).count() == 2
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue