From bd920a4630975f3e5adac6c078dc1a5eaf9a48f6 Mon Sep 17 00:00:00 2001 From: Matt Lehrer Date: Thu, 16 Nov 2023 10:38:45 +0100 Subject: [PATCH] move reviews to new edition --- bookwyrm/tests/views/books/test_editions.py | 30 +++++++++++++++++++++ bookwyrm/views/books/editions.py | 7 +++++ 2 files changed, 37 insertions(+) diff --git a/bookwyrm/tests/views/books/test_editions.py b/bookwyrm/tests/views/books/test_editions.py index a40d97b57..bbae517ac 100644 --- a/bookwyrm/tests/views/books/test_editions.py +++ b/bookwyrm/tests/views/books/test_editions.py @@ -157,3 +157,33 @@ class BookViews(TestCase): self.assertIsInstance(models.ReviewRating.objects.get(user=self.local_user, book=edition2), models.ReviewRating) with self.assertRaises(models.ReviewRating.DoesNotExist): models.ReviewRating.objects.get(user=self.local_user, book=edition1) + + @patch("bookwyrm.suggested_users.rerank_suggestions_task.delay") + @patch("bookwyrm.activitystreams.populate_stream_task.delay") + @patch("bookwyrm.activitystreams.add_book_statuses_task.delay") + @patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async") + def test_move_reviews_on_switch_edition(self, *_): + """updates user's review on a book to new edition""" + work = models.Work.objects.create(title="test work") + edition1 = models.Edition.objects.create(title="first ed", parent_work=work) + edition2 = models.Edition.objects.create(title="second ed", parent_work=work) + + models.Review.objects.create( + book=edition1, + user=self.local_user, + name="blah", + rating=3, + content="not bad" + ) + + self.assertIsInstance(models.Review.objects.get(user=self.local_user, book=edition1), models.Review) + with self.assertRaises(models.Review.DoesNotExist): + models.Review.objects.get(user=self.local_user, book=edition2) + + request = self.factory.post("", {"edition": edition2.id}) + request.user = self.local_user + views.switch_edition(request) + + self.assertIsInstance(models.Review.objects.get(user=self.local_user, book=edition2), models.Review) + with self.assertRaises(models.Review.DoesNotExist): + models.Review.objects.get(user=self.local_user, book=edition1) diff --git a/bookwyrm/views/books/editions.py b/bookwyrm/views/books/editions.py index e34b045bb..f061caab8 100644 --- a/bookwyrm/views/books/editions.py +++ b/bookwyrm/views/books/editions.py @@ -110,4 +110,11 @@ def switch_edition(request): rating.book = new_edition rating.save() + reviews = models.Review.objects.filter( + book__parent_work=new_edition.parent_work, user=request.user + ) + for review in reviews.all(): + review.book = new_edition + review.save() + return redirect(f"/book/{new_edition.id}")