alert_words: Consistently clean whitespace for alert words.

This fixes some gaps in handling of whitespace in alert words.
This commit is contained in:
Tim Abbott
2017-02-16 21:00:02 -08:00
parent c1dfa348a1
commit d564a76f8e
2 changed files with 10 additions and 6 deletions

View File

@@ -128,7 +128,7 @@ class AlertWordTests(ZulipTestCase):
# type: () -> None
self.login("hamlet@zulip.com")
result = self.client_put('/json/users/me/alert_words', {'alert_words': ujson.dumps(['one', 'two', 'three'])})
result = self.client_put('/json/users/me/alert_words', {'alert_words': ujson.dumps(['one ', '\n two', 'three'])})
self.assert_json_success(result)
result = self.client_get('/json/users/me/alert_words')

View File

@@ -18,24 +18,28 @@ def list_alert_words(request, user_profile):
# type: (HttpRequest, UserProfile) -> HttpResponse
return json_success({'alert_words': user_alert_words(user_profile)})
def clean_alert_words(alert_words):
# type: (List[Text]) -> List[Text]
alert_words = [w.strip().lstrip() for w in alert_words]
return [w for w in alert_words if w != ""]
@has_request_variables
def set_alert_words(request, user_profile,
alert_words=REQ(validator=check_list(check_string), default=[])):
# type: (HttpRequest, UserProfile, List[Text]) -> HttpResponse
alert_words = [w for w in alert_words if w.strip()]
do_set_alert_words(user_profile, alert_words)
do_set_alert_words(user_profile, clean_alert_words(alert_words))
return json_success()
@has_request_variables
def add_alert_words(request, user_profile,
alert_words=REQ(validator=check_list(check_string), default=[])):
# type: (HttpRequest, UserProfile, List[str]) -> HttpResponse
do_add_alert_words(user_profile, alert_words)
# type: (HttpRequest, UserProfile, List[Text]) -> HttpResponse
do_set_alert_words(user_profile, clean_alert_words(alert_words))
return json_success()
@has_request_variables
def remove_alert_words(request, user_profile,
alert_words=REQ(validator=check_list(check_string), default=[])):
# type: (HttpRequest, UserProfile, List[str]) -> HttpResponse
# type: (HttpRequest, UserProfile, List[Text]) -> HttpResponse
do_remove_alert_words(user_profile, alert_words)
return json_success()