diff --git a/bookwyrm/importers/importer.py b/bookwyrm/importers/importer.py index 657ede05e..05550429b 100644 --- a/bookwyrm/importers/importer.py +++ b/bookwyrm/importers/importer.py @@ -181,40 +181,42 @@ def handle_imported_book(item): item.book.title, job.source, ) - existing = models.Review.objects.filter( + review = models.Review.objects.filter( user=user, book=item.book, name=review_title, rating=item.rating, published_date=published_date_guess, ).first() - - review = existing or models.Review( - user=user, - book=item.book, - name=review_title, - content=item.review, - rating=item.rating, - published_date=published_date_guess, - privacy=job.privacy, - ) + if not review: + review = models.Review( + user=user, + book=item.book, + name=review_title, + content=item.review, + rating=item.rating, + published_date=published_date_guess, + privacy=job.privacy, + ) + review.save(software="bookwyrm", priority=LOW) else: # just a rating - existing = models.ReviewRating.objects.filter( + review = models.ReviewRating.objects.filter( user=user, book=item.book, published_date=published_date_guess, rating=item.rating, ).first() - review = existing or models.ReviewRating( - user=user, - book=item.book, - rating=item.rating, - published_date=published_date_guess, - privacy=job.privacy, - ) + if not review: + review = models.ReviewRating( + user=user, + book=item.book, + rating=item.rating, + published_date=published_date_guess, + privacy=job.privacy, + ) + review.save(software="bookwyrm", priority=LOW) # only broadcast this review to other bookwyrm instances - review.save(software="bookwyrm", priority=LOW) item.linked_review = review - item.save() + item.save() diff --git a/bookwyrm/tests/importers/test_importer.py b/bookwyrm/tests/importers/test_importer.py index 99cdcd289..6996a92b5 100644 --- a/bookwyrm/tests/importers/test_importer.py +++ b/bookwyrm/tests/importers/test_importer.py @@ -256,6 +256,55 @@ class GenericImporter(TestCase): import_item.refresh_from_db() self.assertEqual(import_item.linked_review.id, review.id) + @patch("bookwyrm.activitystreams.add_status_task.delay") + def test_handle_imported_book_rating_duplicate_with_link(self, *_): + """rating import twice""" + import_job = self.importer.create_job( + self.local_user, self.csv, True, "unlisted" + ) + import_item = import_job.items.filter(index=1).first() + import_item.book = self.book + import_item.save() + + with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"): + handle_imported_book(import_item) + handle_imported_book(import_item) + + review = models.ReviewRating.objects.get(book=self.book, user=self.local_user) + self.assertIsInstance(review, models.ReviewRating) + self.assertEqual(review.rating, 3.0) + self.assertEqual(review.privacy, "unlisted") + + import_item.refresh_from_db() + self.assertEqual(import_item.linked_review.id, review.id) + + @patch("bookwyrm.activitystreams.add_status_task.delay") + def test_handle_imported_book_rating_duplicate_without_link(self, *_): + """rating import twice""" + import_job = self.importer.create_job( + self.local_user, self.csv, True, "unlisted" + ) + import_item = import_job.items.filter(index=1).first() + import_item.book = self.book + import_item.save() + + with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"): + handle_imported_book(import_item) + import_item.refresh_from_db() + import_item.linked_review = None + import_item.save() + + with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"): + handle_imported_book(import_item) + + review = models.ReviewRating.objects.get(book=self.book, user=self.local_user) + self.assertIsInstance(review, models.ReviewRating) + self.assertEqual(review.rating, 3.0) + self.assertEqual(review.privacy, "unlisted") + + import_item.refresh_from_db() + self.assertEqual(import_item.linked_review.id, review.id) + def test_handle_imported_book_reviews_disabled(self, *_): """review import""" import_job = self.importer.create_job(