Skip to content

Commit 402fc66

Browse files
committed
Merge pull request #10 from crccheck/better-naked
Add a way to opt out of template helpers
2 parents 8e5d462 + 4a94c1e commit 402fc66

File tree

4 files changed

+76
-39
lines changed

4 files changed

+76
-39
lines changed

Diff for: README.rst

+56-31
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,28 @@ If you've ever tried making your own admin object tools and you were
77
like me, you immediately gave up. Why can't they be as easy as making
88
Django Admin Actions? Well now they can be.
99

10-
Similar Packages
11-
~~~~~~~~~~~~~~~~
12-
13-
Django Object Actions is very similar to
14-
`django-object-tools <https://github.com/praekelt/django-object-tools>`_,
15-
but does not require messing with your urls.py, does not do anything
16-
special with permissions, and uses the same patterns as making `admin
17-
actions <https://docs.djangoproject.com/en/dev/ref/contrib/admin/actions/#actions-as-modeladmin-methods>`_
18-
in Django.
19-
20-
Installation
21-
------------
10+
Quick-Start Guide
11+
-----------------
2212

2313
Install Django Object Actions::
2414

2515
pip install django-object-actions
2616

2717
Add ``django_object_actions`` to your ``INSTALLED_APPS``.
2818

29-
Alternate Installation
30-
~~~~~~~~~~~~~~~~~~~~~~
19+
In your admin.py::
20+
21+
from django_object_actions import DjangoObjectActions
22+
23+
24+
class ArticleAdmin(DjangoObjectActions, admin.ModelAdmin):
25+
def publish_this(self, request, obj):
26+
publish_obj(obj)
27+
publish_this.label = "Publish" # optional
28+
publish_this.short_description = "Submit this article to The Texas Tribune" # optional
29+
30+
objectactions = ('publish_this', )
3131

32-
You don't have to add this to ``INSTALLED_APPS``, all you need to to do is copy
33-
the template ``django_object_actions/change_form.html`` some place Django's
34-
template loader `will find it
35-
<https://docs.djangoproject.com/en/dev/ref/settings/#template-dirs>`_.
3632

3733
Usage
3834
-----
@@ -42,13 +38,14 @@ Tools are defined just like defining actions as modeladmin methods, see:
4238
actions <https://docs.djangoproject.com/en/dev/ref/contrib/admin/actions/#actions-as-modeladmin-methods>`_
4339
for examples and detailed syntax. You can return nothing or an http
4440
response. The major difference being the functions you write will take
45-
an object instance instead of a queryset::
46-
47-
def toolfunc(self, request, obj)
41+
an object instance instead of a queryset (see *Re-using Admin Actions* below).
4842

4943
Tools are exposed by putting them in an ``objectactions`` attribute in
5044
your modeladmin like::
5145

46+
from django_object_actions import DjangoObjectActions
47+
48+
5249
class MyModelAdmin(DjangoObjectActions, admin.ModelAdmin):
5350
def toolfunc(self, request, obj):
5451
pass
@@ -62,15 +59,18 @@ Normally, you would do something to the object and go back to the same
6259
place, but if you return a HttpResponse, it will follow it (hey, just
6360
like actions!).
6461

62+
If your admin modifies ``get_urls``, ``render_change_form``, or
63+
``change_form_template``, you'll need to take extra care.
64+
6565
Re-using Admin Actions
6666
``````````````````````
6767

6868
If you would like an admin action to also be an object tool, add the
6969
``takes_instance_or_queryset`` decorator like::
7070

7171

72-
from django_object_actions import DjangoObjectActions
73-
from django_object_actions.utils import takes_instance_or_queryset
72+
from django_object_actions import (DjangoObjectActions,
73+
takes_instance_or_queryset)
7474

7575

7676
class RobotAdmin(DjangoObjectActions, admin.ModelAdmin):
@@ -83,16 +83,30 @@ If you would like an admin action to also be an object tool, add the
8383
objectactions = ['tighten_lug_nuts']
8484
actions = ['tighten_lug_nuts']
8585

86+
Alternate Installation
87+
``````````````````````
88+
89+
You don't have to add this to ``INSTALLED_APPS``, all you need to to do is copy
90+
the template ``django_object_actions/change_form.html`` some place Django's
91+
template loader `will find it
92+
<https://docs.djangoproject.com/en/dev/ref/settings/#template-dirs>`_.
93+
94+
If you don't intend to use the template customizations at all, don't add
95+
``django_object_actions`` to your ``INSTALLED_APPS`` at all and use
96+
``BaseDjangoObjectActions`` instead of ``DjangoObjectActions``.
97+
8698

8799
Limitations
88-
~~~~~~~~~~~
100+
-----------
89101

90102
1. ``django-object-actions`` expects functions to be methods of the model admin.
103+
While Django gives you a lot more options for their admin actions.
91104

92105
2. If you provide your own custom ``change_form.html``, you'll also need to
93106
manually copy in the relevant bits of `our change form
94107
<https://github.com/texastribune/django-object-actions/blob/master/django_obj
95-
ect_actions/templates/django_object_actions/change_form.html>`_.
108+
ect_actions/templates/django_object_actions/change_form.html>`_. You can also
109+
use ``from django_object_actions import BaseDjangoObjectActions`` instead.
96110

97111
Development
98112
-----------
@@ -107,12 +121,23 @@ Getting started *(with virtualenvwrapper)*::
107121
pip install -r requirements-dev.txt
108122
export DJANGO_SETTINGS_MODULE=example_project.settings
109123
add2virtualenv .
110-
# start doing stuff
111-
make test
112-
make resetdb
113-
python example_project/manage.py runserver
124+
make test # run test suite
125+
tox # run full test suite, requires more setup
126+
make resetdb # reset the example db
127+
python example_project/manage.py runserver # run debug server
114128

115-
The fixtures will create a user, admin:admin, you can use to log in
116-
immediately.
129+
The fixtures will create a user, admin:admin, you can use to log in immediately.
117130

118131
Various helpers are available as make commands.
132+
133+
134+
Similar Packages
135+
----------------
136+
137+
Django Object Actions is very similar to
138+
`django-object-tools <https://github.com/praekelt/django-object-tools>`_,
139+
but does not require messing with your urls.py, does not do anything
140+
special with permissions, and uses the same patterns as making `admin
141+
actions <https://docs.djangoproject.com/en/dev/ref/contrib/admin/actions/#actions-as-modeladmin-methods>`_
142+
in Django.
143+

Diff for: django_object_actions/__init__.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,9 @@
22
__version__ = "0.1.1"
33

44

5-
from .utils import DjangoObjectActions
5+
# kind of like __all__, make these available for public
6+
from .utils import (
7+
BaseDjangoObjectActions,
8+
DjangoObjectActions,
9+
takes_instance_or_queryset,
10+
)

Diff for: django_object_actions/utils.py

+12-5
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@
88
from django.views.generic.detail import SingleObjectMixin
99

1010

11-
class DjangoObjectActions(object):
11+
class BaseDjangoObjectActions(object):
1212
"""ModelAdmin mixin to add object-tools just like adding admin actions."""
13-
# override default change_form_template
14-
change_form_template = "django_object_actions/change_form.html"
1513
# list to hold each object action tool
1614
objectactions = []
1715

@@ -26,9 +24,13 @@ def get_tool_urls(self):
2624
)
2725
return my_urls
2826

27+
###################################
28+
# EXISTING ADMIN METHODS MODIFIED #
29+
###################################
30+
2931
def get_urls(self):
3032
"""Prepends `get_urls` with our own patterns."""
31-
urls = super(DjangoObjectActions, self).get_urls()
33+
urls = super(BaseDjangoObjectActions, self).get_urls()
3234
return self.get_tool_urls() + urls
3335

3436
def render_change_form(self, request, context, **kwargs):
@@ -43,10 +45,15 @@ def to_dict(tool_name):
4345
short_description=getattr(tool, 'short_description', ''))
4446

4547
context['objectactions'] = [to_dict(x) for x in self.objectactions]
46-
return super(DjangoObjectActions, self).render_change_form(request,
48+
return super(BaseDjangoObjectActions, self).render_change_form(request,
4749
context, **kwargs)
4850

4951

52+
class DjangoObjectActions(BaseDjangoObjectActions):
53+
# override default change_form_template
54+
change_form_template = "django_object_actions/change_form.html"
55+
56+
5057
class ModelToolsView(SingleObjectMixin, View):
5158
"""A special view that run the tool's callable."""
5259
tools = {}

Diff for: example_project/polls/admin.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
from django.db.models import F
44
from django.http import HttpResponseRedirect
55

6-
from django_object_actions import DjangoObjectActions
7-
from django_object_actions.utils import takes_instance_or_queryset
6+
from django_object_actions import (DjangoObjectActions,
7+
takes_instance_or_queryset)
88

99
from .models import Choice, Poll
1010

0 commit comments

Comments
 (0)