Skip to content

Commit

Permalink
Merge pull request #28 from texastribune/random-tweaks
Browse files Browse the repository at this point in the history
Random tweaks
  • Loading branch information
crccheck committed Sep 4, 2014
2 parents 220506a + 4b514e1 commit c6a3419
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 12 deletions.
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ media

.env
.tox/
*.sqlite
.codeintel
*.sublime-*
*.db


# build
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ coverage:
resetdb:
python $(MANAGE) reset_db --router=default --noinput
python $(MANAGE) syncdb --noinput
python $(MANAGE) migrate --noinput
python $(MANAGE) loaddata sample_data

# just a demo of how to get up and running quickly
Expand Down
10 changes: 10 additions & 0 deletions django_object_actions/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,19 @@ def test_can_return_template(self):
response = self.client.get(url)
self.assertTemplateUsed(response, "clear_choices.html")

def test_message_user_sends_message(self):
url = '/admin/polls/poll/1/tools/delete_all_choices/'
self.assertNotIn('messages', self.client.cookies)
self.client.get(url)
self.assertIn('messages', self.client.cookies)

def test_intermediate_page_with_post_works(self):
self.assertTrue(Choice.objects.filter(poll=1).count())
url = '/admin/polls/poll/1/tools/delete_all_choices/'
response = self.client.post(url)
self.assertEqual(response.status_code, 302)
self.assertEqual(Choice.objects.filter(poll=1).count(), 0)

def test_undefined_tool_404s(self):
response = self.client.get('/admin/polls/choice/1/tools/weeeewoooooo/')
self.assertEqual(response.status_code, 404)
35 changes: 28 additions & 7 deletions django_object_actions/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ def to_dict(tool_name):
custom_attrs=custom_attrs,
)

context['objectactions'] = [
to_dict(x) for x in
context['objectactions'] = map(
to_dict,
self.get_object_actions(request, context, **kwargs)
]
)
return super(BaseDjangoObjectActions, self).render_change_form(
request, context, **kwargs)

Expand All @@ -59,9 +59,18 @@ def to_dict(tool_name):
##################

def get_object_actions(self, request, context, **kwargs):
"""Override this to customize what actions get sent."""
return self.objectactions

def get_djoa_button_attrs(self, tool):
"""
Get the HTML attributes associated with a tool.
There are some standard attributes (class and title) that the template
will always want. Any number of additional attributes can be specified
and passed on. This is kinda awkward and due for a refactor for
readability.
"""
attrs = getattr(tool, 'attrs', {})
# href is not allowed to be set. should an exception be raised instead?
if 'href' in attrs:
Expand Down Expand Up @@ -101,23 +110,35 @@ def get(self, request, **kwargs):
try:
ret = self.tools[kwargs['tool']](request, obj)
except KeyError:
raise Http404
raise Http404(u'Tool does not exist')
if isinstance(ret, HttpResponse):
return ret
back = request.path.rsplit('/', 3)[0] + '/'
return HttpResponseRedirect(back)

# Allow POST
# HACK to allow POST requests too easily
post = get

def message_user(self, request, message):
"""
Mimic Django admin actions's `message_user`.
Like the second example:
https://docs.djangoproject.com/en/1.7/ref/contrib/admin/actions/#custom-admin-action
"""
# copied from django.contrib.admin.options
# included to mimic admin actions
messages.info(request, message)


class QuerySetIsh(QuerySet):
"""Takes an instance and mimics it coming from a QuerySet."""
"""
Takes an instance and mimics it coming from a QuerySet.
This is a hack to support the `takes_instance_or_queryset` decorator so
that you can re-use functions written for standard Django admin actions and
use them for Object Tools too.
"""
def __init__(self, instance=None, *args, **kwargs):
try:
model = instance._meta.model
Expand All @@ -141,7 +162,7 @@ def get(self, *args, **kwargs):


def takes_instance_or_queryset(func):
"""Decorator that makes standard actions compatible."""
"""Decorator that makes standard Django admin actions compatible."""
@wraps(func)
def decorated_function(self, request, queryset):
# func follows the prototype documented at:
Expand Down
1 change: 1 addition & 0 deletions example_project/polls/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def delete_all_choices(self, request, obj):
obj.choice_set.all().delete()
return

self.message_user(request, 'All choices deleted')
return render_to_response('clear_choices.html',
dict(object=obj), context_instance=RequestContext(request))
delete_all_choices.label = "Delete All Choices"
Expand Down
1 change: 1 addition & 0 deletions example_project/polls/templates/404.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{# DJANGO 1.4 compatibility #}
2 changes: 1 addition & 1 deletion example_project/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def project_dir(*paths):
TEMPLATE_DEBUG = DEBUG

DATABASES = {'default': dj_database_url.config(default='sqlite:///' +
project_dir('example_project.sqlite'))}
project_dir('example_project.db'))}

# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Django>=1.4.2
Django==1.7

dj-database-url==0.3.0
# for heroku support
Expand Down

0 comments on commit c6a3419

Please sign in to comment.