Add indexing of unsplash photos
This commit is contained in:
parent
997da9779c
commit
e62f9de9fc
2 changed files with 9 additions and 2 deletions
|
@ -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"]
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue