2017-02-08 04:39:55 +01:00
|
|
|
from typing import Text
|
|
|
|
|
|
|
|
from django.utils.translation import ugettext as _
|
|
|
|
|
|
|
|
from zerver.lib.actions import do_change_full_name
|
|
|
|
from zerver.lib.request import JsonableError
|
2017-07-03 18:35:12 +02:00
|
|
|
from zerver.models import UserProfile, Service
|
2017-02-08 04:39:55 +01:00
|
|
|
|
2017-02-08 04:51:01 +01:00
|
|
|
def check_full_name(full_name_raw):
|
|
|
|
# type: (Text) -> Text
|
|
|
|
full_name = full_name_raw.strip()
|
|
|
|
if len(full_name) > UserProfile.MAX_NAME_LENGTH:
|
|
|
|
raise JsonableError(_("Name too long!"))
|
2017-05-12 04:21:49 +02:00
|
|
|
if len(full_name) < UserProfile.MIN_NAME_LENGTH:
|
|
|
|
raise JsonableError(_("Name too short!"))
|
2017-02-08 04:51:01 +01:00
|
|
|
if list(set(full_name).intersection(UserProfile.NAME_INVALID_CHARS)):
|
|
|
|
raise JsonableError(_("Invalid characters in name!"))
|
|
|
|
return full_name
|
|
|
|
|
2017-06-21 13:46:58 +02:00
|
|
|
def check_short_name(short_name_raw):
|
|
|
|
# type: (Text) -> Text
|
|
|
|
short_name = short_name_raw.strip()
|
|
|
|
if len(short_name) == 0:
|
|
|
|
raise JsonableError(_("Bad name or username"))
|
|
|
|
return short_name
|
|
|
|
|
2017-04-07 07:28:28 +02:00
|
|
|
def check_change_full_name(user_profile, full_name_raw, acting_user):
|
|
|
|
# type: (UserProfile, Text, UserProfile) -> Text
|
2017-02-08 04:39:55 +01:00
|
|
|
"""Verifies that the user's proposed full name is valid. The caller
|
|
|
|
is responsible for checking check permissions. Returns the new
|
|
|
|
full name, which may differ from what was passed in (because this
|
|
|
|
function strips whitespace)."""
|
2017-02-08 04:51:01 +01:00
|
|
|
new_full_name = check_full_name(full_name_raw)
|
2017-04-07 07:28:28 +02:00
|
|
|
do_change_full_name(user_profile, new_full_name, acting_user)
|
2017-02-08 04:39:55 +01:00
|
|
|
return new_full_name
|
2017-05-30 19:19:48 +02:00
|
|
|
|
|
|
|
def check_valid_bot_type(bot_type):
|
|
|
|
# type: (int) -> None
|
|
|
|
if bot_type not in UserProfile.ALLOWED_BOT_TYPES:
|
|
|
|
raise JsonableError(_('Invalid bot type'))
|
2017-07-03 18:35:12 +02:00
|
|
|
|
|
|
|
def check_valid_interface_type(interface_type):
|
|
|
|
# type: (int) -> None
|
|
|
|
if interface_type not in Service.ALLOWED_INTERFACE_TYPES:
|
|
|
|
raise JsonableError(_('Invalid interface type'))
|