root/lib/sct/sphenecoll/sphene/sphboard/forms.py

Revision 146:cf11e5504b2e, 5.4 kB (checked in by Daniele Varrazzo <piro@develer.com>, 1 year ago)

First implementation of private messages.

Line 
1 """
2 Collection of forms for the board application
3
4
5 i have created this module quite late, so most forms are still
6 in views.py
7 """
8
9 from django import forms
10 from django.utils.translation import string_concat
11 from django.utils.translation import ugettext_lazy as _
12
13 from sphene.community import sphutils
14 from sphene.community.middleware import get_current_user
15
16 from sphene.sphboard.models import Poll, PollChoice, Post
17 from sphene.sphboard.renderers import describe_render_choices
18 from sphene.sphboard.models import POST_MARKUP_CHOICES, PostAttachment
19 from sphene.sphboard.widgets import UsersField, IconSelect
20 from sphene.sphboard import boardforms
21
22
23 class PollForm(forms.ModelForm):
24
25     class Meta:
26         model = Poll
27
28
29 class PollChoiceForm(forms.ModelForm):
30
31     class Meta:
32         model = PollChoice
33         exclude = ('count',)
34
35
36 class PostModelForm(forms.ModelForm):
37     """
38     a new version of the sphene.sphboard.views.PostForm class ..
39     still in development, not usable yet.
40     """
41
42     def __init__(self, *args, **kwargs):
43         super(PostModelForm, self).__init__(*args, **kwargs)
44         #if not sphutils.has_captcha_support() or get_current_user().is_authenticated():
45         #    del self.fields['captcha']
46
47         # if there is no choice for 'markup' .. don't show it.
48         # (we compare to len == 2 because the first option is always '----------')
49         if len( self.fields['markup'].widget.choices ) == 2: #POST_MARKUP_CHOICES ) == 1:
50             del self.fields['markup']
51
52
53     class Meta:
54         model = Post
55
56
57 class PostForm(forms.Form):
58     subject = forms.CharField( label = _(u"Subject" ) )
59     to = UsersField( label = _(u"To" ) )
60     icon = forms.CharField(label=_(u"Post Icon"), widget = IconSelect())
61     body = forms.CharField( label = _(u"Body"),
62                             widget = forms.Textarea( attrs = { 'rows': 10, 'cols': 70 } ),
63                             help_text = describe_render_choices(), )
64     markup = forms.CharField( label=_('Markup'),
65                               widget = forms.Select( choices = POST_MARKUP_CHOICES, ) )
66     captcha = sphutils.CaptchaField(label=_('Captcha'),
67                                     widget=sphutils.CaptchaWidget,
68                                     help_text = _(u'Please enter the result of the above calculation.'),
69                                     )
70
71     def __init__(self, *args, **kwargs):
72         super(PostForm, self).__init__(*args, **kwargs)
73         if not self.has_recipients():
74             del self.fields['to']
75         if not sphutils.has_captcha_support() or get_current_user().is_authenticated():
76             del self.fields['captcha']
77         if len( POST_MARKUP_CHOICES ) == 1:
78             del self.fields['markup']
79
80     def has_recipients(self):
81         return False
82
83     def init_for_category_type(self, category_type, post):
84         """
85         Called after initialization with the category type instance.
86
87         Arguments:
88         category_type: the category_type instance of the category.
89         post: the post which is edited (if any)
90         """
91         pass
92
93
94 class PrivatePostForm(PostForm):
95     def has_recipients(self):
96         return True
97
98
99 class PostPollForm(forms.Form):
100     question = forms.CharField( label = _( u'Question' ) )
101     answers = forms.CharField( label = _( u'Answers (1 per line)' ),
102                                widget = forms.Textarea( attrs = { 'rows': 5, 'cols': 80 } ) )
103     choicesPerUser = forms.IntegerField( label = _(u'Allowed Choices per User'),
104                                          help_text = _(u'Enter how many answers a user can select.'),
105                                          min_value = 1,
106                                          max_value = 100,
107                                          initial = 1, )
108
109
110 class PostAttachmentForm(forms.ModelForm):
111     class Meta:
112         model = PostAttachment
113         fields = ('fileupload',)
114
115
116 class AnnotateForm(forms.Form):
117     body = forms.CharField( label=_(u'Body'), widget = forms.Textarea( attrs = { 'rows': 10,
118                                                                'cols': 80, }, ),
119                                                      help_text = describe_render_choices(), )
120     markup = forms.CharField( label=_('Markup'),
121                               widget = forms.Select( choices = POST_MARKUP_CHOICES, ) )
122     hide_post = forms.BooleanField( label=_('Hide Post'), required = False )
123
124     def __init__(self, *args, **kwargs):
125         super(AnnotateForm, self).__init__(*args, **kwargs)
126         if len( POST_MARKUP_CHOICES ) == 1:
127             del self.fields['markup']
128
129     def clean(self):
130         if 'markup' not in self.cleaned_data and len( POST_MARKUP_CHOICES ):
131             self.cleaned_data['markup'] = POST_MARKUP_CHOICES[0][0]
132
133         return self.cleaned_data
134
135
136 class MoveForm(forms.Form):
137     """
138     A basic form which allows a user to select a target
139     category.
140
141     This should not be used allown, but in stead use
142     MoveAndAnnotateForm
143     """
144     category = boardforms.SelectCategoryField(label = _(u'Category'),
145                                               help_text = _(u'Select target category'))
146
147
148 class MoveAndAnnotateForm(MoveForm, AnnotateForm):
149
150
151     def __init__(self, *args, **kwargs):
152         super(MoveAndAnnotateForm, self).__init__(*args, **kwargs)
153
154         del self.fields['hide_post']
155
156         self.fields['body'].help_text = string_concat(_(u'Please describe why this thread had to be moved.'), ' ', self.fields['body'].help_text)
Note: See TracBrowser for help on using the browser.