Prevent edit and add thumbnail to chooser and list

This commit is contained in:
Jake Howard 2022-07-12 15:48:40 +01:00
parent 72dc3cc4ea
commit a229bb530a
Signed by: jake
GPG key ID: 57AFB45680EDD477
5 changed files with 44 additions and 4 deletions

View file

@ -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"]

View file

@ -8,6 +8,7 @@
<col width="16%" />
<thead>
<tr class="table-headers">
<th></th>
<th>{% trans "Unsplash ID" %}</th>
<th>{% trans "Description" %}</th>
</tr>
@ -15,6 +16,11 @@
<tbody>
{% for row in rows %}
<tr>
<td>
<a class="item-choice" href="{{ row.choose_url }}">
<img src="{{ row.item.get_thumbnail_url }}" width="165"/>
</a>
</td>
<td class="title">
<h2><a class="item-choice" href="{{ row.choose_url }}">{{ row.item.unsplash_id }}</a></h2>
</td>

View file

@ -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)

View file

@ -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(
"<img src='{}' width=165 class='admin-thumb'/>",
instance.get_thumbnail_url(),
)
@hooks.register("register_admin_viewset")
def register_person_chooser_viewset() -> UnsplashPhotoChooserViewSet:

View file

@ -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("<img src='{}' width=165>", instance.get_thumbnail_url())