Merge pull request #850 from bmispelon/ticket-19916

Fix 19916. Smarter tokenizing of contrib.comment's templatetags arguments
This commit is contained in:
Aymeric Augustin 2013-03-07 12:12:29 -08:00
commit 477d737e1e
2 changed files with 38 additions and 4 deletions

View File

@ -17,7 +17,7 @@ class BaseCommentNode(template.Node):
@classmethod @classmethod
def handle_token(cls, parser, token): def handle_token(cls, parser, token):
"""Class method to parse get_comment_list/count/form and return a Node.""" """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': if tokens[1] != 'for':
raise template.TemplateSyntaxError("Second argument in %r tag must be 'for'" % tokens[0]) raise template.TemplateSyntaxError("Second argument in %r tag must be 'for'" % tokens[0])
@ -146,7 +146,7 @@ class RenderCommentFormNode(CommentFormNode):
@classmethod @classmethod
def handle_token(cls, parser, token): def handle_token(cls, parser, token):
"""Class method to parse render_comment_form and return a Node.""" """Class method to parse render_comment_form and return a Node."""
tokens = token.contents.split() tokens = token.split_contents()
if tokens[1] != 'for': if tokens[1] != 'for':
raise template.TemplateSyntaxError("Second argument in %r tag must be 'for'" % tokens[0]) raise template.TemplateSyntaxError("Second argument in %r tag must be 'for'" % tokens[0])
@ -182,7 +182,7 @@ class RenderCommentListNode(CommentListNode):
@classmethod @classmethod
def handle_token(cls, parser, token): def handle_token(cls, parser, token):
"""Class method to parse render_comment_list and return a Node.""" """Class method to parse render_comment_list and return a Node."""
tokens = token.contents.split() tokens = token.split_contents()
if tokens[1] != 'for': if tokens[1] != 'for':
raise template.TemplateSyntaxError("Second argument in %r tag must be 'for'" % tokens[0]) 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.forms import CommentForm
from django.contrib.comments.models import Comment 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, Library, libraries
from ..models import Article, Author from ..models import Article, Author
from . import CommentTestCase from . import CommentTestCase
register = Library()
@register.filter
def noop(variable, param=None):
return variable
libraries['comment_testtags'] = register
class CommentTemplateTagTests(CommentTestCase): class CommentTemplateTagTests(CommentTestCase):
@ -32,6 +40,9 @@ class CommentTemplateTagTests(CommentTestCase):
def testGetCommentFormFromObject(self): def testGetCommentFormFromObject(self):
self.testGetCommentForm("{% get_comment_form for a as form %}") 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): def testRenderCommentForm(self, tag=None):
t = "{% load comments %}" + (tag or "{% render_comment_form for comment_tests.article a.id %}") 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)) ctx, out = self.render(t, a=Article.objects.get(pk=1))
@ -44,6 +55,9 @@ class CommentTemplateTagTests(CommentTestCase):
def testRenderCommentFormFromObject(self): def testRenderCommentFormFromObject(self):
self.testRenderCommentForm("{% render_comment_form for a %}") 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): def testRenderCommentFormFromObjectWithQueryCount(self):
with self.assertNumQueries(1): with self.assertNumQueries(1):
self.testRenderCommentFormFromObject() self.testRenderCommentFormFromObject()
@ -65,6 +79,10 @@ class CommentTemplateTagTests(CommentTestCase):
self.createSomeComments() self.createSomeComments()
self.verifyGetCommentCount("{% get_comment_count for a as cc %}") 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): def verifyGetCommentList(self, tag=None):
c1, c2, c3, c4 = Comment.objects.all()[:4] 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 %}")
@ -84,6 +102,10 @@ class CommentTemplateTagTests(CommentTestCase):
self.createSomeComments() self.createSomeComments()
self.verifyGetCommentList("{% get_comment_list for a as cl %}") 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): def testGetCommentPermalink(self):
c1, c2, c3, c4 = self.createSomeComments() c1, c2, c3, c4 = self.createSomeComments()
t = "{% load comments %}{% get_comment_list for comment_tests.author author.id as cl %}" 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) ctx, out = self.render(t, author=author)
self.assertEqual(out, "/cr/%s/%s/#c%s-by-Joe Somebody" % (ct.id, author.id, c2.id)) 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): def testRenderCommentList(self, tag=None):
t = "{% load comments %}" + (tag or "{% render_comment_list for comment_tests.article a.id %}") 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)) ctx, out = self.render(t, a=Article.objects.get(pk=1))
@ -114,6 +145,9 @@ class CommentTemplateTagTests(CommentTestCase):
def testRenderCommentListFromObject(self): def testRenderCommentListFromObject(self):
self.testRenderCommentList("{% render_comment_list for a %}") 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): def testNumberQueries(self):
""" """
Ensure that the template tags use cached content types to reduce the Ensure that the template tags use cached content types to reduce the