Handle more cases
This should now handle: - adding a sub-directory - removing a sub-directory
This commit is contained in:
1 parent
f41faa444d
commit
8c465f0afe
1 file changed
+30
-9
@@ -1,4 +1,5 @@
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from tempfile import TemporaryDirectory
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.db.models.signals import pre_save, post_save, pre_delete, post_delete
|
from django.db.models.signals import pre_save, post_save, pre_delete, post_delete
|
||||||
from django.dispatch import receiver
|
from django.dispatch import receiver
|
||||||
@@ -29,15 +30,35 @@ def source_pre_save(sender, instance, **kwargs):
|
|||||||
return
|
return
|
||||||
existing_dirpath = existing_source.directory_path.resolve(strict=True)
|
existing_dirpath = existing_source.directory_path.resolve(strict=True)
|
||||||
new_dirpath = instance.directory_path.resolve(strict=False)
|
new_dirpath = instance.directory_path.resolve(strict=False)
|
||||||
rename_source_directory = (
|
if existing_dirpath != new_dirpath:
|
||||||
existing_dirpath != new_dirpath and
|
path_name = lambda p: p.name
|
||||||
not new_dirpath.exists()
|
relative_dir = existing_source.directory
|
||||||
)
|
rd_parents = Path(relative_dir).parents
|
||||||
if rename_source_directory:
|
rd_parents_set = set(map(path_name, rd_parents))
|
||||||
tmp_dirpath = existing_dirpath.parent / ('.tmp.' + existing_dirpath.name)
|
ad_parents = existing_dirpath.parents
|
||||||
existing_dirpath.rename(tmp_dirpath)
|
ad_parents_set = set(map(path_name, ad_parents))
|
||||||
mkdir_p(new_dirpath.parent)
|
# the names in the relative path are also in the absolute path
|
||||||
tmp_dirpath.rename(new_dirpath)
|
parents_count = len(ad_parents_set.intersection(rd_parents_set))
|
||||||
|
work_directory = existing_dirpath
|
||||||
|
while parents_count > 0:
|
||||||
|
work_directory = work_directory.parent
|
||||||
|
parents_count -= 1
|
||||||
|
with TemporaryDirectory(suffix='.'+new_dirpath.name, prefix='.tmp.', dir=work_directory) as tmp_dir:
|
||||||
|
tmp_dirpath = Path(tmp_dir)
|
||||||
|
existed = None
|
||||||
|
if new_dirpath.exists():
|
||||||
|
existed = tmp_dirpath / 'existed'
|
||||||
|
new_dirpath.rename(existed)
|
||||||
|
previous = tmp_dirpath / 'previous'
|
||||||
|
existing_dirpath.rename(previous)
|
||||||
|
mkdir_p(new_dirpath.parent)
|
||||||
|
previous.rename(new_dirpath)
|
||||||
|
if existed and existed.is_dir():
|
||||||
|
existed.rename(new_dirpath / '.existed')
|
||||||
|
# TODO: merge the contents of the directories
|
||||||
|
pass
|
||||||
|
elif existed:
|
||||||
|
existed.rename(new_dirpath / '.existed')
|
||||||
recreate_index_source_task = (
|
recreate_index_source_task = (
|
||||||
existing_source.name != instance.name or
|
existing_source.name != instance.name or
|
||||||
existing_source.index_schedule != instance.index_schedule
|
existing_source.index_schedule != instance.index_schedule
|
||||||
|
|||||||
Reference in new issue
Block a user