Skip to content

Commit

Permalink
add: tools can now return httpresponse objects
Browse files Browse the repository at this point in the history
  • Loading branch information
crccheck committed Oct 20, 2012
1 parent b589718 commit e20748d
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
6 changes: 4 additions & 2 deletions django_object_actions/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.conf.urls.defaults import patterns
from django.contrib import messages
from django.http import Http404, HttpResponseRedirect
from django.http import Http404, HttpResponse, HttpResponseRedirect
from django.views.generic import View
from django.views.generic.detail import SingleObjectMixin

Expand Down Expand Up @@ -70,9 +70,11 @@ def get(self, request, **kwargs):
# is instantiated with `model` and the urlpattern has `pk`.
obj = self.get_object()
try:
self.tools[kwargs['tool']](request, obj)
ret = self.tools[kwargs['tool']](request, obj)
except KeyError:
raise Http404
if isinstance(ret, HttpResponse):
return ret
back = request.path.rsplit('/', 3)[0] + '/'
return HttpResponseRedirect(back)

Expand Down
9 changes: 8 additions & 1 deletion example_project/polls/admin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from django.contrib import admin
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect

from django_object_actions import DjangoObjectActions

Expand All @@ -23,7 +25,12 @@ def reset_vote(self, request, obj):
obj.save()
increment_vote.short_description = "0"

objectactions = ('increment_vote', 'decrement_vote', 'reset_vote')
def edit_poll(self, request, obj):
url = reverse('admin:polls_poll_change', args=(obj.poll.pk,))
return HttpResponseRedirect(url)

objectactions = ('increment_vote', 'decrement_vote', 'reset_vote',
'edit_poll')

admin.site.register(Choice, ChoiceAdmin)

Expand Down
28 changes: 27 additions & 1 deletion example_project/polls/fixtures/initial_data.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,30 @@
[
{
"pk": 1,
"model": "polls.poll",
"fields": {
"pub_date": "2012-10-20T18:20:35Z",
"question": "Do you like me?"
}
},
{
"pk": 1,
"model": "polls.choice",
"fields": {
"choice_text": "Yes",
"poll": 1,
"votes": 0
}
},
{
"pk": 2,
"model": "polls.choice",
"fields": {
"choice_text": "No",
"poll": 1,
"votes": 100
}
},
{
"pk": 1,
"model": "auth.user",
Expand All @@ -9,7 +35,7 @@
"is_active": true,
"is_superuser": true,
"is_staff": true,
"last_login": "2012-10-20T05:40:42.400Z",
"last_login": "2012-10-20T07:24:32.769Z",
"groups": [],
"user_permissions": [],
"password": "pbkdf2_sha256$10000$nkAXXVn8arMe$KbNP0czxCzGZy43JvavqrxdCIdrvBgumpWX0K16rwbI=",
Expand Down
8 changes: 8 additions & 0 deletions example_project/test_app/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,11 @@ def test_configured_mixin_works(self):
response = self.client.get('/admin/polls/choice/add/')
self.assertEqual(response.status_code, 200)
self.assertIn('objectactions', response.context_data)

def test_tool_can_return_httpresponse(self):
# we know this url works because of fixtures
url = 'http://localhost:8000/admin/polls/choice/2/tools/edit_poll/'
response = self.client.get(url)
# we expect a redirect
self.assertEqual(response.status_code, 302)
self.assertTrue(response['location'].endswith('/admin/polls/poll/1/'))

0 comments on commit e20748d

Please sign in to comment.