From 3ce417b6dffbe993442d188f3141ac3c61f9c153 Mon Sep 17 00:00:00 2001 From: Alexandr Karpov Date: Wed, 26 Apr 2023 13:47:02 +0300 Subject: [PATCH] fixed file and folder delete --- akarpov/files/models.py | 3 +++ akarpov/files/services/folders.py | 11 ++++++++++ akarpov/files/signals.py | 23 +++++++++++++-------- akarpov/files/urls.py | 6 +++++- akarpov/files/views.py | 34 +++++++++++++++++++++++++++++++ akarpov/templates/files/list.html | 4 ++++ 6 files changed, 72 insertions(+), 9 deletions(-) create mode 100644 akarpov/files/services/folders.py diff --git a/akarpov/files/models.py b/akarpov/files/models.py index 7433b41..8f7467e 100644 --- a/akarpov/files/models.py +++ b/akarpov/files/models.py @@ -124,6 +124,9 @@ class FileInTrash(TimeStampedModel): user = ForeignKey("users.User", related_name="trash_files", on_delete=CASCADE) file = FileField(blank=False, upload_to=trash_file_upload) + def __str__(self): + return self.name + class FileReport(Model): created = DateTimeField(auto_now_add=True) diff --git a/akarpov/files/services/folders.py b/akarpov/files/services/folders.py new file mode 100644 index 0000000..970b3bf --- /dev/null +++ b/akarpov/files/services/folders.py @@ -0,0 +1,11 @@ +from akarpov.files.models import BaseFileItem, Folder + + +def delete_folder(folder: Folder): + for child in BaseFileItem.objects.filter(parent=folder): + if not child.is_file: + delete_folder(child) + else: + if child.file: + child.delete() + folder.delete() diff --git a/akarpov/files/signals.py b/akarpov/files/signals.py index b70cc5c..39d7619 100644 --- a/akarpov/files/signals.py +++ b/akarpov/files/signals.py @@ -15,7 +15,7 @@ def file_on_create(sender, instance: FileModel, created, **kwargs): if created: for folder in instance.get_top_folders(): folder.modified = now() - folder.size += instance.file.size + folder.size += instance.file_size folder.amount += 1 folder.save() process_file.apply_async( @@ -29,18 +29,25 @@ def file_on_create(sender, instance: FileModel, created, **kwargs): @receiver(post_delete, sender=FileModel) def move_file_to_trash(sender, instance, **kwargs): if instance.file: + file_size = 0 + path = instance.file.path + file_dir = "/".join(path.split("/")[:-1]) + "/" + + if os.path.isfile(path): + file_size = instance.file.size + for folder in instance.get_top_folders(): folder.modified = now() - folder.size -= instance.file.size + folder.size -= file_size folder.amount -= 1 folder.save() - name = instance.file.name.split("/")[-1] - trash = FileInTrash(user=instance.user, name=name) - trash.file = File(instance.file, name=name) - trash.save() - path = instance.file.path - file_dir = "/".join(path.split("/")[:-1]) + "/" + if os.path.isfile(path): + name = instance.file.name.split("/")[-1] + trash = FileInTrash(user=instance.user, name=name) + trash.file = File(instance.file, name=name) + trash.save() + if os.path.isfile(path): os.remove(path) if os.path.isdir(file_dir) and len(os.listdir(file_dir)) == 0: diff --git a/akarpov/files/urls.py b/akarpov/files/urls.py index 8f68ab4..4843f68 100644 --- a/akarpov/files/urls.py +++ b/akarpov/files/urls.py @@ -5,11 +5,13 @@ ChunkedUploadView, TopFolderView, delete_file_view, + delete_folder_view, file_report_list, file_table, file_update, files_view, folder_create, + folder_update, folder_view, report_file, ) @@ -32,9 +34,11 @@ path("api/chunked_upload/", ChunkedUploadView.as_view(), name="api_chunked_upload"), path("api/folder/create/", folder_create, name="folder_create"), path("api/file/report/", report_file, name="file_report"), + path("api/file/delete/", delete_file_view, name="delete"), path("api/folder/create/", folder_create, name="sub_folder_create"), + path("api/folder/delete/", delete_folder_view, name="folder_delete"), path("", files_view, name="view"), path("/update", file_update, name="update"), - path("/delete", delete_file_view, name="delete"), path("f/", folder_view, name="folder"), + path("f//update", folder_update, name="folder_update"), ] diff --git a/akarpov/files/views.py b/akarpov/files/views.py index 0cfb2b5..7d5b858 100644 --- a/akarpov/files/views.py +++ b/akarpov/files/views.py @@ -30,6 +30,7 @@ from akarpov.files.forms import FileForm, FolderForm from akarpov.files.models import BaseFileItem, File, FileReport, Folder from akarpov.files.previews import extensions, meta, meta_extensions, previews +from akarpov.files.services.folders import delete_folder from akarpov.files.services.preview import get_base_meta from akarpov.files.tables import FileTable @@ -202,8 +203,11 @@ def get_context_data(self, **kwargs): class DeleteFileView(LoginRequiredMixin, RedirectView): def get_redirect_url(self, *args, **kwargs): file = get_object_or_404(File, slug=kwargs["slug"]) + parent = file.parent if file.user == self.request.user: file.delete() + if parent: + return reverse("files:folder", kwargs={"slug": parent.slug}) return reverse("files:main") @@ -308,3 +312,33 @@ class ListFileReports(SuperUserRequiredMixin, ListView): file_report_list = ListFileReports.as_view() + + +class FolderUpdateView(LoginRequiredMixin, UpdateView): + model = Folder + form_class = FolderForm + + def get_object(self, *args): + file = get_object_or_404(Folder, slug=self.kwargs["slug"]) + if file.user != self.request.user: + raise PermissionDenied + return file + + template_name = "files/form.html" + + +folder_update = FolderUpdateView.as_view() + + +class DeleteFolderView(LoginRequiredMixin, RedirectView): + def get_redirect_url(self, *args, **kwargs): + folder = get_object_or_404(Folder, slug=kwargs["slug"]) + parent = folder.parent + if folder.user == self.request.user: + delete_folder(folder) + if parent: + return reverse("files:folder", kwargs={"slug": parent.slug}) + return reverse("files:main") + + +delete_folder_view = DeleteFolderView.as_view() diff --git a/akarpov/templates/files/list.html b/akarpov/templates/files/list.html index 006ea62..c310c95 100644 --- a/akarpov/templates/files/list.html +++ b/akarpov/templates/files/list.html @@ -42,6 +42,10 @@ {% for f in folders %} {% endfor %} + + + + {% else %}