def _validate_username(username): """Validate the username. Arguments: username (unicode): The proposed username. Returns: None Raises: errors.AccountUsernameInvalid """ try: _validate_unicode(username) _validate_type(username, six.string_types, accounts.USERNAME_BAD_TYPE_MSG) _validate_length(username, accounts.USERNAME_MIN_LENGTH, accounts.USERNAME_MAX_LENGTH, accounts.USERNAME_BAD_LENGTH_MSG) with override_language('en'): # `validate_username` provides a proper localized message, however the API needs only the English # message by convention. validate_username(username) except (UnicodeError, errors.AccountDataBadType, errors.AccountDataBadLength) as username_err: raise errors.AccountUsernameInvalid(text_type(username_err)) except ValidationError as validation_err: raise errors.AccountUsernameInvalid(validation_err.message)
def perform_username_updates(self, username_update_list): updated = [] not_found = [] failed = [] for username_update_dict in username_update_list: old_username = username_update_dict["old_username"] new_username = username_update_dict["new_username"] try: validate_username(new_username) with transaction.atomic(): update_count = User.objects.filter( username=old_username).update(username=new_username) if update_count > 0: UserSocialAuth.objects.filter(uid=old_username).update( uid=new_username) except Exception as exc: failed.append({ "old_username": old_username, "new_username": new_username, "exception": str(exc) }) else: if update_count == 0: not_found.append(username_update_dict) else: updated.append(username_update_dict) return {"updated": updated, "failed": failed, "not_found": not_found}
def clean_username(self): """ Validate a string of comma-separated usernames and return a list of usernames. """ usernames_csv_string = self.cleaned_data.get('username') if usernames_csv_string: usernames = usernames_csv_string.split(',') if len(usernames) > self.MAX_USERNAME_COUNT: raise ValidationError( u"Too many usernames in a single request - {}. A maximum of {} is allowed".format( len(usernames), self.MAX_USERNAME_COUNT, ) ) for username in usernames: validate_username(username) return usernames return usernames_csv_string