From c2ffdb8f03da3e1c6caceaa573efb9d66ec27700 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 26 Apr 2021 10:04:33 -0700 Subject: [PATCH 1/3] Import ratings as ratings, not reviews --- bookwyrm/importers/importer.py | 45 ++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/bookwyrm/importers/importer.py b/bookwyrm/importers/importer.py index c1e418979..89c62e735 100644 --- 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, + ) From 4deb94714cf411b9383b03a3ba294a7bb1ebb1b0 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 26 Apr 2021 13:48:54 -0700 Subject: [PATCH 2/3] Adds test for import rating --- .../tests/importers/test_goodreads_import.py | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/bookwyrm/tests/importers/test_goodreads_import.py b/bookwyrm/tests/importers/test_goodreads_import.py index 0e39d5ecc..655567d05 100644 --- a/bookwyrm/tests/importers/test_goodreads_import.py +++ b/bookwyrm/tests/importers/test_goodreads_import.py @@ -228,6 +228,32 @@ class GoodreadsImport(TestCase): 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) From 674c4a99dfdd043e99d60f5bddbcba0846fa5f29 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 26 Apr 2021 13:56:09 -0700 Subject: [PATCH 3/3] Adds missing data file --- bookwyrm/tests/data/goodreads-rating.csv | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 bookwyrm/tests/data/goodreads-rating.csv diff --git a/bookwyrm/tests/data/goodreads-rating.csv b/bookwyrm/tests/data/goodreads-rating.csv new file mode 100644 index 000000000..fec0c77d5 --- /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,,,,, +