Prevent edit and add thumbnail to chooser and list
This commit is contained in:
parent
72dc3cc4ea
commit
a229bb530a
5 changed files with 44 additions and 4 deletions
|
@ -7,3 +7,6 @@ class UnsplashPhoto(models.Model):
|
||||||
|
|
||||||
def get_description(self) -> str:
|
def get_description(self) -> str:
|
||||||
return self.data["description"]
|
return self.data["description"]
|
||||||
|
|
||||||
|
def get_thumbnail_url(self) -> str:
|
||||||
|
return self.data["urls"]["thumb"]
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
<col width="16%" />
|
<col width="16%" />
|
||||||
<thead>
|
<thead>
|
||||||
<tr class="table-headers">
|
<tr class="table-headers">
|
||||||
|
<th></th>
|
||||||
<th>{% trans "Unsplash ID" %}</th>
|
<th>{% trans "Unsplash ID" %}</th>
|
||||||
<th>{% trans "Description" %}</th>
|
<th>{% trans "Description" %}</th>
|
||||||
</tr>
|
</tr>
|
||||||
|
@ -15,6 +16,11 @@
|
||||||
<tbody>
|
<tbody>
|
||||||
{% for row in rows %}
|
{% for row in rows %}
|
||||||
<tr>
|
<tr>
|
||||||
|
<td>
|
||||||
|
<a class="item-choice" href="{{ row.choose_url }}">
|
||||||
|
<img src="{{ row.item.get_thumbnail_url }}" width="165"/>
|
||||||
|
</a>
|
||||||
|
</td>
|
||||||
<td class="title">
|
<td class="title">
|
||||||
<h2><a class="item-choice" href="{{ row.choose_url }}">{{ row.item.unsplash_id }}</a></h2>
|
<h2><a class="item-choice" href="{{ row.choose_url }}">{{ row.item.unsplash_id }}</a></h2>
|
||||||
</td>
|
</td>
|
||||||
|
|
|
@ -20,7 +20,10 @@ class UnsplashPhotoChooserMixin(ModelChooserMixin):
|
||||||
results_template = "unsplash/results.html"
|
results_template = "unsplash/results.html"
|
||||||
|
|
||||||
def get_object_string(self, instance: UnsplashPhoto) -> str:
|
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:
|
def get_row_data(self, item: UnsplashPhoto) -> dict:
|
||||||
item_data = super().get_row_data(item)
|
item_data = super().get_row_data(item)
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
from typing import Type
|
from typing import Type
|
||||||
|
|
||||||
from django.core.exceptions import ValidationError
|
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.admin.forms.models import WagtailAdminModelForm
|
||||||
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
|
from wagtail.contrib.modeladmin.views import CreateView, EditView, IndexView
|
||||||
from wagtail.core import hooks
|
from wagtail.core import hooks
|
||||||
|
|
||||||
from .models import UnsplashPhoto
|
from .models import UnsplashPhoto
|
||||||
|
@ -37,18 +39,43 @@ class UnsplashPhotoCreateView(CreateView):
|
||||||
return CreateFormClass
|
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
|
@modeladmin_register
|
||||||
class UnsplashPhotoAdmin(ModelAdmin):
|
class UnsplashPhotoAdmin(ModelAdmin):
|
||||||
model = UnsplashPhoto
|
model = UnsplashPhoto
|
||||||
list_display = ["unsplash_id", "description"]
|
list_display = ["unsplash_id", "thumbnail", "description"]
|
||||||
form_fields_exclude = ["data"]
|
form_fields_exclude = ["data"]
|
||||||
search_fields = ["unsplash_id", "data__description"]
|
search_fields = ["unsplash_id", "data__description"]
|
||||||
create_view_class = UnsplashPhotoCreateView
|
create_view_class = UnsplashPhotoCreateView
|
||||||
|
index_view_class = UnsplashPhotoIndexView
|
||||||
|
edit_view_class = UnsplashPhotoEditView
|
||||||
menu_icon = "image"
|
menu_icon = "image"
|
||||||
|
|
||||||
def description(self, instance: UnsplashPhoto) -> str:
|
def description(self, instance: UnsplashPhoto) -> str:
|
||||||
return instance.get_description()
|
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")
|
@hooks.register("register_admin_viewset")
|
||||||
def register_person_chooser_viewset() -> UnsplashPhotoChooserViewSet:
|
def register_person_chooser_viewset() -> UnsplashPhotoChooserViewSet:
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
from django.utils.html import format_html
|
||||||
from generic_chooser.widgets import AdminChooser
|
from generic_chooser.widgets import AdminChooser
|
||||||
|
|
||||||
from .models import UnsplashPhoto
|
from .models import UnsplashPhoto
|
||||||
|
@ -12,4 +13,4 @@ class UnsplashPhotoChooser(AdminChooser):
|
||||||
choose_modal_url_name = "unsplash_photo_chooser:choose"
|
choose_modal_url_name = "unsplash_photo_chooser:choose"
|
||||||
|
|
||||||
def get_title(self, instance: UnsplashPhoto) -> str:
|
def get_title(self, instance: UnsplashPhoto) -> str:
|
||||||
return instance.unsplash_id
|
return format_html("<img src='{}' width=165>", instance.get_thumbnail_url())
|
||||||
|
|
Loading…
Reference in a new issue