Skip to content

Commit 61fb1e5

Browse files
authored
Add support for all primary key formats (#75)
* support pks that are strings * change pattern to be the same as what the Django admin uses
1 parent 38905d0 commit 61fb1e5

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

Diff for: django_object_actions/tests/test_admin.py

+22
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from __future__ import unicode_literals
55

66
from django.core.urlresolvers import reverse
7+
from django.http import HttpResponse
8+
from mock import patch
79

810
from .tests import LoggedInTestCase
911
from example_project.polls.factories import CommentFactory, PollFactory
@@ -19,6 +21,26 @@ def test_action_on_a_model_with_uuid_pk_works(self):
1921
response = self.client.get(action_url)
2022
self.assertRedirects(response, comment_url)
2123

24+
@patch('django_object_actions.utils.ChangeActionView.get')
25+
def test_action_on_a_model_with_arbitrary_pk_works(self, mock_view):
26+
mock_view.return_value = HttpResponse()
27+
action_url = '/admin/polls/comment/{0}/actions/hodor/'.format(' i am a pk ')
28+
29+
self.client.get(action_url)
30+
31+
self.assertTrue(mock_view.called)
32+
self.assertEqual(mock_view.call_args[1]['pk'], ' i am a pk ')
33+
34+
@patch('django_object_actions.utils.ChangeActionView.get')
35+
def test_action_on_a_model_with_slash_in_pk_works(self, mock_view):
36+
mock_view.return_value = HttpResponse()
37+
action_url = '/admin/polls/comment/{0}/actions/hodor/'.format('pk/slash')
38+
39+
self.client.get(action_url)
40+
41+
self.assertTrue(mock_view.called)
42+
self.assertEqual(mock_view.call_args[1]['pk'], 'pk/slash')
43+
2244

2345
class ChangeTests(LoggedInTestCase):
2446
def test_buttons_load(self):

Diff for: django_object_actions/utils.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,9 @@ def _get_action_urls(self):
115115
for action in chain(self.change_actions, self.changelist_actions):
116116
actions[action] = getattr(self, action)
117117
return [
118-
# change, supports pks that are numbers or uuids
119-
url(r'^(?P<pk>[0-9a-f\-]+)/actions/(?P<tool>\w+)/$',
118+
# change, supports the same pks the admin does
119+
# https://github.com/django/django/blob/stable/1.10.x/django/contrib/admin/options.py#L555
120+
url(r'^(?P<pk>.+)/actions/(?P<tool>\w+)/$',
120121
self.admin_site.admin_view( # checks permissions
121122
ChangeActionView.as_view(
122123
model=self.model,

0 commit comments

Comments
 (0)