2022-09-07 11:49:42 +00:00
|
|
|
from django.db import models
|
2023-07-24 03:52:25 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2022-09-07 11:49:42 +00:00
|
|
|
|
2023-01-16 11:02:09 +00:00
|
|
|
from accounts.const import (
|
2023-08-28 07:43:45 +00:00
|
|
|
AutomationTypes
|
2023-01-16 11:02:09 +00:00
|
|
|
)
|
2023-03-08 10:52:00 +00:00
|
|
|
from common.db import fields
|
|
|
|
from common.db.models import JMSBaseModel
|
2023-08-28 07:43:45 +00:00
|
|
|
from .base import AccountBaseAutomation, ChangeSecretMixin
|
2022-09-07 11:49:42 +00:00
|
|
|
|
2023-08-28 07:43:45 +00:00
|
|
|
__all__ = ['ChangeSecretAutomation', 'ChangeSecretRecord', ]
|
2023-01-16 11:02:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ChangeSecretAutomation(ChangeSecretMixin, AccountBaseAutomation):
|
2022-11-08 09:54:51 +00:00
|
|
|
recipients = models.ManyToManyField('users.User', verbose_name=_("Recipient"), blank=True)
|
2022-09-07 11:49:42 +00:00
|
|
|
|
2022-10-12 10:08:57 +00:00
|
|
|
def save(self, *args, **kwargs):
|
2022-10-19 09:05:21 +00:00
|
|
|
self.type = AutomationTypes.change_secret
|
2022-10-12 10:08:57 +00:00
|
|
|
super().save(*args, **kwargs)
|
|
|
|
|
2022-09-07 11:49:42 +00:00
|
|
|
class Meta:
|
2022-10-19 03:39:11 +00:00
|
|
|
verbose_name = _("Change secret automation")
|
2022-09-07 11:49:42 +00:00
|
|
|
|
2022-10-19 09:05:21 +00:00
|
|
|
def to_attr_json(self):
|
|
|
|
attr_json = super().to_attr_json()
|
|
|
|
attr_json.update({
|
|
|
|
'recipients': {
|
|
|
|
str(recipient.id): (str(recipient), bool(recipient.secret_key))
|
|
|
|
for recipient in self.recipients.all()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return attr_json
|
|
|
|
|
2022-10-13 09:47:29 +00:00
|
|
|
|
|
|
|
class ChangeSecretRecord(JMSBaseModel):
|
2023-01-16 11:02:09 +00:00
|
|
|
execution = models.ForeignKey('accounts.AutomationExecution', on_delete=models.CASCADE)
|
2022-11-03 14:39:48 +00:00
|
|
|
asset = models.ForeignKey('assets.Asset', on_delete=models.CASCADE, null=True)
|
2023-01-16 11:02:09 +00:00
|
|
|
account = models.ForeignKey('accounts.Account', on_delete=models.CASCADE, null=True)
|
2022-10-13 09:47:29 +00:00
|
|
|
old_secret = fields.EncryptTextField(blank=True, null=True, verbose_name=_('Old secret'))
|
2023-08-14 06:47:51 +00:00
|
|
|
new_secret = fields.EncryptTextField(blank=True, null=True, verbose_name=_('New secret'))
|
2022-10-13 09:47:29 +00:00
|
|
|
date_started = models.DateTimeField(blank=True, null=True, verbose_name=_('Date started'))
|
|
|
|
date_finished = models.DateTimeField(blank=True, null=True, verbose_name=_('Date finished'))
|
2023-10-30 06:24:34 +00:00
|
|
|
status = models.CharField(max_length=16, default='pending', verbose_name=_('Status'))
|
2022-10-13 09:47:29 +00:00
|
|
|
error = models.TextField(blank=True, null=True, verbose_name=_('Error'))
|
|
|
|
|
|
|
|
class Meta:
|
2023-01-16 11:02:09 +00:00
|
|
|
ordering = ('-date_created',)
|
2022-10-19 09:05:21 +00:00
|
|
|
verbose_name = _("Change secret record")
|
2022-10-13 09:47:29 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.account.__str__()
|