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] 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(