bgneal@83
|
1 """
|
bgneal@83
|
2 Forms for the forums application.
|
bgneal@83
|
3 """
|
bgneal@83
|
4 from django import forms
|
bgneal@95
|
5 from django.conf import settings
|
bgneal@83
|
6
|
bgneal@110
|
7 from forums.models import Forum
|
bgneal@83
|
8 from forums.models import Topic
|
bgneal@83
|
9 from forums.models import Post
|
bgneal@285
|
10 from forums.attachments import AttachmentProcessor
|
bgneal@83
|
11
|
bgneal@83
|
12
|
bgneal@106
|
13 class NewPostForm(forms.Form):
|
bgneal@86
|
14 """Form for creating a new post."""
|
bgneal@135
|
15 body = forms.CharField(label='',
|
bgneal@135
|
16 widget=forms.Textarea(attrs={'class': 'markItUp smileyTarget'}))
|
bgneal@89
|
17 topic_id = forms.IntegerField(widget=forms.HiddenInput)
|
bgneal@89
|
18 topic = None
|
bgneal@83
|
19
|
bgneal@89
|
20 class Media:
|
bgneal@95
|
21 css = {
|
bgneal@123
|
22 'all': (settings.GPP_THIRD_PARTY_CSS['markitup'] +
|
bgneal@123
|
23 settings.GPP_THIRD_PARTY_CSS['jquery-ui']),
|
bgneal@95
|
24 }
|
bgneal@123
|
25 js = (settings.GPP_THIRD_PARTY_JS['markitup'] +
|
bgneal@123
|
26 settings.GPP_THIRD_PARTY_JS['jquery-ui'] +
|
bgneal@123
|
27 ('js/forums.js', ))
|
bgneal@89
|
28
|
bgneal@285
|
29 def __init__(self, *args, **kwargs):
|
bgneal@285
|
30 super(NewPostForm, self).__init__(*args, **kwargs)
|
bgneal@285
|
31 attachments = args[0].getlist('attachment') if len(args) else []
|
bgneal@285
|
32 self.attach_proc = AttachmentProcessor(attachments)
|
bgneal@285
|
33
|
bgneal@89
|
34 def clean_topic_id(self):
|
bgneal@89
|
35 id = self.cleaned_data['topic_id']
|
bgneal@89
|
36 try:
|
bgneal@102
|
37 self.topic = Topic.objects.select_related().get(pk=id)
|
bgneal@89
|
38 except Topic.DoesNotExist:
|
bgneal@89
|
39 raise forms.ValidationError('invalid topic')
|
bgneal@89
|
40 return id
|
bgneal@89
|
41
|
bgneal@89
|
42 def save(self, user, ip=None):
|
bgneal@86
|
43 """
|
bgneal@86
|
44 Creates a new post from the form data and supplied arguments.
|
bgneal@86
|
45 """
|
bgneal@89
|
46 post = Post(topic=self.topic, user=user, body=self.cleaned_data['body'],
|
bgneal@89
|
47 user_ip=ip)
|
bgneal@86
|
48 post.save()
|
bgneal@285
|
49 self.attach_proc.save_attachments(post)
|
bgneal@89
|
50 return post
|
bgneal@83
|
51
|
bgneal@83
|
52
|
bgneal@83
|
53 class NewTopicForm(forms.Form):
|
bgneal@102
|
54 """
|
bgneal@102
|
55 Form for creating a new topic and 1st post to that topic.
|
bgneal@102
|
56 Superusers and moderators can also create the topic as a sticky or initially
|
bgneal@102
|
57 locked.
|
bgneal@102
|
58 """
|
bgneal@95
|
59 name = forms.CharField(label='Subject', max_length=255,
|
bgneal@95
|
60 widget=forms.TextInput(attrs={'size': 64}))
|
bgneal@135
|
61 body = forms.CharField(label='',
|
bgneal@135
|
62 widget=forms.Textarea(attrs={'class': 'markItUp smileyTarget'}))
|
bgneal@102
|
63 user = None
|
bgneal@102
|
64 forum = None
|
bgneal@102
|
65 has_mod_fields = False
|
bgneal@83
|
66
|
bgneal@95
|
67 class Media:
|
bgneal@95
|
68 css = {
|
bgneal@123
|
69 'all': (settings.GPP_THIRD_PARTY_CSS['markitup'] +
|
bgneal@123
|
70 settings.GPP_THIRD_PARTY_CSS['jquery-ui']),
|
bgneal@95
|
71 }
|
bgneal@123
|
72 js = (settings.GPP_THIRD_PARTY_JS['markitup'] +
|
bgneal@123
|
73 settings.GPP_THIRD_PARTY_JS['jquery-ui'] +
|
bgneal@123
|
74 ('js/forums.js', ))
|
bgneal@95
|
75
|
bgneal@102
|
76 def __init__(self, user, forum, *args, **kwargs):
|
bgneal@102
|
77 super(NewTopicForm, self).__init__(*args, **kwargs)
|
bgneal@102
|
78 self.user = user
|
bgneal@102
|
79 self.forum = forum
|
bgneal@102
|
80
|
bgneal@102
|
81 if user.is_superuser or user in forum.moderators.all():
|
bgneal@102
|
82 self.fields['sticky'] = forms.BooleanField(required=False)
|
bgneal@102
|
83 self.fields['locked'] = forms.BooleanField(required=False)
|
bgneal@102
|
84 self.has_mod_fields = True
|
bgneal@102
|
85
|
bgneal@285
|
86 attachments = args[0].getlist('attachment') if len(args) else []
|
bgneal@285
|
87 self.attach_proc = AttachmentProcessor(attachments)
|
bgneal@285
|
88
|
bgneal@102
|
89 def save(self, ip=None):
|
bgneal@83
|
90 """
|
bgneal@83
|
91 Creates the new Topic and first Post from the form data and supplied
|
bgneal@86
|
92 arguments.
|
bgneal@83
|
93 """
|
bgneal@102
|
94 topic = Topic(forum=self.forum,
|
bgneal@83
|
95 name=self.cleaned_data['name'],
|
bgneal@102
|
96 user=self.user,
|
bgneal@102
|
97 sticky=self.has_mod_fields and self.cleaned_data['sticky'],
|
bgneal@102
|
98 locked=self.has_mod_fields and self.cleaned_data['locked'])
|
bgneal@83
|
99 topic.save()
|
bgneal@83
|
100
|
bgneal@83
|
101 post = Post(topic=topic,
|
bgneal@102
|
102 user=self.user,
|
bgneal@83
|
103 body=self.cleaned_data['body'],
|
bgneal@83
|
104 user_ip=ip)
|
bgneal@83
|
105 post.save()
|
bgneal@285
|
106
|
bgneal@285
|
107 self.attach_proc.save_attachments(post)
|
bgneal@285
|
108
|
bgneal@83
|
109 return topic
|
bgneal@106
|
110
|
bgneal@106
|
111
|
bgneal@106
|
112 class PostForm(forms.ModelForm):
|
bgneal@106
|
113 """
|
bgneal@108
|
114 Form for editing an existing post or a new, non-quick post.
|
bgneal@106
|
115 """
|
bgneal@135
|
116 body = forms.CharField(label='',
|
bgneal@135
|
117 widget=forms.Textarea(attrs={'class': 'markItUp smileyTarget'}))
|
bgneal@106
|
118
|
bgneal@106
|
119 class Meta:
|
bgneal@106
|
120 model = Post
|
bgneal@106
|
121 fields = ('body', )
|
bgneal@106
|
122
|
bgneal@106
|
123 class Media:
|
bgneal@106
|
124 css = {
|
bgneal@123
|
125 'all': (settings.GPP_THIRD_PARTY_CSS['markitup'] +
|
bgneal@123
|
126 settings.GPP_THIRD_PARTY_CSS['jquery-ui']),
|
bgneal@106
|
127 }
|
bgneal@123
|
128 js = (settings.GPP_THIRD_PARTY_JS['markitup'] +
|
bgneal@123
|
129 settings.GPP_THIRD_PARTY_JS['jquery-ui'] +
|
bgneal@123
|
130 ('js/forums.js', ))
|
bgneal@110
|
131
|
bgneal@110
|
132
|
bgneal@110
|
133 class MoveTopicForm(forms.Form):
|
bgneal@111
|
134 """
|
bgneal@111
|
135 Form for a moderator to move a topic to a forum.
|
bgneal@111
|
136 """
|
bgneal@111
|
137 forums = forms.ModelChoiceField(label='Move to forum',
|
bgneal@111
|
138 queryset=Forum.objects.none())
|
bgneal@110
|
139
|
bgneal@111
|
140 def __init__(self, user, *args, **kwargs):
|
bgneal@111
|
141 hide_label = kwargs.pop('hide_label', False)
|
bgneal@111
|
142 required = kwargs.pop('required', True)
|
bgneal@111
|
143 super(MoveTopicForm, self).__init__(*args, **kwargs)
|
bgneal@111
|
144 self.fields['forums'].queryset = \
|
bgneal@111
|
145 Forum.objects.forums_for_user(user).order_by('name')
|
bgneal@111
|
146 if hide_label:
|
bgneal@111
|
147 self.fields['forums'].label = ''
|
bgneal@111
|
148 self.fields['forums'].required = required
|
bgneal@110
|
149
|
bgneal@115
|
150
|
bgneal@115
|
151 class SplitTopicForm(forms.Form):
|
bgneal@115
|
152 """
|
bgneal@115
|
153 Form for a moderator to split posts from a topic to a new topic.
|
bgneal@115
|
154 """
|
bgneal@115
|
155 name = forms.CharField(label='New topic title', max_length=255,
|
bgneal@115
|
156 widget=forms.TextInput(attrs={'size': 64}))
|
bgneal@115
|
157 forums = forms.ModelChoiceField(label='Forum for new topic',
|
bgneal@115
|
158 queryset=Forum.objects.none())
|
bgneal@115
|
159 post_ids = []
|
bgneal@115
|
160 split_at = False
|
bgneal@115
|
161
|
bgneal@115
|
162 def __init__(self, user, *args, **kwargs):
|
bgneal@115
|
163 super(SplitTopicForm, self).__init__(*args, **kwargs)
|
bgneal@115
|
164 self.fields['forums'].queryset = \
|
bgneal@115
|
165 Forum.objects.forums_for_user(user).order_by('name')
|
bgneal@115
|
166
|
bgneal@115
|
167 def clean(self):
|
bgneal@115
|
168 self.post_ids = self.data.getlist('post_ids')
|
bgneal@115
|
169 if len(self.post_ids) == 0:
|
bgneal@115
|
170 raise forms.ValidationError('Please select some posts')
|
bgneal@115
|
171
|
bgneal@115
|
172 self.split_at = 'split-at' in self.data
|
bgneal@115
|
173 if self.split_at and len(self.post_ids) > 1:
|
bgneal@115
|
174 raise forms.ValidationError('Please select only one post to split the topic at')
|
bgneal@115
|
175
|
bgneal@115
|
176 return self.cleaned_data
|