Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added a templatetag for getting the right formclass.

  • Loading branch information...
commit 7b219391238085d937d966f37c7f849ff07a94f0 1 parent c33034b
Alex Gaynor alex authored
2  dialogos/forms.py
@@ -7,7 +7,7 @@
7 7
8 8 class BaseCommentForm(forms.ModelForm):
9 9 def __init__(self, *args, **kwargs):
10   - self.request = kwargs.pop("request")
  10 + self.request = kwargs.pop("request", None)
11 11 self.obj = kwargs.pop("obj")
12 12 super(BaseCommentForm, self).__init__(*args, **kwargs)
13 13
27 dialogos/templatetags/dialogos_tags.py
@@ -2,6 +2,7 @@
2 2
3 3 from django.contrib.contenttypes.models import ContentType
4 4
  5 +from dialogos.forms import UnauthenticatedCommentForm, AuthenticatedCommentForm
5 6 from dialogos.models import Comment
6 7
7 8
@@ -54,6 +55,20 @@ def render(self, context):
54 55 return ""
55 56
56 57
  58 +class CommentFormNode(BaseCommentNode):
  59 + requires_as_var = False
  60 +
  61 + def render(self, context):
  62 + obj = self.obj.resolve(context)
  63 + user = context.get("user")
  64 + if user is None or not user.is_authenticated():
  65 + form = UnauthenticatedCommentForm(obj=obj)
  66 + else:
  67 + form = AuthenticatedCommentForm(obj=obj)
  68 + context[self.varname] = form
  69 + return ""
  70 +
  71 +
57 72 @register.tag
58 73 def comment_count(parser, token):
59 74 """
@@ -73,3 +88,15 @@ def comments(parser, token):
73 88 {% comments obj as var %}
74 89 """
75 90 return CommentsNode.handle_token(parser, token)
  91 +
  92 +@register.tag
  93 +def comment_form(parser, token):
  94 + """
  95 + Usage:
  96 +
  97 + {% comment_form obj as comment_form %}
  98 +
  99 + Will read the `user` var out of the contex to know if the form should be
  100 + form an auth'd user or not.
  101 + """
  102 + return CommentFormNode.handle_token(parser, token)
20 dialogos/tests.py
@@ -6,6 +6,7 @@
6 6
7 7 from eldarion.test import TestCase
8 8
  9 +from dialogos.forms import UnauthenticatedCommentForm, AuthenticatedCommentForm
9 10 from dialogos.models import Comment
10 11
11 12
@@ -91,3 +92,22 @@ def test_ttag_comments(self):
91 92 ""
92 93 )
93 94 self.assertEqual(list(c["cs"]), list(Comment.objects.all()))
  95 +
  96 + def test_ttag_comment_form(self):
  97 + g = User.objects.create(username="Sauron")
  98 + c = Context({"o": g})
  99 + self.assert_renders(
  100 + "{% load dialogos_tags %}{% comment_form o as comment_form %}",
  101 + c,
  102 + ""
  103 + )
  104 + self.assertTrue(isinstance(c["comment_form"], UnauthenticatedCommentForm))
  105 +
  106 + with self.login("gimli", "gloin"):
  107 + c = Context({"o": g, "user": self.user})
  108 + self.assert_renders(
  109 + "{% load dialogos_tags %}{% comment_form o as comment_form %}",
  110 + c,
  111 + ""
  112 + )
  113 + self.assertTrue(isinstance(c["comment_form"], AuthenticatedCommentForm))

0 comments on commit 7b21939

Please sign in to comment.
Something went wrong with that request. Please try again.