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
|
|
|
|
2023-01-16 11:02:09 +00:00
|
|
|
from assets.models import Asset
|
|
|
|
from accounts.const import SecretType, Source
|
|
|
|
from accounts.models import Account, AccountTemplate
|
|
|
|
from accounts.tasks import push_accounts_to_assets
|
|
|
|
from common.serializers.fields import ObjectRelatedField, LabeledChoiceField
|
|
|
|
from common.serializers import SecretReadableMixin, BulkModelSerializer
|
2022-09-23 10:59:19 +00:00
|
|
|
from .base import BaseAccountSerializer
|
2021-07-08 06:23:18 +00:00
|
|
|
|
|
|
|
|
2023-01-30 10:59:12 +00:00
|
|
|
class AccountSerializerCreateValidateMixin:
|
2023-02-01 06:43:58 +00:00
|
|
|
id: str
|
|
|
|
template: bool
|
2023-01-30 10:59:12 +00:00
|
|
|
push_now: bool
|
2023-02-01 06:43:58 +00:00
|
|
|
replace_attrs: callable
|
2023-01-30 10:59:12 +00:00
|
|
|
|
2023-02-01 06:43:58 +00:00
|
|
|
def to_internal_value(self, data):
|
2023-02-01 07:48:30 +00:00
|
|
|
ret = super().to_internal_value(data)
|
|
|
|
self.id = ret.pop('id', None)
|
|
|
|
self.push_now = ret.pop('push_now', False)
|
|
|
|
self.template = ret.pop('template', False)
|
|
|
|
return ret
|
2023-02-01 06:43:58 +00:00
|
|
|
|
|
|
|
def set_secret(self, attrs):
|
|
|
|
_id = self.id
|
|
|
|
template = self.template
|
|
|
|
|
|
|
|
if _id and template:
|
2023-01-30 10:59:12 +00:00
|
|
|
account_template = AccountTemplate.objects.get(id=_id)
|
|
|
|
attrs['secret'] = account_template.secret
|
2023-02-01 06:43:58 +00:00
|
|
|
elif _id and not template:
|
|
|
|
account = Account.objects.get(id=_id)
|
|
|
|
attrs['secret'] = account.secret
|
|
|
|
|
|
|
|
def validate(self, attrs):
|
|
|
|
self.set_secret(attrs)
|
2023-01-30 10:59:12 +00:00
|
|
|
return super().validate(attrs)
|
|
|
|
|
2023-02-01 06:43:58 +00:00
|
|
|
def create(self, validated_data):
|
|
|
|
instance = super().create(validated_data)
|
|
|
|
if self.push_now:
|
|
|
|
push_accounts_to_assets.delay([instance.id], [instance.asset_id])
|
|
|
|
return instance
|
|
|
|
|
2023-01-30 10:59:12 +00:00
|
|
|
|
|
|
|
class AccountSerializerCreateMixin(
|
|
|
|
AccountSerializerCreateValidateMixin, BulkModelSerializer
|
|
|
|
):
|
2023-02-01 06:43:58 +00:00
|
|
|
template = serializers.BooleanField(
|
|
|
|
default=False, label=_("Template"), write_only=True
|
2022-09-06 11:57:03 +00:00
|
|
|
)
|
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
|
|
|
|
|
|
|
|
2022-12-21 10:36:15 +00:00
|
|
|
class AccountAssetSerializer(serializers.ModelSerializer):
|
|
|
|
platform = ObjectRelatedField(read_only=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Asset
|
|
|
|
fields = ['id', 'name', 'address', 'platform']
|
|
|
|
|
|
|
|
def to_internal_value(self, data):
|
|
|
|
if isinstance(data, dict):
|
|
|
|
i = data.get('id')
|
|
|
|
else:
|
|
|
|
i = data
|
|
|
|
|
|
|
|
try:
|
|
|
|
return Asset.objects.get(id=i)
|
|
|
|
except Asset.DoesNotExist:
|
|
|
|
raise serializers.ValidationError(_('Asset not found'))
|
|
|
|
|
|
|
|
|
2022-09-23 10:59:19 +00:00
|
|
|
class AccountSerializer(AccountSerializerCreateMixin, BaseAccountSerializer):
|
2022-12-21 10:36:15 +00:00
|
|
|
asset = AccountAssetSerializer(label=_('Asset'))
|
2022-12-27 09:54:34 +00:00
|
|
|
source = LabeledChoiceField(choices=Source.choices, label=_("Source"), read_only=True)
|
2022-12-06 10:50:21 +00:00
|
|
|
su_from = ObjectRelatedField(
|
2022-12-06 11:41:26 +00:00
|
|
|
required=False, queryset=Account.objects, allow_null=True, allow_empty=True,
|
2022-12-07 11:26:25 +00:00
|
|
|
label=_('Su from'), attrs=('id', 'name', 'username')
|
2022-12-06 10:50:21 +00:00
|
|
|
)
|
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-12-21 10:36:15 +00:00
|
|
|
+ ['su_from', 'version', 'asset'] \
|
2022-12-27 09:54:34 +00:00
|
|
|
+ ['template', 'push_now', 'source']
|
2022-09-23 10:59:19 +00:00
|
|
|
extra_kwargs = {
|
|
|
|
**BaseAccountSerializer.Meta.extra_kwargs,
|
|
|
|
'name': {'required': False, 'allow_null': True},
|
|
|
|
}
|
|
|
|
|
2022-12-21 10:36:15 +00:00
|
|
|
def validate_name(self, value):
|
|
|
|
if not value:
|
|
|
|
value = self.initial_data.get('username')
|
|
|
|
return value
|
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)
|