Skip to content

Commit c6a3419

Browse files
committed
Merge pull request #28 from texastribune/random-tweaks
Random tweaks
2 parents 220506a + 4b514e1 commit c6a3419

File tree

8 files changed

+44
-12
lines changed

8 files changed

+44
-12
lines changed

.gitignore

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ media
1010

1111
.env
1212
.tox/
13-
*.sqlite
14-
.codeintel
15-
*.sublime-*
13+
*.db
1614

1715

1816
# build

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ coverage:
3232
resetdb:
3333
python $(MANAGE) reset_db --router=default --noinput
3434
python $(MANAGE) syncdb --noinput
35+
python $(MANAGE) migrate --noinput
3536
python $(MANAGE) loaddata sample_data
3637

3738
# just a demo of how to get up and running quickly

django_object_actions/tests/tests.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,19 @@ def test_can_return_template(self):
5050
response = self.client.get(url)
5151
self.assertTemplateUsed(response, "clear_choices.html")
5252

53+
def test_message_user_sends_message(self):
54+
url = '/admin/polls/poll/1/tools/delete_all_choices/'
55+
self.assertNotIn('messages', self.client.cookies)
56+
self.client.get(url)
57+
self.assertIn('messages', self.client.cookies)
58+
5359
def test_intermediate_page_with_post_works(self):
5460
self.assertTrue(Choice.objects.filter(poll=1).count())
5561
url = '/admin/polls/poll/1/tools/delete_all_choices/'
5662
response = self.client.post(url)
5763
self.assertEqual(response.status_code, 302)
5864
self.assertEqual(Choice.objects.filter(poll=1).count(), 0)
65+
66+
def test_undefined_tool_404s(self):
67+
response = self.client.get('/admin/polls/choice/1/tools/weeeewoooooo/')
68+
self.assertEqual(response.status_code, 404)

django_object_actions/utils.py

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ def to_dict(tool_name):
4747
custom_attrs=custom_attrs,
4848
)
4949

50-
context['objectactions'] = [
51-
to_dict(x) for x in
50+
context['objectactions'] = map(
51+
to_dict,
5252
self.get_object_actions(request, context, **kwargs)
53-
]
53+
)
5454
return super(BaseDjangoObjectActions, self).render_change_form(
5555
request, context, **kwargs)
5656

@@ -59,9 +59,18 @@ def to_dict(tool_name):
5959
##################
6060

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

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

110-
# Allow POST
119+
# HACK to allow POST requests too easily
111120
post = get
112121

113122
def message_user(self, request, message):
123+
"""
124+
Mimic Django admin actions's `message_user`.
125+
126+
Like the second example:
127+
https://docs.djangoproject.com/en/1.7/ref/contrib/admin/actions/#custom-admin-action
128+
"""
114129
# copied from django.contrib.admin.options
115130
# included to mimic admin actions
116131
messages.info(request, message)
117132

118133

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

142163

143164
def takes_instance_or_queryset(func):
144-
"""Decorator that makes standard actions compatible."""
165+
"""Decorator that makes standard Django admin actions compatible."""
145166
@wraps(func)
146167
def decorated_function(self, request, queryset):
147168
# func follows the prototype documented at:

example_project/polls/admin.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ def delete_all_choices(self, request, obj):
6868
obj.choice_set.all().delete()
6969
return
7070

71+
self.message_user(request, 'All choices deleted')
7172
return render_to_response('clear_choices.html',
7273
dict(object=obj), context_instance=RequestContext(request))
7374
delete_all_choices.label = "Delete All Choices"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{# DJANGO 1.4 compatibility #}

example_project/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def project_dir(*paths):
1313
TEMPLATE_DEBUG = DEBUG
1414

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

1818
# Local time zone for this installation. Choices can be found here:
1919
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Django>=1.4.2
1+
Django==1.7
22

33
dj-database-url==0.3.0
44
# for heroku support

0 commit comments

Comments
 (0)