Add indexing of unsplash photos

This commit is contained in:
Jake Howard 2022-09-20 09:24:00 +01:00
parent 997da9779c
commit e62f9de9fc
Signed by: jake
GPG key ID: 57AFB45680EDD477
2 changed files with 9 additions and 2 deletions

View file

@ -2,6 +2,7 @@ from typing import TypedDict
from django.db import models from django.db import models
from django.utils import timezone from django.utils import timezone
from wagtail.search import index
class ImageURLs(TypedDict): class ImageURLs(TypedDict):
@ -12,12 +13,17 @@ class ImageURLs(TypedDict):
thumb: str thumb: str
class UnsplashPhoto(models.Model): class UnsplashPhoto(index.Indexed, models.Model):
unsplash_id = models.CharField(unique=True, max_length=11, db_index=True) unsplash_id = models.CharField(unique=True, max_length=11, db_index=True)
data = models.JSONField() data = models.JSONField()
created = models.DateTimeField(auto_now_add=True) created = models.DateTimeField(auto_now_add=True)
data_last_updated = models.DateTimeField(default=timezone.now) 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: def get_description(self) -> str:
return self.data["description"] return self.data["description"]

View file

@ -4,6 +4,7 @@ from django.core.exceptions import ValidationError
from django.http.response import Http404 from django.http.response import Http404
from django.utils.html import format_html from django.utils.html import format_html
from wagtail.admin.forms.models import WagtailAdminModelForm 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.options import ModelAdmin, modeladmin_register
from wagtail.contrib.modeladmin.views import CreateView, EditView, IndexView from wagtail.contrib.modeladmin.views import CreateView, EditView, IndexView
from wagtail.core import hooks from wagtail.core import hooks
@ -61,7 +62,7 @@ class UnsplashPhotoAdmin(ModelAdmin):
model = UnsplashPhoto model = UnsplashPhoto
list_display = ["unsplash_id", "thumbnail", "description", "data_last_updated"] list_display = ["unsplash_id", "thumbnail", "description", "data_last_updated"]
form_fields_exclude = ["data", "data_last_updated"] form_fields_exclude = ["data", "data_last_updated"]
search_fields = ["unsplash_id", "data__description"] search_handler_class = WagtailBackendSearchHandler
create_view_class = UnsplashPhotoCreateView create_view_class = UnsplashPhotoCreateView
index_view_class = UnsplashPhotoIndexView index_view_class = UnsplashPhotoIndexView
edit_view_class = UnsplashPhotoEditView edit_view_class = UnsplashPhotoEditView