From e62f9de9fc5f5dab0b098120b09ef1017a6609c4 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Tue, 20 Sep 2022 09:24:00 +0100 Subject: [PATCH] Add indexing of unsplash photos --- website/contrib/unsplash/models.py | 8 +++++++- website/contrib/unsplash/wagtail_hooks.py | 3 ++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/website/contrib/unsplash/models.py b/website/contrib/unsplash/models.py index 4a3708b..b802ccc 100644 --- a/website/contrib/unsplash/models.py +++ b/website/contrib/unsplash/models.py @@ -2,6 +2,7 @@ from typing import TypedDict from django.db import models from django.utils import timezone +from wagtail.search import index class ImageURLs(TypedDict): @@ -12,12 +13,17 @@ class ImageURLs(TypedDict): thumb: str -class UnsplashPhoto(models.Model): +class UnsplashPhoto(index.Indexed, models.Model): unsplash_id = models.CharField(unique=True, max_length=11, db_index=True) data = models.JSONField() created = models.DateTimeField(auto_now_add=True) data_last_updated = models.DateTimeField(default=timezone.now) + search_fields = [ + index.SearchField("unsplash_id", boost=10), + index.SearchField("get_description"), + ] + def get_description(self) -> str: return self.data["description"] diff --git a/website/contrib/unsplash/wagtail_hooks.py b/website/contrib/unsplash/wagtail_hooks.py index 3ad142f..19768d5 100644 --- a/website/contrib/unsplash/wagtail_hooks.py +++ b/website/contrib/unsplash/wagtail_hooks.py @@ -4,6 +4,7 @@ from django.core.exceptions import ValidationError from django.http.response import Http404 from django.utils.html import format_html from wagtail.admin.forms.models import WagtailAdminModelForm +from wagtail.contrib.modeladmin.helpers import WagtailBackendSearchHandler from wagtail.contrib.modeladmin.options import ModelAdmin, modeladmin_register from wagtail.contrib.modeladmin.views import CreateView, EditView, IndexView from wagtail.core import hooks @@ -61,7 +62,7 @@ class UnsplashPhotoAdmin(ModelAdmin): model = UnsplashPhoto list_display = ["unsplash_id", "thumbnail", "description", "data_last_updated"] form_fields_exclude = ["data", "data_last_updated"] - search_fields = ["unsplash_id", "data__description"] + search_handler_class = WagtailBackendSearchHandler create_view_class = UnsplashPhotoCreateView index_view_class = UnsplashPhotoIndexView edit_view_class = UnsplashPhotoEditView