(self, commands)
return True
批量保存命令到数据库, command的顺序和save中一致
批量保存命令到数据库, command的顺序和save中一致
批量保存命令到数据库 command的顺序和save中一致
def bulk_save(self, commands):
        """
        批量保存命令到数据库, command的顺序和save中一致
        """
        _commands = []
        for c in commands:
            _commands.append(self.model(
                user=c["user"], asset=c["asset"], system_user=c["system_user"],
                input=c["input"], output=c["output"], session=c["session"],
                risk_level=c.get("risk_level", 0), org_id=c["org_id"],
                timestamp=c["timestamp"]
            ))
        error = False
        try:
            with transaction.atomic():
                self.model.objects.bulk_create(_commands)
        except OperationalError:
            error = True
        except:
            return False

        if not error:
            return True
        for command in _commands:
            try:
                with transaction.atomic():
                    command.save()
            except OperationalError:
                command.output = str(command.output.encode())
                command.save()
        return True
def bulk_save ( self , commands ) : _commands = [ ] for c in commands : _commands . append ( self . model ( user = c [ "user" ] , asset = c [ "asset" ] , system_user = c [ "system_user" ] , input = c [ "input" ] , output = c [ "output" ] , session = c [ "session" ] , risk_level = c . get ( "risk_level" , 0 ) , org_id = c [ "org_id" ] , timestamp = c [ "timestamp" ] ) ) error = False try : with transaction . atomic ( ) : self . model . objects . bulk_create ( _commands ) except OperationalError : error = True except : return False if not error : return True for command in _commands : try : with transaction . atomic ( ) : command . save ( ) except OperationalError : command . output = str ( command . output . encode ( ) ) command . save ( ) return True