comparison gpp/forums/forms.py @ 295:26fc9ac9a0eb

Fixing #133; add the ability to edit a topic's title when you edit the first post in that topic.
author Brian Neal <bgneal@gmail.com>
date Sat, 08 Jan 2011 22:04:54 +0000
parents 72fd300685d5
children 2ff5f4c1476d
comparison
equal deleted inserted replaced
294:254db4cb6a86 295:26fc9ac9a0eb
153 js = (settings.GPP_THIRD_PARTY_JS['markitup'] + 153 js = (settings.GPP_THIRD_PARTY_JS['markitup'] +
154 settings.GPP_THIRD_PARTY_JS['jquery-ui'] + 154 settings.GPP_THIRD_PARTY_JS['jquery-ui'] +
155 ('js/forums.js', )) 155 ('js/forums.js', ))
156 156
157 def __init__(self, *args, **kwargs): 157 def __init__(self, *args, **kwargs):
158 topic_name = kwargs.pop('topic_name', None)
158 super(PostForm, self).__init__(*args, **kwargs) 159 super(PostForm, self).__init__(*args, **kwargs)
160
161 if topic_name is not None: # this is a "first post"
162 self.fields.insert(0, 'name', forms.CharField(label='Subject',
163 max_length=255,
164 widget=forms.TextInput(attrs={'size': 64})))
165 self.initial['name'] = topic_name
159 166
160 attachments = args[0].getlist('attachment') if len(args) else [] 167 attachments = args[0].getlist('attachment') if len(args) else []
161 self.attach_proc = AttachmentProcessor(attachments) 168 self.attach_proc = AttachmentProcessor(attachments)
162 169
163 # If this form is being used to edit an existing post, and that post 170 # If this form is being used to edit an existing post, and that post
164 # has attachments, create a hidden post_id field. The client-side 171 # has attachments, create a hidden post_id field. The client-side
165 # AJAX will use this as a cue to retrieve the HTML for the embedded 172 # AJAX will use this as a cue to retrieve the HTML for the embedded
166 # media. 173 # media.
167 if 'instance' in kwargs: 174 if 'instance' in kwargs:
174 data = self.cleaned_data['body'] 181 data = self.cleaned_data['body']
175 if not data and not self.attach_proc.has_attachments(): 182 if not data and not self.attach_proc.has_attachments():
176 raise forms.ValidationError('This field is required.') 183 raise forms.ValidationError('This field is required.')
177 return data 184 return data
178 185
186 def save(self, *args, **kwargs):
187 commit = kwargs.get('commit', False)
188 post = super(PostForm, self).save(*args, **kwargs)
189
190 # Are we saving a "first post"?
191 if 'name' in self.cleaned_data:
192 post.topic.name = self.cleaned_data['name']
193 if commit:
194 post.topic.save()
195 return post
196
179 197
180 class MoveTopicForm(forms.Form): 198 class MoveTopicForm(forms.Form):
181 """ 199 """
182 Form for a moderator to move a topic to a forum. 200 Form for a moderator to move a topic to a forum.
183 """ 201 """