1 from django.forms.fields import CharField
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 def __init__(self, *args, **kwargs):
27 super(AdminInfluenceCommentForm, self).__init__(*args, **kwargs)
29 class Meta(ThreadedCommentForm.Meta):
30 fields = ThreadedCommentForm.Meta.fields + ('is_public',)
31 exclude = ('markup', )
33 class AdminPrivateInfluenceCommentForm(ThreadedCommentForm):
34 comment = CharField(widget=Textarea(attrs={'cols' : 80}))
35 def __init__(self, *args, **kwargs):
36 super(AdminPrivateInfluenceCommentForm, self).__init__(*args, **kwargs)
37 self.instance.is_public = False
39 class Meta(ThreadedCommentForm.Meta):
40 exclude = ('markup', )
42 def get_influence_comment_form(is_staff, reply_to):
43 """ Gets the form class that a user can use to reply to the given comment.
44 reply_to can be None, in which case the form class for a new comment is
47 allow_private = is_staff
49 allow_public = reply_to.is_public
52 return _get_influence_comment_form(allow_markup, allow_public, allow_private)
54 def _get_influence_comment_form(allow_markup, allow_public, allow_private):
55 """ Internal wrapper that selects the right form class depending on the
56 given options. Should not be called directly, call
57 get_influence_comment_form instead. """
58 if not allow_markup and allow_public and allow_private:
59 return AdminInfluenceCommentForm
60 elif not allow_markup and not allow_public and allow_private:
61 return AdminPrivateInfluenceCommentForm
62 elif not allow_markup and allow_public and not allow_private:
63 return InfluenceCommentForm
65 raise Exception("Unsupported configuration")
67 class InfluenceForm(ContextModelForm):
70 fields = ('character', 'contact', 'summary', 'description')
72 class CharacterForm(ContextModelForm):