예제 #1
0
    def check_username(self, in_data):
        try:
            username = in_data['username'].strip()
        except KeyError:
            return HttpResponseBadRequest('You must specify a username')
        if username == 'admin' or username == 'demo_user':
            return {'error': _('Username {} is reserved.').format(username)}
        try:
            validate_email("{}@example.com".format(username))
            if BAD_MOBILE_USERNAME_REGEX.search(username) is not None:
                raise ValidationError("Username contained an invalid character")
        except ValidationError:
            if '..' in username:
                return {
                    'error': _("Username may not contain consecutive . (period).")
                }
            if username.endswith('.'):
                return {
                    'error': _("Username may not end with a . (period).")
                }
            return {
                'error': _("Username may not contain special characters.")
            }

        full_username = format_username(username, self.domain)
        exists = user_exists(full_username)
        if exists.exists:
            if exists.is_deleted:
                result = {'warning': _('Username {} belonged to a user that was deleted.'
                                       ' Reusing it may have unexpected consequences.').format(username)}
            else:
                result = {'error': _('Username {} is already taken').format(username)}
        else:
            result = {'success': _('Username {} is available').format(username)}
        return result
예제 #2
0
    def check_username(self, in_data):
        try:
            username = in_data['username'].strip()
        except KeyError:
            return HttpResponseBadRequest('You must specify a username')
        if username == 'admin' or username == 'demo_user':
            return {'error': _('Username {} is reserved.').format(username)}
        try:
            validate_email("{}@example.com".format(username))
            if BAD_MOBILE_USERNAME_REGEX.search(username) is not None:
                raise ValidationError("Username contained an invalid character")
        except ValidationError:
            if '..' in username:
                return {
                    'error': _("Username may not contain consecutive . (period).")
                }
            if username.endswith('.'):
                return {
                    'error': _("Username may not end with a . (period).")
                }
            return {
                'error': _("Username may not contain special characters.")
            }

        full_username = format_username(username, self.domain)
        exists = user_exists(full_username)
        if exists.exists:
            if exists.is_deleted:
                result = {'warning': _('Username {} belonged to a user that was deleted.'
                                       ' Reusing it may have unexpected consequences.').format(username)}
            else:
                result = {'error': _('Username {} is already taken').format(username)}
        else:
            result = {'success': _('Username {} is available').format(username)}
        return result
예제 #3
0
def clean_mobile_worker_username(domain, username, name_too_long_message=None,
        name_reserved_message=None, name_exists_message=None):

    max_username_length = get_mobile_worker_max_username_length(domain)

    if len(username) > max_username_length:
        raise forms.ValidationError(name_too_long_message or
            _('Username %(username)s is too long.  Must be under %(max_length)s characters.')
            % {'username': username, 'max_length': max_username_length})

    if username in UNALLOWED_MOBILE_WORKER_NAMES:
        raise forms.ValidationError(name_reserved_message or
            _('The username "%(username)s" is reserved for CommCare.')
            % {'username': username})

    username = format_username(username, domain)
    validate_username(username)

    exists = user_exists(username)
    if exists.exists:
        if exists.is_deleted:
            raise forms.ValidationError(_('This username was used previously.'))
        raise forms.ValidationError(name_exists_message or
            _('This Mobile Worker already exists.'))

    return username