Fix 19916. Smarter tokenizing of contrib.comment's templatetags arguments.

This commit is contained in:
Baptiste Mispelon 2013-02-25 22:53:14 +01:00
parent 4cccb85e29
commit 81804ae474
2 changed files with 38 additions and 4 deletions

View File

@ -17,7 +17,7 @@ class BaseCommentNode(template.Node):
@classmethod
def handle_token(cls, parser, token):
"""Class method to parse get_comment_list/count/form and return a Node."""
tokens = token.contents.split()
tokens = token.split_contents()
if tokens[1] != 'for':
raise template.TemplateSyntaxError("Second argument in %r tag must be 'for'" % tokens[0])
@ -146,7 +146,7 @@ class RenderCommentFormNode(CommentFormNode):
@classmethod
def handle_token(cls, parser, token):
"""Class method to parse render_comment_form and return a Node."""
tokens = token.contents.split()
tokens = token.split_contents()
if tokens[1] != 'for':
raise template.TemplateSyntaxError("Second argument in %r tag must be 'for'" % tokens[0])
@ -182,7 +182,7 @@ class RenderCommentListNode(CommentListNode):
@classmethod
def handle_token(cls, parser, token):
"""Class method to parse render_comment_list and return a Node."""
tokens = token.contents.split()
tokens = token.split_contents()
if tokens[1] != 'for':
raise template.TemplateSyntaxError("Second argument in %r tag must be 'for'" % tokens[0])

View File

@ -3,11 +3,19 @@ from __future__ import absolute_import
from django.contrib.comments.forms import CommentForm
from django.contrib.comments.models import Comment
from django.contrib.contenttypes.models import ContentType
from django.template import Template, Context
from django.template import Template, Context, Library, libraries
from ..models import Article, Author
from . import CommentTestCase
register = Library()
@register.filter
def noop(variable, param=None):
return variable
libraries['comment_testtags'] = register
class CommentTemplateTagTests(CommentTestCase):
@ -32,6 +40,9 @@ class CommentTemplateTagTests(CommentTestCase):
def testGetCommentFormFromObject(self):
self.testGetCommentForm("{% get_comment_form for a as form %}")
def testWhitespaceInGetCommentFormTag(self):
self.testGetCommentForm("{% load comment_testtags %}{% get_comment_form for a|noop:'x y' as form %}")
def testRenderCommentForm(self, tag=None):
t = "{% load comments %}" + (tag or "{% render_comment_form for comment_tests.article a.id %}")
ctx, out = self.render(t, a=Article.objects.get(pk=1))
@ -44,6 +55,9 @@ class CommentTemplateTagTests(CommentTestCase):
def testRenderCommentFormFromObject(self):
self.testRenderCommentForm("{% render_comment_form for a %}")
def testWhitespaceInRenderCommentFormTag(self):
self.testRenderCommentForm("{% load comment_testtags %}{% render_comment_form for a|noop:'x y' %}")
def testRenderCommentFormFromObjectWithQueryCount(self):
with self.assertNumQueries(1):
self.testRenderCommentFormFromObject()
@ -65,6 +79,10 @@ class CommentTemplateTagTests(CommentTestCase):
self.createSomeComments()
self.verifyGetCommentCount("{% get_comment_count for a as cc %}")
def testWhitespaceInGetCommentCountTag(self):
self.createSomeComments()
self.verifyGetCommentCount("{% load comment_testtags %}{% get_comment_count for a|noop:'x y' as cc %}")
def verifyGetCommentList(self, tag=None):
c1, c2, c3, c4 = Comment.objects.all()[:4]
t = "{% load comments %}" + (tag or "{% get_comment_list for comment_tests.author a.id as cl %}")
@ -84,6 +102,10 @@ class CommentTemplateTagTests(CommentTestCase):
self.createSomeComments()
self.verifyGetCommentList("{% get_comment_list for a as cl %}")
def testWhitespaceInGetCommentListTag(self):
self.createSomeComments()
self.verifyGetCommentList("{% load comment_testtags %}{% get_comment_list for a|noop:'x y' as cl %}")
def testGetCommentPermalink(self):
c1, c2, c3, c4 = self.createSomeComments()
t = "{% load comments %}{% get_comment_list for comment_tests.author author.id as cl %}"
@ -102,6 +124,15 @@ class CommentTemplateTagTests(CommentTestCase):
ctx, out = self.render(t, author=author)
self.assertEqual(out, "/cr/%s/%s/#c%s-by-Joe Somebody" % (ct.id, author.id, c2.id))
def testWhitespaceInGetCommentPermalinkTag(self):
c1, c2, c3, c4 = self.createSomeComments()
t = "{% load comments comment_testtags %}{% get_comment_list for comment_tests.author author.id as cl %}"
t += "{% get_comment_permalink cl.0|noop:'x y' %}"
ct = ContentType.objects.get_for_model(Author)
author = Author.objects.get(pk=1)
ctx, out = self.render(t, author=author)
self.assertEqual(out, "/cr/%s/%s/#c%s" % (ct.id, author.id, c2.id))
def testRenderCommentList(self, tag=None):
t = "{% load comments %}" + (tag or "{% render_comment_list for comment_tests.article a.id %}")
ctx, out = self.render(t, a=Article.objects.get(pk=1))
@ -114,6 +145,9 @@ class CommentTemplateTagTests(CommentTestCase):
def testRenderCommentListFromObject(self):
self.testRenderCommentList("{% render_comment_list for a %}")
def testWhitespaceInRenderCommentListTag(self):
self.testRenderCommentList("{% load comment_testtags %}{% render_comment_list for a|noop:'x y' %}")
def testNumberQueries(self):
"""
Ensure that the template tags use cached content types to reduce the