Ticket #16042: 16042.5.diff
File 16042.5.diff, 5.9 KB (added by , 13 years ago) |
---|
-
django/contrib/comments/templatetags/comments.py
diff --git a/django/contrib/comments/templatetags/comments.py b/django/contrib/comments/templatetags/comments.py index 024c4f6..ce1825e 100644
a b class BaseCommentNode(template.Node): 47 47 def lookup_content_type(token, tagname): 48 48 try: 49 49 app, model = token.split('.') 50 return ContentType.objects.get (app_label=app, model=model)50 return ContentType.objects.get_by_natural_key(app, model) 51 51 except ValueError: 52 52 raise template.TemplateSyntaxError("Third argument in %r must be in the format 'app.model'" % tagname) 53 53 except ContentType.DoesNotExist: -
tests/regressiontests/comment_tests/tests/templatetag_tests.py
diff --git a/tests/regressiontests/comment_tests/tests/templatetag_tests.py b/tests/regressiontests/comment_tests/tests/templatetag_tests.py index 0ee34ac..99c3ca3 100644
a b 1 from __future__ import with_statement 2 1 3 from django.contrib.comments.forms import CommentForm 4 from django.contrib.comments.models import Comment 2 5 from django.contrib.contenttypes.models import ContentType 3 6 from django.template import Template, Context 4 7 from regressiontests.comment_tests.models import Article, Author … … class CommentTemplateTagTests(CommentTestCase): 44 47 self.testRenderCommentFormFromObject() 45 48 self.assertNumQueries(1, test) 46 49 47 def testGetCommentCount(self, tag=None): 48 self.createSomeComments() 50 def verifyGetCommentCount(self, tag=None): 49 51 t = "{% load comments %}" + (tag or "{% get_comment_count for comment_tests.article a.id as cc %}") + "{{ cc }}" 50 52 ctx, out = self.render(t, a=Article.objects.get(pk=1)) 51 53 self.assertEqual(out, "2") 52 54 55 def testGetCommentCount(self): 56 self.createSomeComments() 57 self.verifyGetCommentCount("{% get_comment_count for comment_tests.article a.id as cc %}") 58 53 59 def testGetCommentCountFromLiteral(self): 54 self.testGetCommentCount("{% get_comment_count for comment_tests.article 1 as cc %}") 60 self.createSomeComments() 61 self.verifyGetCommentCount("{% get_comment_count for comment_tests.article 1 as cc %}") 55 62 56 63 def testGetCommentCountFromObject(self): 57 self.testGetCommentCount("{% get_comment_count for a as cc %}") 64 self.createSomeComments() 65 self.verifyGetCommentCount("{% get_comment_count for a as cc %}") 58 66 59 def testGetCommentList(self, tag=None):60 c1, c2, c3, c4 = self.createSomeComments()61 t = "{% load comments %}" + (tag or "{% get_comment_list for comment_tests.author a.id as cl %}")67 def verifyGetCommentList(self, tag=None): 68 c1, c2, c3, c4 = Comment.objects.all()[:4] 69 t = "{% load comments %}" + (tag or "{% get_comment_list for comment_tests.author a.id as cl %}") 62 70 ctx, out = self.render(t, a=Author.objects.get(pk=1)) 63 71 self.assertEqual(out, "") 64 72 self.assertEqual(list(ctx["cl"]), [c2]) 65 73 74 def testGetCommentList(self): 75 self.createSomeComments() 76 self.verifyGetCommentList("{% get_comment_list for comment_tests.author a.id as cl %}") 77 66 78 def testGetCommentListFromLiteral(self): 67 self.testGetCommentList("{% get_comment_list for comment_tests.author 1 as cl %}") 79 self.createSomeComments() 80 self.verifyGetCommentList("{% get_comment_list for comment_tests.author 1 as cl %}") 68 81 69 82 def testGetCommentListFromObject(self): 70 self.testGetCommentList("{% get_comment_list for a as cl %}") 83 self.createSomeComments() 84 self.verifyGetCommentList("{% get_comment_list for a as cl %}") 71 85 72 86 def testGetCommentPermalink(self): 73 87 c1, c2, c3, c4 = self.createSomeComments() … … class CommentTemplateTagTests(CommentTestCase): 99 113 def testRenderCommentListFromObject(self): 100 114 self.testRenderCommentList("{% render_comment_list for a %}") 101 115 116 def testNumberQueries(self): 117 """ 118 Ensure that the template tags use cached content types to reduce the 119 number of DB queries. 120 Refs #16042. 121 """ 122 123 self.createSomeComments() 124 125 # {% render_comment_list %} ----------------- 126 127 # Clear CT cache 128 ContentType.objects.clear_cache() 129 with self.assertNumQueries(4): 130 self.testRenderCommentListFromObject() 131 132 # Force the CT to be cached 133 ct = ContentType.objects.get_for_model(Article) 134 with self.assertNumQueries(3): 135 self.testRenderCommentListFromObject() 136 137 # {% get_comment_list %} -------------------- 138 139 ContentType.objects.clear_cache() 140 with self.assertNumQueries(4): 141 self.verifyGetCommentList() 142 143 ct = ContentType.objects.get_for_model(Author) 144 with self.assertNumQueries(3): 145 self.verifyGetCommentList() 146 147 # {% render_comment_form %} ----------------- 148 149 ContentType.objects.clear_cache() 150 with self.assertNumQueries(3): 151 self.testRenderCommentForm() 152 153 ct = ContentType.objects.get_for_model(Article) 154 with self.assertNumQueries(2): 155 self.testRenderCommentForm() 156 157 # {% get_comment_form %} -------------------- 158 159 ContentType.objects.clear_cache() 160 with self.assertNumQueries(3): 161 self.testGetCommentForm() 162 163 ct = ContentType.objects.get_for_model(Article) 164 with self.assertNumQueries(2): 165 self.testGetCommentForm() 166 167 # {% get_comment_count %} ------------------- 168 169 ContentType.objects.clear_cache() 170 with self.assertNumQueries(3): 171 self.verifyGetCommentCount() 172 173 ct = ContentType.objects.get_for_model(Article) 174 with self.assertNumQueries(2): 175 self.verifyGetCommentCount()