2022-08-24 08:36:42 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
from rest_framework import serializers
|
2021-07-08 06:23:18 +00:00
|
|
|
|
2022-05-17 13:12:59 +00:00
|
|
|
from common.drf.serializers import SecretReadableMixin
|
2022-11-08 11:18:04 +00:00
|
|
|
from common.drf.fields import ObjectRelatedField, LabeledChoiceField
|
2022-11-04 03:39:34 +00:00
|
|
|
from assets.tasks import push_accounts_to_assets
|
2022-09-13 13:18:04 +00:00
|
|
|
from assets.models import Account, AccountTemplate, Asset
|
2022-09-23 10:59:19 +00:00
|
|
|
from .base import BaseAccountSerializer
|
2022-11-08 11:18:04 +00:00
|
|
|
from assets.const import SecretType
|
2021-07-08 06:23:18 +00:00
|
|
|
|
|
|
|
|
2022-09-06 11:57:03 +00:00
|
|
|
class AccountSerializerCreateMixin(serializers.ModelSerializer):
|
|
|
|
template = serializers.UUIDField(
|
|
|
|
required=False, allow_null=True, write_only=True,
|
|
|
|
label=_('Account template')
|
|
|
|
)
|
2022-09-13 13:07:20 +00:00
|
|
|
push_now = serializers.BooleanField(
|
|
|
|
default=False, label=_("Push now"), write_only=True
|
|
|
|
)
|
2022-10-20 12:06:58 +00:00
|
|
|
has_secret = serializers.BooleanField(label=_("Has secret"), read_only=True)
|
2022-09-06 11:57:03 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def validate_template(value):
|
|
|
|
try:
|
2022-09-19 09:00:03 +00:00
|
|
|
return AccountTemplate.objects.get(id=value)
|
2022-09-06 11:57:03 +00:00
|
|
|
except AccountTemplate.DoesNotExist:
|
|
|
|
raise serializers.ValidationError(_('Account template not found'))
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def replace_attrs(account_template: AccountTemplate, attrs: dict):
|
|
|
|
exclude_fields = [
|
2022-09-23 10:59:19 +00:00
|
|
|
'_state', 'org_id', 'id', 'date_created',
|
|
|
|
'date_updated'
|
2022-09-06 11:57:03 +00:00
|
|
|
]
|
2022-09-19 09:00:03 +00:00
|
|
|
template_attrs = {
|
|
|
|
k: v for k, v in account_template.__dict__.items()
|
|
|
|
if k not in exclude_fields
|
|
|
|
}
|
2022-09-06 11:57:03 +00:00
|
|
|
for k, v in template_attrs.items():
|
|
|
|
attrs.setdefault(k, v)
|
|
|
|
|
|
|
|
def validate(self, attrs):
|
|
|
|
account_template = attrs.pop('template', None)
|
|
|
|
if account_template:
|
|
|
|
self.replace_attrs(account_template, attrs)
|
2022-09-13 13:07:20 +00:00
|
|
|
self.push_now = attrs.pop('push_now', False)
|
2022-09-06 11:57:03 +00:00
|
|
|
return super().validate(attrs)
|
|
|
|
|
2022-09-13 13:07:20 +00:00
|
|
|
def create(self, validated_data):
|
|
|
|
instance = super().create(validated_data)
|
|
|
|
if self.push_now:
|
2022-11-04 03:39:34 +00:00
|
|
|
push_accounts_to_assets.delay([instance.id], [instance.asset_id])
|
2022-09-13 13:07:20 +00:00
|
|
|
return instance
|
|
|
|
|
2022-09-06 11:57:03 +00:00
|
|
|
|
2022-09-23 10:59:19 +00:00
|
|
|
class AccountSerializer(AccountSerializerCreateMixin, BaseAccountSerializer):
|
2022-09-19 09:00:03 +00:00
|
|
|
asset = ObjectRelatedField(
|
|
|
|
required=False, queryset=Asset.objects,
|
2022-10-25 07:07:51 +00:00
|
|
|
label=_('Asset'), attrs=('id', 'name', 'address', 'platform_id')
|
2022-09-19 09:00:03 +00:00
|
|
|
)
|
2022-12-06 10:50:21 +00:00
|
|
|
su_from = ObjectRelatedField(
|
|
|
|
required=False, queryset=Account.objects,
|
|
|
|
label=_('Account'), attrs=('id', 'name', 'username')
|
|
|
|
)
|
2021-07-08 06:23:18 +00:00
|
|
|
|
2022-09-23 10:59:19 +00:00
|
|
|
class Meta(BaseAccountSerializer.Meta):
|
2022-07-15 10:03:32 +00:00
|
|
|
model = Account
|
2022-09-23 10:59:19 +00:00
|
|
|
fields = BaseAccountSerializer.Meta.fields \
|
2022-10-20 08:39:11 +00:00
|
|
|
+ ['su_from', 'version', 'asset'] \
|
|
|
|
+ ['template', 'push_now']
|
2022-09-23 10:59:19 +00:00
|
|
|
extra_kwargs = {
|
|
|
|
**BaseAccountSerializer.Meta.extra_kwargs,
|
|
|
|
'name': {'required': False, 'allow_null': True},
|
|
|
|
}
|
|
|
|
|
|
|
|
def __init__(self, *args, data=None, **kwargs):
|
|
|
|
super().__init__(*args, data=data, **kwargs)
|
|
|
|
if data and 'name' not in data:
|
2022-11-24 07:25:09 +00:00
|
|
|
username = data.get('username')
|
|
|
|
if username is not None:
|
|
|
|
data['name'] = username
|
2022-10-17 09:56:19 +00:00
|
|
|
if hasattr(self, 'initial_data') and \
|
|
|
|
not getattr(self, 'initial_data', None):
|
|
|
|
delattr(self, 'initial_data')
|
2022-02-15 02:46:16 +00:00
|
|
|
|
2021-07-08 06:23:18 +00:00
|
|
|
@classmethod
|
|
|
|
def setup_eager_loading(cls, queryset):
|
|
|
|
""" Perform necessary eager loading of data. """
|
2022-09-23 10:59:19 +00:00
|
|
|
queryset = queryset.prefetch_related('asset', 'asset__platform')
|
2021-07-08 06:23:18 +00:00
|
|
|
return queryset
|
|
|
|
|
|
|
|
|
2022-05-17 13:12:59 +00:00
|
|
|
class AccountSecretSerializer(SecretReadableMixin, AccountSerializer):
|
2021-07-08 06:23:18 +00:00
|
|
|
class Meta(AccountSerializer.Meta):
|
|
|
|
extra_kwargs = {
|
2022-10-18 12:37:17 +00:00
|
|
|
'secret': {'write_only': False},
|
2021-07-08 06:23:18 +00:00
|
|
|
}
|
2021-07-13 04:18:19 +00:00
|
|
|
|
|
|
|
|
2022-10-20 08:39:11 +00:00
|
|
|
class AccountHistorySerializer(serializers.ModelSerializer):
|
2022-11-08 11:18:04 +00:00
|
|
|
secret_type = LabeledChoiceField(choices=SecretType.choices, label=_('Secret type'))
|
|
|
|
|
2022-10-20 08:39:11 +00:00
|
|
|
class Meta:
|
|
|
|
model = Account.history.model
|
|
|
|
fields = ['id', 'secret', 'secret_type', 'version', 'history_date', 'history_user']
|
|
|
|
read_only_fields = fields
|
|
|
|
|
|
|
|
|
2021-07-13 04:18:19 +00:00
|
|
|
class AccountTaskSerializer(serializers.Serializer):
|
|
|
|
ACTION_CHOICES = (
|
|
|
|
('test', 'test'),
|
|
|
|
)
|
|
|
|
action = serializers.ChoiceField(choices=ACTION_CHOICES, write_only=True)
|
|
|
|
task = serializers.CharField(read_only=True)
|