changeset 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 254db4cb6a86
children 0eed8161ca39
files gpp/forums/forms.py gpp/forums/views/main.py
diffstat 2 files changed, 30 insertions(+), 3 deletions(-) [+]
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):
     """
--- a/gpp/forums/views/main.py	Wed Jan 05 04:09:35 2011 +0000
+++ b/gpp/forums/views/main.py	Sat Jan 08 22:04:54 2011 +0000
@@ -364,8 +364,13 @@
     if not can_edit:
         return HttpResponseForbidden("You don't have permission to edit that post.")
 
+    topic_name = None
+    first_post = Post.objects.filter(topic=post.topic).order_by('creation_date')[0]
+    if first_post.id == post.id:
+        topic_name = post.topic.name
+
     if request.method == "POST":
-        form = PostForm(request.POST, instance=post)
+        form = PostForm(request.POST, instance=post, topic_name=topic_name)
         if form.is_valid():
             if antispam.utils.spam_check(request, form.cleaned_data['body']):
                 return HttpResponseRedirect(reverse('antispam-suspended'))
@@ -373,12 +378,16 @@
             post.touch()
             post.save()
 
+            # if we are editing a first post, save the parent topic as well
+            if topic_name:
+                post.topic.save()
+
             # Save any attachments
             form.attach_proc.save_attachments(post)
 
             return HttpResponseRedirect(post.get_absolute_url())
     else:
-        form = PostForm(instance=post)
+        form = PostForm(instance=post, topic_name=topic_name)
 
     post.user_profile = request.user.get_profile()