Fixed #24285 -- Made for_user argument optional for {% get_admin_log %} tag

This commit is contained in:
Yair Chuchem 2015-02-05 20:25:59 +02:00 committed by Tim Graham
parent 35b3158d52
commit eb5ebcc2d0
2 changed files with 23 additions and 4 deletions

View File

@ -13,14 +13,13 @@ class AdminLogNode(template.Node):
def render(self, context): def render(self, context):
if self.user is None: if self.user is None:
context[self.varname] = LogEntry.objects.all().select_related('content_type', 'user')[:self.limit] entries = LogEntry.objects.all()
else: else:
user_id = self.user user_id = self.user
if not user_id.isdigit(): if not user_id.isdigit():
user_id = context[self.user].pk user_id = context[self.user].pk
context[self.varname] = LogEntry.objects.filter( entries = LogEntry.objects.filter(user__pk=user_id)
user__pk=user_id, context[self.varname] = entries.select_related('content_type', 'user')[:int(self.limit)]
).select_related('content_type', 'user')[:int(self.limit)]
return '' return ''

View File

@ -3,11 +3,13 @@ from __future__ import unicode_literals
import datetime import datetime
from django.contrib import admin from django.contrib import admin
from django.contrib.admin.models import LogEntry
from django.contrib.admin.options import IncorrectLookupParameters from django.contrib.admin.options import IncorrectLookupParameters
from django.contrib.admin.templatetags.admin_list import pagination from django.contrib.admin.templatetags.admin_list import pagination
from django.contrib.admin.tests import AdminSeleniumWebDriverTestCase from django.contrib.admin.tests import AdminSeleniumWebDriverTestCase
from django.contrib.admin.views.main import ALL_VAR, SEARCH_VAR, ChangeList from django.contrib.admin.views.main import ALL_VAR, SEARCH_VAR, ChangeList
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.template import Context, Template from django.template import Context, Template
from django.test import TestCase, override_settings from django.test import TestCase, override_settings
@ -685,6 +687,24 @@ class AdminLogNodeTestCase(TestCase):
# it doesn't render any string. # it doesn't render any string.
self.assertEqual(template.render(context), '') self.assertEqual(template.render(context), '')
def test_get_admin_log_templatetag_no_user(self):
"""
The {% get_admin_log %} tag should work without specifying a user.
"""
user = User(username='jondoe', password='secret', email='super@example.com')
user.save()
ct = ContentType.objects.get_for_model(User)
LogEntry.objects.log_action(user.pk, ct.pk, user.pk, repr(user), 1)
t = Template(
'{% load log %}'
'{% get_admin_log 100 as admin_log %}'
'{% for entry in admin_log %}'
'{{ entry|safe }}'
'{% endfor %}'
)
self.assertEqual(t.render(Context({})), 'Added "<User: jondoe>".')
@override_settings(PASSWORD_HASHERS=['django.contrib.auth.hashers.SHA1PasswordHasher'], @override_settings(PASSWORD_HASHERS=['django.contrib.auth.hashers.SHA1PasswordHasher'],
ROOT_URLCONF="admin_changelist.urls") ROOT_URLCONF="admin_changelist.urls")