Browse Source

Use moderation.* api to change thread state when replying/editing

Rafał Pitoń 10 years ago
parent
commit
65c63ba13a

+ 10 - 1
misago/threads/posting/threadclose.py

@@ -1,3 +1,4 @@
+from misago.threads import moderation
 from misago.threads.forms.posting import ThreadCloseForm
 from misago.threads.posting import PostingMiddleware, START
 
@@ -18,7 +19,15 @@ class ThreadCloseFormMiddleware(PostingMiddleware):
             return ThreadCloseForm(prefix=self.prefix, initial=initial)
 
     def pre_save(self, form):
-        if form.is_valid():
+        if form.is_valid() and self.mode == START:
             if self.thread_is_closed != form.cleaned_data.get('is_closed'):
                 self.thread.is_closed = form.cleaned_data.get('is_closed')
                 self.thread.update_fields.append('is_closed')
+
+    def post_save(self, form):
+        if form.is_valid() and self.mode != START:
+            if self.thread_is_closed != form.cleaned_data.get('is_closed'):
+                if self.thread.is_closed:
+                    moderation.open_thread(self.user, self.thread)
+                else:
+                    moderation.close_thread(self.user, self.thread)

+ 14 - 1
misago/threads/posting/threadlabel.py

@@ -1,4 +1,6 @@
+from misago.threads import moderation
 from misago.threads.forms.posting import ThreadLabelForm
+from misago.threads.models import Label
 from misago.threads.posting import PostingMiddleware, START
 
 
@@ -21,7 +23,7 @@ class ThreadLabelFormMiddleware(PostingMiddleware):
                                     initial=initial)
 
     def pre_save(self, form):
-        if form.is_valid():
+        if form.is_valid() and self.mode == START:
             if self.thread_label_id != form.cleaned_data.get('label'):
                 if form.cleaned_data.get('label'):
                     self.thread.label_id = form.cleaned_data.get('label')
@@ -29,3 +31,14 @@ class ThreadLabelFormMiddleware(PostingMiddleware):
                 else:
                     self.thread.label = None
                     self.thread.update_fields.append('label')
+
+    def post_save(self, form):
+        if form.is_valid() and self.mode != START:
+            if self.thread_label_id != form.cleaned_data.get('label'):
+                if form.cleaned_data.get('label'):
+                    labels_dict = Label.objects.get_cached_labels_dict()
+                    new_label = labels_dict.get(form.cleaned_data.get('label'))
+                    if new_label:
+                        label_thread(self.user, self.thread, new_label)
+                else:
+                    unlabel_thread(self.user, self.thread)

+ 11 - 2
misago/threads/posting/threadpin.py

@@ -1,5 +1,6 @@
+from misago.threads import moderation
 from misago.threads.forms.posting import ThreadPinForm
-from misago.threads.posting import PostingMiddleware
+from misago.threads.posting import PostingMiddleware, START
 
 
 class ThreadPinFormMiddleware(PostingMiddleware):
@@ -18,7 +19,15 @@ class ThreadPinFormMiddleware(PostingMiddleware):
             return ThreadPinForm(prefix=self.prefix, initial=initial)
 
     def pre_save(self, form):
-        if form.is_valid():
+        if form.is_valid() and self.mode == START:
             if self.is_pinned != form.cleaned_data.get('is_pinned'):
                 self.thread.is_pinned = form.cleaned_data.get('is_pinned')
                 self.thread.update_fields.append('is_pinned')
+
+    def post_save(self, form):
+        if form.is_valid() and self.mode != START:
+            if self.is_pinned != form.cleaned_data.get('is_pinned'):
+                if self.thread.is_pinned:
+                    moderation.unpin_thread(self.user, self.thread)
+                else:
+                    moderation.pin_thread(self.user, self.thread)