Skip to content
This repository has been archived by the owner on Feb 19, 2021. It is now read-only.

Commit

Permalink
Handle Tag model for insensitive search
Browse files Browse the repository at this point in the history
  • Loading branch information
CkuT committed Nov 11, 2019
1 parent 5b0b832 commit 77b0f65
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 16 deletions.
16 changes: 9 additions & 7 deletions src/documents/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
from django.utils.safestring import mark_safe
from djangoql.admin import DjangoQLSearchMixin

from documents.actions import (add_tag_to_selected,
remove_correspondent_from_selected,
remove_tag_from_selected,
set_correspondent_on_selected)
from paperless.utils import slugify
from documents.actions import (
add_tag_to_selected,
remove_correspondent_from_selected,
remove_tag_from_selected,
set_correspondent_on_selected
)
from paperless.utils import make_searchable

from .models import Correspondent, Document, Log, Tag

Expand Down Expand Up @@ -176,7 +178,7 @@ class Media:
"correspondent__name",
"searchable_title",
"searchable_content",
"tags__name",
"tags__searchable_name",
)
readonly_fields = ("added", "file_type", "storage_type",)
list_display = ("title", "created", "added", "thumbnail", "correspondent",
Expand Down Expand Up @@ -343,7 +345,7 @@ def _html_tag(kind, inside=None, **kwargs):
return format_html("<{} {}/>", kind, attributes)

def get_search_results(self, request, queryset, search_term):
search_term = slugify(search_term)
search_term = make_searchable(search_term)
return super().get_search_results(request, queryset, search_term)


Expand Down
25 changes: 21 additions & 4 deletions src/documents/migrations/0023_document_searchable_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@

from django.db import migrations, models

from paperless.utils import slugify as slugifyOCR

def slugifyOCR(content):
return (
unicodedata.normalize("NFKD", content.casefold())
.encode("ASCII", "ignore")
.decode("utf-8")
)


class Migration(migrations.Migration):
Expand All @@ -20,12 +26,16 @@ def casefold_forwards(apps, schema_editor):
doc.searchable_content = slugifyOCR(doc.content)
doc.save()

def casefold_backwards(apps, schema_editor):
pass
Tag = apps.get_model("documents", "Tag")

for tag in Tag.objects.all():
tag.searchable_name = slugifyOCR(tag.name)
tag.save()

def database_backwards(self, app_label, schema_editor, from_state, to_state):
migrations.RemoveField(model_name="document", name="searchable_content"),
migrations.RemoveField(model_name="document", name="searchable_title"),
migrations.RemoveField(model_name="tag", name="searchable_name"),

operations = [
migrations.AddField(
Expand All @@ -40,5 +50,12 @@ def database_backwards(self, app_label, schema_editor, from_state, to_state):
max_length=128, blank=True, db_index=True, editable=False
),
),
migrations.RunPython(casefold_forwards, casefold_backwards),
migrations.AddField(
model_name="tag",
name="searchable_name",
field=models.CharField(
max_length=128, blank=True, db_index=True, editable=False
),
),
migrations.RunPython(casefold_forwards, migrations.RunPython.noop),
]
16 changes: 12 additions & 4 deletions src/documents/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@
import dateutil.parser
from django.conf import settings
from django.db import models
from django.template.defaultfilters import slugify
from django.utils import timezone
from django.utils.text import slugify
from fuzzywuzzy import fuzz

from paperless.utils import slugify as slugifyOCR
from paperless.utils import make_searchable

from .managers import LogManager

Expand Down Expand Up @@ -185,6 +184,15 @@ class Tag(MatchingModel):

colour = models.PositiveIntegerField(choices=COLOURS, default=1)

searchable_name = models.CharField(
max_length=128, blank=True, db_index=True, editable=False,
)

def save(self, *args, **kwargs):
if self.name is not None:
self.searchable_name = make_searchable(self.name)
return super().save(*args, **kwargs)


class Document(models.Model):

Expand Down Expand Up @@ -283,9 +291,9 @@ def __str__(self):

def save(self, *args, **kwargs):
if self.title is not None:
self.searchable_title = slugifyOCR(self.title)
self.searchable_title = make_searchable(self.title)
if self.content is not None:
self.searchable_content = slugifyOCR(self.content)
self.searchable_content = make_searchable(self.content)
return super().save(*args, **kwargs)

@property
Expand Down
12 changes: 12 additions & 0 deletions src/documents/tests/test_tag_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from unittest import mock

from django.test import TestCase

from ..models import Tag


class TestTag(TestCase):
def test_searchable_name(self):
tag = Tag.objects.create(name="Titlé")
self.assertEqual(tag.name, "Titlé")
self.assertEqual(tag.searchable_name, "title")
2 changes: 1 addition & 1 deletion src/paperless/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import unicodedata


def slugify(content):
def make_searchable(content):
return (
unicodedata.normalize("NFKD", content.casefold())
.encode("ASCII", "ignore")
Expand Down

0 comments on commit 77b0f65

Please sign in to comment.