mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2026-02-11 23:59:31 -06:00
Trash versions when deleting head docs
This commit is contained in:
@@ -309,16 +309,28 @@ def modify_custom_fields(
|
|||||||
@shared_task
|
@shared_task
|
||||||
def delete(doc_ids: list[int]) -> Literal["OK"]:
|
def delete(doc_ids: list[int]) -> Literal["OK"]:
|
||||||
try:
|
try:
|
||||||
Document.objects.filter(id__in=doc_ids).delete()
|
head_ids = (
|
||||||
|
Document.objects.filter(id__in=doc_ids, head_version__isnull=True)
|
||||||
|
.values_list("id", flat=True)
|
||||||
|
.distinct()
|
||||||
|
)
|
||||||
|
version_ids = (
|
||||||
|
Document.objects.filter(head_version_id__in=head_ids)
|
||||||
|
.values_list("id", flat=True)
|
||||||
|
.distinct()
|
||||||
|
)
|
||||||
|
delete_ids = list({*doc_ids, *version_ids})
|
||||||
|
|
||||||
|
Document.objects.filter(id__in=delete_ids).delete()
|
||||||
|
|
||||||
from documents import index
|
from documents import index
|
||||||
|
|
||||||
with index.open_index_writer() as writer:
|
with index.open_index_writer() as writer:
|
||||||
for id in doc_ids:
|
for id in delete_ids:
|
||||||
index.remove_document_by_id(writer, id)
|
index.remove_document_by_id(writer, id)
|
||||||
|
|
||||||
status_mgr = DocumentsStatusManager()
|
status_mgr = DocumentsStatusManager()
|
||||||
status_mgr.send_documents_deleted(doc_ids)
|
status_mgr.send_documents_deleted(delete_ids)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
if "Data too long for column" in str(e):
|
if "Data too long for column" in str(e):
|
||||||
logger.warning(
|
logger.warning(
|
||||||
|
|||||||
@@ -436,6 +436,12 @@ class Document(SoftDeleteModel, ModelWithOwner):
|
|||||||
tags_to_add = self.tags.model.objects.filter(id__in=tag_ids)
|
tags_to_add = self.tags.model.objects.filter(id__in=tag_ids)
|
||||||
self.tags.add(*tags_to_add)
|
self.tags.add(*tags_to_add)
|
||||||
|
|
||||||
|
def delete(self, using=None, *, keep_parents=False):
|
||||||
|
# If deleting a head document, move all versions to trash as well.
|
||||||
|
if self.head_version_id is None:
|
||||||
|
Document.objects.filter(head_version=self).delete()
|
||||||
|
return super().delete(using=using, keep_parents=keep_parents)
|
||||||
|
|
||||||
|
|
||||||
class SavedView(ModelWithOwner):
|
class SavedView(ModelWithOwner):
|
||||||
class DisplayMode(models.TextChoices):
|
class DisplayMode(models.TextChoices):
|
||||||
|
|||||||
@@ -78,6 +78,28 @@ class TestDocument(TestCase):
|
|||||||
empty_trash([document.pk])
|
empty_trash([document.pk])
|
||||||
self.assertEqual(mock_unlink.call_count, 2)
|
self.assertEqual(mock_unlink.call_count, 2)
|
||||||
|
|
||||||
|
def test_delete_head_deletes_versions(self) -> None:
|
||||||
|
head = Document.objects.create(
|
||||||
|
correspondent=Correspondent.objects.create(name="Test0"),
|
||||||
|
title="Head",
|
||||||
|
content="content",
|
||||||
|
checksum="checksum",
|
||||||
|
mime_type="application/pdf",
|
||||||
|
)
|
||||||
|
Document.objects.create(
|
||||||
|
head_version=head,
|
||||||
|
correspondent=head.correspondent,
|
||||||
|
title="Version",
|
||||||
|
content="content",
|
||||||
|
checksum="checksum2",
|
||||||
|
mime_type="application/pdf",
|
||||||
|
)
|
||||||
|
|
||||||
|
head.delete()
|
||||||
|
|
||||||
|
self.assertEqual(Document.objects.count(), 0)
|
||||||
|
self.assertEqual(Document.deleted_objects.count(), 2)
|
||||||
|
|
||||||
def test_file_name(self) -> None:
|
def test_file_name(self) -> None:
|
||||||
doc = Document(
|
doc = Document(
|
||||||
mime_type="application/pdf",
|
mime_type="application/pdf",
|
||||||
|
|||||||
Reference in New Issue
Block a user