def change_user_name_handler()

in modular_api_cli/modular_handler/user_handler.py [0:0]


    def change_user_name_handler(self, old_username, new_username) \
            -> CommandResponse:
        _LOG.info(f'Going to change username for user \'{old_username}\'')
        existing_user = self.user_service.describe_user(username=old_username)
        if not existing_user:
            _LOG.error('User does not exists')
            raise ModularApiConfigurationException(
                f'User \'{old_username}\' does not exists. '
                f'Can not change username')

        if existing_user.state != ACTIVATED_STATE:
            _LOG.error('User blocked or deleted')
            raise ModularApiBadRequestException(
                f'User \'{old_username}\' is blocked or deleted.'
                f'{line_sep}To get more detailed information '
                f'please execute command:{line_sep}'
                f'modular user describe --username {old_username}')

        if self.user_service.calculate_user_hash(existing_user) != \
                existing_user.hash:
            click.confirm(
                f'User \'{old_username}\' is compromised. '
                f'Command execution leads to user entity hash '
                f'sum recalculation. Are you sure?',
                abort=True)

        existing_user.last_modification_date = utc_time_now().isoformat()
        existing_user.username = new_username
        self.user_service.save_user_with_recalculated_hash(
            user_item=existing_user
        )
        _LOG.debug(f'New user with username: {new_username} created.')

        _LOG.debug(f'Deleting old user {old_username}')
        existing_user.username = old_username
        self.user_service.delete_user(user_item=existing_user)
        _LOG.info('Username successfully updated')
        return CommandResponse(
            message=f'User \'{old_username}\' name has been '
                    f'updated to {new_username}')