Skip to content

Commit

Permalink
Add support for all primary key formats (#75)
Browse files Browse the repository at this point in the history
* support pks that are strings

* change pattern to be the same as what the Django admin uses
  • Loading branch information
crccheck authored Dec 4, 2016
1 parent 38905d0 commit 61fb1e5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
22 changes: 22 additions & 0 deletions django_object_actions/tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from __future__ import unicode_literals

from django.core.urlresolvers import reverse
from django.http import HttpResponse
from mock import patch

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

@patch('django_object_actions.utils.ChangeActionView.get')
def test_action_on_a_model_with_arbitrary_pk_works(self, mock_view):
mock_view.return_value = HttpResponse()
action_url = '/admin/polls/comment/{0}/actions/hodor/'.format(' i am a pk ')

self.client.get(action_url)

self.assertTrue(mock_view.called)
self.assertEqual(mock_view.call_args[1]['pk'], ' i am a pk ')

@patch('django_object_actions.utils.ChangeActionView.get')
def test_action_on_a_model_with_slash_in_pk_works(self, mock_view):
mock_view.return_value = HttpResponse()
action_url = '/admin/polls/comment/{0}/actions/hodor/'.format('pk/slash')

self.client.get(action_url)

self.assertTrue(mock_view.called)
self.assertEqual(mock_view.call_args[1]['pk'], 'pk/slash')


class ChangeTests(LoggedInTestCase):
def test_buttons_load(self):
Expand Down
5 changes: 3 additions & 2 deletions django_object_actions/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,9 @@ def _get_action_urls(self):
for action in chain(self.change_actions, self.changelist_actions):
actions[action] = getattr(self, action)
return [
# change, supports pks that are numbers or uuids
url(r'^(?P<pk>[0-9a-f\-]+)/actions/(?P<tool>\w+)/$',
# change, supports the same pks the admin does
# https://github.com/django/django/blob/stable/1.10.x/django/contrib/admin/options.py#L555
url(r'^(?P<pk>.+)/actions/(?P<tool>\w+)/$',
self.admin_site.admin_view( # checks permissions
ChangeActionView.as_view(
model=self.model,
Expand Down

0 comments on commit 61fb1e5

Please sign in to comment.