diff gpp/forums/forms.py @ 102:e67c4dd98db5

Forums: new topic form sprouts boolean fields for sticky and locking if the user has rights. Implemented the locked logic. Fixed a bug where topics where getting out of order (the view_count was bumping the update_date because of auto_now).
author Brian Neal <bgneal@gmail.com>
date Wed, 16 Sep 2009 02:01:57 +0000
parents eb9f99382476
children cb72577785df
line wrap: on
line diff
--- a/gpp/forums/forms.py	Wed Sep 16 00:39:27 2009 +0000
+++ b/gpp/forums/forms.py	Wed Sep 16 02:01:57 2009 +0000
@@ -33,7 +33,7 @@
     def clean_topic_id(self):
         id = self.cleaned_data['topic_id']
         try:
-            self.topic = Topic.objects.get(pk=id)
+            self.topic = Topic.objects.select_related().get(pk=id)
         except Topic.DoesNotExist:
             raise forms.ValidationError('invalid topic')
         return id 
@@ -50,10 +50,17 @@
 
 
 class NewTopicForm(forms.Form):
-    """Form for creating a new topic and 1st post to that topic."""
+    """
+    Form for creating a new topic and 1st post to that topic.
+    Superusers and moderators can also create the topic as a sticky or initially
+    locked.
+    """
     name = forms.CharField(label='Subject', max_length=255,
             widget=forms.TextInput(attrs={'size': 64}))
     body = forms.CharField(label='', widget=forms.Textarea)
+    user = None
+    forum = None
+    has_mod_fields = False
 
     class Media:
         css = {
@@ -62,21 +69,33 @@
         js = settings.GPP_THIRD_PARTY_JS['markitup'] + \
             ('js/forums.js', )
 
-    def save(self, forum, user, ip=None):
+    def __init__(self, user, forum, *args, **kwargs):
+        super(NewTopicForm, self).__init__(*args, **kwargs)
+        self.user = user
+        self.forum = forum
+
+        if user.is_superuser or user in forum.moderators.all():
+            self.fields['sticky'] = forms.BooleanField(required=False)
+            self.fields['locked'] = forms.BooleanField(required=False)
+            self.has_mod_fields = True
+
+    def save(self, ip=None):
         """
         Creates the new Topic and first Post from the form data and supplied
         arguments.
         """
-        topic = Topic(forum=forum,
+        topic = Topic(forum=self.forum,
                 name=self.cleaned_data['name'],
-                user=user)
+                user=self.user,
+                sticky=self.has_mod_fields and self.cleaned_data['sticky'],
+                locked=self.has_mod_fields and self.cleaned_data['locked'])
         topic.save()
 
         post = Post(topic=topic,
-                user=user,
+                user=self.user,
                 body=self.cleaned_data['body'],
                 user_ip=ip)
         post.save()
 
-        bump_post_count(user)
+        bump_post_count(self.user)
         return topic