@@ -20,7 +20,7 @@ def patterns(prefix, *args):
2020
2121
2222class AdminRowActionsMixin (object ):
23-
23+
2424 """ModelAdmin mixin to add row actions just like adding admin actions"""
2525
2626 rowactions = []
@@ -32,103 +32,106 @@ def media(self):
3232 media .add_js (['js/jquery.dropdown.min.js' ])
3333 media .add_css ({'all' : ['css/jquery.dropdown.min.css' ]})
3434 return media
35-
35+
3636 def get_list_display (self , request ):
37+ self ._request = request
3738 list_display = super (AdminRowActionsMixin , self ).get_list_display (request )
3839 if '_row_actions' not in list_display :
3940 list_display += ('_row_actions' ,)
4041 return list_display
41-
42+
4243 def get_actions_list (self , obj , includePk = True ):
43-
44+
4445 def to_dict (tool_name ):
4546 return dict (
4647 name = tool_name ,
4748 label = getattr (tool , 'label' , tool_name ).replace ('_' , ' ' ).title (),
4849 )
49-
50+
5051 items = []
51-
52+
5253 row_actions = self .get_row_actions (obj )
5354 url_prefix = '{}/' .format (obj .pk if includePk else '' )
54-
55+
5556 for tool in row_actions :
5657 if isinstance (tool , string_types ): # Just a str naming a callable
5758 tool_dict = to_dict (tool )
5859 items .append ({
5960 'label' : tool_dict ['label' ],
6061 'url' : '{}rowactions/{}/' .format (url_prefix , tool ),
62+ 'method' : tool_dict .get ('POST' , 'GET' )
6163 })
62-
64+
6365 elif isinstance (tool , dict ): # A parameter dict
6466 tool ['enabled' ] = tool .get ('enabled' , True )
65- if 'action' in tool : # If 'action' is specified then use our generic url in preference to 'url' value
67+ if 'action' in tool : # If 'action' is specified then use our generic url in preference to 'url' value
6668 if isinstance (tool ['action' ], tuple ):
6769 self ._named_row_actions [tool ['action' ][0 ]] = tool ['action' ][1 ]
6870 tool ['url' ] = '{}rowactions/{}/' .format (url_prefix , tool ['action' ][0 ])
6971 else :
7072 tool ['url' ] = '{}rowactions/{}/' .format (url_prefix , tool ['action' ])
7173 items .append (tool )
72-
74+
7375 return items
74-
76+
7577 def _row_actions (self , obj ):
76-
78+
7779 items = self .get_actions_list (obj )
7880 if items :
7981 html = Dropdown (
8082 label = _ ("Actions" ),
8183 items = items ,
84+ request = getattr (self , '_request' )
8285 ).render ()
8386
8487 return html
8588 return ''
8689 _row_actions .short_description = ''
8790 _row_actions .allow_tags = True
88-
91+
8992 def get_tool_urls (self ):
90-
93+
9194 """Gets the url patterns that route each tool to a special view"""
92-
95+
9396 my_urls = patterns (
9497 '' ,
9598 url (r'^(?P<pk>\d+)/rowactions/(?P<tool>\w+)/$' ,
9699 self .admin_site .admin_view (ModelToolsView .as_view (model = self .model ))
97100 )
98101 )
99102 return my_urls
100-
103+
101104 ###################################
102105 # EXISTING ADMIN METHODS MODIFIED #
103106 ###################################
104-
107+
105108 def get_urls (self ):
106-
109+
107110 """Prepends `get_urls` with our own patterns"""
108-
111+
109112 urls = super (AdminRowActionsMixin , self ).get_urls ()
110113 return self .get_tool_urls () + urls
111-
114+
112115 ##################
113116 # CUSTOM METHODS #
114117 ##################
115-
118+
116119 def get_row_actions (self , obj ):
117120 return getattr (self , 'rowactions' , False ) or []
118-
121+
119122 def get_change_actions (self , request , object_id , form_url ):
120-
123+
121124 # If we're also using django_object_actions
122125 # then try to reuse row actions as object actions
123-
126+
124127 change_actions = super (AdminRowActionsMixin , self ).get_change_actions (request , object_id , form_url )
125-
128+
126129 # Make this reuse opt-in
127130 if getattr (self , 'reuse_row_actions_as_object_actions' , False ):
128131
129132 obj = self .model .objects .get (pk = object_id )
130133 row_actions = self .get_actions_list (obj , False ) if obj else []
131-
134+
132135 for row_action in row_actions :
133136 # Object actions only supports strings as action indentifiers
134137 if isinstance (row_action , string_types ):
0 commit comments