Make the is_public field unchecked by default.
[matthijs/projects/xerxes.git] / influences / forms.py
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
6
7 #
8 # A few comment form classes, to handle the various cases (staff/no staff,
9 # reply to public/private post)
10
11 # It is probably possible to reduce this mess a bit using metaclasses, but I
12 # didn't get this to work yet.
13
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)
20
21     class Meta(ThreadedCommentForm.Meta):
22         exclude = ('markup', )
23
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)
29
30     class Meta(ThreadedCommentForm.Meta):
31         fields = ThreadedCommentForm.Meta.fields + ('is_public',)
32         exclude = ('markup', )
33
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
39
40     class Meta(ThreadedCommentForm.Meta):
41         exclude = ('markup', )
42
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
46     returned. """
47     allow_markup = False
48     allow_private = is_staff
49     if reply_to:
50         allow_public = reply_to.is_public
51     else:
52         allow_public = True
53     return _get_influence_comment_form(allow_markup, allow_public, allow_private)
54     
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
65     else:
66         raise Exception("Unsupported configuration")
67
68 class InfluenceForm(ContextModelForm):
69     class Meta:
70         model = Influence
71         fields = ('character', 'contact', 'summary', 'description')
72
73 class CharacterForm(ContextModelForm):
74     class Meta:
75         model = Character
76         fields = ('name')
77