Code

Ticket #16042: 16042.3.diff

File 16042.3.diff, 3.0 KB (added by thejaswi_puthraya, 3 years ago)

changes as per hvdklauw's latest comments

Line 
1diff --git a/django/contrib/comments/templatetags/comments.py b/django/contrib/comments/templatetags/comments.py
2index 7962a40..52c731c 100644
3--- a/django/contrib/comments/templatetags/comments.py
4+++ b/django/contrib/comments/templatetags/comments.py
5@@ -47,7 +47,7 @@ class BaseCommentNode(template.Node):
6     def lookup_content_type(token, tagname):
7         try:
8             app, model = token.split('.')
9-            return ContentType.objects.get(app_label=app, model=model)
10+            return ContentType.objects.get_by_natural_key(app, model)
11         except ValueError:
12             raise template.TemplateSyntaxError("Third argument in %r must be in the format 'app.model'" % tagname)
13         except ContentType.DoesNotExist:
14diff --git a/tests/regressiontests/comment_tests/tests/templatetag_tests.py b/tests/regressiontests/comment_tests/tests/templatetag_tests.py
15index 0ead6c2..307a66d 100644
16--- a/tests/regressiontests/comment_tests/tests/templatetag_tests.py
17+++ b/tests/regressiontests/comment_tests/tests/templatetag_tests.py
18@@ -1,9 +1,12 @@
19 from django.contrib.comments.forms import CommentForm
20 from django.contrib.comments.models import Comment
21 from django.contrib.contenttypes.models import ContentType
22-from django.template import Template, Context
23+from django.template import Template, Context, RequestContext
24 from regressiontests.comment_tests.models import Article, Author
25 from regressiontests.comment_tests.tests import CommentTestCase
26+from django.db import connection
27+from django.conf import settings
28+from django.test import RequestFactory
29 
30 class CommentTemplateTagTests(CommentTestCase):
31 
32@@ -100,3 +103,36 @@ class CommentTemplateTagTests(CommentTestCase):
33     def testRenderCommentListFromObject(self):
34         self.testRenderCommentList("{% render_comment_list for a %}")
35 
36+class CommentTemplateTagNumQueries(CommentTemplateTagTests):
37+    def setUp(self):
38+        super(CommentTemplateTagNumQueries, self).setUp()
39+        self.debug = settings.DEBUG
40+        settings.DEBUG = True
41+
42+    def tearDown(self):
43+        settings.DEBUG = self.debug
44+        super(CommentTemplateTagNumQueries, self).tearDown()
45+
46+    def render(self, t, **c):
47+        rf = RequestFactory()
48+        request = rf.get("/")
49+        ctx = RequestContext(request, c)
50+        out = Template(t).render(ctx)
51+        return ctx, out
52+
53+    def renderCommentList(self, tag=None):
54+        connection.queries = []
55+        t = "{% load comments %}" + (tag or "{% render_comment_list for comment_tests.article a.id %}")
56+        ctx, out = self.render(t, a=Article.objects.get(pk=1))
57+        return len(connection.queries)
58+
59+    def testNumberQueries(self):
60+        self.createSomeComments()
61+
62+        num_queries = self.renderCommentList()
63+        num_queries_2 = self.renderCommentList()
64+        self.assertEquals(num_queries, 3)
65+        self.assertEquals(num_queries, num_queries_2)
66+        ContentType.objects.__class__._cache = {}
67+        num_queries_3 = self.renderCommentList()
68+        self.assertEquals(4, num_queries_3)