2023-02-21 00:34:31 +00:00
|
|
|
from django.db import models
|
2023-03-23 10:57:22 +00:00
|
|
|
from django.db.models import Q
|
2023-02-21 00:34:31 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
2023-03-23 10:57:22 +00:00
|
|
|
from accounts.const import AutomationTypes, Source
|
|
|
|
from accounts.models import Account
|
2023-02-23 10:08:56 +00:00
|
|
|
from orgs.mixins.models import JMSOrgBaseModel
|
2023-01-16 11:02:09 +00:00
|
|
|
from .base import AccountBaseAutomation
|
2022-10-27 10:53:10 +00:00
|
|
|
|
2023-02-21 00:34:31 +00:00
|
|
|
__all__ = ['GatherAccountsAutomation', 'GatheredAccount']
|
|
|
|
|
|
|
|
|
|
|
|
class GatheredAccount(JMSOrgBaseModel):
|
|
|
|
present = models.BooleanField(default=True, verbose_name=_("Present"))
|
|
|
|
date_last_login = models.DateTimeField(null=True, verbose_name=_("Date last login"))
|
|
|
|
asset = models.ForeignKey('assets.Asset', on_delete=models.CASCADE, verbose_name=_("Asset"))
|
|
|
|
username = models.CharField(max_length=32, blank=True, db_index=True, verbose_name=_('Username'))
|
|
|
|
address_last_login = models.CharField(max_length=39, default='', verbose_name=_("Address last login"))
|
|
|
|
|
|
|
|
@property
|
|
|
|
def address(self):
|
|
|
|
return self.asset.address
|
|
|
|
|
2023-03-23 10:57:22 +00:00
|
|
|
@staticmethod
|
|
|
|
def sync_accounts(gathered_accounts):
|
|
|
|
account_objs = []
|
|
|
|
for gathered_account in gathered_accounts:
|
|
|
|
asset_id = gathered_account.asset_id
|
|
|
|
username = gathered_account.username
|
|
|
|
accounts = Account.objects.filter(
|
|
|
|
Q(asset_id=asset_id, username=username) |
|
|
|
|
Q(asset_id=asset_id, name=username)
|
|
|
|
)
|
|
|
|
if accounts.exists():
|
|
|
|
continue
|
|
|
|
account = Account(
|
|
|
|
asset_id=asset_id, username=username,
|
|
|
|
name=username, source=Source.COLLECTED
|
|
|
|
)
|
|
|
|
account_objs.append(account)
|
|
|
|
Account.objects.bulk_create(account_objs)
|
|
|
|
|
2023-02-21 00:34:31 +00:00
|
|
|
class Meta:
|
2023-02-23 10:08:56 +00:00
|
|
|
verbose_name = _('Gather account automation')
|
2023-02-21 00:34:31 +00:00
|
|
|
unique_together = [
|
|
|
|
('username', 'asset'),
|
|
|
|
]
|
|
|
|
ordering = ['asset']
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return '{}: {}'.format(self.asset, self.username)
|
2022-10-27 10:53:10 +00:00
|
|
|
|
|
|
|
|
2023-01-16 11:02:09 +00:00
|
|
|
class GatherAccountsAutomation(AccountBaseAutomation):
|
2023-03-23 10:57:22 +00:00
|
|
|
is_sync_account = models.BooleanField(
|
|
|
|
default=False, blank=True, verbose_name=_("Is sync account")
|
|
|
|
)
|
|
|
|
|
|
|
|
def to_attr_json(self):
|
|
|
|
attr_json = super().to_attr_json()
|
|
|
|
attr_json.update({
|
|
|
|
'is_sync_account': self.is_sync_account,
|
|
|
|
})
|
|
|
|
return attr_json
|
|
|
|
|
2022-10-27 10:53:10 +00:00
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
self.type = AutomationTypes.gather_accounts
|
|
|
|
super().save(*args, **kwargs)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
verbose_name = _("Gather asset accounts")
|