jumpserver/apps/terminal/models/applet/applet.py

65 lines
2.4 KiB
Python
Raw Normal View History

2022-10-25 04:57:34 +00:00
import yaml
import os.path
2022-10-25 11:31:13 +00:00
from django.conf import settings
from django.core.files.storage import default_storage
2022-10-22 03:17:02 +00:00
from django.db import models
from django.utils.translation import gettext_lazy as _
from common.db.models import JMSBaseModel
__all__ = ['Applet', 'AppletPublication']
class Applet(JMSBaseModel):
class Type(models.TextChoices):
2022-10-25 04:57:34 +00:00
general = 'general', _('General')
2022-10-22 03:17:02 +00:00
web = 'web', _('Web')
2022-10-25 04:57:34 +00:00
name = models.SlugField(max_length=128, verbose_name=_('Name'), unique=True)
2022-10-25 11:31:13 +00:00
display_name = models.CharField(max_length=128, verbose_name=_('Display name'))
2022-10-22 03:17:02 +00:00
version = models.CharField(max_length=16, verbose_name=_('Version'))
author = models.CharField(max_length=128, verbose_name=_('Author'))
2022-10-25 04:57:34 +00:00
type = models.CharField(max_length=16, verbose_name=_('Type'), default='general', choices=Type.choices)
2022-10-26 09:21:52 +00:00
is_active = models.BooleanField(default=True, verbose_name=_('Is active'))
2022-10-22 03:17:02 +00:00
protocols = models.JSONField(default=list, verbose_name=_('Protocol'))
2022-10-25 04:57:34 +00:00
tags = models.JSONField(default=list, verbose_name=_('Tags'))
2022-10-22 03:17:02 +00:00
comment = models.TextField(default='', blank=True, verbose_name=_('Comment'))
2022-11-01 12:37:04 +00:00
hosts = models.ManyToManyField(
through_fields=('applet', 'host'), through='AppletPublication',
to='AppletHost', verbose_name=_('Hosts')
)
2022-10-22 03:17:02 +00:00
def __str__(self):
return self.name
2022-10-25 11:31:13 +00:00
@property
def path(self):
return default_storage.path('applets/{}'.format(self.name))
2022-10-25 04:57:34 +00:00
@property
def manifest(self):
path = os.path.join(self.path, 'manifest.yml')
if not os.path.exists(path):
return None
with open(path, 'r') as f:
return yaml.safe_load(f)
@property
def icon(self):
path = os.path.join(self.path, 'icon.png')
if not os.path.exists(path):
return None
2022-10-25 11:31:13 +00:00
return os.path.join(settings.MEDIA_URL, 'applets', self.name, 'icon.png')
2022-10-25 04:57:34 +00:00
2022-10-22 03:17:02 +00:00
class AppletPublication(JMSBaseModel):
2022-10-28 10:19:44 +00:00
applet = models.ForeignKey('Applet', on_delete=models.PROTECT, related_name='publications', verbose_name=_('Applet'))
host = models.ForeignKey('AppletHost', on_delete=models.PROTECT, related_name='publications', verbose_name=_('Host'))
2022-11-01 09:04:44 +00:00
status = models.CharField(max_length=16, default='', verbose_name=_('Status'))
2022-10-22 03:17:02 +00:00
comment = models.TextField(default='', blank=True, verbose_name=_('Comment'))
class Meta:
2022-10-25 04:57:34 +00:00
unique_together = ('applet', 'host')