Ticket #16042: 16042.4.diff
File 16042.4.diff, 5.8 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 7962a40..52c731c 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 0ead6c2..99c3ca3 100644
a b 1 from __future__ import with_statement 2 1 3 from django.contrib.comments.forms import CommentForm 2 4 from django.contrib.comments.models import Comment 3 5 from django.contrib.contenttypes.models import ContentType … … class CommentTemplateTagTests(CommentTestCase): 45 47 self.testRenderCommentFormFromObject() 46 48 self.assertNumQueries(1, test) 47 49 48 def testGetCommentCount(self, tag=None): 49 self.createSomeComments() 50 def verifyGetCommentCount(self, tag=None): 50 51 t = "{% load comments %}" + (tag or "{% get_comment_count for comment_tests.article a.id as cc %}") + "{{ cc }}" 51 52 ctx, out = self.render(t, a=Article.objects.get(pk=1)) 52 53 self.assertEqual(out, "2") 53 54 55 def testGetCommentCount(self): 56 self.createSomeComments() 57 self.verifyGetCommentCount("{% get_comment_count for comment_tests.article a.id as cc %}") 58 54 59 def testGetCommentCountFromLiteral(self): 55 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 %}") 56 62 57 63 def testGetCommentCountFromObject(self): 58 self.testGetCommentCount("{% get_comment_count for a as cc %}") 64 self.createSomeComments() 65 self.verifyGetCommentCount("{% get_comment_count for a as cc %}") 59 66 60 def testGetCommentList(self, tag=None):61 c1, c2, c3, c4 = self.createSomeComments()62 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 %}") 63 70 ctx, out = self.render(t, a=Author.objects.get(pk=1)) 64 71 self.assertEqual(out, "") 65 72 self.assertEqual(list(ctx["cl"]), [c2]) 66 73 74 def testGetCommentList(self): 75 self.createSomeComments() 76 self.verifyGetCommentList("{% get_comment_list for comment_tests.author a.id as cl %}") 77 67 78 def testGetCommentListFromLiteral(self): 68 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 %}") 69 81 70 82 def testGetCommentListFromObject(self): 71 self.testGetCommentList("{% get_comment_list for a as cl %}") 83 self.createSomeComments() 84 self.verifyGetCommentList("{% get_comment_list for a as cl %}") 72 85 73 86 def testGetCommentPermalink(self): 74 87 c1, c2, c3, c4 = self.createSomeComments() … … class CommentTemplateTagTests(CommentTestCase): 100 113 def testRenderCommentListFromObject(self): 101 114 self.testRenderCommentList("{% render_comment_list for a %}") 102 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()