From b1b40e3c986906cc13dc3e18dd7ea3c111d04dbb Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sun, 10 Jan 2021 09:36:11 -0800 Subject: [PATCH] Don't double-add readthroughs in re-import --- bookwyrm/outgoing.py | 7 +++++++ bookwyrm/tests/test_outgoing.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/bookwyrm/outgoing.py b/bookwyrm/outgoing.py index bc170748..a02ac40f 100644 --- a/bookwyrm/outgoing.py +++ b/bookwyrm/outgoing.py @@ -180,6 +180,13 @@ def handle_imported_book(user, item, include_reviews, privacy): broadcast(user, shelf_book.to_add_activity(user), privacy=privacy) for read in item.reads: + # check for an existing readthrough with the same dates + if models.ReadThrough.objects.filter( + user=user, book=item.book, + start_date=read.start_date, + finish_date=read.finish_date + ).exists(): + continue read.book = item.book read.user = user read.save() diff --git a/bookwyrm/tests/test_outgoing.py b/bookwyrm/tests/test_outgoing.py index ba0d7d7d..ee66d9a9 100644 --- a/bookwyrm/tests/test_outgoing.py +++ b/bookwyrm/tests/test_outgoing.py @@ -323,6 +323,37 @@ class Outgoing(TestCase): self.assertEqual(readthrough.finish_date.day, 25) + def test_handle_import_twice(self): + ''' re-importing books ''' + shelf = self.local_user.shelf_set.filter(identifier='read').first() + import_job = models.ImportJob.objects.create(user=self.local_user) + datafile = pathlib.Path(__file__).parent.joinpath('data/goodreads.csv') + csv_file = open(datafile, 'r') + for index, entry in enumerate(list(csv.DictReader(csv_file))): + import_item = models.ImportItem.objects.create( + job_id=import_job.id, index=index, data=entry, book=self.book) + break + + with patch('bookwyrm.broadcast.broadcast_task.delay'): + outgoing.handle_imported_book( + self.local_user, import_item, False, 'public') + outgoing.handle_imported_book( + self.local_user, import_item, False, 'public') + + shelf.refresh_from_db() + self.assertEqual(shelf.books.first(), self.book) + + readthrough = models.ReadThrough.objects.get(user=self.local_user) + self.assertEqual(readthrough.book, self.book) + # I can't remember how to create dates and I don't want to look it up. + self.assertEqual(readthrough.start_date.year, 2020) + self.assertEqual(readthrough.start_date.month, 10) + self.assertEqual(readthrough.start_date.day, 21) + self.assertEqual(readthrough.finish_date.year, 2020) + self.assertEqual(readthrough.finish_date.month, 10) + self.assertEqual(readthrough.finish_date.day, 25) + + def test_handle_imported_book_review(self): ''' goodreads review import ''' import_job = models.ImportJob.objects.create(user=self.local_user)