Code

Ticket #13609: 13609.diff

File 13609.diff, 4.4 KB (added by ojii, 3 years ago)
Line 
1diff --git a/django/contrib/admin/validation.py b/django/contrib/admin/validation.py
2index 027db63..f6de85c 100644
3--- a/django/contrib/admin/validation.py
4+++ b/django/contrib/admin/validation.py
5@@ -6,6 +6,8 @@ from django.forms.models import (BaseModelForm, BaseModelFormSet, fields_for_mod
6 from django.contrib.admin.util import get_fields_from_path, NotRelationField
7 from django.contrib.admin.options import flatten_fieldsets, BaseModelAdmin
8 from django.contrib.admin.options import HORIZONTAL, VERTICAL
9+from django.template.base import TemplateDoesNotExist
10+from django.template.loader import find_template
11 
12 
13 __all__ = ['validate']
14@@ -199,6 +201,16 @@ def validate_inline(cls, parent, parent_model):
15             raise ImproperlyConfigured("%s cannot exclude the field "
16                     "'%s' - this is the foreign key to the parent model "
17                     "%s." % (cls.__name__, fk.name, parent_model.__name__))
18+   
19+    # template
20+    template = getattr(cls, 'template', None)
21+    if template is None:
22+        raise ImproperlyConfigured("%s.template cannot be None" % cls.__name__)
23+    try:
24+        find_template(template)
25+    except TemplateDoesNotExist:
26+        raise ImproperlyConfigured("%s.template cannot be loaded, template "
27+               "'%s' could not be found" % (cls.__name__, template))
28 
29 def validate_base(cls, model):
30     opts = model._meta
31diff --git a/tests/regressiontests/admin_inlines/models.py b/tests/regressiontests/admin_inlines/models.py
32index 4e5c4e3..a5da886 100644
33--- a/tests/regressiontests/admin_inlines/models.py
34+++ b/tests/regressiontests/admin_inlines/models.py
35@@ -6,6 +6,7 @@ from django.db import models
36 from django.contrib import admin
37 from django.contrib.contenttypes.models import ContentType
38 from django.contrib.contenttypes import generic
39+from django.contrib.admin.options import InlineModelAdmin
40 
41 class Parent(models.Model):
42     name = models.CharField(max_length=50)
43@@ -123,3 +124,12 @@ class InlineWeakness(admin.TabularInline):
44     extra = 1
45 
46 admin.site.register(Fashionista, inlines=[InlineWeakness])
47+
48+# Don't register this because we only need it to test validation
49+class InnerInlineNoTemplate(InlineModelAdmin):
50+    model = Inner
51+
52+# Don't register this because we only need it to test validation
53+class InnerInlineNonExistingTemplate(admin.TabularInline):
54+    model = Inner
55+    template = "does/not/exist.html"
56\ No newline at end of file
57diff --git a/tests/regressiontests/admin_inlines/tests.py b/tests/regressiontests/admin_inlines/tests.py
58index b10474d..c5ffbb7 100644
59--- a/tests/regressiontests/admin_inlines/tests.py
60+++ b/tests/regressiontests/admin_inlines/tests.py
61@@ -1,10 +1,15 @@
62 from django.contrib.admin.helpers import InlineAdminForm
63 from django.contrib.contenttypes.models import ContentType
64+from django.contrib.admin.validation import validate_inline
65+from django.core.exceptions import ImproperlyConfigured
66 from django.test import TestCase
67 
68 # local test models
69 from models import (Holder, Inner, InnerInline, Holder2, Inner2, Holder3,
70-    Inner3, Person, OutfitItem, Fashionista, Teacher, Parent, Child)
71+    Inner3, Person, OutfitItem, Fashionista, Teacher, Parent, Child,
72+    InnerInlineNonExistingTemplate, InnerInlineNoTemplate, HolderAdmin)
73+
74+# local test models
75 
76 
77 class TestInline(TestCase):
78@@ -66,6 +71,15 @@ class TestInline(TestCase):
79         response = self.client.post('/test_admin/admin/admin_inlines/fashionista/add/', data)
80         self.assertEqual(response.status_code, 302)
81         self.assertEqual(len(Fashionista.objects.filter(person__firstname='Imelda')), 1)
82+   
83+    def test_inline_template_validation(self):
84+        self.assertRaises(ImproperlyConfigured, validate_inline,
85+                          InnerInlineNoTemplate, HolderAdmin, Holder)
86+        self.assertRaises(ImproperlyConfigured, validate_inline,
87+                          InnerInlineNonExistingTemplate, HolderAdmin, Holder)
88+        # check that regular inlines don't raise an exception:
89+        validate_inline(InnerInline, HolderAdmin, Holder)
90+       
91 
92 class TestInlineMedia(TestCase):
93     fixtures = ['admin-views-users.xml']
94@@ -118,4 +132,4 @@ class TestInlineAdminForm(TestCase):
95 
96         iaf = InlineAdminForm(None, None, {}, {}, joe)
97         parent_ct = ContentType.objects.get_for_model(Parent)
98-        self.assertEqual(iaf.original.content_type, parent_ct)
99+        self.assertEqual(iaf.original.content_type, parent_ct)
100\ No newline at end of file