Skip to content

Commit fe8ce91

Browse files
committed
Fix how extra context set by admins was not passed (#64)
* refactor admin to organize things better * add failing test case for extra context s * fixing context data
1 parent bc4b38b commit fe8ce91

File tree

5 files changed

+84
-22
lines changed

5 files changed

+84
-22
lines changed

django_object_actions/tests/test_admin.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from example_project.polls.factories import CommentFactory, PollFactory
1010

1111

12-
class CommentTest(LoggedInTestCase):
12+
class CommentTests(LoggedInTestCase):
1313
def test_action_on_a_model_with_uuid_pk_works(self):
1414
comment = CommentFactory()
1515
comment_url = reverse('admin:polls_comment_change', args=(comment.pk,))
@@ -20,13 +20,20 @@ def test_action_on_a_model_with_uuid_pk_works(self):
2020
self.assertRedirects(response, comment_url)
2121

2222

23-
class ChangeTest(LoggedInTestCase):
23+
class ChangeTests(LoggedInTestCase):
2424
def test_buttons_load(self):
2525
url = '/admin/polls/choice/'
2626
response = self.client.get(url)
2727
self.assertIn('objectactions', response.context_data)
2828
self.assertIn('Delete_all', response.rendered_content)
2929

30+
def test_changelist_template_context(self):
31+
url = reverse('admin:polls_poll_changelist')
32+
response = self.client.get(url)
33+
self.assertIn('objectactions', response.context_data)
34+
self.assertIn('tools_view_name', response.context_data)
35+
self.assertIn('foo', response.context_data)
36+
3037
def test_changelist_action_view(self):
3138
url = '/admin/polls/choice/actions/delete_all/'
3239
response = self.client.get(url)
@@ -55,10 +62,22 @@ def test_get_changelist_can_remove_action(self):
5562
self.assertNotIn(action_url, response.rendered_content)
5663

5764

58-
class MultipleAdmins(LoggedInTestCase):
65+
class ChangeListTests(LoggedInTestCase):
66+
def test_changelist_template_context(self):
67+
poll = PollFactory()
68+
url = reverse('admin:polls_poll_change', args=(poll.pk,))
69+
70+
response = self.client.get(url)
71+
self.assertIn('objectactions', response.context_data)
72+
self.assertIn('tools_view_name', response.context_data)
73+
self.assertIn('foo', response.context_data)
74+
75+
76+
class MultipleAdminsTests(LoggedInTestCase):
5977
def test_redirect_back_from_secondary_admin(self):
60-
poll = PollFactory.create()
61-
admin_change_url = reverse('admin:polls_poll_change', args=(poll.pk,), current_app='support')
78+
poll = PollFactory()
79+
admin_change_url = reverse('admin:polls_poll_change', args=(poll.pk,),
80+
current_app='support')
6281
action_url = '/support/polls/poll/1/actions/question_mark/'
6382
self.assertTrue(admin_change_url.startswith('/support/'))
6483

django_object_actions/utils.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,24 +45,26 @@ def get_urls(self):
4545
return self._get_action_urls() + urls
4646

4747
def change_view(self, request, object_id, form_url='', extra_context=None):
48-
extra_context = {
48+
extra_context = extra_context or {}
49+
extra_context.update({
4950
'objectactions': [
5051
self._get_tool_dict(action) for action in
5152
self.get_change_actions(request, object_id, form_url)
5253
],
5354
'tools_view_name': self.tools_view_name,
54-
}
55+
})
5556
return super(BaseDjangoObjectActions, self).change_view(
5657
request, object_id, form_url, extra_context)
5758

5859
def changelist_view(self, request, extra_context=None):
59-
extra_context = {
60+
extra_context = extra_context or {}
61+
extra_context.update({
6062
'objectactions': [
6163
self._get_tool_dict(action) for action in
6264
self.get_changelist_actions(request)
6365
],
6466
'tools_view_name': self.tools_view_name,
65-
}
67+
})
6668
return super(BaseDjangoObjectActions, self).changelist_view(
6769
request, extra_context)
6870

example_project/polls/admin.py

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
class ChoiceAdmin(DjangoObjectActions, admin.ModelAdmin):
1616
list_display = ('poll', 'choice_text', 'votes')
1717

18+
# Actions
19+
#########
20+
1821
@takes_instance_or_queryset
1922
def increment_vote(self, request, queryset):
2023
queryset.update(votes=F('votes') + 1)
@@ -26,6 +29,11 @@ def increment_vote(self, request, queryset):
2629
'class': 'addlink',
2730
}
2831

32+
actions = ['increment_vote']
33+
34+
# Object actions
35+
################
36+
2937
def decrement_vote(self, request, obj):
3038
obj.votes -= 1
3139
obj.save()
@@ -51,8 +59,6 @@ def raise_key_error(self, request, obj):
5159
'raise_key_error',
5260
)
5361
changelist_actions = ('delete_all',)
54-
actions = ['increment_vote']
55-
5662
admin.site.register(Choice, ChoiceAdmin)
5763

5864

@@ -62,16 +68,34 @@ class ChoiceInline(admin.StackedInline):
6268

6369

6470
class PollAdmin(DjangoObjectActions, admin.ModelAdmin):
71+
# List
72+
######
73+
74+
list_display = ('question', 'pub_date', 'was_published_recently')
75+
list_filter = ['pub_date']
76+
search_fields = ['question']
77+
date_hierarchy = 'pub_date'
78+
79+
def changelist_view(self, request, extra_context=None):
80+
extra_context = {'foo': 'changelist_view'}
81+
return super(PollAdmin, self).changelist_view(request, extra_context)
82+
83+
# Detail
84+
########
85+
6586
fieldsets = [
6687
(None, {'fields': ['question']}),
6788
('Date information',
6889
{'fields': ['pub_date'], 'classes': ['collapse']}),
6990
]
7091
inlines = [ChoiceInline]
71-
list_display = ('question', 'pub_date', 'was_published_recently')
72-
list_filter = ['pub_date']
73-
search_fields = ['question']
74-
date_hierarchy = 'pub_date'
92+
93+
def change_view(self, request, object_id, form_url='', extra_context=None):
94+
extra = {'foo': 'change_view'}
95+
return super(PollAdmin, self).change_view(request, object_id, form_url, extra)
96+
97+
# Object actions
98+
################
7599

76100
def delete_all_choices(self, request, obj):
77101
from django.shortcuts import render_to_response
@@ -105,11 +129,14 @@ def get_change_actions(self, request, object_id, form_url):
105129
actions.remove('question_mark')
106130

107131
return actions
108-
109132
admin.site.register(Poll, PollAdmin)
110133

111134

112135
class CommentAdmin(DjangoObjectActions, admin.ModelAdmin):
136+
137+
# Object actions
138+
################
139+
113140
def hodor(self, request, obj):
114141
if not obj.comment:
115142
# bail because we need a comment

example_project/polls/factories.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,23 @@ class Meta:
2020
password = factory.PostGenerationMethodCall('set_password', 'password')
2121

2222

23-
class CommentFactory(factory.DjangoModelFactory):
24-
class Meta:
25-
model = models.Comment
26-
27-
2823
class PollFactory(factory.DjangoModelFactory):
2924
class Meta:
3025
model = models.Poll
3126

3227
question = factory.LazyAttribute(lambda __: fake.sentence())
3328
pub_date = factory.LazyAttribute(lambda __: timezone.now())
29+
30+
31+
class ChoiceFactory(factory.DjangoModelFactory):
32+
class Meta:
33+
model = models.Choice
34+
35+
poll = factory.SubFactory(PollFactory)
36+
choice_text = factory.LazyAttribute(lambda __: fake.word())
37+
votes = factory.LazyAttribute(lambda __: fake.pyint())
38+
39+
40+
class CommentFactory(factory.DjangoModelFactory):
41+
class Meta:
42+
model = models.Comment

example_project/settings.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ def project_dir(*paths):
1010

1111

1212
DEBUG = True
13-
TEMPLATE_DEBUG = DEBUG
13+
TEMPLATES = [
14+
{
15+
'BACKEND': 'django.template.backends.django.DjangoTemplates',
16+
'APP_DIRS': True,
17+
},
18+
]
1419

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

0 commit comments

Comments
 (0)