7a222e2324daaa2087b8a3efa91cdfaf621d3d5a
[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
26 class AdminPrivateInfluenceCommentForm(ThreadedCommentForm):
27     def __init__(self, *args, **kwargs):
28         super(AdminPrivateInfluenceCommentForm, self).__init__(*args, **kwargs)
29         self.instance.is_public = False
30
31 def get_influence_comment_form(is_staff, reply_to):
32     """ Gets the form class that a user can use to reply to the given comment.
33     reply_to can be None, in which case the form class for a new comment is
34     returned. """
35     allow_markup = allow_private = is_staff
36     if reply_to:
37         allow_public = reply_to.is_public
38     else:
39         allow_public = True
40     return _get_influence_comment_form(allow_markup, allow_public, allow_private)
41     
42 def _get_influence_comment_form(allow_markup, allow_public, allow_private):
43     """ Internal wrapper that selects the right form class depending on the
44     given options. Should not be called directly, call
45     get_influence_comment_form instead. """
46     if allow_markup and allow_public and allow_private:
47         return AdminInfluenceCommentForm
48     elif allow_markup and not allow_public and allow_private:
49         return AdminPrivateInfluenceCommentForm
50     elif not allow_markup and allow_public and not allow_private:
51         return InfluenceCommentForm
52     else:
53         raise Exception("Unsupported configuration")
54
55 class InfluenceForm(ContextModelForm):
56     class Meta:
57         model = Influence
58         fields = ('character', 'contact', 'summary', 'description')
59
60 class CharacterForm(ContextModelForm):
61     class Meta:
62         model = Character
63         fields = ('name')
64