Ticket #16042: 16042.5.diff

File 16042.5.diff, 5.9 KB (added by Preston Holmes, 13 years ago)

patch improved with missing import

  • 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):  
    4747    def lookup_content_type(token, tagname):
    4848        try:
    4949            app, model = token.split('.')
    50             return ContentType.objects.get(app_label=app, model=model)
     50            return ContentType.objects.get_by_natural_key(app, model)
    5151        except ValueError:
    5252            raise template.TemplateSyntaxError("Third argument in %r must be in the format 'app.model'" % tagname)
    5353        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  
     1from __future__ import with_statement
     2
    13from django.contrib.comments.forms import CommentForm
     4from django.contrib.comments.models import Comment
    25from django.contrib.contenttypes.models import ContentType
    36from django.template import Template, Context
    47from regressiontests.comment_tests.models import Article, Author
    class CommentTemplateTagTests(CommentTestCase):  
    4447            self.testRenderCommentFormFromObject()
    4548        self.assertNumQueries(1, test)
    4649
    47     def testGetCommentCount(self, tag=None):
    48         self.createSomeComments()
     50    def verifyGetCommentCount(self, tag=None):
    4951        t = "{% load comments %}" + (tag or "{% get_comment_count for comment_tests.article a.id as cc %}") + "{{ cc }}"
    5052        ctx, out = self.render(t, a=Article.objects.get(pk=1))
    5153        self.assertEqual(out, "2")
    5254
     55    def testGetCommentCount(self):
     56        self.createSomeComments()
     57        self.verifyGetCommentCount("{% get_comment_count for comment_tests.article a.id as cc %}")
     58
    5359    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 %}")
    5562
    5663    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 %}")
    5866
    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 %}")
    6270        ctx, out = self.render(t, a=Author.objects.get(pk=1))
    6371        self.assertEqual(out, "")
    6472        self.assertEqual(list(ctx["cl"]), [c2])
    6573
     74    def testGetCommentList(self):
     75        self.createSomeComments()
     76        self.verifyGetCommentList("{% get_comment_list for comment_tests.author a.id as cl %}")
     77
    6678    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 %}")
    6881
    6982    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 %}")
    7185
    7286    def testGetCommentPermalink(self):
    7387        c1, c2, c3, c4 = self.createSomeComments()
    class CommentTemplateTagTests(CommentTestCase):  
    99113    def testRenderCommentListFromObject(self):
    100114        self.testRenderCommentList("{% render_comment_list for a %}")
    101115
     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()
Back to Top