mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-21 23:46:18 -05:00
Cannot delete a directory if it contains a tagged FileGroup.
This commit is contained in:
parent
29c352b423
commit
b194189c9b
5 changed files with 58 additions and 18 deletions
|
|
@ -35,7 +35,7 @@ from wrolpi.dates import set_test_now
|
|||
from wrolpi.db import postgres_engine, get_db_args
|
||||
from wrolpi.downloader import DownloadManager, DownloadResult, Download, Downloader, \
|
||||
downloads_manager_config_context
|
||||
from wrolpi.files.models import Directory
|
||||
from wrolpi.files.models import Directory, FileGroup
|
||||
from wrolpi.root_api import BLUEPRINTS, api_app
|
||||
from wrolpi.tags import Tag
|
||||
from wrolpi.vars import PROJECT_DIR
|
||||
|
|
@ -341,8 +341,8 @@ def example_mobi(test_directory) -> pathlib.Path:
|
|||
|
||||
|
||||
@pytest.fixture
|
||||
def make_files_structure(test_directory) -> Callable[[Union[List, Dict]], List[pathlib.Path]]:
|
||||
def create_files(paths: List) -> List[pathlib.Path]:
|
||||
def make_files_structure(test_directory) -> Callable:
|
||||
def create_files(paths: List, file_groups: bool = False, session: Session = None) -> List[pathlib.Path]:
|
||||
files = []
|
||||
|
||||
@iterify(list)
|
||||
|
|
@ -366,6 +366,11 @@ def make_files_structure(test_directory) -> Callable[[Union[List, Dict]], List[p
|
|||
path.write_text(content)
|
||||
elif isinstance(content, bytes):
|
||||
path.write_bytes(content)
|
||||
|
||||
if file_groups:
|
||||
for idx, path in enumerate(files):
|
||||
fg = FileGroup.from_paths(session, path)
|
||||
files[idx] = fg
|
||||
return files
|
||||
|
||||
return create_files
|
||||
|
|
|
|||
|
|
@ -192,10 +192,6 @@ class FileConflict(APIError):
|
|||
pass
|
||||
|
||||
|
||||
class DirectoryConflict(APIError):
|
||||
pass
|
||||
|
||||
|
||||
error_codes = iter(range(1, 1000))
|
||||
|
||||
API_ERRORS = {
|
||||
|
|
|
|||
|
|
@ -257,10 +257,11 @@ async def post_rename(_: Request, body: schema.Rename):
|
|||
body=schema.Directory,
|
||||
)
|
||||
@validate(schema.Directory)
|
||||
@wrol_mode_check
|
||||
async def delete_directory(_: Request, body: schema.Directory):
|
||||
path = get_media_directory() / body.path
|
||||
if path.is_dir():
|
||||
shutil.rmtree(path)
|
||||
lib.delete_directory(path, recursive=True)
|
||||
else:
|
||||
raise FileNotFoundError(f'No directory found: {path}')
|
||||
|
||||
|
|
|
|||
|
|
@ -46,7 +46,7 @@ logger = logger.getChild(__name__)
|
|||
|
||||
__all__ = ['list_directories_contents', 'delete', 'split_path_stem_and_suffix', 'refresh_files', 'search_files',
|
||||
'get_mimetype', 'split_file_name_words', 'get_primary_file', 'get_file_statistics', 'estimate_search',
|
||||
'move', 'rename', 'remove_directory']
|
||||
'move', 'rename', 'delete_directory']
|
||||
|
||||
|
||||
@optional_session
|
||||
|
|
@ -166,7 +166,7 @@ def delete(*paths: Union[str, pathlib.Path]):
|
|||
raise FileGroupIsTagged(f"Cannot delete {file_group} because it is tagged")
|
||||
for path in paths:
|
||||
if path.is_dir():
|
||||
remove_directory(path)
|
||||
delete_directory(path)
|
||||
else:
|
||||
path.unlink()
|
||||
|
||||
|
|
@ -1073,7 +1073,7 @@ def _move(destination: pathlib.Path, *sources: pathlib.Path) -> List[Tuple[pathl
|
|||
plan = do_plan(plan)
|
||||
for source in sources:
|
||||
if source.is_dir():
|
||||
remove_directory(source)
|
||||
delete_directory(source)
|
||||
logger.info(f'Move execution completed')
|
||||
return plan
|
||||
except Exception as e:
|
||||
|
|
@ -1084,16 +1084,27 @@ def _move(destination: pathlib.Path, *sources: pathlib.Path) -> List[Tuple[pathl
|
|||
directories = sorted(walk(destination), key=lambda i: len(i.parents), reverse=True)
|
||||
for path in directories:
|
||||
if path.is_dir():
|
||||
remove_directory(path)
|
||||
delete_directory(path)
|
||||
# Remove destination only if it did not exist at the start.
|
||||
if destination.is_dir() and destination_existed is False:
|
||||
remove_directory(destination)
|
||||
delete_directory(destination)
|
||||
raise
|
||||
|
||||
|
||||
def remove_directory(directory: pathlib.Path):
|
||||
"""Remove a directory (only if it is empty) remove it's Directory record."""
|
||||
if directory.is_dir():
|
||||
def delete_directory(directory: pathlib.Path, recursive: bool = False):
|
||||
"""Remove a directory, remove it's Directory record.
|
||||
|
||||
Will refuse to delete a directory if it contains Tagged Files."""
|
||||
if recursive:
|
||||
with get_db_session() as session:
|
||||
tagged = session.query(FileGroup) \
|
||||
.filter(FileGroup.primary_path.like(f'{directory}/%')) \
|
||||
.join(TagFile, TagFile.file_group_id == FileGroup.id) \
|
||||
.limit(1).one_or_none()
|
||||
if tagged:
|
||||
raise FileGroupIsTagged()
|
||||
shutil.rmtree(directory)
|
||||
else:
|
||||
directory.rmdir()
|
||||
with get_db_curs(commit=True) as curs:
|
||||
stmt = 'DELETE FROM directory WHERE path=%s'
|
||||
|
|
@ -1165,7 +1176,7 @@ async def rename_directory(directory: pathlib.Path, new_name: str) -> pathlib.Pa
|
|||
paths = list(directory.iterdir())
|
||||
await move(new_directory, *paths)
|
||||
# Remove the old directory.
|
||||
remove_directory(directory)
|
||||
delete_directory(directory)
|
||||
|
||||
return new_directory
|
||||
|
||||
|
|
|
|||
|
|
@ -543,7 +543,7 @@ async def test_move(test_session, test_directory, make_files_structure, test_asy
|
|||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_rename(test_session, test_directory, make_files_structure, test_async_client):
|
||||
async def test_rename_file(test_session, test_directory, make_files_structure, test_async_client):
|
||||
foo, = make_files_structure({
|
||||
'foo/bar/baz.txt': 'asdf',
|
||||
})
|
||||
|
|
@ -570,3 +570,30 @@ async def test_rename_directory(test_session, test_directory, make_files_structu
|
|||
assert not (test_directory / 'foo/bar').exists()
|
||||
assert (test_directory / 'foo/qux/baz.txt').is_file()
|
||||
assert (test_directory / 'foo/qux/baz.txt').read_text() == 'asdf'
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_delete_directory_tagged(test_session, test_directory, make_files_structure, test_async_client,
|
||||
tag_factory, insert_file_group):
|
||||
"""Cannot delete a directory if it contained a tagged FileGroup."""
|
||||
bar, = make_files_structure({
|
||||
'foo/bar.txt': 'bar'
|
||||
}, file_groups=True, session=test_session)
|
||||
bar: FileGroup
|
||||
tag = tag_factory()
|
||||
bar.add_tag(tag)
|
||||
test_session.commit()
|
||||
|
||||
# Cannot deleted tagged file.
|
||||
content = dict(path=str(bar.primary_path.parent))
|
||||
request, response = await test_async_client.post('/api/files/delete_directory', content=json.dumps(content))
|
||||
assert response.status_code == HTTPStatus.CONFLICT
|
||||
assert (test_directory / 'foo').is_dir()
|
||||
|
||||
# Remove tag, directory is deleted recursively.
|
||||
bar.remove_tag(tag)
|
||||
test_session.commit()
|
||||
content = dict(path=str(bar.primary_path.parent))
|
||||
request, response = await test_async_client.post('/api/files/delete_directory', content=json.dumps(content))
|
||||
assert response.status_code == HTTPStatus.NO_CONTENT
|
||||
assert not (test_directory / 'foo').exists()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue