diff --git a/website/contrib/unsplash/models.py b/website/contrib/unsplash/models.py index c1d725a..73587f0 100644 --- a/website/contrib/unsplash/models.py +++ b/website/contrib/unsplash/models.py @@ -7,3 +7,6 @@ class UnsplashPhoto(models.Model): def get_description(self) -> str: return self.data["description"] + + def get_thumbnail_url(self) -> str: + return self.data["urls"]["thumb"] diff --git a/website/contrib/unsplash/templates/unsplash/results.html b/website/contrib/unsplash/templates/unsplash/results.html index 71f9f66..6216ea4 100644 --- a/website/contrib/unsplash/templates/unsplash/results.html +++ b/website/contrib/unsplash/templates/unsplash/results.html @@ -8,6 +8,7 @@ + {% trans "Unsplash ID" %} {% trans "Description" %} @@ -15,6 +16,11 @@ {% for row in rows %} + + + + +

{{ row.item.unsplash_id }}

diff --git a/website/contrib/unsplash/views.py b/website/contrib/unsplash/views.py index b97eb80..8f9f049 100644 --- a/website/contrib/unsplash/views.py +++ b/website/contrib/unsplash/views.py @@ -20,7 +20,10 @@ class UnsplashPhotoChooserMixin(ModelChooserMixin): results_template = "unsplash/results.html" def get_object_string(self, instance: UnsplashPhoto) -> str: - return instance.unsplash_id + """ + Just use the description. It doesn't like showing HTML here + """ + return instance.get_description() def get_row_data(self, item: UnsplashPhoto) -> dict: item_data = super().get_row_data(item) diff --git a/website/contrib/unsplash/wagtail_hooks.py b/website/contrib/unsplash/wagtail_hooks.py index 027a2ec..9a7111d 100644 --- a/website/contrib/unsplash/wagtail_hooks.py +++ b/website/contrib/unsplash/wagtail_hooks.py @@ -1,9 +1,11 @@ from typing import Type 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.options import ModelAdmin, modeladmin_register -from wagtail.contrib.modeladmin.views import CreateView +from wagtail.contrib.modeladmin.views import CreateView, EditView, IndexView from wagtail.core import hooks from .models import UnsplashPhoto @@ -37,18 +39,43 @@ class UnsplashPhotoCreateView(CreateView): return CreateFormClass +class UnsplashPhotoIndexView(IndexView): + def get_buttons_for_obj(self, obj: UnsplashPhoto) -> list: + buttons = super().get_buttons_for_obj(obj) + assert buttons[0]["label"] == "Edit" + buttons.pop(0) + return buttons + + +class UnsplashPhotoEditView(EditView): + """ + Prevent access to the edit view + """ + + def dispatch(self, *args: list, **kwargs: dict) -> None: + raise Http404 + + @modeladmin_register class UnsplashPhotoAdmin(ModelAdmin): model = UnsplashPhoto - list_display = ["unsplash_id", "description"] + list_display = ["unsplash_id", "thumbnail", "description"] form_fields_exclude = ["data"] search_fields = ["unsplash_id", "data__description"] create_view_class = UnsplashPhotoCreateView + index_view_class = UnsplashPhotoIndexView + edit_view_class = UnsplashPhotoEditView menu_icon = "image" def description(self, instance: UnsplashPhoto) -> str: return instance.get_description() + def thumbnail(self, instance: UnsplashPhoto) -> str: + return format_html( + "", + instance.get_thumbnail_url(), + ) + @hooks.register("register_admin_viewset") def register_person_chooser_viewset() -> UnsplashPhotoChooserViewSet: diff --git a/website/contrib/unsplash/widgets.py b/website/contrib/unsplash/widgets.py index 3cba351..03fad15 100644 --- a/website/contrib/unsplash/widgets.py +++ b/website/contrib/unsplash/widgets.py @@ -1,3 +1,4 @@ +from django.utils.html import format_html from generic_chooser.widgets import AdminChooser from .models import UnsplashPhoto @@ -12,4 +13,4 @@ class UnsplashPhotoChooser(AdminChooser): choose_modal_url_name = "unsplash_photo_chooser:choose" def get_title(self, instance: UnsplashPhoto) -> str: - return instance.unsplash_id + return format_html("", instance.get_thumbnail_url())