1 from django.forms.fields import CharField, BooleanField
2 from django.forms.widgets import Textarea
3 from threadedcomments.forms import ThreadedCommentForm
4 from xerxes.tools.forms import ContextModelForm
5 from models import Influence, Character
8 # A few comment form classes, to handle the various cases (staff/no staff,
9 # reply to public/private post)
11 # It is probably possible to reduce this mess a bit using metaclasses, but I
12 # didn't get this to work yet.
14 class InfluenceCommentForm(ThreadedCommentForm):
15 # Force the textare to 80 columns. This is really a hack, we should
16 # rather create a template tag to do this at the template level.
17 comment = CharField(widget=Textarea(attrs={'cols' : 80}))
18 def __init__(self, *args, **kwargs):
19 super(InfluenceCommentForm, self).__init__(*args, **kwargs)
21 class Meta(ThreadedCommentForm.Meta):
22 exclude = ('markup', )
24 class AdminInfluenceCommentForm(ThreadedCommentForm):
25 comment = CharField(widget=Textarea(attrs={'cols' : 80}))
26 is_public = BooleanField(required=False, initial=False)
27 def __init__(self, *args, **kwargs):
28 super(AdminInfluenceCommentForm, self).__init__(*args, **kwargs)
30 class Meta(ThreadedCommentForm.Meta):
31 fields = ThreadedCommentForm.Meta.fields + ('is_public',)
32 exclude = ('markup', )
34 class AdminPrivateInfluenceCommentForm(ThreadedCommentForm):
35 comment = CharField(widget=Textarea(attrs={'cols' : 80}))
36 def __init__(self, *args, **kwargs):
37 super(AdminPrivateInfluenceCommentForm, self).__init__(*args, **kwargs)
38 self.instance.is_public = False
40 class Meta(ThreadedCommentForm.Meta):
41 exclude = ('markup', )
43 def get_influence_comment_form(is_staff, reply_to):
44 """ Gets the form class that a user can use to reply to the given comment.
45 reply_to can be None, in which case the form class for a new comment is
48 allow_private = is_staff
50 allow_public = reply_to.is_public
53 return _get_influence_comment_form(allow_markup, allow_public, allow_private)
55 def _get_influence_comment_form(allow_markup, allow_public, allow_private):
56 """ Internal wrapper that selects the right form class depending on the
57 given options. Should not be called directly, call
58 get_influence_comment_form instead. """
59 if not allow_markup and allow_public and allow_private:
60 return AdminInfluenceCommentForm
61 elif not allow_markup and not allow_public and allow_private:
62 return AdminPrivateInfluenceCommentForm
63 elif not allow_markup and allow_public and not allow_private:
64 return InfluenceCommentForm
66 raise Exception("Unsupported configuration")
68 class InfluenceForm(ContextModelForm):
71 fields = ('character', 'contact', 'summary', 'description')
73 class CharacterForm(ContextModelForm):