Skip to content

Commit e300918

Browse files
committed
rename get_object_actions since it broke anyways
1 parent c0a63b8 commit e300918

File tree

5 files changed

+41
-26
lines changed

5 files changed

+41
-26
lines changed

CHANGELOG.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
1-
##0.8.0 (unreleased)
1+
0.8.0 (unreleased)
2+
------------------
23

3-
Breaking changes:
4+
### Breaking changes
45

5-
.get_objectactions
6+
* Deleted `get_objectactions(request, context, **kwargs)`. If you used this
7+
before, use `get_change_actions(request, object_id, form_url)` instead. To
8+
get at the original object, instead of `context['original']`, you can use
9+
`self.model.get(pk=object_id)`, `self.get_object(request, object_id)`, etc.
10+
This isn't as convenient as it used to be, but now it uses the officially
11+
documented way to add extra context to admin views.
12+
https://docs.djangoproject.com/en/dev/ref/contrib/admin/#other-methods
13+
14+
* Renamed `objectactions`. In your admin, instead of defining your actions in
15+
the `objectactions` attribute, use `change_actions`.

README.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,12 @@ Programmatically Disabling Object Admin Actions
120120
``````````````````````````````````````````````
121121

122122
You can programmatically disable registered actions by defining your own custom
123-
``get_object_actions()`` method. In this example, certain actions only apply to
123+
``get_change_actions()`` method. In this example, certain actions only apply to
124124
certain object states (i.e. You should not be able to close an company account
125125
if the account is already closed)::
126126

127-
def get_object_actions(self, request, object_id, **kwargs):
128-
actions = super().get_object_actions(request, object_id, **kwargs)
127+
def get_change_actions(self, request, object_id, **kwargs):
128+
actions = super().get_change_actions(request, object_id, **kwargs)
129129

130130
obj = self.model.get(pk=object_id)
131131

django_object_actions/tests/test_utils.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,19 @@ def test_get_tool_urls_trivial_case(self, mock_site):
2424
self.assertEqual(len(urls), 2)
2525
self.assertEqual(urls[0].name, 'app_model_tools')
2626

27-
def test_get_object_actions_gets_attribute(self):
27+
def test_get_change_actions_gets_attribute(self):
2828
# Set up
29-
mock_objectactions = mock.Mock()
30-
self.instance.objectactions = mock_objectactions
29+
self.instance.change_actions = mock.Mock()
3130

3231
# Test
33-
returned_value = self.instance.get_object_actions(
32+
returned_value = self.instance.get_change_actions(
3433
request=mock.Mock(),
3534
object_id=mock.Mock(),
3635
form_url=mock.Mock(),
3736
)
3837

3938
# Assert
40-
self.assertEqual(id(mock_objectactions), id(returned_value))
39+
self.assertEqual(id(self.instance.change_actions), id(returned_value))
4140

4241
def test__get_tool_button_attrs_returns_defaults(self):
4342
# TODO: use `mock`

django_object_actions/utils.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,17 @@ class BaseDjangoObjectActions(object):
2222
----------
2323
model : django.db.models.Model
2424
The Django Model these tools work on. This is populated by Django.
25-
objectactions : list of str
26-
Write the names of the callable attributes (methods) of the model admin
27-
that can be used as tools in the change view.
25+
change_actions : list of str
26+
Write the names of the methods of the model admin that can be used as
27+
tools in the change view.
2828
changelist_actions : list of str
29-
Write the names of the callable attributes (methods) of the model admin
30-
that can be used as tools in the changelist view.
29+
Write the names of the methods of the model admin that can be used as
30+
tools in the changelist view.
3131
tools_view_name : str
3232
The name of the Django Object Actions admin view, including the 'admin'
3333
namespace. Populated by `get_tool_urls`.
3434
"""
35-
objectactions = []
35+
change_actions = []
3636
changelist_actions = []
3737
tools_view_name = None
3838

@@ -52,7 +52,7 @@ def get_tool_urls(self):
5252

5353
self.tools_view_name = 'admin:' + model_tools_url_name
5454

55-
for tool in chain(self.objectactions, self.changelist_actions):
55+
for tool in chain(self.change_actions, self.changelist_actions):
5656
tools[tool] = getattr(self, tool)
5757
return [
5858
# change, supports pks that are numbers or uuids
@@ -90,7 +90,7 @@ def change_view(self, request, object_id, form_url='', extra_context=None):
9090
extra_context = {
9191
'objectactions': [
9292
self._get_tool_dict(action) for action in
93-
self.get_object_actions(request, object_id, form_url)
93+
self.get_change_actions(request, object_id, form_url)
9494
],
9595
'tools_view_name': self.tools_view_name,
9696
}
@@ -122,7 +122,7 @@ def _get_tool_dict(self, tool_name):
122122
custom_attrs=custom_attrs,
123123
)
124124

125-
def get_object_actions(self, request, object_id, form_url):
125+
def get_change_actions(self, request, object_id, form_url):
126126
"""
127127
Override this to customize what actions get to the change view.
128128
@@ -131,14 +131,14 @@ def get_object_actions(self, request, object_id, form_url):
131131
For example, to restrict actions to superusers, you could do:
132132
133133
class ChoiceAdmin(DjangoObjectActions, admin.ModelAdmin):
134-
def get_object_actions(self, request, **kwargs):
134+
def get_change_actions(self, request, **kwargs):
135135
if request.user.is_superuser:
136-
return super(ChoiceAdmin, self).get_object_actions(
136+
return super(ChoiceAdmin, self).get_change_actions(
137137
request, **kwargs
138138
)
139139
return []
140140
"""
141-
return self.objectactions
141+
return self.change_actions
142142

143143
def get_changelist_actions(self, request):
144144
"""

example_project/polls/admin.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def edit_poll(self, request, obj):
4545
def raise_key_error(self, request, obj):
4646
raise KeyError
4747

48-
objectactions = (
48+
change_actions = (
4949
'increment_vote', 'decrement_vote', 'reset_vote', 'edit_poll',
5050
'raise_key_error',
5151
)
@@ -86,7 +86,13 @@ def delete_all_choices(self, request, obj):
8686
dict(object=obj), context_instance=RequestContext(request))
8787
delete_all_choices.label = "Delete All Choices"
8888

89-
objectactions = ('delete_all_choices', )
89+
change_actions = ('delete_all_choices', )
90+
91+
def get_change_actions(self, request, object_id, form_url):
92+
actions = super(PollAdmin, self).get_change_actions(request, object_id, form_url)
93+
import ipdb; ipdb.set_trace()
94+
return actions
95+
9096
admin.site.register(Poll, PollAdmin)
9197

9298

@@ -97,5 +103,5 @@ def hodor(self, request, obj):
97103
return
98104
obj.comment = ' '.join(['hodor' for x in obj.comment.split()])
99105
obj.save()
100-
objectactions = ('hodor', )
106+
change_actions = ('hodor', )
101107
admin.site.register(Comment, CommentAdmin)

0 commit comments

Comments
 (0)