summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorSimon Willison <simon@simonwillison.net>2008-06-13 15:42:43 +0000
committerSimon Willison <simon@simonwillison.net>2008-06-13 15:42:43 +0000
commit725293d51ad6f5dbefb59fa9adfd76d09e1526c6 (patch)
treef945d43b5db9f30d5d1cf6e517cfcf4df88ce1da /tests
parentf33bdf7e9cbe48d51966abb72cbda0e8b865bdb9 (diff)
newforms-admin: Made it easier to specify a custom template to be used in the admin section. You can now specify index_template and login_template properties on an AdminSite subclass, and change_form_template, change_list_template, object_history_template and delete_confirmation_template properties on a ModelAdmin subclass.
git-svn-id: http://code.djangoproject.com/svn/django/branches/newforms-admin@7630 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests')
-rw-r--r--tests/regressiontests/admin_views/models.py9
-rw-r--r--tests/regressiontests/admin_views/tests.py57
-rw-r--r--tests/templates/custom_admin/change_form.html1
-rw-r--r--tests/templates/custom_admin/change_list.html (renamed from tests/templates/admin/admin_views/customarticle/change_list.html)0
-rw-r--r--tests/templates/custom_admin/delete_confirmation.html1
-rw-r--r--tests/templates/custom_admin/index.html6
-rw-r--r--tests/templates/custom_admin/login.html6
-rw-r--r--tests/templates/custom_admin/object_history.html1
8 files changed, 77 insertions, 4 deletions
diff --git a/tests/regressiontests/admin_views/models.py b/tests/regressiontests/admin_views/models.py
index b6d247a65c..2c0ba1d909 100644
--- a/tests/regressiontests/admin_views/models.py
+++ b/tests/regressiontests/admin_views/models.py
@@ -1,7 +1,6 @@
from django.db import models
from django.contrib import admin
-
class Article(models.Model):
"""
A simple article to test admin views. Test backwards compabilty.
@@ -24,6 +23,14 @@ class CustomArticle(models.Model):
date = models.DateTimeField()
class CustomArticleAdmin(admin.ModelAdmin):
+ """
+ Tests various hooks for using custom templates and contexts.
+ """
+ change_list_template = 'custom_admin/change_list.html'
+ change_form_template = 'custom_admin/change_form.html'
+ object_history_template = 'custom_admin/object_history.html'
+ delete_confirmation_template = 'custom_admin/delete_confirmation.html'
+
def changelist_view(self, request):
"Test that extra_context works"
return super(CustomArticleAdmin, self).changelist_view(request, extra_context={
diff --git a/tests/regressiontests/admin_views/tests.py b/tests/regressiontests/admin_views/tests.py
index 30161299cc..1e3b334221 100644
--- a/tests/regressiontests/admin_views/tests.py
+++ b/tests/regressiontests/admin_views/tests.py
@@ -5,7 +5,7 @@ from django.contrib.contenttypes.models import ContentType
from django.contrib.admin.sites import LOGIN_FORM_KEY, _encode_post_data
# local test models
-from models import Article
+from models import Article, CustomArticle
def get_perm(Model, perm):
"""Return the permission object, for the Model"""
@@ -183,13 +183,64 @@ class AdminViewPermissionsTest(TestCase):
self.failUnlessEqual(Article.objects.get(pk=1).content, '<p>edited article</p>')
self.client.get('/test_admin/admin/logout/')
- def testCustomChangelistView(self):
+ def testCustomModelAdminTemplates(self):
self.client.get('/test_admin/admin/')
self.client.post('/test_admin/admin/', self.super_login)
+
+ # Test custom change list template with custom extra context
request = self.client.get('/test_admin/admin/admin_views/customarticle/')
self.failUnlessEqual(request.status_code, 200)
self.assert_("var hello = 'Hello!';" in request.content)
-
+ self.assertTemplateUsed(request, 'custom_admin/change_list.html')
+
+ # Test custom change form template
+ request = self.client.get('/test_admin/admin/admin_views/customarticle/add/')
+ self.assertTemplateUsed(request, 'custom_admin/change_form.html')
+
+ # Add an article so we can test delete and history views
+ post = self.client.post('/test_admin/admin/admin_views/customarticle/add/', {
+ 'content': '<p>great article</p>',
+ 'date_0': '2008-03-18',
+ 'date_1': '10:54:39'
+ })
+ self.assertRedirects(post, '/test_admin/admin/admin_views/customarticle/')
+ self.failUnlessEqual(CustomArticle.objects.all().count(), 1)
+
+ # Test custom delete and object history templates
+ request = self.client.get('/test_admin/admin/admin_views/customarticle/1/delete/')
+ self.assertTemplateUsed(request, 'custom_admin/delete_confirmation.html')
+ request = self.client.get('/test_admin/admin/admin_views/customarticle/1/history/')
+ self.assertTemplateUsed(request, 'custom_admin/object_history.html')
+
+ self.client.get('/test_admin/admin/logout/')
+
+ def testCustomAdminSiteTemplates(self):
+ from django.contrib import admin
+ self.assertEqual(admin.site.index_template, None)
+ self.assertEqual(admin.site.login_template, None)
+
+ self.client.get('/test_admin/admin/logout/')
+ request = self.client.get('/test_admin/admin/')
+ self.assertTemplateUsed(request, 'admin/login.html')
+ self.client.post('/test_admin/admin/', self.changeuser_login)
+ request = self.client.get('/test_admin/admin/')
+ self.assertTemplateUsed(request, 'admin/index.html')
+
+ self.client.get('/test_admin/admin/logout/')
+ admin.site.login_template = 'custom_admin/login.html'
+ admin.site.index_template = 'custom_admin/index.html'
+ request = self.client.get('/test_admin/admin/')
+ self.assertTemplateUsed(request, 'custom_admin/login.html')
+ self.assert_('Hello from a custom login template' in request.content)
+ self.client.post('/test_admin/admin/', self.changeuser_login)
+ 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)
+
+ self.client.get('/test_admin/admin/logout/')
+ admin.site.login_template = None
+ admin.site.index_template = None
+
def testDeleteView(self):
"""Delete view should restrict access and actually delete items."""
diff --git a/tests/templates/custom_admin/change_form.html b/tests/templates/custom_admin/change_form.html
new file mode 100644
index 0000000000..f42ba4b649
--- /dev/null
+++ b/tests/templates/custom_admin/change_form.html
@@ -0,0 +1 @@
+{% extends "admin/change_form.html" %}
diff --git a/tests/templates/admin/admin_views/customarticle/change_list.html b/tests/templates/custom_admin/change_list.html
index eebc9c7e30..eebc9c7e30 100644
--- a/tests/templates/admin/admin_views/customarticle/change_list.html
+++ b/tests/templates/custom_admin/change_list.html
diff --git a/tests/templates/custom_admin/delete_confirmation.html b/tests/templates/custom_admin/delete_confirmation.html
new file mode 100644
index 0000000000..9353c5bfc8
--- /dev/null
+++ b/tests/templates/custom_admin/delete_confirmation.html
@@ -0,0 +1 @@
+{% extends "admin/delete_confirmation.html" %}
diff --git a/tests/templates/custom_admin/index.html b/tests/templates/custom_admin/index.html
new file mode 100644
index 0000000000..d52033ee2d
--- /dev/null
+++ b/tests/templates/custom_admin/index.html
@@ -0,0 +1,6 @@
+{% extends "admin/index.html" %}
+
+{% block content %}
+Hello from a custom index template
+{{ block.super }}
+{% endblock %}
diff --git a/tests/templates/custom_admin/login.html b/tests/templates/custom_admin/login.html
new file mode 100644
index 0000000000..e10a26952f
--- /dev/null
+++ b/tests/templates/custom_admin/login.html
@@ -0,0 +1,6 @@
+{% extends "admin/login.html" %}
+
+{% block content %}
+Hello from a custom login template
+{{ block.super }}
+{% endblock %}
diff --git a/tests/templates/custom_admin/object_history.html b/tests/templates/custom_admin/object_history.html
new file mode 100644
index 0000000000..aee3b5bcba
--- /dev/null
+++ b/tests/templates/custom_admin/object_history.html
@@ -0,0 +1 @@
+{% extends "admin/object_history.html" %}