fixed file and folder delete

This commit is contained in:
Alexander Karpov 2023-04-26 13:47:02 +03:00
parent 062b794005
commit 3ce417b6df
6 changed files with 72 additions and 9 deletions

View File

@ -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)

View File

@ -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()

View File

@ -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()
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()
path = instance.file.path
file_dir = "/".join(path.split("/")[:-1]) + "/"
if os.path.isfile(path):
os.remove(path)
if os.path.isdir(file_dir) and len(os.listdir(file_dir)) == 0:

View File

@ -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/<str:slug>", report_file, name="file_report"),
path("api/file/delete/<str:slug>", delete_file_view, name="delete"),
path("api/folder/create/<str:slug>", folder_create, name="sub_folder_create"),
path("api/folder/delete/<str:slug>", delete_folder_view, name="folder_delete"),
path("<str:slug>", files_view, name="view"),
path("<str:slug>/update", file_update, name="update"),
path("<str:slug>/delete", delete_file_view, name="delete"),
path("f/<str:slug>", folder_view, name="folder"),
path("f/<str:slug>/update", folder_update, name="folder_update"),
]

View File

@ -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()

View File

@ -42,6 +42,10 @@
{% for f in folders %}
<li class="breadcrumb-item"><a href="{% url 'files:folder' slug=f.slug %}">{{ f.name }}</a></li>
{% endfor %}
<a class="justify-content-center ms-2" href="{% url 'files:folder_update' folder_slug %}">
<i style="color:black" class="bi bi-pen fs-6"></i></a>
<a class="justify-content-center ms-2" href="{% url 'files:folder_delete' folder_slug %}">
<i style="color:black" class="bi bi-trash fs-6"></i></a>
</ol>
</nav>
{% else %}