diff 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
line wrap: on
line diff
--- a/gpp/forums/forms.py	Wed Jan 05 04:09:35 2011 +0000
+++ b/gpp/forums/forms.py	Sat Jan 08 22:04:54 2011 +0000
@@ -155,11 +155,18 @@
                 ('js/forums.js', ))
 
     def __init__(self, *args, **kwargs):
+        topic_name = kwargs.pop('topic_name', None)
         super(PostForm, self).__init__(*args, **kwargs)
 
+        if topic_name is not None:  # this is a "first post"
+            self.fields.insert(0, 'name', forms.CharField(label='Subject',
+                    max_length=255,
+                    widget=forms.TextInput(attrs={'size': 64})))
+            self.initial['name'] = topic_name
+
         attachments = args[0].getlist('attachment') if len(args) else []
         self.attach_proc = AttachmentProcessor(attachments)
-        
+
         # If this form is being used to edit an existing post, and that post
         # has attachments, create a hidden post_id field. The client-side
         # AJAX will use this as a cue to retrieve the HTML for the embedded
@@ -176,6 +183,17 @@
             raise forms.ValidationError('This field is required.')
         return data
 
+    def save(self, *args, **kwargs):
+        commit = kwargs.get('commit', False)
+        post = super(PostForm, self).save(*args, **kwargs)
+
+        # Are we saving a "first post"?
+        if 'name' in self.cleaned_data:
+            post.topic.name = self.cleaned_data['name']
+            if commit:
+                post.topic.save()
+        return post
+
 
 class MoveTopicForm(forms.Form):
     """