Merge pull request #3353 from dato/fix_quotation_str_pagenum

Fix creation of quotations with no end position
This commit is contained in:
Bart Schuurmans 2024-04-15 13:11:55 +02:00 committed by GitHub
commit 70f803a1f6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 13 additions and 8 deletions

View file

@ -392,10 +392,10 @@ class Quotation(BookStatus):
def _format_position(self) -> Optional[str]:
"""serialize page position"""
beg = self.position
end = self.endposition or 0
end = self.endposition
if self.position_mode != "PG" or not beg:
return None
return f"pp. {beg}-{end}" if end > beg else f"p. {beg}"
return f"pp. {beg}-{end}" if end else f"p. {beg}"
@property
def pure_content(self):

View file

@ -340,8 +340,11 @@ class Status(TestCase):
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"),
("single pos", "7", "", "p. 7"),
("missing beg", "", "10", None),
("page range", "7", "10", "pp. 7-10"),
("page range roman", "xv", "xvi", "pp. xv-xvi"),
("page range reverse", "14", "10", "pp. 14-10"),
]
for desc, beg, end, pages in tests:
with self.subTest(desc):
@ -355,10 +358,12 @@ class Status(TestCase):
position_mode="PG",
)
activity = status.to_activity(pure=True)
self.assertRegex(
activity["content"],
f'^<p>"my quote"</p> <p>— <a .+</a>, {pages}</p>$',
)
if pages:
pages_re = re.escape(pages)
expect_re = f'^<p>"my quote"</p> <p>— <a .+</a>, {pages_re}</p>$'
else:
expect_re = '^<p>"my quote"</p> <p>— <a .+</a></p>$'
self.assertRegex(activity["content"], expect_re)
def test_review_to_activity(self, *_):
"""subclass of the base model version with a "pure" serializer"""