Просмотр исходного кода

fix #877: sync thread's has_unapproved_posts flag on thread approve by moderator

Rafał Pitoń 8 лет назад
Родитель
Сommit
c655a22b49

+ 5 - 1
misago/threads/moderation/threads.py

@@ -104,10 +104,14 @@ def merge_thread(request, thread, other_thread):
 @atomic
 def approve_thread(request, thread):
     if thread.is_unapproved:
-        thread.is_unapproved = False
         thread.first_post.is_unapproved = False
         thread.first_post.save(update_fields=['is_unapproved'])
 
+        thread.is_unapproved = False
+
+        unapproved_post_qs = thread.post_set.filter(is_unapproved=True)
+        thread.has_unapproved_posts = unapproved_post_qs.exists()
+
         record_event(request, thread, 'approved')
         return True
     else:

+ 0 - 1
misago/threads/serializers/thread.py

@@ -75,7 +75,6 @@ class ThreadSerializer(serializers.ModelSerializer, MutableFields):
             acl = obj.acl
         except AttributeError:
             return False
-
         return acl.get('can_approve') and obj.has_unapproved_posts
 
     def get_is_new(self, obj):

+ 60 - 7
misago/threads/tests/test_thread_patch_api.py

@@ -6,6 +6,8 @@ from django.utils import six, timezone
 from misago.acl.testutils import override_acl
 from misago.categories.models import Category
 
+from misago.threads.models import Thread
+
 from .test_threads_api import ThreadsApiTestCase
 
 
@@ -60,6 +62,9 @@ class ThreadChangeTitleApiTests(ThreadPatchApiTestCase):
         )
         self.assertEqual(response.status_code, 200)
 
+        response_json = response.json()
+        self.assertEqual(response_json['title'], "Lorem ipsum change!")
+
         thread_json = self.get_thread_json()
         self.assertEqual(thread_json['title'], "Lorem ipsum change!")
 
@@ -143,6 +148,9 @@ class ThreadPinGloballyApiTests(ThreadPatchApiTestCase):
         )
         self.assertEqual(response.status_code, 200)
 
+        response_json = response.json()
+        self.assertEqual(response_json['weight'], 2)
+
         thread_json = self.get_thread_json()
         self.assertEqual(thread_json['weight'], 2)
 
@@ -167,6 +175,9 @@ class ThreadPinGloballyApiTests(ThreadPatchApiTestCase):
         )
         self.assertEqual(response.status_code, 200)
 
+        response_json = response.json()
+        self.assertEqual(response_json['weight'], 0)
+
         thread_json = self.get_thread_json()
         self.assertEqual(thread_json['weight'], 0)
 
@@ -239,6 +250,9 @@ class ThreadPinLocallyApiTests(ThreadPatchApiTestCase):
         )
         self.assertEqual(response.status_code, 200)
 
+        response_json = response.json()
+        self.assertEqual(response_json['weight'], 1)
+
         thread_json = self.get_thread_json()
         self.assertEqual(thread_json['weight'], 1)
 
@@ -263,6 +277,9 @@ class ThreadPinLocallyApiTests(ThreadPatchApiTestCase):
         )
         self.assertEqual(response.status_code, 200)
 
+        response_json = response.json()
+        self.assertEqual(response_json['weight'], 0)
+
         thread_json = self.get_thread_json()
         self.assertEqual(thread_json['weight'], 0)
 
@@ -385,14 +402,14 @@ class ThreadMoveApiTests(ThreadPatchApiTestCase):
         )
         self.assertEqual(response.status_code, 200)
 
+        reponse_json = response.json()
+        self.assertEqual(reponse_json['category'], self.category_b.pk)
+
         self.override_other_acl({})
 
         thread_json = self.get_thread_json()
         self.assertEqual(thread_json['category']['id'], self.category_b.pk)
 
-        reponse_json = response.json()
-        self.assertEqual(reponse_json['category'], self.category_b.pk)
-
     def test_move_thread_with_top(self):
         """api moves thread to other category, sets top"""
         self.override_acl({'can_move_threads': True})
@@ -419,14 +436,14 @@ class ThreadMoveApiTests(ThreadPatchApiTestCase):
         )
         self.assertEqual(response.status_code, 200)
 
+        reponse_json = response.json()
+        self.assertEqual(reponse_json['category'], self.category_b.pk)
+
         self.override_other_acl({})
 
         thread_json = self.get_thread_json()
         self.assertEqual(thread_json['category']['id'], self.category_b.pk)
 
-        reponse_json = response.json()
-        self.assertEqual(reponse_json['category'], self.category_b.pk)
-
     def test_move_thread_no_permission(self):
         """api move thread to other category with no permission fails"""
         self.override_acl({'can_move_threads': False})
@@ -563,6 +580,9 @@ class ThreadCloseApiTests(ThreadPatchApiTestCase):
         )
         self.assertEqual(response.status_code, 200)
 
+        response_json = response.json()
+        self.assertTrue(response_json['is_closed'])
+
         thread_json = self.get_thread_json()
         self.assertTrue(thread_json['is_closed'])
 
@@ -587,6 +607,9 @@ class ThreadCloseApiTests(ThreadPatchApiTestCase):
         )
         self.assertEqual(response.status_code, 200)
 
+        response_json = response.json()
+        self.assertFalse(response_json['is_closed'])
+
         thread_json = self.get_thread_json()
         self.assertFalse(thread_json['is_closed'])
 
@@ -646,9 +669,15 @@ class ThreadCloseApiTests(ThreadPatchApiTestCase):
 class ThreadApproveApiTests(ThreadPatchApiTestCase):
     def test_approve_thread(self):
         """api makes it possible to approve thread"""
-        self.thread.is_unapproved = True
+        self.thread.first_post.is_unapproved = True
+        self.thread.first_post.save()
+
+        self.thread.synchronize()
         self.thread.save()
 
+        self.assertTrue(self.thread.is_unapproved)
+        self.assertTrue(self.thread.has_unapproved_posts)
+
         self.override_acl({'can_approve_content': 1})
 
         response = self.patch(
@@ -662,8 +691,17 @@ class ThreadApproveApiTests(ThreadPatchApiTestCase):
         )
         self.assertEqual(response.status_code, 200)
 
+        response_json = response.json()
+        self.assertFalse(response_json['is_unapproved'])
+        self.assertFalse(response_json['has_unapproved_posts'])
+
         thread_json = self.get_thread_json()
         self.assertFalse(thread_json['is_unapproved'])
+        self.assertFalse(thread_json['has_unapproved_posts'])
+
+        thread = Thread.objects.get(pk=self.thread.pk)
+        self.assertFalse(thread.is_unapproved)
+        self.assertFalse(thread.has_unapproved_posts)
 
     def test_unapprove_thread(self):
         """api returns permission error on approval removal"""
@@ -700,6 +738,9 @@ class ThreadHideApiTests(ThreadPatchApiTestCase):
         )
         self.assertEqual(response.status_code, 200)
 
+        reponse_json = response.json()
+        self.assertTrue(reponse_json['is_hidden'])
+
         self.override_acl({'can_hide_threads': 1})
 
         thread_json = self.get_thread_json()
@@ -728,6 +769,9 @@ class ThreadHideApiTests(ThreadPatchApiTestCase):
         )
         self.assertEqual(response.status_code, 200)
 
+        reponse_json = response.json()
+        self.assertFalse(reponse_json['is_hidden'])
+
         self.override_acl({'can_hide_threads': 1})
 
         thread_json = self.get_thread_json()
@@ -795,6 +839,9 @@ class ThreadSubscribeApiTests(ThreadPatchApiTestCase):
 
         self.assertEqual(response.status_code, 200)
 
+        reponse_json = response.json()
+        self.assertFalse(reponse_json['subscription'])
+
         thread_json = self.get_thread_json()
         self.assertFalse(thread_json['subscription'])
 
@@ -815,6 +862,9 @@ class ThreadSubscribeApiTests(ThreadPatchApiTestCase):
 
         self.assertEqual(response.status_code, 200)
 
+        reponse_json = response.json()
+        self.assertTrue(reponse_json['subscription'])
+
         thread_json = self.get_thread_json()
         self.assertTrue(thread_json['subscription'])
 
@@ -835,6 +885,9 @@ class ThreadSubscribeApiTests(ThreadPatchApiTestCase):
 
         self.assertEqual(response.status_code, 200)
 
+        reponse_json = response.json()
+        self.assertIsNone(reponse_json['subscription'])
+
         thread_json = self.get_thread_json()
         self.assertIsNone(thread_json['subscription'])
 

+ 21 - 0
misago/threads/tests/test_thread_postpatch_api.py

@@ -107,6 +107,9 @@ class PostProtectApiTests(ThreadPostPatchApiTestCase):
         )
         self.assertEqual(response.status_code, 200)
 
+        reponse_json = response.json()
+        self.assertTrue(reponse_json['is_protected'])
+
         self.refresh_post()
         self.assertTrue(self.post.is_protected)
 
@@ -128,6 +131,9 @@ class PostProtectApiTests(ThreadPostPatchApiTestCase):
         )
         self.assertEqual(response.status_code, 200)
 
+        reponse_json = response.json()
+        self.assertFalse(reponse_json['is_protected'])
+
         self.refresh_post()
         self.assertFalse(self.post.is_protected)
 
@@ -217,6 +223,9 @@ class PostApproveApiTests(ThreadPostPatchApiTestCase):
         )
         self.assertEqual(response.status_code, 200)
 
+        reponse_json = response.json()
+        self.assertFalse(reponse_json['is_unapproved'])
+
         self.refresh_post()
         self.assertFalse(self.post.is_unapproved)
 
@@ -333,6 +342,9 @@ class PostHideApiTests(ThreadPostPatchApiTestCase):
         )
         self.assertEqual(response.status_code, 200)
 
+        reponse_json = response.json()
+        self.assertTrue(reponse_json['is_hidden'])
+
         self.refresh_post()
         self.assertTrue(self.post.is_hidden)
 
@@ -357,6 +369,9 @@ class PostHideApiTests(ThreadPostPatchApiTestCase):
         )
         self.assertEqual(response.status_code, 200)
 
+        reponse_json = response.json()
+        self.assertFalse(reponse_json['is_hidden'])
+
         self.refresh_post()
         self.assertFalse(self.post.is_hidden)
 
@@ -375,6 +390,9 @@ class PostHideApiTests(ThreadPostPatchApiTestCase):
         )
         self.assertEqual(response.status_code, 200)
 
+        reponse_json = response.json()
+        self.assertTrue(reponse_json['is_hidden'])
+
         self.refresh_post()
         self.assertTrue(self.post.is_hidden)
 
@@ -399,6 +417,9 @@ class PostHideApiTests(ThreadPostPatchApiTestCase):
         )
         self.assertEqual(response.status_code, 200)
 
+        reponse_json = response.json()
+        self.assertFalse(reponse_json['is_hidden'])
+
         self.refresh_post()
         self.assertFalse(self.post.is_hidden)