1+ from django .utils .functional import cached_property
12from django .utils .translation import gettext_lazy as _
23
34from wagtail .admin .ui .tables import Column
1011
1112from ..forms import get_media_form
1213from ..models import get_media_model
14+ from ..permissions import permission_policy
1315
1416
1517Media = get_media_model ()
1618
1719
1820class MediaChooserMixin :
21+ construct_queryset_hook_name = "construct_media_chooser_queryset"
22+
1923 def get_object_list (self ):
20- return Media .objects .select_related ("collection" ).only (
21- "title" , "file" , "type" , "collection__name" , "created_at"
24+ return (
25+ permission_policy .instances_user_has_any_permission_for (
26+ self .request .user , ["choose" ]
27+ )
28+ .select_related ("collection" )
29+ .only ("title" , "file" , "type" , "collection__name" , "created_at" )
2230 )
2331
2432 @property
@@ -30,6 +38,25 @@ def columns(self):
3038 Column ("created_at" , label = "Uploaded" , accessor = "created_at" ),
3139 ]
3240
41+ def get_filter_form (self ):
42+ FilterForm = self .get_filter_form_class ()
43+ return FilterForm (self .request .GET , collections = self .collections )
44+
45+ @cached_property
46+ def collections (self ):
47+ collections = self .permission_policy .collections_user_has_permission_for (
48+ self .request .user , "choose"
49+ )
50+ if len (collections ) < 2 :
51+ return None
52+
53+ return collections
54+
55+ def get_context_data (self , ** kwargs ):
56+ context = super ().get_context_data (** kwargs )
57+ context ["collections" ] = self .collections
58+ return context
59+
3360
3461class MediaChooseView (MediaChooserMixin , ChooseView ):
3562 pass
@@ -43,12 +70,29 @@ class MediaChosenView(ChosenView):
4370 def get_object (self , pk ):
4471 return Media .objects .only ("title" , "thumbnail" ).get (pk = pk )
4572
73+ def get_chosen_response_data (self , media_item , preview_image_filter = "max-165x165" ):
74+ """
75+ Given a media item, return the json data to pass back to the image chooser panel
76+ """
77+ response_data = super ().get_chosen_response_data (media_item )
78+ if not media_item .thumbnail :
79+ return response_data
80+
81+ preview_image = media_item .thumbnail
82+ response_data ["preview" ] = {
83+ "url" : preview_image .url ,
84+ "width" : 128 ,
85+ "height" : 128 ,
86+ }
87+ return response_data
88+
4689
4790class MediaChooserViewSet (ChooserViewSet ):
4891 choose_view_class = MediaChooseView
4992 chosen_view_class = MediaChosenView
5093 choose_results_view_class = MediaChooseResultsView
5194 creation_form_class = get_media_form (Media )
95+ permission_policy = permission_policy
5296
5397 icon = "media"
5498 choose_one_text = _ ("Choose a media item" )
@@ -60,4 +104,8 @@ class MediaChooserViewSet(ChooserViewSet):
60104 form_fields = ["type" , "title" , "file" , "collection" , "thumbnail" ]
61105
62106
63- media_chooser_viewset = MediaChooserViewSet ("media_chooser" , model = Media )
107+ media_chooser_viewset = MediaChooserViewSet (
108+ "media_chooser" ,
109+ model = Media ,
110+ url_prefix = "media/chooser" ,
111+ )
0 commit comments