Repository: bookwyrm-social/bookwyrm

Issue:
Imported ratings added as reviews
During a goodreads import, star ratings seem to be added as Reviews, rather than ReviewRatings


Discussion:

Resolving patch:
diff --git a/bookwyrm/importers/importer.py b/bookwyrm/importers/importer.py
--- a/bookwyrm/importers/importer.py
+++ b/bookwyrm/importers/importer.py
@@ -116,24 +116,33 @@ def handle_imported_book(source, user, item, include_reviews, privacy):
         read.save()
 
     if include_reviews and (item.rating or item.review):
-        review_title = (
-            "Review of {!r} on {!r}".format(
-                item.book.title,
-                source,
-            )
-            if item.review
-            else ""
-        )
-
         # we don't know the publication date of the review,
         # but "now" is a bad guess
         published_date_guess = item.date_read or item.date_added
-        models.Review.objects.create(
-            user=user,
-            book=item.book,
-            name=review_title,
-            content=item.review,
-            rating=item.rating,
-            published_date=published_date_guess,
-            privacy=privacy,
-        )
+        if item.review:
+            review_title = (
+                "Review of {!r} on {!r}".format(
+                    item.book.title,
+                    source,
+                )
+                if item.review
+                else ""
+            )
+            models.Review.objects.create(
+                user=user,
+                book=item.book,
+                name=review_title,
+                content=item.review,
+                rating=item.rating,
+                published_date=published_date_guess,
+                privacy=privacy,
+            )
+        else:
+            # just a rating
+            models.ReviewRating.objects.create(
+                user=user,
+                book=item.book,
+                rating=item.rating,
+                published_date=published_date_guess,
+                privacy=privacy,
+            )


Test patch:
diff --git a/bookwyrm/tests/data/goodreads-rating.csv b/bookwyrm/tests/data/goodreads-rating.csv
new file mode 100644
--- /dev/null
+++ b/bookwyrm/tests/data/goodreads-rating.csv
@@ -0,0 +1,5 @@
+Book Id,Title,Author,Author l-f,Additional Authors,ISBN,ISBN13,My Rating,Average Rating,Publisher,Binding,Number of Pages,Year Published,Original Publication Year,Date Read,Date Added,Bookshelves,Bookshelves with positions,Exclusive Shelf,My Review,Spoiler,Private Notes,Read Count,Recommended For,Recommended By,Owned Copies,Original Purchase Date,Original Purchase Location,Condition,Condition Description,BCID
+42036538,Gideon the Ninth (The Locked Tomb #1),Tamsyn Muir,"Muir, Tamsyn",,"=""1250313198""","=""9781250313195""",0,4.20,Tor,Hardcover,448,2019,2019,2020/10/25,2020/10/21,,,read,,,,1,,,0,,,,,
+52691223,Subcutanean,Aaron A. Reed,"Reed, Aaron A.",,"=""""","=""""",0,4.45,,Paperback,232,2020,,2020/03/06,2020/03/05,,,read,,,,1,,,0,,,,,
+28694510,Patisserie at Home,Mélanie Dupuis,"Dupuis, Mélanie",Anne Cazor,"=""0062445316""","=""9780062445315""",2,4.60,Harper Design,Hardcover,288,2016,,,2019/07/08,,,read,,,,2,,,0,,,,,
+
diff --git a/bookwyrm/tests/importers/test_goodreads_import.py b/bookwyrm/tests/importers/test_goodreads_import.py
--- a/bookwyrm/tests/importers/test_goodreads_import.py
+++ b/bookwyrm/tests/importers/test_goodreads_import.py
@@ -228,6 +228,32 @@ def test_handle_imported_book_review(self, _):
         self.assertEqual(review.published_date.day, 8)
         self.assertEqual(review.privacy, "unlisted")
 
+    @patch("bookwyrm.activitystreams.ActivityStream.add_status")
+    def test_handle_imported_book_rating(self, _):
+        """goodreads rating import"""
+        import_job = models.ImportJob.objects.create(user=self.user)
+        datafile = pathlib.Path(__file__).parent.joinpath(
+            "../data/goodreads-rating.csv"
+        )
+        csv_file = open(datafile, "r")
+        entry = list(csv.DictReader(csv_file))[2]
+        entry = self.importer.parse_fields(entry)
+        import_item = models.ImportItem.objects.create(
+            job_id=import_job.id, index=0, data=entry, book=self.book
+        )
+
+        with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
+            handle_imported_book(
+                self.importer.service, self.user, import_item, True, "unlisted"
+            )
+        review = models.ReviewRating.objects.get(book=self.book, user=self.user)
+        self.assertIsInstance(review, models.ReviewRating)
+        self.assertEqual(review.rating, 2)
+        self.assertEqual(review.published_date.year, 2019)
+        self.assertEqual(review.published_date.month, 7)
+        self.assertEqual(review.published_date.day, 8)
+        self.assertEqual(review.privacy, "unlisted")
+
     def test_handle_imported_book_reviews_disabled(self):
         """goodreads review import"""
         import_job = models.ImportJob.objects.create(user=self.user)