mirror of https://github.com/django/django.git
Fixed #16042 -- Use the content types caching in the comments contrib app. Thanks, ptone, Julien Phalip and Thejaswi Puthraya.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@16737 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
64e16c094b
commit
8258fe7845
|
@ -47,7 +47,7 @@ class BaseCommentNode(template.Node):
|
||||||
def lookup_content_type(token, tagname):
|
def lookup_content_type(token, tagname):
|
||||||
try:
|
try:
|
||||||
app, model = token.split('.')
|
app, model = token.split('.')
|
||||||
return ContentType.objects.get(app_label=app, model=model)
|
return ContentType.objects.get_by_natural_key(app, model)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
raise template.TemplateSyntaxError("Third argument in %r must be in the format 'app.model'" % tagname)
|
raise template.TemplateSyntaxError("Third argument in %r must be in the format 'app.model'" % tagname)
|
||||||
except ContentType.DoesNotExist:
|
except ContentType.DoesNotExist:
|
||||||
|
|
|
@ -1,4 +1,7 @@
|
||||||
|
from __future__ import with_statement
|
||||||
|
|
||||||
from django.contrib.comments.forms import CommentForm
|
from django.contrib.comments.forms import CommentForm
|
||||||
|
from django.contrib.comments.models import Comment
|
||||||
from django.contrib.contenttypes.models import ContentType
|
from django.contrib.contenttypes.models import ContentType
|
||||||
from django.template import Template, Context
|
from django.template import Template, Context
|
||||||
from regressiontests.comment_tests.models import Article, Author
|
from regressiontests.comment_tests.models import Article, Author
|
||||||
|
@ -44,30 +47,41 @@ class CommentTemplateTagTests(CommentTestCase):
|
||||||
self.testRenderCommentFormFromObject()
|
self.testRenderCommentFormFromObject()
|
||||||
self.assertNumQueries(1, test)
|
self.assertNumQueries(1, test)
|
||||||
|
|
||||||
def testGetCommentCount(self, tag=None):
|
def verifyGetCommentCount(self, tag=None):
|
||||||
self.createSomeComments()
|
|
||||||
t = "{% load comments %}" + (tag or "{% get_comment_count for comment_tests.article a.id as cc %}") + "{{ cc }}"
|
t = "{% load comments %}" + (tag or "{% get_comment_count for comment_tests.article a.id as cc %}") + "{{ cc }}"
|
||||||
ctx, out = self.render(t, a=Article.objects.get(pk=1))
|
ctx, out = self.render(t, a=Article.objects.get(pk=1))
|
||||||
self.assertEqual(out, "2")
|
self.assertEqual(out, "2")
|
||||||
|
|
||||||
|
def testGetCommentCount(self):
|
||||||
|
self.createSomeComments()
|
||||||
|
self.verifyGetCommentCount("{% get_comment_count for comment_tests.article a.id as cc %}")
|
||||||
|
|
||||||
def testGetCommentCountFromLiteral(self):
|
def testGetCommentCountFromLiteral(self):
|
||||||
self.testGetCommentCount("{% get_comment_count for comment_tests.article 1 as cc %}")
|
self.createSomeComments()
|
||||||
|
self.verifyGetCommentCount("{% get_comment_count for comment_tests.article 1 as cc %}")
|
||||||
|
|
||||||
def testGetCommentCountFromObject(self):
|
def testGetCommentCountFromObject(self):
|
||||||
self.testGetCommentCount("{% get_comment_count for a as cc %}")
|
self.createSomeComments()
|
||||||
|
self.verifyGetCommentCount("{% get_comment_count for a as cc %}")
|
||||||
|
|
||||||
def testGetCommentList(self, tag=None):
|
def verifyGetCommentList(self, tag=None):
|
||||||
c1, c2, c3, c4 = self.createSomeComments()
|
c1, c2, c3, c4 = Comment.objects.all()[:4]
|
||||||
t = "{% load comments %}" + (tag or "{% get_comment_list for comment_tests.author a.id as cl %}")
|
t = "{% load comments %}" + (tag or "{% get_comment_list for comment_tests.author a.id as cl %}")
|
||||||
ctx, out = self.render(t, a=Author.objects.get(pk=1))
|
ctx, out = self.render(t, a=Author.objects.get(pk=1))
|
||||||
self.assertEqual(out, "")
|
self.assertEqual(out, "")
|
||||||
self.assertEqual(list(ctx["cl"]), [c2])
|
self.assertEqual(list(ctx["cl"]), [c2])
|
||||||
|
|
||||||
|
def testGetCommentList(self):
|
||||||
|
self.createSomeComments()
|
||||||
|
self.verifyGetCommentList("{% get_comment_list for comment_tests.author a.id as cl %}")
|
||||||
|
|
||||||
def testGetCommentListFromLiteral(self):
|
def testGetCommentListFromLiteral(self):
|
||||||
self.testGetCommentList("{% get_comment_list for comment_tests.author 1 as cl %}")
|
self.createSomeComments()
|
||||||
|
self.verifyGetCommentList("{% get_comment_list for comment_tests.author 1 as cl %}")
|
||||||
|
|
||||||
def testGetCommentListFromObject(self):
|
def testGetCommentListFromObject(self):
|
||||||
self.testGetCommentList("{% get_comment_list for a as cl %}")
|
self.createSomeComments()
|
||||||
|
self.verifyGetCommentList("{% get_comment_list for a as cl %}")
|
||||||
|
|
||||||
def testGetCommentPermalink(self):
|
def testGetCommentPermalink(self):
|
||||||
c1, c2, c3, c4 = self.createSomeComments()
|
c1, c2, c3, c4 = self.createSomeComments()
|
||||||
|
@ -99,3 +113,63 @@ class CommentTemplateTagTests(CommentTestCase):
|
||||||
def testRenderCommentListFromObject(self):
|
def testRenderCommentListFromObject(self):
|
||||||
self.testRenderCommentList("{% render_comment_list for a %}")
|
self.testRenderCommentList("{% render_comment_list for a %}")
|
||||||
|
|
||||||
|
def testNumberQueries(self):
|
||||||
|
"""
|
||||||
|
Ensure that the template tags use cached content types to reduce the
|
||||||
|
number of DB queries.
|
||||||
|
Refs #16042.
|
||||||
|
"""
|
||||||
|
|
||||||
|
self.createSomeComments()
|
||||||
|
|
||||||
|
# {% render_comment_list %} -----------------
|
||||||
|
|
||||||
|
# Clear CT cache
|
||||||
|
ContentType.objects.clear_cache()
|
||||||
|
with self.assertNumQueries(4):
|
||||||
|
self.testRenderCommentListFromObject()
|
||||||
|
|
||||||
|
# Force the CT to be cached
|
||||||
|
ct = ContentType.objects.get_for_model(Article)
|
||||||
|
with self.assertNumQueries(3):
|
||||||
|
self.testRenderCommentListFromObject()
|
||||||
|
|
||||||
|
# {% get_comment_list %} --------------------
|
||||||
|
|
||||||
|
ContentType.objects.clear_cache()
|
||||||
|
with self.assertNumQueries(4):
|
||||||
|
self.verifyGetCommentList()
|
||||||
|
|
||||||
|
ct = ContentType.objects.get_for_model(Author)
|
||||||
|
with self.assertNumQueries(3):
|
||||||
|
self.verifyGetCommentList()
|
||||||
|
|
||||||
|
# {% render_comment_form %} -----------------
|
||||||
|
|
||||||
|
ContentType.objects.clear_cache()
|
||||||
|
with self.assertNumQueries(3):
|
||||||
|
self.testRenderCommentForm()
|
||||||
|
|
||||||
|
ct = ContentType.objects.get_for_model(Article)
|
||||||
|
with self.assertNumQueries(2):
|
||||||
|
self.testRenderCommentForm()
|
||||||
|
|
||||||
|
# {% get_comment_form %} --------------------
|
||||||
|
|
||||||
|
ContentType.objects.clear_cache()
|
||||||
|
with self.assertNumQueries(3):
|
||||||
|
self.testGetCommentForm()
|
||||||
|
|
||||||
|
ct = ContentType.objects.get_for_model(Article)
|
||||||
|
with self.assertNumQueries(2):
|
||||||
|
self.testGetCommentForm()
|
||||||
|
|
||||||
|
# {% get_comment_count %} -------------------
|
||||||
|
|
||||||
|
ContentType.objects.clear_cache()
|
||||||
|
with self.assertNumQueries(3):
|
||||||
|
self.verifyGetCommentCount()
|
||||||
|
|
||||||
|
ct = ContentType.objects.get_for_model(Article)
|
||||||
|
with self.assertNumQueries(2):
|
||||||
|
self.verifyGetCommentCount()
|
||||||
|
|
Loading…
Reference in New Issue