Move ratings and reviews when switching editions (#3117)

Merged from mattlehrer/move-ratings-and-reviews-when-switching-editions.
Fixes: #2926.
This commit is contained in:
Adeodato Simó 2024-02-21 18:48:32 -03:00 committed by GitHub
commit 1fabe51261
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 79 additions and 0 deletions

View file

@ -133,3 +133,73 @@ class BookViews(TestCase):
self.assertEqual(models.ShelfBook.objects.get().book, edition2)
self.assertEqual(models.ReadThrough.objects.get().book, edition2)
@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_ratings_on_switch_edition(self, *_):
"""updates user's rating 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.ReviewRating.objects.create(
book=edition1,
user=self.local_user,
rating=3,
)
self.assertIsInstance(
models.ReviewRating.objects.get(user=self.local_user, book=edition1),
models.ReviewRating,
)
with self.assertRaises(models.ReviewRating.DoesNotExist):
models.ReviewRating.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.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)

View file

@ -103,4 +103,13 @@ def switch_edition(request):
readthrough.book = new_edition
readthrough.save()
reviews = models.Review.objects.filter(
book__parent_work=new_edition.parent_work, user=request.user
)
for review in reviews.all():
# because ratings are a subclass of reviews,
# this will pick up both ratings and reviews
review.book = new_edition
review.save()
return redirect(f"/book/{new_edition.id}")