mirror of
https://github.com/Alexander-D-Karpov/akarpov
synced 2024-11-22 19:06:41 +03:00
fixed file and folder delete
This commit is contained in:
parent
062b794005
commit
3ce417b6df
|
@ -124,6 +124,9 @@ class FileInTrash(TimeStampedModel):
|
||||||
user = ForeignKey("users.User", related_name="trash_files", on_delete=CASCADE)
|
user = ForeignKey("users.User", related_name="trash_files", on_delete=CASCADE)
|
||||||
file = FileField(blank=False, upload_to=trash_file_upload)
|
file = FileField(blank=False, upload_to=trash_file_upload)
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return self.name
|
||||||
|
|
||||||
|
|
||||||
class FileReport(Model):
|
class FileReport(Model):
|
||||||
created = DateTimeField(auto_now_add=True)
|
created = DateTimeField(auto_now_add=True)
|
||||||
|
|
11
akarpov/files/services/folders.py
Normal file
11
akarpov/files/services/folders.py
Normal 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()
|
|
@ -15,7 +15,7 @@ def file_on_create(sender, instance: FileModel, created, **kwargs):
|
||||||
if created:
|
if created:
|
||||||
for folder in instance.get_top_folders():
|
for folder in instance.get_top_folders():
|
||||||
folder.modified = now()
|
folder.modified = now()
|
||||||
folder.size += instance.file.size
|
folder.size += instance.file_size
|
||||||
folder.amount += 1
|
folder.amount += 1
|
||||||
folder.save()
|
folder.save()
|
||||||
process_file.apply_async(
|
process_file.apply_async(
|
||||||
|
@ -29,18 +29,25 @@ def file_on_create(sender, instance: FileModel, created, **kwargs):
|
||||||
@receiver(post_delete, sender=FileModel)
|
@receiver(post_delete, sender=FileModel)
|
||||||
def move_file_to_trash(sender, instance, **kwargs):
|
def move_file_to_trash(sender, instance, **kwargs):
|
||||||
if instance.file:
|
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():
|
for folder in instance.get_top_folders():
|
||||||
folder.modified = now()
|
folder.modified = now()
|
||||||
folder.size -= instance.file.size
|
folder.size -= file_size
|
||||||
folder.amount -= 1
|
folder.amount -= 1
|
||||||
folder.save()
|
folder.save()
|
||||||
|
|
||||||
|
if os.path.isfile(path):
|
||||||
name = instance.file.name.split("/")[-1]
|
name = instance.file.name.split("/")[-1]
|
||||||
trash = FileInTrash(user=instance.user, name=name)
|
trash = FileInTrash(user=instance.user, name=name)
|
||||||
trash.file = File(instance.file, name=name)
|
trash.file = File(instance.file, name=name)
|
||||||
trash.save()
|
trash.save()
|
||||||
path = instance.file.path
|
|
||||||
file_dir = "/".join(path.split("/")[:-1]) + "/"
|
|
||||||
if os.path.isfile(path):
|
if os.path.isfile(path):
|
||||||
os.remove(path)
|
os.remove(path)
|
||||||
if os.path.isdir(file_dir) and len(os.listdir(file_dir)) == 0:
|
if os.path.isdir(file_dir) and len(os.listdir(file_dir)) == 0:
|
||||||
|
|
|
@ -5,11 +5,13 @@
|
||||||
ChunkedUploadView,
|
ChunkedUploadView,
|
||||||
TopFolderView,
|
TopFolderView,
|
||||||
delete_file_view,
|
delete_file_view,
|
||||||
|
delete_folder_view,
|
||||||
file_report_list,
|
file_report_list,
|
||||||
file_table,
|
file_table,
|
||||||
file_update,
|
file_update,
|
||||||
files_view,
|
files_view,
|
||||||
folder_create,
|
folder_create,
|
||||||
|
folder_update,
|
||||||
folder_view,
|
folder_view,
|
||||||
report_file,
|
report_file,
|
||||||
)
|
)
|
||||||
|
@ -32,9 +34,11 @@
|
||||||
path("api/chunked_upload/", ChunkedUploadView.as_view(), name="api_chunked_upload"),
|
path("api/chunked_upload/", ChunkedUploadView.as_view(), name="api_chunked_upload"),
|
||||||
path("api/folder/create/", folder_create, name="folder_create"),
|
path("api/folder/create/", folder_create, name="folder_create"),
|
||||||
path("api/file/report/<str:slug>", report_file, name="file_report"),
|
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/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>", files_view, name="view"),
|
||||||
path("<str:slug>/update", file_update, name="update"),
|
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>", folder_view, name="folder"),
|
||||||
|
path("f/<str:slug>/update", folder_update, name="folder_update"),
|
||||||
]
|
]
|
||||||
|
|
|
@ -30,6 +30,7 @@
|
||||||
from akarpov.files.forms import FileForm, FolderForm
|
from akarpov.files.forms import FileForm, FolderForm
|
||||||
from akarpov.files.models import BaseFileItem, File, FileReport, Folder
|
from akarpov.files.models import BaseFileItem, File, FileReport, Folder
|
||||||
from akarpov.files.previews import extensions, meta, meta_extensions, previews
|
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.services.preview import get_base_meta
|
||||||
from akarpov.files.tables import FileTable
|
from akarpov.files.tables import FileTable
|
||||||
|
|
||||||
|
@ -202,8 +203,11 @@ def get_context_data(self, **kwargs):
|
||||||
class DeleteFileView(LoginRequiredMixin, RedirectView):
|
class DeleteFileView(LoginRequiredMixin, RedirectView):
|
||||||
def get_redirect_url(self, *args, **kwargs):
|
def get_redirect_url(self, *args, **kwargs):
|
||||||
file = get_object_or_404(File, slug=kwargs["slug"])
|
file = get_object_or_404(File, slug=kwargs["slug"])
|
||||||
|
parent = file.parent
|
||||||
if file.user == self.request.user:
|
if file.user == self.request.user:
|
||||||
file.delete()
|
file.delete()
|
||||||
|
if parent:
|
||||||
|
return reverse("files:folder", kwargs={"slug": parent.slug})
|
||||||
return reverse("files:main")
|
return reverse("files:main")
|
||||||
|
|
||||||
|
|
||||||
|
@ -308,3 +312,33 @@ class ListFileReports(SuperUserRequiredMixin, ListView):
|
||||||
|
|
||||||
|
|
||||||
file_report_list = ListFileReports.as_view()
|
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()
|
||||||
|
|
|
@ -42,6 +42,10 @@
|
||||||
{% for f in folders %}
|
{% for f in folders %}
|
||||||
<li class="breadcrumb-item"><a href="{% url 'files:folder' slug=f.slug %}">{{ f.name }}</a></li>
|
<li class="breadcrumb-item"><a href="{% url 'files:folder' slug=f.slug %}">{{ f.name }}</a></li>
|
||||||
{% endfor %}
|
{% 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>
|
</ol>
|
||||||
</nav>
|
</nav>
|
||||||
{% else %}
|
{% else %}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user