Merge pull request #791 from mouse-reeve/review-pure-title

Fixes review name in pure serialization
This commit is contained in:
Mouse Reeve 2021-03-24 10:42:46 -07:00 committed by GitHub
commit 4f22657dc2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 68 additions and 11 deletions

View file

@ -448,3 +448,8 @@ class IntegerField(ActivitypubFieldMixin, models.IntegerField):
class DecimalField(ActivitypubFieldMixin, models.DecimalField): class DecimalField(ActivitypubFieldMixin, models.DecimalField):
""" activitypub-aware boolean field """ """ activitypub-aware boolean field """
def field_to_activity(self, value):
if not value:
return None
return float(value)

View file

@ -304,13 +304,10 @@ class Review(Status):
@property @property
def pure_name(self): def pure_name(self):
""" clarify review names for mastodon serialization """ """ clarify review names for mastodon serialization """
if self.rating: template = get_template("snippets/generated_status/review_pure_name.html")
return 'Review of "{}" ({:d} stars): {}'.format( return template.render(
self.book.title, {"book": self.book, "rating": self.rating, "name": self.name}
self.rating, ).strip()
self.name,
)
return 'Review of "{}": {}'.format(self.book.title, self.name)
@property @property
def pure_content(self): def pure_content(self):

View file

@ -1,3 +1,3 @@
{% load i18n %}{% load humanize %} {% load i18n %}{% load humanize %}
{% blocktrans with title=book.title path=book.remote_id rating=rating count counter=rating %}Rated <em><a href="{{ path }}">{{ title }}</a></em>: {{ rating }} star{% plural %}Rated <em><a href="{{ path }}">{{ title }}</a></em>: {{ rating }} stars{% endblocktrans %} {% blocktrans with title=book.title path=book.remote_id display_rating=rating|floatformat:"0" count counter=rating %}Rated <em><a href="{{ path }}">{{ title }}</a></em>: {{ display_rating }} star{% plural %}Rated <em><a href="{{ path }}">{{ title }}</a></em>: {{ display_rating }} stars{% endblocktrans %}

View file

@ -0,0 +1,10 @@
{% load i18n %}
{% if rating %}
{% blocktrans with book_title=book.title display_rating=rating|floatformat:"0" review_title=name count counter=rating %}Review of <em>{{ book_title }}</em> ({{ display_rating }} star): {{ review_title }}{% plural %}Review of <em>{{ book_title }}</em> ({{ display_rating }} stars): {{ review_title }}{% endblocktrans %}
{% else %}
{% blocktrans with book_title=book.title review_title=name %}Review of <em>{{ book_title }}</em>: {{ review_title }}{% endblocktrans %}
{% endif %}

View file

@ -252,7 +252,7 @@ class Status(TestCase):
status = models.Review.objects.create( status = models.Review.objects.create(
name="Review name", name="Review name",
content="test content", content="test content",
rating=3, rating=3.0,
user=self.local_user, user=self.local_user,
book=self.book, book=self.book,
) )
@ -269,7 +269,7 @@ class Status(TestCase):
status = models.Review.objects.create( status = models.Review.objects.create(
name="Review name", name="Review name",
content="test content", content="test content",
rating=3, rating=3.0,
user=self.local_user, user=self.local_user,
book=self.book, book=self.book,
) )
@ -277,7 +277,8 @@ class Status(TestCase):
self.assertEqual(activity["id"], status.remote_id) self.assertEqual(activity["id"], status.remote_id)
self.assertEqual(activity["type"], "Article") self.assertEqual(activity["type"], "Article")
self.assertEqual( self.assertEqual(
activity["name"], 'Review of "%s" (3 stars): Review name' % self.book.title activity["name"],
"Review of <em>%s</em> (3 stars): Review name" % self.book.title,
) )
self.assertEqual(activity["content"], "test content") self.assertEqual(activity["content"], "test content")
self.assertEqual(activity["attachment"][0].type, "Document") self.assertEqual(activity["attachment"][0].type, "Document")
@ -287,6 +288,50 @@ class Status(TestCase):
) )
self.assertEqual(activity["attachment"][0].name, "Test Edition") 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 <em>%s</em>: 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_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 <em><a href="%s">%s</a></em>: 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, *_): def test_favorite(self, *_):
""" fav a status """ """ fav a status """
real_broadcast = models.Favorite.broadcast real_broadcast = models.Favorite.broadcast