2022-07-12 02:54:23 +00:00
|
|
|
from django.db import models
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
from simple_history.models import HistoricalRecords
|
|
|
|
|
2022-09-06 11:57:03 +00:00
|
|
|
from common.utils import lazyproperty
|
|
|
|
|
|
|
|
from .base import BaseAccount
|
2022-07-12 02:54:23 +00:00
|
|
|
|
2022-08-19 10:49:00 +00:00
|
|
|
__all__ = ['Account', 'AccountTemplate']
|
2022-07-12 02:54:23 +00:00
|
|
|
|
|
|
|
|
2022-09-06 11:57:03 +00:00
|
|
|
class Account(BaseAccount):
|
2022-09-07 12:01:04 +00:00
|
|
|
asset = models.ForeignKey('assets.Asset', related_name='accounts', on_delete=models.CASCADE, verbose_name=_('Asset'))
|
2022-07-15 10:57:52 +00:00
|
|
|
version = models.IntegerField(default=0, verbose_name=_('Version'))
|
2022-07-12 02:54:23 +00:00
|
|
|
history = HistoricalRecords()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
verbose_name = _('Account')
|
|
|
|
unique_together = [('username', 'asset')]
|
|
|
|
permissions = [
|
2022-07-27 08:51:39 +00:00
|
|
|
('view_accountsecret', _('Can view asset account secret')),
|
|
|
|
('change_accountsecret', _('Can change asset account secret')),
|
|
|
|
('view_historyaccount', _('Can view asset history account')),
|
|
|
|
('view_historyaccountsecret', _('Can view asset history account secret')),
|
2022-07-12 02:54:23 +00:00
|
|
|
]
|
2022-07-13 08:36:49 +00:00
|
|
|
|
2022-09-06 11:57:03 +00:00
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
return "{}({})_{}".format(self.asset_name, self.ip, self.username)
|
|
|
|
|
|
|
|
@lazyproperty
|
|
|
|
def ip(self):
|
|
|
|
return self.asset.ip
|
|
|
|
|
|
|
|
@lazyproperty
|
|
|
|
def asset_name(self):
|
|
|
|
return self.asset.name
|
|
|
|
|
2022-07-13 08:36:49 +00:00
|
|
|
def __str__(self):
|
2022-08-11 07:45:03 +00:00
|
|
|
return '{}@{}'.format(self.username, self.asset.name)
|
2022-08-19 10:49:00 +00:00
|
|
|
|
|
|
|
|
2022-09-06 11:57:03 +00:00
|
|
|
class AccountTemplate(BaseAccount):
|
2022-08-19 10:49:00 +00:00
|
|
|
class Meta:
|
|
|
|
verbose_name = _('Account template')
|
2022-09-06 11:57:03 +00:00
|
|
|
unique_together = (
|
|
|
|
('name', 'org_id'),
|
|
|
|
)
|
2022-08-19 10:49:00 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
2022-09-06 11:57:03 +00:00
|
|
|
return self.username
|