jumpserver/apps/applications/models.py

63 lines
2.1 KiB
Python
Raw Normal View History

2016-10-15 09:14:56 +00:00
from __future__ import unicode_literals
from django.db import models
2016-10-15 10:28:49 +00:00
from django.utils.translation import ugettext_lazy as _
2016-10-15 09:14:56 +00:00
2016-10-15 10:28:49 +00:00
from users.models import User
class Terminal(models.Model):
2016-10-15 15:34:02 +00:00
TYPE_CHOICES = (
('SSH', 'SSH Terminal'),
('Web', 'Web Terminal')
2016-10-15 15:34:02 +00:00
)
2016-10-17 09:28:07 +00:00
name = models.CharField(max_length=30, unique=True, verbose_name=_('Name'))
remote_addr = models.GenericIPAddressField(verbose_name=_('Remote address'), blank=True, null=True)
2016-12-26 16:59:52 +00:00
type = models.CharField(choices=TYPE_CHOICES, max_length=3, blank=True, verbose_name=_('Terminal type'))
2017-01-02 16:11:44 +00:00
user = models.OneToOneField(User, related_name='terminal', verbose_name='Application user',
null=True, on_delete=models.CASCADE)
2016-12-25 09:44:39 +00:00
url = models.CharField(max_length=100, blank=True, verbose_name=_('URL to login'))
is_accepted = models.BooleanField(default=False, verbose_name='Is Accepted')
2016-10-15 15:34:02 +00:00
date_created = models.DateTimeField(auto_now_add=True)
2016-10-19 10:33:14 +00:00
comment = models.TextField(blank=True, verbose_name=_('Comment'))
2016-10-15 10:28:49 +00:00
@property
def is_active(self):
if self.user and self.user.is_active:
return True
return False
@is_active.setter
def is_active(self, active):
if self.user:
self.user.is_active = active
self.user.save()
2016-12-25 09:44:39 +00:00
def create_related_app_user(self):
user, access_key = User.create_app_user(name=self.name, comment=self.comment)
self.user = user
self.save()
return user, access_key
2016-10-17 07:24:41 +00:00
2017-01-02 16:11:44 +00:00
def delete(self, using=None, keep_parents=False):
if self.user:
self.user.delete()
return super(Terminal, self).delete(using=using, keep_parents=keep_parents)
def __unicode__(self):
active = 'Active' if self.user and self.user.is_active else 'Disabled'
return '%s: %s' % (self.name, active)
__str__ = __unicode__
2016-10-15 10:28:49 +00:00
class Meta:
2016-12-26 16:59:52 +00:00
ordering = ('is_accepted',)
2016-10-15 10:28:49 +00:00
class TerminalHeatbeat(models.Model):
2016-10-15 15:34:02 +00:00
terminal = models.ForeignKey(Terminal, on_delete=models.CASCADE)
2016-10-18 15:49:04 +00:00
date_created = models.DateTimeField(auto_now_add=True)
2016-10-15 10:28:49 +00:00
class Meta:
db_table = 'terminal_heatbeat'