Permalink
Browse files

Added a templatetag for getting the right formclass.

  • Loading branch information...
1 parent c33034b commit 7b219391238085d937d966f37c7f849ff07a94f0 @alex alex committed Aug 20, 2010
Showing with 48 additions and 1 deletion.
  1. +1 −1 dialogos/forms.py
  2. +27 −0 dialogos/templatetags/dialogos_tags.py
  3. +20 −0 dialogos/tests.py
View
@@ -7,7 +7,7 @@
class BaseCommentForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
- self.request = kwargs.pop("request")
+ self.request = kwargs.pop("request", None)
self.obj = kwargs.pop("obj")
super(BaseCommentForm, self).__init__(*args, **kwargs)
@@ -2,6 +2,7 @@
from django.contrib.contenttypes.models import ContentType
+from dialogos.forms import UnauthenticatedCommentForm, AuthenticatedCommentForm
from dialogos.models import Comment
@@ -54,6 +55,20 @@ def render(self, context):
return ""
+class CommentFormNode(BaseCommentNode):
+ requires_as_var = False
+
+ def render(self, context):
+ obj = self.obj.resolve(context)
+ user = context.get("user")
+ if user is None or not user.is_authenticated():
+ form = UnauthenticatedCommentForm(obj=obj)
+ else:
+ form = AuthenticatedCommentForm(obj=obj)
+ context[self.varname] = form
+ return ""
+
+
@register.tag
def comment_count(parser, token):
"""
@@ -73,3 +88,15 @@ def comments(parser, token):
{% comments obj as var %}
"""
return CommentsNode.handle_token(parser, token)
+
+@register.tag
+def comment_form(parser, token):
+ """
+ Usage:
+
+ {% comment_form obj as comment_form %}
+
+ Will read the `user` var out of the contex to know if the form should be
+ form an auth'd user or not.
+ """
+ return CommentFormNode.handle_token(parser, token)
View
@@ -6,6 +6,7 @@
from eldarion.test import TestCase
+from dialogos.forms import UnauthenticatedCommentForm, AuthenticatedCommentForm
from dialogos.models import Comment
@@ -91,3 +92,22 @@ def test_ttag_comments(self):
""
)
self.assertEqual(list(c["cs"]), list(Comment.objects.all()))
+
+ def test_ttag_comment_form(self):
+ g = User.objects.create(username="Sauron")
+ c = Context({"o": g})
+ self.assert_renders(
+ "{% load dialogos_tags %}{% comment_form o as comment_form %}",
+ c,
+ ""
+ )
+ self.assertTrue(isinstance(c["comment_form"], UnauthenticatedCommentForm))
+
+ with self.login("gimli", "gloin"):
+ c = Context({"o": g, "user": self.user})
+ self.assert_renders(
+ "{% load dialogos_tags %}{% comment_form o as comment_form %}",
+ c,
+ ""
+ )
+ self.assertTrue(isinstance(c["comment_form"], AuthenticatedCommentForm))

0 comments on commit 7b21939

Please sign in to comment.