Repository: bookwyrm-social/bookwyrm

Issue:
Duplicated Reading Activity Entries
Hello!

I'm noticing that I'm getting multiple instances of duplicated reading activity when pulling information from a Goodreads import. These occur primarily in two different areas: 1) changing to a different edition of a book that already as a reading activity entry (so I have a single reading activity that duplicates into two different entries) and 2) books that I unshelved and re-imported (some I'd unshelved by accident).

If it helps: I'm using Vivaldi.

For example:
![image](https://user-images.githubusercontent.com/77229429/104121057-cba9a280-533b-11eb-897a-639db9003cd8.png)
![image](https://user-images.githubusercontent.com/77229429/104121066-dd8b4580-533b-11eb-88fa-105c09425ad0.png)



Discussion:
Just adding a bit more to this in case it helps? Because it seems that reading activity (also possibly ratings) are saved and automatically added to books when you change the edition, even you've deleted that information (in the case of reading activity) or unshelved the book entirely.

Same process (change edition):

![image](https://user-images.githubusercontent.com/77229429/104123843-a9b91b80-534d-11eb-9cf2-41b11eecd827.png)
![image](https://user-images.githubusercontent.com/77229429/104123886-dff69b00-534d-11eb-8e12-b47ac1990e57.png)

Resolving patch:
diff --git a/bookwyrm/outgoing.py b/bookwyrm/outgoing.py
--- 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()


Test patch:
diff --git a/bookwyrm/tests/test_outgoing.py b/bookwyrm/tests/test_outgoing.py
--- a/bookwyrm/tests/test_outgoing.py
+++ b/bookwyrm/tests/test_outgoing.py
@@ -323,6 +323,37 @@ def test_handle_imported_book_already_shelved(self):
         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)