@@ -47,10 +47,10 @@ def to_dict(tool_name):
47
47
custom_attrs = custom_attrs ,
48
48
)
49
49
50
- context ['objectactions' ] = [
51
- to_dict ( x ) for x in
50
+ context ['objectactions' ] = map (
51
+ to_dict ,
52
52
self .get_object_actions (request , context , ** kwargs )
53
- ]
53
+ )
54
54
return super (BaseDjangoObjectActions , self ).render_change_form (
55
55
request , context , ** kwargs )
56
56
@@ -59,9 +59,18 @@ def to_dict(tool_name):
59
59
##################
60
60
61
61
def get_object_actions (self , request , context , ** kwargs ):
62
+ """Override this to customize what actions get sent."""
62
63
return self .objectactions
63
64
64
65
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
+ """
65
74
attrs = getattr (tool , 'attrs' , {})
66
75
# href is not allowed to be set. should an exception be raised instead?
67
76
if 'href' in attrs :
@@ -101,23 +110,35 @@ def get(self, request, **kwargs):
101
110
try :
102
111
ret = self .tools [kwargs ['tool' ]](request , obj )
103
112
except KeyError :
104
- raise Http404
113
+ raise Http404 ( u'Tool does not exist' )
105
114
if isinstance (ret , HttpResponse ):
106
115
return ret
107
116
back = request .path .rsplit ('/' , 3 )[0 ] + '/'
108
117
return HttpResponseRedirect (back )
109
118
110
- # Allow POST
119
+ # HACK to allow POST requests too easily
111
120
post = get
112
121
113
122
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
+ """
114
129
# copied from django.contrib.admin.options
115
130
# included to mimic admin actions
116
131
messages .info (request , message )
117
132
118
133
119
134
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
+ """
121
142
def __init__ (self , instance = None , * args , ** kwargs ):
122
143
try :
123
144
model = instance ._meta .model
@@ -141,7 +162,7 @@ def get(self, *args, **kwargs):
141
162
142
163
143
164
def takes_instance_or_queryset (func ):
144
- """Decorator that makes standard actions compatible."""
165
+ """Decorator that makes standard Django admin actions compatible."""
145
166
@wraps (func )
146
167
def decorated_function (self , request , queryset ):
147
168
# func follows the prototype documented at:
0 commit comments