2020-12-10 12:50:22 +00:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2022-03-30 11:07:49 +00:00
|
|
|
import time
|
|
|
|
|
2020-12-10 12:50:22 +00:00
|
|
|
from django.db import models
|
|
|
|
from django.db.models.signals import post_save
|
2022-02-17 12:13:31 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
2020-12-10 12:50:22 +00:00
|
|
|
from ..backends.command.models import AbstractSessionCommand
|
|
|
|
|
|
|
|
|
|
|
|
class CommandManager(models.Manager):
|
|
|
|
def bulk_create(self, objs, **kwargs):
|
|
|
|
resp = super().bulk_create(objs, **kwargs)
|
|
|
|
for i in objs:
|
|
|
|
post_save.send(i.__class__, instance=i, created=True)
|
|
|
|
return resp
|
|
|
|
|
|
|
|
|
|
|
|
class Command(AbstractSessionCommand):
|
|
|
|
objects = CommandManager()
|
|
|
|
|
2022-03-30 11:07:49 +00:00
|
|
|
@classmethod
|
|
|
|
def generate_fake(cls, count=100, org=None):
|
|
|
|
import uuid
|
|
|
|
import datetime
|
|
|
|
from orgs.models import Organization
|
|
|
|
from common.utils import random_string
|
|
|
|
|
|
|
|
if not org:
|
|
|
|
org = Organization.default()
|
|
|
|
|
|
|
|
d = datetime.datetime.now() - datetime.timedelta(days=1)
|
|
|
|
commands = [
|
|
|
|
cls(**{
|
|
|
|
'user': random_string(6),
|
|
|
|
'asset': random_string(10),
|
|
|
|
'system_user': random_string(6),
|
|
|
|
'session': str(uuid.uuid4()),
|
|
|
|
'input': random_string(16),
|
|
|
|
'output': random_string(64),
|
|
|
|
'timestamp': int(d.timestamp()),
|
|
|
|
'org_id': str(org.id)
|
|
|
|
})
|
|
|
|
for i in range(count)
|
|
|
|
]
|
|
|
|
cls.objects.bulk_create(commands)
|
|
|
|
print(f'Create {len(commands)} commands of org ({org})')
|
|
|
|
|
2020-12-10 12:50:22 +00:00
|
|
|
class Meta:
|
|
|
|
db_table = "terminal_command"
|
|
|
|
ordering = ('-timestamp',)
|
2022-02-17 12:13:31 +00:00
|
|
|
verbose_name = _('Command record')
|