Browse Source

Preserve protected status after merge

Rafał Pitoń 7 years ago
parent
commit
9bbec9ee6a

+ 6 - 2
misago/threads/models/post.py

@@ -137,8 +137,12 @@ class Post(models.Model):
         other_post.parsed = six.text_type('\n').join((other_post.parsed, self.parsed))
         other_post.parsed = six.text_type('\n').join((other_post.parsed, self.parsed))
         update_post_checksum(other_post)
         update_post_checksum(other_post)
 
 
-        if self.thread.best_answer_id == self.id:
+        if self.is_protected:
+            other_post.is_protected = True
+        if self.is_best_answer:
             self.thread.best_answer = other_post
             self.thread.best_answer = other_post
+        if other_post.is_best_answer:
+            self.thread.best_answer_is_protected = other_post.is_protected
 
 
         from misago.threads.signals import merge_post
         from misago.threads.signals import merge_post
         merge_post.send(sender=self, other_post=other_post)
         merge_post.send(sender=self, other_post=other_post)
@@ -146,7 +150,7 @@ class Post(models.Model):
     def move(self, new_thread):
     def move(self, new_thread):
         from misago.threads.signals import move_post
         from misago.threads.signals import move_post
 
 
-        if self.thread.best_answer_id == self.id:
+        if self.is_best_answer:
             self.thread.clear_best_answer()
             self.thread.clear_best_answer()
 
 
         self.category = new_thread.category
         self.category = new_thread.category

+ 42 - 1
misago/threads/tests/test_thread_postmerge_api.py

@@ -456,7 +456,7 @@ class ThreadPostMergeApiTestCase(AuthenticatedUserTestCase):
         self.assertEqual(response.status_code, 200)
         self.assertEqual(response.status_code, 200)
 
 
     def test_merge_with_hidden_thread(self):
     def test_merge_with_hidden_thread(self):
-        """api recjects attempt to merge posts with different visibility"""
+        """api excludes thread's first post from visibility checks"""
         self.thread.first_post.is_hidden = True
         self.thread.first_post.is_hidden = True
         self.thread.first_post.poster = self.user
         self.thread.first_post.poster = self.user
         self.thread.first_post.save()
         self.thread.first_post.save()
@@ -474,6 +474,23 @@ class ThreadPostMergeApiTestCase(AuthenticatedUserTestCase):
         )
         )
         self.assertEqual(response.status_code, 200)
         self.assertEqual(response.status_code, 200)
 
 
+    def test_merge_protected(self):
+        """api preserves protected status after merge"""
+        response = self.client.post(
+            self.api_link,
+            json.dumps({
+                'posts': [
+                    testutils.reply_thread(self.thread, poster="Bob", is_protected=True).pk,
+                    testutils.reply_thread(self.thread, poster="Bob", is_protected=False).pk,
+                ]
+            }),
+            content_type="application/json",
+        )
+        self.assertEqual(response.status_code, 200)
+
+        merged_post = self.thread.post_set.order_by('-id')[0]
+        self.assertTrue(merged_post.is_protected)
+
     def test_merge_best_answer(self):
     def test_merge_best_answer(self):
         """api merges best answer with other post"""
         """api merges best answer with other post"""
         best_answer = testutils.reply_thread(self.thread, poster="Bob")
         best_answer = testutils.reply_thread(self.thread, poster="Bob")
@@ -519,6 +536,30 @@ class ThreadPostMergeApiTestCase(AuthenticatedUserTestCase):
         self.refresh_thread()
         self.refresh_thread()
         self.assertEqual(self.thread.best_answer, other_post)
         self.assertEqual(self.thread.best_answer, other_post)
 
 
+    def test_merge_best_answer_in_protected(self):
+        """api merges best answer into protected post"""
+        best_answer = testutils.reply_thread(self.thread, poster="Bob")
+        
+        self.thread.set_best_answer(self.user, best_answer)
+        self.thread.save()
+         
+        response = self.client.post(
+            self.api_link,
+            json.dumps({
+                'posts': [
+                    best_answer.pk,
+                    testutils.reply_thread(self.thread, poster="Bob", is_protected=True).pk,
+                ]
+            }),
+            content_type="application/json",
+        )
+        self.assertEqual(response.status_code, 200)
+
+        self.refresh_thread()
+        self.assertEqual(self.thread.best_answer, best_answer)
+        self.assertTrue(self.thread.best_answer.is_protected)
+        self.assertTrue(self.thread.best_answer_is_protected)
+
     def test_merge_remove_reads(self):
     def test_merge_remove_reads(self):
         """two posts merge removes read tracker from post"""
         """two posts merge removes read tracker from post"""
         post_a = testutils.reply_thread(self.thread, poster=self.user, message="Battęry")
         post_a = testutils.reply_thread(self.thread, poster=self.user, message="Battęry")

+ 2 - 0
misago/threads/testutils.py

@@ -77,6 +77,7 @@ def reply_thread(
         is_unapproved=False,
         is_unapproved=False,
         is_hidden=False,
         is_hidden=False,
         is_event=False,
         is_event=False,
+        is_protected=False,
         has_reports=False,
         has_reports=False,
         has_open_reports=False,
         has_open_reports=False,
         posted_on=None,
         posted_on=None,
@@ -96,6 +97,7 @@ def reply_thread(
         'is_event': is_event,
         'is_event': is_event,
         'is_unapproved': is_unapproved,
         'is_unapproved': is_unapproved,
         'is_hidden': is_hidden,
         'is_hidden': is_hidden,
+        'is_protected': is_protected,
         'has_reports': has_reports,
         'has_reports': has_reports,
         'has_open_reports': has_open_reports,
         'has_open_reports': has_open_reports,
     }
     }