move ratings to new edition

This commit is contained in:
Matt Lehrer 2023-11-16 10:38:41 +01:00
parent ab9cea1742
commit 7684101f15
2 changed files with 35 additions and 0 deletions

View file

@ -129,3 +129,31 @@ 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)

View file

@ -103,4 +103,11 @@ def switch_edition(request):
readthrough.book = new_edition
readthrough.save()
ratings = models.ReviewRating.objects.filter(
book__parent_work=new_edition.parent_work, user=request.user
)
for rating in ratings.all():
rating.book = new_edition
rating.save()
return redirect(f"/book/{new_edition.id}")