2022-07-15 10:03:32 +00:00
|
|
|
from django.db.models import F
|
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
|
|
|
|
|
|
|
from orgs.mixins.serializers import BulkOrgResourceModelSerializer
|
2022-05-17 13:12:59 +00:00
|
|
|
from common.drf.serializers import SecretReadableMixin
|
2022-09-13 13:18:04 +00:00
|
|
|
from common.drf.fields import ObjectRelatedField
|
|
|
|
from assets.models import Account, AccountTemplate, Asset
|
2022-09-06 11:57:03 +00:00
|
|
|
from assets.serializers.base import AuthValidateMixin
|
|
|
|
from .common import AccountFieldsSerializerMixin
|
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-09-06 11:57:03 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def validate_template(value):
|
|
|
|
AccountTemplate.objects.get_or_create()
|
|
|
|
model = AccountTemplate
|
|
|
|
try:
|
|
|
|
return model.objects.get(id=value)
|
|
|
|
except AccountTemplate.DoesNotExist:
|
|
|
|
raise serializers.ValidationError(_('Account template not found'))
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def replace_attrs(account_template: AccountTemplate, attrs: dict):
|
|
|
|
exclude_fields = [
|
|
|
|
'_state', 'org_id', 'date_verified', 'id',
|
|
|
|
'date_created', 'date_updated', 'created_by'
|
|
|
|
]
|
|
|
|
template_attrs = {k: v for k, v in account_template.__dict__.items() if k not in exclude_fields}
|
|
|
|
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:
|
|
|
|
print("Start push account to asset")
|
|
|
|
# Todo: push it
|
|
|
|
pass
|
|
|
|
return instance
|
|
|
|
|
2022-09-06 11:57:03 +00:00
|
|
|
|
|
|
|
class AccountSerializer(AuthValidateMixin,
|
|
|
|
AccountSerializerCreateMixin,
|
|
|
|
AccountFieldsSerializerMixin,
|
|
|
|
BulkOrgResourceModelSerializer):
|
2022-09-13 13:18:04 +00:00
|
|
|
asset = ObjectRelatedField(required=False, queryset=Asset.objects, label=_('Asset'), attrs=('id', 'name', 'ip'))
|
2022-01-17 07:58:39 +00:00
|
|
|
platform = serializers.ReadOnlyField(label=_("Platform"))
|
2021-07-08 06:23:18 +00:00
|
|
|
|
2022-09-06 11:57:03 +00:00
|
|
|
class Meta(AccountFieldsSerializerMixin.Meta):
|
2022-07-15 10:03:32 +00:00
|
|
|
model = Account
|
2022-09-06 11:57:03 +00:00
|
|
|
fields = AccountFieldsSerializerMixin.Meta.fields \
|
2022-09-13 13:07:20 +00:00
|
|
|
+ ['template', 'push_now']
|
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-14 12:55:14 +00:00
|
|
|
queryset = queryset.prefetch_related('asset')
|
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):
|
2022-01-17 07:58:39 +00:00
|
|
|
fields_backup = [
|
2022-08-11 07:45:03 +00:00
|
|
|
'name', 'ip', 'platform', 'protocols', 'username', 'password',
|
2022-01-17 07:58:39 +00:00
|
|
|
'private_key', 'public_key', 'date_created', 'date_updated', 'version'
|
|
|
|
]
|
2021-07-08 06:23:18 +00:00
|
|
|
extra_kwargs = {
|
|
|
|
'password': {'write_only': False},
|
|
|
|
'private_key': {'write_only': False},
|
|
|
|
'public_key': {'write_only': False},
|
|
|
|
}
|
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)
|