Disallow staff to set the markup of new comments.
[matthijs/projects/xerxes.git] / influences / forms.py
1 from threadedcomments.forms import ThreadedCommentForm
2 from xerxes.tools.forms import ContextModelForm
3 from models import Influence, Character
4
5 #
6 # A few comment form classes, to handle the various cases (staff/no staff,
7 # reply to public/private post)
8
9 # It is probably possible to reduce this mess a bit using metaclasses, but I
10 # didn't get this to work yet.
11
12 class InfluenceCommentForm(ThreadedCommentForm):
13     def __init__(self, *args, **kwargs):
14         super(InfluenceCommentForm, self).__init__(*args, **kwargs)
15
16     class Meta(ThreadedCommentForm.Meta):
17         exclude = ('markup', )
18
19 class AdminInfluenceCommentForm(ThreadedCommentForm):
20     def __init__(self, *args, **kwargs):
21         super(AdminInfluenceCommentForm, self).__init__(*args, **kwargs)
22
23     class Meta(ThreadedCommentForm.Meta):
24         fields = ThreadedCommentForm.Meta.fields + ('is_public',)
25         exclude = ('markup', )
26
27 class AdminPrivateInfluenceCommentForm(ThreadedCommentForm):
28     def __init__(self, *args, **kwargs):
29         super(AdminPrivateInfluenceCommentForm, self).__init__(*args, **kwargs)
30         self.instance.is_public = False
31
32     class Meta(ThreadedCommentForm.Meta):
33         exclude = ('markup', )
34
35 def get_influence_comment_form(is_staff, reply_to):
36     """ Gets the form class that a user can use to reply to the given comment.
37     reply_to can be None, in which case the form class for a new comment is
38     returned. """
39     allow_markup = False
40     allow_private = is_staff
41     if reply_to:
42         allow_public = reply_to.is_public
43     else:
44         allow_public = True
45     return _get_influence_comment_form(allow_markup, allow_public, allow_private)
46     
47 def _get_influence_comment_form(allow_markup, allow_public, allow_private):
48     """ Internal wrapper that selects the right form class depending on the
49     given options. Should not be called directly, call
50     get_influence_comment_form instead. """
51     if not allow_markup and allow_public and allow_private:
52         return AdminInfluenceCommentForm
53     elif not allow_markup and not allow_public and allow_private:
54         return AdminPrivateInfluenceCommentForm
55     elif not allow_markup and allow_public and not allow_private:
56         return InfluenceCommentForm
57     else:
58         raise Exception("Unsupported configuration")
59
60 class InfluenceForm(ContextModelForm):
61     class Meta:
62         model = Influence
63         fields = ('character', 'contact', 'summary', 'description')
64
65 class CharacterForm(ContextModelForm):
66     class Meta:
67         model = Character
68         fields = ('name')
69