jumpserver/apps/ops/models/adhoc.py

45 lines
1.5 KiB
Python
Raw Normal View History

2017-03-09 06:55:33 +00:00
# ~*~ coding: utf-8 ~*~
2022-11-11 11:20:17 +00:00
import uuid
2018-03-30 14:03:43 +00:00
2017-03-09 06:55:33 +00:00
from django.db import models
2023-07-24 03:52:25 +00:00
from django.utils.translation import gettext_lazy as _
2017-12-10 16:29:25 +00:00
2022-10-08 08:55:14 +00:00
from common.utils import get_logger
2017-03-09 06:55:33 +00:00
__all__ = ["AdHoc"]
2017-03-09 06:55:33 +00:00
2024-09-06 09:41:09 +00:00
from common.db.models import JMSBaseModel
from ops.const import AdHocModules, Scope
2022-12-15 09:25:21 +00:00
2017-12-22 13:42:12 +00:00
logger = get_logger(__file__)
2017-03-09 06:55:33 +00:00
2024-09-06 09:41:09 +00:00
class AdHoc(JMSBaseModel):
2022-11-11 11:20:17 +00:00
id = models.UUIDField(default=uuid.uuid4, primary_key=True)
name = models.CharField(max_length=128, verbose_name=_('Name'))
2022-10-08 08:55:14 +00:00
pattern = models.CharField(max_length=1024, verbose_name=_("Pattern"), default='all')
module = models.CharField(max_length=128, choices=AdHocModules.choices, default=AdHocModules.shell,
2022-11-11 11:20:17 +00:00
verbose_name=_('Module'))
args = models.CharField(max_length=8192, default='', verbose_name=_('Args'))
creator = models.ForeignKey('users.User', verbose_name=_("Creator"), on_delete=models.SET_NULL, null=True)
2022-11-29 11:52:48 +00:00
comment = models.CharField(max_length=1024, default='', verbose_name=_('Comment'), null=True, blank=True)
2024-09-06 09:41:09 +00:00
scope = models.CharField(max_length=64, default=Scope.public, verbose_name=_('Scope'))
2022-11-29 11:52:48 +00:00
2022-11-11 11:20:17 +00:00
@property
def row_count(self):
if len(self.args) == 0:
return 0
count = str(self.args).count('\n')
return count + 1
2022-10-08 11:12:04 +00:00
2022-11-11 11:20:17 +00:00
@property
def size(self):
return len(self.args)
2017-12-21 18:08:29 +00:00
2017-12-15 07:50:15 +00:00
def __str__(self):
2022-10-08 08:55:14 +00:00
return "{}: {}".format(self.module, self.args)
class Meta:
2024-09-06 09:41:09 +00:00
unique_together = [('name', 'creator')]
2023-02-15 03:17:57 +00:00
verbose_name = _("Adhoc")