Check for duplicates

This commit is contained in:
Mouse Reeve 2021-11-14 08:23:26 -08:00
parent 6cca3f9772
commit 9e673834dc
2 changed files with 72 additions and 21 deletions

View file

@ -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()

View file

@ -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(