diff --git a/bookwyrm/models/status.py b/bookwyrm/models/status.py index e51f2ba07..5d6109468 100644 --- a/bookwyrm/models/status.py +++ b/bookwyrm/models/status.py @@ -1,5 +1,6 @@ """ models for storing different kinds of Activities """ from dataclasses import MISSING +from typing import Optional import re from django.apps import apps @@ -269,7 +270,7 @@ class GeneratedNote(Status): """indicate the book in question for mastodon (or w/e) users""" message = self.content books = ", ".join( - f'"{book.title}"' + f'{book.title}' for book in self.mention_books.all() ) return f"{self.user.display_name} {message} {books}" @@ -320,17 +321,14 @@ class Comment(BookStatus): @property def pure_content(self): """indicate the book in question for mastodon (or w/e) users""" - if self.progress_mode == "PG" and self.progress and (self.progress > 0): - return_value = ( - f'{self.content}

(comment on ' - f'"{self.book.title}", page {self.progress})

' - ) - else: - return_value = ( - f'{self.content}

(comment on ' - f'"{self.book.title}")

' - ) - return return_value + progress = self.progress or 0 + citation = ( + f'comment on ' + f"{self.book.title}" + ) + if self.progress_mode == "PG" and progress > 0: + citation += f", p. {progress}" + return f"{self.content}

({citation})

" activity_serializer = activitypub.Comment @@ -354,22 +352,24 @@ class Quotation(BookStatus): blank=True, ) + def _format_position(self) -> Optional[str]: + """serialize page position""" + beg = self.position + end = self.endposition or 0 + if self.position_mode != "PG" or not beg: + return None + return f"pp. {beg}-{end}" if end > beg else f"p. {beg}" + @property def pure_content(self): """indicate the book in question for mastodon (or w/e) users""" quote = re.sub(r"^

", '

"', self.quote) quote = re.sub(r"

$", '"

', quote) - if self.position_mode == "PG" and self.position and (self.position > 0): - return_value = ( - f'{quote}

-- ' - f'"{self.book.title}", page {self.position}

{self.content}' - ) - else: - return_value = ( - f'{quote}

-- ' - f'"{self.book.title}"

{self.content}' - ) - return return_value + title, href = self.book.title, self.book.remote_id + citation = f'— {title}' + if position := self._format_position(): + citation += f", {position}" + return f"{quote}

{citation}

{self.content}" activity_serializer = activitypub.Quotation diff --git a/bookwyrm/templates/book/editions/editions.html b/bookwyrm/templates/book/editions/editions.html index aa2b68bdb..e1766d1e1 100644 --- a/bookwyrm/templates/book/editions/editions.html +++ b/bookwyrm/templates/book/editions/editions.html @@ -5,7 +5,7 @@ {% block content %}
-

{% blocktrans with work_path=work.local_path work_title=work|book_title %}Editions of "{{ work_title }}"{% endblocktrans %}

+

{% blocktrans with work_path=work.local_path work_title=work|book_title %}Editions of {{ work_title }}{% endblocktrans %}

{% include 'book/editions/edition_filters.html' %} diff --git a/bookwyrm/tests/models/test_status_model.py b/bookwyrm/tests/models/test_status_model.py index 72aa0ca6c..760849f28 100644 --- a/bookwyrm/tests/models/test_status_model.py +++ b/bookwyrm/tests/models/test_status_model.py @@ -212,7 +212,7 @@ class Status(TestCase): def test_generated_note_to_pure_activity(self, *_): """subclass of the base model version with a "pure" serializer""" status = models.GeneratedNote.objects.create( - content="test content", user=self.local_user + content="reads", user=self.local_user ) status.mention_books.set([self.book]) status.mention_users.set([self.local_user]) @@ -220,7 +220,7 @@ class Status(TestCase): self.assertEqual(activity["id"], status.remote_id) self.assertEqual( activity["content"], - f'mouse test content "Test Edition"', + f'mouse reads Test Edition', ) self.assertEqual(len(activity["tag"]), 2) self.assertEqual(activity["type"], "Note") @@ -249,14 +249,18 @@ class Status(TestCase): def test_comment_to_pure_activity(self, *_): """subclass of the base model version with a "pure" serializer""" status = models.Comment.objects.create( - content="test content", user=self.local_user, book=self.book + content="test content", user=self.local_user, book=self.book, progress=27 ) activity = status.to_activity(pure=True) self.assertEqual(activity["id"], status.remote_id) self.assertEqual(activity["type"], "Note") self.assertEqual( activity["content"], - f'test content

(comment on "Test Edition")

', + ( + "test content" + f'

(comment on ' + "Test Edition, p. 27)

" + ), ) self.assertEqual(activity["attachment"][0]["type"], "Document") # self.assertTrue( @@ -295,7 +299,11 @@ class Status(TestCase): self.assertEqual(activity["type"], "Note") self.assertEqual( activity["content"], - f'a sickening sense

-- "Test Edition"

test content', + ( + "a sickening sense " + f'

' + "Test Edition

test content" + ), ) self.assertEqual(activity["attachment"][0]["type"], "Document") self.assertTrue( @@ -306,6 +314,29 @@ class Status(TestCase): ) self.assertEqual(activity["attachment"][0]["name"], "Test Edition") + def test_quotation_page_serialization(self, *_): + """serialization of quotation page position""" + tests = [ + ("single pos", 7, None, "p. 7"), + ("page range", 7, 10, "pp. 7-10"), + ] + for desc, beg, end, pages in tests: + with self.subTest(desc): + status = models.Quotation.objects.create( + quote="

my quote

", + content="", + user=self.local_user, + book=self.book, + position=beg, + endposition=end, + position_mode="PG", + ) + activity = status.to_activity(pure=True) + self.assertRegex( + activity["content"], + f'^

"my quote"

, {pages}

$', + ) + def test_review_to_activity(self, *_): """subclass of the base model version with a "pure" serializer""" status = models.Review.objects.create(