1 from django.forms.fields import CharField, BooleanField
2 from django.forms.widgets import Textarea
3 from django.forms.models import ModelMultipleChoiceField
4 from threadedcomments.forms import ThreadedCommentForm
5 from xerxes.tools.forms import ContextModelForm
6 from xerxes.tools.widgets import DropDownMultiple
7 from models import Influence, Character
10 # A few comment form classes, to handle the various cases (staff/no staff,
11 # reply to public/private post)
13 # It is probably possible to reduce this mess a bit using metaclasses, but I
14 # didn't get this to work yet.
16 class InfluenceCommentForm(ThreadedCommentForm):
17 # Force the textare to 80 columns. This is really a hack, we should
18 # rather create a template tag to do this at the template level.
19 comment = CharField(widget=Textarea(attrs={'cols' : 80}))
20 def __init__(self, *args, **kwargs):
21 super(InfluenceCommentForm, self).__init__(*args, **kwargs)
23 class Meta(ThreadedCommentForm.Meta):
24 exclude = ('markup', )
26 class AdminInfluenceCommentForm(ThreadedCommentForm):
27 comment = CharField(widget=Textarea(attrs={'cols' : 80}))
28 is_public = BooleanField(required=False, initial=False)
29 def __init__(self, *args, **kwargs):
30 super(AdminInfluenceCommentForm, self).__init__(*args, **kwargs)
32 class Meta(ThreadedCommentForm.Meta):
33 fields = ThreadedCommentForm.Meta.fields + ('is_public',)
34 exclude = ('markup', )
36 class AdminPrivateInfluenceCommentForm(ThreadedCommentForm):
37 comment = CharField(widget=Textarea(attrs={'cols' : 80}))
38 def __init__(self, *args, **kwargs):
39 super(AdminPrivateInfluenceCommentForm, self).__init__(*args, **kwargs)
40 self.instance.is_public = False
42 class Meta(ThreadedCommentForm.Meta):
43 exclude = ('markup', )
45 def get_influence_comment_form(is_staff, reply_to):
46 """ Gets the form class that a user can use to reply to the given comment.
47 reply_to can be None, in which case the form class for a new comment is
50 allow_private = is_staff
52 allow_public = reply_to.is_public
55 return _get_influence_comment_form(allow_markup, allow_public, allow_private)
57 def _get_influence_comment_form(allow_markup, allow_public, allow_private):
58 """ Internal wrapper that selects the right form class depending on the
59 given options. Should not be called directly, call
60 get_influence_comment_form instead. """
61 if not allow_markup and allow_public and allow_private:
62 return AdminInfluenceCommentForm
63 elif not allow_markup and not allow_public and allow_private:
64 return AdminPrivateInfluenceCommentForm
65 elif not allow_markup and allow_public and not allow_private:
66 return InfluenceCommentForm
68 raise Exception("Unsupported configuration")
70 class InfluenceForm(ContextModelForm):
71 # Manually define this field so we can select the DropDownMultiple
72 # widget. However, we leave the queryset empty, which characters can
73 # be selected depends on the logged in user and should be set by
74 # setting the choices property in the view.
75 other_characters = ModelMultipleChoiceField(queryset=Character.objects.none(), widget=DropDownMultiple)
78 fields = ('initiator', 'summary', 'other_characters', 'other_contacts', 'description')
80 class CharacterForm(ContextModelForm):
83 fields = ('name', 'type')