Skip to content

Commit

Permalink
zoom, enhance sector 128
Browse files Browse the repository at this point in the history
  • Loading branch information
zerolab committed Apr 23, 2023
1 parent 9e90364 commit 7fede7b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
54 changes: 51 additions & 3 deletions src/wagtailmedia/views/viewsets.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.utils.functional import cached_property
from django.utils.translation import gettext_lazy as _

from wagtail.admin.ui.tables import Column
Expand All @@ -10,15 +11,22 @@

from ..forms import get_media_form
from ..models import get_media_model
from ..permissions import permission_policy


Media = get_media_model()


class MediaChooserMixin:
construct_queryset_hook_name = "construct_media_chooser_queryset"

def get_object_list(self):
return Media.objects.select_related("collection").only(
"title", "file", "type", "collection__name", "created_at"
return (
permission_policy.instances_user_has_any_permission_for(
self.request.user, ["choose"]
)
.select_related("collection")
.only("title", "file", "type", "collection__name", "created_at")
)

@property
Expand All @@ -30,6 +38,25 @@ def columns(self):
Column("created_at", label="Uploaded", accessor="created_at"),
]

def get_filter_form(self):
FilterForm = self.get_filter_form_class()
return FilterForm(self.request.GET, collections=self.collections)

@cached_property
def collections(self):
collections = self.permission_policy.collections_user_has_permission_for(
self.request.user, "choose"
)
if len(collections) < 2:
return None

return collections

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["collections"] = self.collections
return context


class MediaChooseView(MediaChooserMixin, ChooseView):
pass
Expand All @@ -43,12 +70,29 @@ class MediaChosenView(ChosenView):
def get_object(self, pk):
return Media.objects.only("title", "thumbnail").get(pk=pk)

def get_chosen_response_data(self, media_item, preview_image_filter="max-165x165"):
"""
Given a media item, return the json data to pass back to the image chooser panel
"""
response_data = super().get_chosen_response_data(media_item)
if not media_item.thumbnail:
return response_data

preview_image = media_item.thumbnail
response_data["preview"] = {
"url": preview_image.url,
"width": 128,
"height": 128,
}
return response_data


class MediaChooserViewSet(ChooserViewSet):
choose_view_class = MediaChooseView
chosen_view_class = MediaChosenView
choose_results_view_class = MediaChooseResultsView
creation_form_class = get_media_form(Media)
permission_policy = permission_policy

icon = "media"
choose_one_text = _("Choose a media item")
Expand All @@ -60,4 +104,8 @@ class MediaChooserViewSet(ChooserViewSet):
form_fields = ["type", "title", "file", "collection", "thumbnail"]


media_chooser_viewset = MediaChooserViewSet("media_chooser", model=Media)
media_chooser_viewset = MediaChooserViewSet(
"media_chooser",
model=Media,
url_prefix="media/chooser",
)
2 changes: 1 addition & 1 deletion src/wagtailmedia/wagtail_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ def describe_collection_media(collection):


class MediaAdminURLFinder(ModelAdminURLFinder):
permission_policy = permission_policy
edit_url_name = "wagtailmedia:edit"
permission_policy = permission_policy


register_admin_url_finder(get_media_model(), MediaAdminURLFinder)
Expand Down

0 comments on commit 7fede7b

Please sign in to comment.