From 25fd7276ea23d69a707414eb874363601cdcc433 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adeodato=20Sim=C3=B3?= Date: Sun, 17 Sep 2023 10:46:11 -0300 Subject: [PATCH 1/5] `pure_content()` refactor: shorter conditionals --- bookwyrm/models/status.py | 32 +++++++++++--------------------- 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/bookwyrm/models/status.py b/bookwyrm/models/status.py index e51f2ba07..8c98028a0 100644 --- a/bookwyrm/models/status.py +++ b/bookwyrm/models/status.py @@ -320,17 +320,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", page {progress}" + return f"{self.content}

({citation})

" activity_serializer = activitypub.Comment @@ -359,17 +356,10 @@ class Quotation(BookStatus): """indicate the book in question for mastodon (or w/e) users""" quote = re.sub(r"^

", '

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

$", '"

', quote) + citation = f'-- "{self.book.title}"' 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 + citation += f", page {self.position}" + return f"{quote}

{citation}

{self.content}" activity_serializer = activitypub.Quotation From 1322a0c6939f364a023aa0df1c01e9685d4c31f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adeodato=20Sim=C3=B3?= Date: Sun, 17 Sep 2023 15:05:34 -0300 Subject: [PATCH 2/5] =?UTF-8?q?Substitute=20=E2=80=9Cp.=E2=80=9D=20for=20?= =?UTF-8?q?=E2=80=9Cpage=E2=80=9D=20in=20page=20progress=20serialization?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bookwyrm/models/status.py | 4 ++-- bookwyrm/tests/models/test_status_model.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/bookwyrm/models/status.py b/bookwyrm/models/status.py index 8c98028a0..1040ace8f 100644 --- a/bookwyrm/models/status.py +++ b/bookwyrm/models/status.py @@ -326,7 +326,7 @@ class Comment(BookStatus): f'"{self.book.title}"' ) if self.progress_mode == "PG" and progress > 0: - citation += f", page {progress}" + citation += f", p. {progress}" return f"{self.content}

({citation})

" activity_serializer = activitypub.Comment @@ -358,7 +358,7 @@ class Quotation(BookStatus): quote = re.sub(r"

$", '"

', quote) citation = f'-- "{self.book.title}"' if self.position_mode == "PG" and self.position and (self.position > 0): - citation += f", page {self.position}" + citation += f", p. {self.position}" return f"{quote}

{citation}

{self.content}" activity_serializer = activitypub.Quotation diff --git a/bookwyrm/tests/models/test_status_model.py b/bookwyrm/tests/models/test_status_model.py index 72aa0ca6c..d41b80575 100644 --- a/bookwyrm/tests/models/test_status_model.py +++ b/bookwyrm/tests/models/test_status_model.py @@ -249,14 +249,14 @@ 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")

', + f'test content

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

', ) self.assertEqual(activity["attachment"][0]["type"], "Document") # self.assertTrue( From ce3885d4f6bed6761cac6761682a8982a4170188 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adeodato=20Sim=C3=B3?= Date: Sun, 17 Sep 2023 01:40:23 -0300 Subject: [PATCH 3/5] Use `endposition` when serializing Quotation --- bookwyrm/models/status.py | 13 ++++++++++-- bookwyrm/tests/models/test_status_model.py | 23 ++++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/bookwyrm/models/status.py b/bookwyrm/models/status.py index 1040ace8f..d51de5278 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 @@ -351,14 +352,22 @@ 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) citation = f'-- "{self.book.title}"' - if self.position_mode == "PG" and self.position and (self.position > 0): - citation += f", p. {self.position}" + if position := self._format_position(): + citation += f", {position}" return f"{quote}

{citation}

{self.content}" activity_serializer = activitypub.Quotation diff --git a/bookwyrm/tests/models/test_status_model.py b/bookwyrm/tests/models/test_status_model.py index d41b80575..15d73de9c 100644 --- a/bookwyrm/tests/models/test_status_model.py +++ b/bookwyrm/tests/models/test_status_model.py @@ -306,6 +306,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( From cc05cabcb57d340f87a3ae44856fe6574bec7eb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adeodato=20Sim=C3=B3?= Date: Sun, 17 Sep 2023 15:32:29 -0300 Subject: [PATCH 4/5] Note content: use italics for book titles + em-dash for Quotation --- bookwyrm/models/status.py | 7 ++++--- bookwyrm/tests/models/test_status_model.py | 18 +++++++++++++----- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/bookwyrm/models/status.py b/bookwyrm/models/status.py index d51de5278..5d6109468 100644 --- a/bookwyrm/models/status.py +++ b/bookwyrm/models/status.py @@ -270,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}" @@ -324,7 +324,7 @@ class Comment(BookStatus): progress = self.progress or 0 citation = ( f'comment on ' - f'"{self.book.title}"' + f"{self.book.title}" ) if self.progress_mode == "PG" and progress > 0: citation += f", p. {progress}" @@ -365,7 +365,8 @@ class Quotation(BookStatus): """indicate the book in question for mastodon (or w/e) users""" quote = re.sub(r"^

", '

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

$", '"

', quote) - citation = f'-- "{self.book.title}"' + 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}" diff --git a/bookwyrm/tests/models/test_status_model.py b/bookwyrm/tests/models/test_status_model.py index 15d73de9c..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") @@ -256,7 +256,11 @@ class Status(TestCase): self.assertEqual(activity["type"], "Note") self.assertEqual( activity["content"], - f'test content

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

', + ( + "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( @@ -326,7 +334,7 @@ class Status(TestCase): activity = status.to_activity(pure=True) self.assertRegex( activity["content"], - f'^

"my quote"

-- , {pages}

$', + f'^

"my quote"

, {pages}

$', ) def test_review_to_activity(self, *_): From fadf30b94216a407aceb3d089595fc40e8bc446e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adeodato=20Sim=C3=B3?= Date: Sun, 17 Sep 2023 15:45:27 -0300 Subject: [PATCH 5/5] Also use italics for book title in editions.html template --- bookwyrm/templates/book/editions/editions.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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' %}