Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added templatetags

  • Loading branch information...
commit 6948e644a4dc93e093ee4902665b60b6769c47c7 1 parent 959512e
Alex Gaynor alex authored
3  dialogos/models.py
View
@@ -21,3 +21,6 @@ class Comment(models.Model):
submit_date = models.DateTimeField(default=datetime.now)
ip_address = models.IPAddressField(null=True)
public = models.BooleanField(default=True)
+
+ def __unicode__(self):
+ return "pk=%d" % self.pk
0  dialogos/templatetags/__init__.py
View
No changes.
75 dialogos/templatetags/dialogos_tags.py
View
@@ -0,0 +1,75 @@
+from django import template
+
+from django.contrib.contenttypes.models import ContentType
+
+from dialogos.models import Comment
+
+
+register = template.Library()
+
+
+class BaseCommentNode(template.Node):
+ @classmethod
+ def handle_token(cls, parser, token):
+ bits = token.split_contents()
+ if not cls.requires_as_var and len(bits) == 2:
+ return cls(parser.compile_filter(bits[1]))
+ elif len(bits) == 4:
+ if bits[2] != "as":
+ raise template.TemplateSyntaxError("%r's 2nd argument must be 'as'" % bits[0])
+ return cls(parser.compile_filter(bits[1]), bits[3])
+ if cls.requires_as_var:
+ args = "1 argument"
+ else:
+ args = "either 1 or 3 arguments"
+ raise template.TemplateSyntaxError("%r takes %s" % (bits[0], args))
+
+ def __init__(self, obj, varname=None):
+ self.obj = obj
+ self.varname = varname
+
+ def get_comments(self, context):
+ obj = self.obj.resolve(context)
+ return Comment.objects.filter(
+ object_id=obj.pk,
+ content_type=ContentType.objects.get_for_model(obj)
+ )
+
+class CommentCountNode(BaseCommentNode):
+ requires_as_var = False
+
+ def render(self, context):
+ comments = self.get_comments(context).count()
+ if self.varname is not None:
+ context[self.varname] = comments
+ return ""
+ return unicode(comments)
+
+
+class CommentsNode(BaseCommentNode):
+ requires_as_var = True
+
+ def render(self, context):
+ context[self.varname] = self.get_comments(context)
+ return ""
+
+
+@register.tag
+def get_comment_count(parser, token):
+ """
+ Usage:
+
+ {% get_comment_count obj %}
+ or
+ {% get_comment_count obj as var %}
+ """
+ return CommentCountNode.handle_token(parser, token)
+
+@register.tag
+def get_comments(parser, token):
+ """
+ Usage:
+
+ {% get_comments obj as var %}
+ """
+ return CommentsNode.handle_token(parser, token)
25 dialogos/tests.py
View
@@ -15,7 +15,7 @@ def setUp(self):
def assert_renders(self, tmpl, context, value):
tmpl = Template(tmpl)
- self.assertEqual(tmpl.render(Context(context)), value)
+ self.assertEqual(tmpl.render(context), value)
def post_comment(self, obj, data):
@@ -50,7 +50,7 @@ def test_post_comment(self):
self.assertEqual(c.comment, "I thought you were watching the hobbits?")
self.assertEqual(c.author, self.user)
- def test_templatetags(self):
+ def test_ttag_get_comment_count(self):
g = User.objects.create(username="Sauron")
self.post_comment(g, data={
"name": "Gandalf",
@@ -63,6 +63,25 @@ def test_templatetags(self):
self.assert_renders(
"{% load dialogos_tags %}{% get_comment_count o %}",
- {"o": g},
+ Context({"o": g}),
"2"
)
+
+ def test_ttag_get_comments(self):
+ g = User.objects.create(username="Sauron")
+ self.post_comment(g, data={
+ "name": "Gandalf",
+ "comment": "You can't win",
+ })
+ self.post_comment(g, data={
+ "name": "Gollum",
+ "comment": "We wants our precious",
+ })
+
+ c = Context({"o": g})
+ self.assert_renders(
+ "{% load dialogos_tags %}{% get_comments o as cs %}",
+ c,
+ ""
+ )
+ self.assertEqual(list(c["cs"]), list(Comment.objects.all()))
Please sign in to comment.
Something went wrong with that request. Please try again.