Browse Source

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.
merge-requests/110/head
Deimos 5 years ago
parent
commit
2532df018c
  1. 7
      tildes/tests/test_title.py
  2. 12
      tildes/tildes/schemas/topic.py

7
tildes/tests/test_title.py

@ -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"

12
tildes/tildes/schemas/topic.py

@ -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."""

Loading…
Cancel
Save