Skip to content

Commit ba9d2fe

Browse files
authored
Merge pull request #15 from mgaitan/allow_post
Add method parameter to allow 'POST' actions
2 parents 98e59cf + 04ee691 commit ba9d2fe

File tree

3 files changed

+42
-32
lines changed

3 files changed

+42
-32
lines changed

django_admin_row_actions/admin.py

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def patterns(prefix, *args):
2020

2121

2222
class 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):

django_admin_row_actions/components.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,13 @@
33

44

55
class BaseComponent(object):
6-
6+
77
template = None
88
instances = []
99

1010
def __init__(self, **kwargs):
11-
12-
# Add ourselves to the class's list of instances
13-
# so we can generate a unique id number for the html
14-
1511
self.__class__.instances.append(weakref.proxy(self))
12+
self.request = kwargs.pop('request')
1613
self.context = kwargs
1714
self.context['dom_id'] = self.get_unique_id()
1815

@@ -24,6 +21,7 @@ def render(self):
2421
return render_to_string(
2522
self.template,
2623
self.context,
24+
request=self.request
2725
)
2826

2927
def __unicode__(self):

django_admin_row_actions/templates/django_admin_row_actions/dropdown.html

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,16 @@
88
{% endif %}
99
<li>
1010
{% if item.enabled %}
11-
<a class="{{ item.classes }}" href="{{ item.url }}" title="{{ item.tooltip }}">{{ item.label }}</a>
11+
{% if item.method == 'POST' %}
12+
<form method="post" action="{{ item.url }}">
13+
{% csrf_token %}
14+
<input class="{{ item.classes }}" type="submit" title="{{ item.tooltip }}" value="{{ item.label }}" />
15+
</form>
16+
{% else %}
17+
18+
<a class="{{ item.classes }}" href="{{ item.url }}" title="{{ item.tooltip }}">{{ item.label }}
19+
</a>
20+
{% endif %}
1221
{% else %}
1322
<span class="{{ item.classes }} jq-disabled" title="{{ item.tooltip }}">{{ item.label }}</span>
1423
{% endif %}

0 commit comments

Comments
 (0)