2020-12-10 12:50:22 +00:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
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 _
|
|
|
|
|
2022-10-22 03:17:02 +00:00
|
|
|
from terminal.backends.command.models import AbstractSessionCommand
|
2020-12-10 12:50:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
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),
|
2022-12-05 05:27:51 +00:00
|
|
|
'account': random_string(6),
|
2022-03-30 11:07:49 +00:00
|
|
|
'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})')
|
|
|
|
|
2022-07-20 08:53:03 +00:00
|
|
|
@classmethod
|
|
|
|
def from_dict(cls, d):
|
|
|
|
self = cls()
|
|
|
|
for k, v in d.items():
|
|
|
|
setattr(self, k, v)
|
|
|
|
return self
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_multi_dict(cls, l):
|
|
|
|
commands = []
|
|
|
|
for d in l:
|
|
|
|
command = cls.from_dict(d)
|
|
|
|
commands.append(command)
|
|
|
|
return commands
|
|
|
|
|
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')
|