From 512c52e129990c740a2723b73bccf0c3cdaf9f33 Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Sat, 5 Sep 2026 09:34:13 -0700 Subject: [PATCH] Add the filter, migration --- src/documents/filters.py | 25 ++++++ ...lter_document_archive_checksum_and_more.py | 86 +++++++++++++++++++ src/documents/models.py | 2 + 3 files changed, 113 insertions(+) create mode 100644 src/documents/migrations/0026_alter_document_archive_checksum_and_more.py diff --git a/src/documents/filters.py b/src/documents/filters.py index 40ae8978be..dc0838e444 100644 --- a/src/documents/filters.py +++ b/src/documents/filters.py @@ -50,6 +50,7 @@ from documents.models import ShareLink from documents.models import ShareLinkBundle from documents.models import StoragePath from documents.models import Tag +from documents.permissions import permitted_document_ids from documents.permissions import permitted_object_ids if TYPE_CHECKING: @@ -793,6 +794,8 @@ class CustomFieldQueryFilter(Filter): class DocumentFilterSet(FilterSet): + has_duplicates = BooleanFilter(method="filter_has_duplicates") + is_tagged = BooleanFilter( label="Is tagged", field_name="tags", @@ -852,6 +855,28 @@ class DocumentFilterSet(FilterSet): mime_type = MimeTypeFilter() + def filter_has_duplicates(self, queryset, name, value): + if value is None: + return queryset + + visible_root_documents = Document.global_objects.filter( + root_document__isnull=True, + pk__in=permitted_document_ids( + getattr(self.request, "user", None), + include_deleted=True, + ), + ).exclude(pk=OuterRef("pk")) + matching_duplicates = visible_root_documents.filter( + Q(checksum=OuterRef("checksum")) + | Q(checksum=OuterRef("archive_checksum")) + | Q(archive_checksum=OuterRef("checksum")) + | Q(archive_checksum=OuterRef("archive_checksum")), + ) + + return queryset.alias( + has_visible_duplicates=Exists(matching_duplicates), + ).filter(has_visible_duplicates=value) + # Backwards compatibility created__date__gt = DateFilter(field_name="created", lookup_expr="gt") created__date__gte = DateFilter(field_name="created", lookup_expr="gte") diff --git a/src/documents/migrations/0026_alter_document_archive_checksum_and_more.py b/src/documents/migrations/0026_alter_document_archive_checksum_and_more.py new file mode 100644 index 0000000000..ea08e437c2 --- /dev/null +++ b/src/documents/migrations/0026_alter_document_archive_checksum_and_more.py @@ -0,0 +1,86 @@ +# Generated by Django 5.2.16 on 2026-09-05 16:29 + +from django.db import migrations +from django.db import models + + +class Migration(migrations.Migration): + dependencies = [ + ("documents", "0025_workflowaction_apply_ai_suggestions"), + ] + + operations = [ + migrations.AlterField( + model_name="document", + name="archive_checksum", + field=models.CharField( + blank=True, + db_index=True, + editable=False, + help_text="The checksum of the archived document.", + max_length=64, + null=True, + verbose_name="archive checksum", + ), + ), + migrations.AlterField( + model_name="savedviewfilterrule", + name="rule_type", + field=models.PositiveSmallIntegerField( + choices=[ + (0, "title contains"), + (1, "content contains"), + (2, "ASN is"), + (3, "correspondent is"), + (4, "document type is"), + (5, "is in inbox"), + (6, "has tag"), + (7, "has any tag"), + (8, "created before"), + (9, "created after"), + (10, "created year is"), + (11, "created month is"), + (12, "created day is"), + (13, "added before"), + (14, "added after"), + (15, "modified before"), + (16, "modified after"), + (17, "does not have tag"), + (18, "does not have ASN"), + (19, "title or content contains"), + (20, "fulltext query"), + (21, "more like this"), + (22, "has tags in"), + (23, "ASN greater than"), + (24, "ASN less than"), + (25, "storage path is"), + (26, "has correspondent in"), + (27, "does not have correspondent in"), + (28, "has document type in"), + (29, "does not have document type in"), + (30, "has storage path in"), + (31, "does not have storage path in"), + (32, "owner is"), + (33, "has owner in"), + (34, "does not have owner"), + (35, "does not have owner in"), + (36, "has custom field value"), + (37, "is shared by me"), + (38, "has custom fields"), + (39, "has custom field in"), + (40, "does not have custom field in"), + (41, "does not have custom field"), + (42, "custom fields query"), + (43, "created to"), + (44, "created from"), + (45, "added to"), + (46, "added from"), + (47, "mime type is"), + (48, "simple title search"), + (49, "simple text search"), + (50, "has duplicates"), + ], + verbose_name="rule type", + ), + ), + ] diff --git a/src/documents/models.py b/src/documents/models.py index 8dc3090f2d..0cc4decdab 100644 --- a/src/documents/models.py +++ b/src/documents/models.py @@ -227,6 +227,7 @@ class Document(SoftDeleteModel, ModelWithOwner): # type: ignore[django-manager- editable=False, blank=True, null=True, + db_index=True, help_text=_("The checksum of the archived document."), ) @@ -706,6 +707,7 @@ class SavedViewFilterRule(models.Model): (47, _("mime type is")), (48, _("simple title search")), (49, _("simple text search")), + (50, _("has duplicates")), ] saved_view = models.ForeignKey(