Add indexes to snippets

This commit is contained in:
Jake Howard 2022-07-26 08:41:40 +01:00
parent 0c3b6633bd
commit 0601ced3f5
Signed by: jake
GPG key ID: 57AFB45680EDD477
3 changed files with 14 additions and 2 deletions

View file

@ -10,6 +10,7 @@ from wagtail.fields import StreamField
from wagtail.images import get_image_model_string from wagtail.images import get_image_model_string
from wagtail.images.views.serve import generate_image_url from wagtail.images.views.serve import generate_image_url
from wagtail.models import Page, PageQuerySet from wagtail.models import Page, PageQuerySet
from wagtail.search import index
from wagtail.snippets.models import register_snippet from wagtail.snippets.models import register_snippet
from website.common.utils import count_words from website.common.utils import count_words
@ -113,7 +114,7 @@ class ListingPage(BasePage, BaseContentMixin): # type: ignore[misc]
@register_snippet @register_snippet
class ReferralLink(models.Model): class ReferralLink(models.Model, index.Indexed):
url = models.URLField() url = models.URLField()
name = models.CharField(max_length=64, unique=True) name = models.CharField(max_length=64, unique=True)
@ -122,5 +123,7 @@ class ReferralLink(models.Model):
FieldPanel("url"), FieldPanel("url"),
] ]
search_fields = [index.AutocompleteField("name"), index.SearchField("url")]
def __str__(self) -> str: def __str__(self) -> str:
return self.name return self.name

View file

@ -3,6 +3,7 @@ from django.http.request import HttpRequest
from django.utils.functional import cached_property from django.utils.functional import cached_property
from django.utils.text import slugify from django.utils.text import slugify
from wagtail.admin.panels import FieldPanel from wagtail.admin.panels import FieldPanel
from wagtail.search import index
from wagtail.snippets.models import register_snippet from wagtail.snippets.models import register_snippet
from website.common.models import BaseContentMixin, BasePage from website.common.models import BaseContentMixin, BasePage
@ -10,7 +11,7 @@ from website.common.utils import TocEntry
@register_snippet @register_snippet
class OnlineAccount(models.Model): class OnlineAccount(models.Model, index.Indexed):
name = models.CharField(max_length=64, unique=True) name = models.CharField(max_length=64, unique=True)
url = models.URLField() url = models.URLField()
username = models.CharField(max_length=64) username = models.CharField(max_length=64)
@ -21,6 +22,12 @@ class OnlineAccount(models.Model):
FieldPanel("url"), FieldPanel("url"),
] ]
search_fields = [
index.AutocompleteField("name"),
index.FilterField("username"),
index.SearchField("url"),
]
def __str__(self) -> str: def __str__(self) -> str:
return self.name return self.name

View file

@ -153,6 +153,8 @@ WAGTAIL_SITE_NAME = "website"
WAGTAILSEARCH_BACKENDS = { WAGTAILSEARCH_BACKENDS = {
"default": { "default": {
"BACKEND": "wagtail.search.backends.database", "BACKEND": "wagtail.search.backends.database",
"AUTO_UPDATE": False,
"ATOMIC_REBUILD": True,
} }
} }