From faae7c0faf859edb9b60a1a45669b9127efa3f37 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Fri, 13 Jun 2008 17:26:30 +0000 Subject: [PATCH] newforms-admin: AdminSite index and display_login_form method can now take an optional extra_context argument, allowing you to inject extra template variables in to them from an over-ridden method on a subclass git-svn-id: http://code.djangoproject.com/svn/django/branches/newforms-admin@7631 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/admin/sites.py | 25 +++++++++++++++------- tests/regressiontests/admin_views/tests.py | 11 ++++++++++ tests/templates/custom_admin/index.html | 2 +- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/django/contrib/admin/sites.py b/django/contrib/admin/sites.py index ffc7e28bdd..ce14b269fb 100644 --- a/django/contrib/admin/sites.py +++ b/django/contrib/admin/sites.py @@ -98,7 +98,7 @@ class AdminSite(object): Handles main URL routing for the admin app. `url` is the remainder of the URL -- e.g. 'comments/comment/'. - """ + """ url = url.rstrip('/') # Trim trailing slash, if it exists. # The 'logout' view doesn't require that the person is logged in. @@ -249,7 +249,7 @@ class AdminSite(object): else: return self.display_login_form(request, ERROR_MESSAGE) - def index(self, request): + def index(self, request, extra_context=None): """ Displays the main admin index page, which lists all of the installed apps that have been registered in this site. @@ -291,13 +291,17 @@ class AdminSite(object): # Sort the models alphabetically within each app. for app in app_list: app['models'].sort(lambda x, y: cmp(x['name'], y['name'])) - - return render_to_response(self.index_template or 'admin/index.html', { + + context = { 'title': _('Site administration'), 'app_list': app_list, - }, context_instance=template.RequestContext(request)) + } + context.update(extra_context or {}) + return render_to_response(self.index_template or 'admin/index.html', context, + context_instance=template.RequestContext(request) + ) - def display_login_form(self, request, error_message=''): + def display_login_form(self, request, error_message='', extra_context=None): request.session.set_test_cookie() if request.POST and request.POST.has_key('post_data'): # User has failed login BUT has previously saved post data. @@ -307,12 +311,17 @@ class AdminSite(object): post_data = _encode_post_data(request.POST) else: post_data = _encode_post_data({}) - return render_to_response(self.login_template or 'admin/login.html', { + + context = { 'title': _('Log in'), 'app_path': request.path, 'post_data': post_data, 'error_message': error_message - }, context_instance=template.RequestContext(request)) + } + context.update(extra_context or {}) + return render_to_response(self.login_template or 'admin/login.html', context, + context_instance=template.RequestContext(request) + ) # This global object represents the default admin site, for the common case. diff --git a/tests/regressiontests/admin_views/tests.py b/tests/regressiontests/admin_views/tests.py index 1e3b334221..a70016d540 100644 --- a/tests/regressiontests/admin_views/tests.py +++ b/tests/regressiontests/admin_views/tests.py @@ -236,8 +236,19 @@ class AdminViewPermissionsTest(TestCase): request = self.client.get('/test_admin/admin/') self.assertTemplateUsed(request, 'custom_admin/index.html') self.assert_('Hello from a custom index template' in request.content) + + # Finally, using monkey patching check we can inject custom_context arguments in to index + original_index = admin.site.index + def index(*args, **kwargs): + kwargs['extra_context'] = {'foo': '*bar*'} + return original_index(*args, **kwargs) + admin.site.index = index + request = self.client.get('/test_admin/admin/') + self.assertTemplateUsed(request, 'custom_admin/index.html') + self.assert_('Hello from a custom index template *bar*' in request.content) self.client.get('/test_admin/admin/logout/') + del admin.site.index # Resets to using the original admin.site.login_template = None admin.site.index_template = None diff --git a/tests/templates/custom_admin/index.html b/tests/templates/custom_admin/index.html index d52033ee2d..75b6ca3d18 100644 --- a/tests/templates/custom_admin/index.html +++ b/tests/templates/custom_admin/index.html @@ -1,6 +1,6 @@ {% extends "admin/index.html" %} {% block content %} -Hello from a custom index template +Hello from a custom index template {{ foo }} {{ block.super }} {% endblock %}