@@ -65,11 +65,25 @@ def has_change_permission(self, request, obj=None):
6565 def has_delete_permission (self , request , obj = None ):
6666 return self .has_permission (request , obj , "delete" )
6767
68+ def has_permission_for_action (self , request , action_id ):
69+ return self .has_permission (
70+ request ,
71+ obj = None ,
72+ permission = action_id ,
73+ )
74+
6875 def has_view_or_change_permission (self , request , obj = None ):
6976 return self .has_view_permission (request , obj ) or self .has_change_permission (
7077 request , obj
7178 )
7279
80+ def process_actions_permissions (self , request , actions ):
81+ result = []
82+ for action in actions :
83+ if self .has_permission_for_action (request , action .action_id ):
84+ result .append (action )
85+ return result
86+
7387 def init_view_dynamic (self , request , request_data = None , ** kwargs ):
7488 if not self .has_view_or_change_permission (request ):
7589 raise PermissionDenied
@@ -96,6 +110,9 @@ def action_view(self, request, action=None, modifier=None):
96110 action_function = getattr (self , action , None )
97111 if not action_function :
98112 raise Http404
113+ permitted_action = self .has_permission_for_action (request , action )
114+ if not permitted_action :
115+ raise PermissionDenied
99116 return action_function (request , modifier )
100117
101118 def get_action_url (self , action , modifier = "template" ):
@@ -367,7 +384,8 @@ def _get_sbadmin_list_actions(self):
367384 * list_actions ,
368385 SBAdminCustomAction (
369386 title = _ (f"Reorder { self .model ._meta .verbose_name } " ),
370- url = self .get_action_url (Action .ENTER_REORDER .value ),
387+ view = self ,
388+ action_id = Action .ENTER_REORDER .value ,
371389 no_params = True ,
372390 ),
373391 ]
@@ -378,7 +396,8 @@ def get_sbadmin_list_actions(self):
378396 self .sbadmin_list_actions = [
379397 SBAdminCustomAction (
380398 title = _ ("Download XLSX" ),
381- url = self .get_action_url (action = Action .XLSX_EXPORT .value ),
399+ view = self ,
400+ action_id = Action .XLSX_EXPORT .value ,
382401 )
383402 ]
384403 return self .sbadmin_list_actions
@@ -388,19 +407,24 @@ def get_sbadmin_list_selection_actions(self):
388407 self .sbadmin_list_selection_actions = [
389408 SBAdminCustomAction (
390409 title = _ ("Export Selected" ),
391- url = self .get_action_url (action = Action .XLSX_EXPORT .value ),
410+ view = self ,
411+ action_id = Action .XLSX_EXPORT .value ,
392412 ),
393413 SBAdminCustomAction (
394414 title = _ ("Delete Selected" ),
395- url = self .get_action_url (action = Action .BULK_DELETE .value ),
415+ view = self ,
416+ action_id = Action .BULK_DELETE .value ,
396417 css_class = "btn-destructive" ,
397418 ),
398419 ]
399420 return self .sbadmin_list_selection_actions
400421
401- def get_sbadmin_list_selection_actions_grouped (self ):
422+ def get_sbadmin_list_selection_actions_grouped (self , request ):
402423 result = {}
403- for action in self .get_sbadmin_list_selection_actions ():
424+ list_selection_actions = self .process_actions_permissions (
425+ request , self .get_sbadmin_list_selection_actions ()
426+ )
427+ for action in list_selection_actions :
404428 if not result .get (action .group ):
405429 result .update ({action .group : []})
406430 result [action .group ].append (action )
0 commit comments