From bde6ce8525c6d16205a5a4bd784fbf83572e9783 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Wed, 24 Mar 2021 09:31:45 -0700 Subject: [PATCH 1/5] Fixes review name in pure serialization --- bookwyrm/models/fields.py | 3 +++ bookwyrm/models/status.py | 15 +++++++-------- .../generated_status/review_pure_name.html | 10 ++++++++++ 3 files changed, 20 insertions(+), 8 deletions(-) create mode 100644 bookwyrm/templates/snippets/generated_status/review_pure_name.html diff --git a/bookwyrm/models/fields.py b/bookwyrm/models/fields.py index 247c6aca6..a5985e8de 100644 --- a/bookwyrm/models/fields.py +++ b/bookwyrm/models/fields.py @@ -448,3 +448,6 @@ class IntegerField(ActivitypubFieldMixin, models.IntegerField): class DecimalField(ActivitypubFieldMixin, models.DecimalField): """ activitypub-aware boolean field """ + + def field_to_activity(self, value): + return float(value) diff --git a/bookwyrm/models/status.py b/bookwyrm/models/status.py index 0ddcfd173..56f30020f 100644 --- a/bookwyrm/models/status.py +++ b/bookwyrm/models/status.py @@ -304,13 +304,12 @@ class Review(Status): @property def pure_name(self): """ clarify review names for mastodon serialization """ - if self.rating: - return 'Review of "{}" ({:d} stars): {}'.format( - self.book.title, - self.rating, - self.name, - ) - return 'Review of "{}": {}'.format(self.book.title, self.name) + template = get_template("snippets/generated_status/review_pure_name.html") + return template.render({ + "book": self.book, + "rating": int(self.rating), + "name": self.name + }).strip() @property def pure_content(self): @@ -332,7 +331,7 @@ class ReviewRating(Review): @property def pure_content(self): template = get_template("snippets/generated_status/rating.html") - return template.render({"book": self.book, "rating": self.rating}).strip() + return template.render({"book": self.book, "rating": int(self.rating)}).strip() activity_serializer = activitypub.Rating pure_type = "Note" diff --git a/bookwyrm/templates/snippets/generated_status/review_pure_name.html b/bookwyrm/templates/snippets/generated_status/review_pure_name.html new file mode 100644 index 000000000..3c133678b --- /dev/null +++ b/bookwyrm/templates/snippets/generated_status/review_pure_name.html @@ -0,0 +1,10 @@ +{% load i18n %} +{% if rating %} + +{% blocktrans with book_title=book.title rating=rating review_title=name count counter=rating %}Review of {{ book_title }} ({{ rating }} star): {{ review_title }}{% plural %}Review of {{ book_title }} ({{ rating }} stars): {{ review_title }}{% endblocktrans %} + +{% else %} + +{% blocktrans with book_title=book.title review_title=name %}Review of {{ book_title }}: {{ review_title }}{% endblocktrans %} + +{% endif %} From 9dc19b8bffe77012bda489c7a408b5db96cf2669 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Wed, 24 Mar 2021 09:34:21 -0700 Subject: [PATCH 2/5] Python formatting --- bookwyrm/models/status.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/bookwyrm/models/status.py b/bookwyrm/models/status.py index 56f30020f..e655616fa 100644 --- a/bookwyrm/models/status.py +++ b/bookwyrm/models/status.py @@ -305,11 +305,9 @@ class Review(Status): def pure_name(self): """ clarify review names for mastodon serialization """ template = get_template("snippets/generated_status/review_pure_name.html") - return template.render({ - "book": self.book, - "rating": int(self.rating), - "name": self.name - }).strip() + return template.render( + {"book": self.book, "rating": int(self.rating), "name": self.name} + ).strip() @property def pure_content(self): From 1d53a06e35dd5b89e1deb0fcf5e458fe1b9d76c0 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Wed, 24 Mar 2021 09:51:49 -0700 Subject: [PATCH 3/5] Fixes decimaal formatting --- bookwyrm/models/fields.py | 2 ++ bookwyrm/models/status.py | 4 ++-- bookwyrm/templates/snippets/generated_status/rating.html | 2 +- .../templates/snippets/generated_status/review_pure_name.html | 2 +- 4 files changed, 6 insertions(+), 4 deletions(-) diff --git a/bookwyrm/models/fields.py b/bookwyrm/models/fields.py index a5985e8de..e034d59ee 100644 --- a/bookwyrm/models/fields.py +++ b/bookwyrm/models/fields.py @@ -450,4 +450,6 @@ class DecimalField(ActivitypubFieldMixin, models.DecimalField): """ activitypub-aware boolean field """ def field_to_activity(self, value): + if not value: + return None return float(value) diff --git a/bookwyrm/models/status.py b/bookwyrm/models/status.py index e655616fa..adc9d39d6 100644 --- a/bookwyrm/models/status.py +++ b/bookwyrm/models/status.py @@ -306,7 +306,7 @@ class Review(Status): """ clarify review names for mastodon serialization """ template = get_template("snippets/generated_status/review_pure_name.html") return template.render( - {"book": self.book, "rating": int(self.rating), "name": self.name} + {"book": self.book, "rating": self.rating, "name": self.name} ).strip() @property @@ -329,7 +329,7 @@ class ReviewRating(Review): @property def pure_content(self): template = get_template("snippets/generated_status/rating.html") - return template.render({"book": self.book, "rating": int(self.rating)}).strip() + return template.render({"book": self.book, "rating": self.rating}).strip() activity_serializer = activitypub.Rating pure_type = "Note" diff --git a/bookwyrm/templates/snippets/generated_status/rating.html b/bookwyrm/templates/snippets/generated_status/rating.html index 13afd94de..c23d0eb8c 100644 --- a/bookwyrm/templates/snippets/generated_status/rating.html +++ b/bookwyrm/templates/snippets/generated_status/rating.html @@ -1,3 +1,3 @@ {% load i18n %}{% load humanize %} -{% blocktrans with title=book.title path=book.remote_id rating=rating count counter=rating %}Rated {{ title }}: {{ rating }} star{% plural %}Rated {{ title }}: {{ rating }} stars{% endblocktrans %} +{% blocktrans with title=book.title path=book.remote_id rating=rating|floatformat:"0" count counter=rating %}Rated {{ title }}: {{ rating }} star{% plural %}Rated {{ title }}: {{ rating }} stars{% endblocktrans %} diff --git a/bookwyrm/templates/snippets/generated_status/review_pure_name.html b/bookwyrm/templates/snippets/generated_status/review_pure_name.html index 3c133678b..f58643840 100644 --- a/bookwyrm/templates/snippets/generated_status/review_pure_name.html +++ b/bookwyrm/templates/snippets/generated_status/review_pure_name.html @@ -1,7 +1,7 @@ {% load i18n %} {% if rating %} -{% blocktrans with book_title=book.title rating=rating review_title=name count counter=rating %}Review of {{ book_title }} ({{ rating }} star): {{ review_title }}{% plural %}Review of {{ book_title }} ({{ rating }} stars): {{ review_title }}{% endblocktrans %} +{% blocktrans with book_title=book.title rating=rating|floatformat:"0" review_title=name count counter=rating %}Review of {{ book_title }} ({{ rating }} star): {{ review_title }}{% plural %}Review of {{ book_title }} ({{ rating }} stars): {{ review_title }}{% endblocktrans %} {% else %} From 73e38f9050af0bf7c1f63a233add9ec4fe67f316 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Wed, 24 Mar 2021 10:07:39 -0700 Subject: [PATCH 4/5] Fixes formatting rating value in template --- .../generated_status/review_pure_name.html | 2 +- bookwyrm/tests/models/test_status_model.py | 25 ++++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/bookwyrm/templates/snippets/generated_status/review_pure_name.html b/bookwyrm/templates/snippets/generated_status/review_pure_name.html index f58643840..a8d3fa5b8 100644 --- a/bookwyrm/templates/snippets/generated_status/review_pure_name.html +++ b/bookwyrm/templates/snippets/generated_status/review_pure_name.html @@ -1,7 +1,7 @@ {% load i18n %} {% if rating %} -{% blocktrans with book_title=book.title rating=rating|floatformat:"0" review_title=name count counter=rating %}Review of {{ book_title }} ({{ rating }} star): {{ review_title }}{% plural %}Review of {{ book_title }} ({{ rating }} stars): {{ review_title }}{% endblocktrans %} +{% blocktrans with book_title=book.title display_rating=rating|floatformat:"0" review_title=name count counter=rating %}Review of {{ book_title }} ({{ display_rating }} star): {{ review_title }}{% plural %}Review of {{ book_title }} ({{ display_rating }} stars): {{ review_title }}{% endblocktrans %} {% else %} diff --git a/bookwyrm/tests/models/test_status_model.py b/bookwyrm/tests/models/test_status_model.py index 54fe7fee9..05cd2f000 100644 --- a/bookwyrm/tests/models/test_status_model.py +++ b/bookwyrm/tests/models/test_status_model.py @@ -277,7 +277,30 @@ class Status(TestCase): self.assertEqual(activity["id"], status.remote_id) self.assertEqual(activity["type"], "Article") self.assertEqual( - activity["name"], 'Review of "%s" (3 stars): Review name' % self.book.title + activity["name"], + "Review of %s (3 stars): Review name" % self.book.title, + ) + self.assertEqual(activity["content"], "test content") + self.assertEqual(activity["attachment"][0].type, "Document") + self.assertEqual( + activity["attachment"][0].url, + "https://%s%s" % (settings.DOMAIN, self.book.cover.url), + ) + self.assertEqual(activity["attachment"][0].name, "Test Edition") + + def test_review_to_pure_activity_no_rating(self, *_): + """ subclass of the base model version with a "pure" serializer """ + status = models.Review.objects.create( + name="Review name", + content="test content", + user=self.local_user, + book=self.book, + ) + activity = status.to_activity(pure=True) + self.assertEqual(activity["id"], status.remote_id) + self.assertEqual(activity["type"], "Article") + self.assertEqual( + activity["name"], "Review of %s: Review name" % self.book.title ) self.assertEqual(activity["content"], "test content") self.assertEqual(activity["attachment"][0].type, "Document") From 30cebc017fcd8653b81034a2ac562e194d9c9244 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Wed, 24 Mar 2021 10:17:45 -0700 Subject: [PATCH 5/5] Updates tests to catch decimal/float/int errors in status serialization --- .../snippets/generated_status/rating.html | 2 +- bookwyrm/tests/models/test_status_model.py | 26 +++++++++++++++++-- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/bookwyrm/templates/snippets/generated_status/rating.html b/bookwyrm/templates/snippets/generated_status/rating.html index c23d0eb8c..b70171cde 100644 --- a/bookwyrm/templates/snippets/generated_status/rating.html +++ b/bookwyrm/templates/snippets/generated_status/rating.html @@ -1,3 +1,3 @@ {% load i18n %}{% load humanize %} -{% blocktrans with title=book.title path=book.remote_id rating=rating|floatformat:"0" count counter=rating %}Rated {{ title }}: {{ rating }} star{% plural %}Rated {{ title }}: {{ rating }} stars{% endblocktrans %} +{% blocktrans with title=book.title path=book.remote_id display_rating=rating|floatformat:"0" count counter=rating %}Rated {{ title }}: {{ display_rating }} star{% plural %}Rated {{ title }}: {{ display_rating }} stars{% endblocktrans %} diff --git a/bookwyrm/tests/models/test_status_model.py b/bookwyrm/tests/models/test_status_model.py index 05cd2f000..97df315f9 100644 --- a/bookwyrm/tests/models/test_status_model.py +++ b/bookwyrm/tests/models/test_status_model.py @@ -252,7 +252,7 @@ class Status(TestCase): status = models.Review.objects.create( name="Review name", content="test content", - rating=3, + rating=3.0, user=self.local_user, book=self.book, ) @@ -269,7 +269,7 @@ class Status(TestCase): status = models.Review.objects.create( name="Review name", content="test content", - rating=3, + rating=3.0, user=self.local_user, book=self.book, ) @@ -310,6 +310,28 @@ class Status(TestCase): ) self.assertEqual(activity["attachment"][0].name, "Test Edition") + def test_reviewrating_to_pure_activity(self, *_): + """ subclass of the base model version with a "pure" serializer """ + status = models.ReviewRating.objects.create( + rating=3.0, + user=self.local_user, + book=self.book, + ) + activity = status.to_activity(pure=True) + self.assertEqual(activity["id"], status.remote_id) + self.assertEqual(activity["type"], "Note") + self.assertEqual( + activity["content"], + 'Rated %s: 3 stars' + % (self.book.remote_id, self.book.title), + ) + self.assertEqual(activity["attachment"][0].type, "Document") + self.assertEqual( + activity["attachment"][0].url, + "https://%s%s" % (settings.DOMAIN, self.book.cover.url), + ) + self.assertEqual(activity["attachment"][0].name, "Test Edition") + def test_favorite(self, *_): """ fav a status """ real_broadcast = models.Favorite.broadcast