Add new fields to InfluenceForm
[matthijs/projects/xerxes.git] / influences / forms.py
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
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     def __init__(self, *args, **kwargs):
27         super(AdminInfluenceCommentForm, self).__init__(*args, **kwargs)
28
29     class Meta(ThreadedCommentForm.Meta):
30         fields = ThreadedCommentForm.Meta.fields + ('is_public',)
31         exclude = ('markup', )
32
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
38
39     class Meta(ThreadedCommentForm.Meta):
40         exclude = ('markup', )
41
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
45     returned. """
46     allow_markup = False
47     allow_private = is_staff
48     if reply_to:
49         allow_public = reply_to.is_public
50     else:
51         allow_public = True
52     return _get_influence_comment_form(allow_markup, allow_public, allow_private)
53     
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
64     else:
65         raise Exception("Unsupported configuration")
66
67 class InfluenceForm(ContextModelForm):
68     class Meta:
69         model = Influence
70         fields = ('initiator', 'summary', 'other_characters', 'other_contacts', 'description')
71
72 class CharacterForm(ContextModelForm):
73     class Meta:
74         model = Character
75         fields = ('name')
76