From be6fc19c16896c34656a1efe5e8c29be7c32a60e Mon Sep 17 00:00:00 2001 From: Celeo Date: Sun, 19 Aug 2018 19:05:49 -0700 Subject: [PATCH] Convert all-caps titles to title case --- tildes/requirements-to-freeze.txt | 1 + tildes/requirements.txt | 1 + tildes/tests/test_topic.py | 16 ++++++++++++++++ tildes/tildes/models/topic/topic.py | 6 ++++++ 4 files changed, 24 insertions(+) diff --git a/tildes/requirements-to-freeze.txt b/tildes/requirements-to-freeze.txt index 39cc8a1..5e15100 100644 --- a/tildes/requirements-to-freeze.txt +++ b/tildes/requirements-to-freeze.txt @@ -37,6 +37,7 @@ SQLAlchemy SQLAlchemy-Utils stripe testing.redis +titlecase webargs webtest zope.sqlalchemy diff --git a/tildes/requirements.txt b/tildes/requirements.txt index aacc564..74dc993 100644 --- a/tildes/requirements.txt +++ b/tildes/requirements.txt @@ -83,6 +83,7 @@ SQLAlchemy-Utils==0.33.3 stripe==2.4.0 testing.common.database==2.0.3 testing.redis==1.1.1 +titlecase==0.12.0 toml==0.9.4 traitlets==4.3.2 transaction==2.2.1 diff --git a/tildes/tests/test_topic.py b/tildes/tests/test_topic.py index 723d91e..e9c21fd 100644 --- a/tildes/tests/test_topic.py +++ b/tildes/tests/test_topic.py @@ -130,3 +130,19 @@ def test_multiple_edits_update_time(text_topic): def test_topic_initial_last_activity_time(text_topic): """Ensure last_activity_time is initially the same as created_time.""" assert text_topic.last_activity_time == text_topic.created_time + + +def test_convert_all_caps(session_user, session_group): + """Ensure that all-caps titles are converted to title case.""" + topic = Topic.create_link_topic( + session_group, session_user, "THE TITLE", "http://example.com" + ) + assert topic.title == "The Title" + + +def test_no_convert_partial_caps_title(session_user, session_group): + """Ensure that partially-caps titles are not converted to title case.""" + topic = Topic.create_link_topic( + session_group, session_user, "This is a TITLE", "http://example.com" + ) + assert topic.title == "This is a TITLE" diff --git a/tildes/tildes/models/topic/topic.py b/tildes/tildes/models/topic/topic.py index b822862..137054f 100644 --- a/tildes/tildes/models/topic/topic.py +++ b/tildes/tildes/models/topic/topic.py @@ -20,6 +20,7 @@ from sqlalchemy.ext.mutable import MutableDict from sqlalchemy.orm import deferred, relationship from sqlalchemy.sql.expression import text from sqlalchemy_utils import Ltree +from titlecase import titlecase from tildes.enums import TopicType from tildes.lib.database import ArrayOfLtree @@ -172,6 +173,11 @@ class Topic(DatabaseModel): new_topic = cls() new_topic.group_id = group.group_id new_topic.user_id = author.user_id + + # if the title is all caps, convert to title case + if title.isupper(): + title = titlecase(title) + new_topic.title = title return new_topic