Strip trailing periods from topic titles

Note that this will also prevent a title from ending with "...". I did a
search for all titles that ended in that, and none of them seemed
essential (and probably should have been removed), so I think this
should be fine.
This commit is contained in:
Deimos
2020-02-26 15:08:56 -07:00
parent 2c7572016d
commit 2532df018c
2 changed files with 19 additions and 0 deletions

View File

@@ -45,6 +45,13 @@ def test_whitespace_trimmed(title_schema):
assert result["title"] == "actual title"
def test_trailing_periods_trimmed(title_schema):
"""Ensure trailing periods on a title are removed."""
title = "This is an interesting story."
result = title_schema.load({"title": title})
assert not result["title"].endswith(".")
def test_consecutive_whitespace_removed(title_schema):
"""Ensure consecutive whitespace in a title is compressed."""
title = "sure are \n a lot of spaces"

View File

@@ -36,6 +36,18 @@ class TopicSchema(Schema):
user = Nested(UserSchema, dump_only=True)
group = Nested(GroupSchema, dump_only=True)
@pre_load
def prepare_title(self, data: dict, many: bool, partial: Any) -> dict:
"""Prepare the title before it's validated."""
# pylint: disable=unused-argument
if "title" not in data:
return data
# strip any trailing periods
data["title"] = data["title"].rstrip(".")
return data
@pre_load
def prepare_tags(self, data: dict, many: bool, partial: Any) -> dict:
"""Prepare the tags before they're validated."""