mirror of https://github.com/jumpserver/jumpserver
parent
28d117bf3d
commit
f18f2df4ab
|
@ -10,7 +10,25 @@ from common.serializers import SecretReadableMixin, BulkModelSerializer
|
|||
from .base import BaseAccountSerializer
|
||||
|
||||
|
||||
class AccountSerializerCreateMixin(BulkModelSerializer):
|
||||
class AccountSerializerCreateValidateMixin:
|
||||
replace_attrs: callable
|
||||
push_now: bool
|
||||
|
||||
def validate(self, attrs):
|
||||
_id = attrs.pop('id', None)
|
||||
if _id:
|
||||
account_template = AccountTemplate.objects.get(id=_id)
|
||||
attrs['secret'] = account_template.secret
|
||||
account_template = attrs.pop('template', None)
|
||||
if account_template:
|
||||
self.replace_attrs(account_template, attrs)
|
||||
self.push_now = attrs.pop('push_now', False)
|
||||
return super().validate(attrs)
|
||||
|
||||
|
||||
class AccountSerializerCreateMixin(
|
||||
AccountSerializerCreateValidateMixin, BulkModelSerializer
|
||||
):
|
||||
template = serializers.UUIDField(
|
||||
required=False, allow_null=True, write_only=True,
|
||||
label=_('Account template')
|
||||
|
@ -40,13 +58,6 @@ class AccountSerializerCreateMixin(BulkModelSerializer):
|
|||
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)
|
||||
self.push_now = attrs.pop('push_now', False)
|
||||
return super().validate(attrs)
|
||||
|
||||
def create(self, validated_data):
|
||||
instance = super().create(validated_data)
|
||||
if self.push_now:
|
||||
|
|
|
@ -7,6 +7,7 @@ from django.utils.translation import ugettext_lazy as _
|
|||
from rest_framework import serializers
|
||||
|
||||
from accounts.models import Account, AccountTemplate
|
||||
from accounts.serializers import AccountSerializerCreateValidateMixin
|
||||
from common.serializers import WritableNestedModelSerializer, SecretReadableMixin, CommonModelSerializer
|
||||
from common.serializers.fields import LabeledChoiceField
|
||||
from orgs.mixins.serializers import BulkOrgResourceModelSerializer
|
||||
|
@ -46,7 +47,9 @@ class AssetPlatformSerializer(serializers.ModelSerializer):
|
|||
}
|
||||
|
||||
|
||||
class AssetAccountSerializer(CommonModelSerializer):
|
||||
class AssetAccountSerializer(
|
||||
AccountSerializerCreateValidateMixin, CommonModelSerializer
|
||||
):
|
||||
add_org_fields = False
|
||||
push_now = serializers.BooleanField(
|
||||
default=False, label=_("Push now"), write_only=True
|
||||
|
@ -91,13 +94,6 @@ class AssetAccountSerializer(CommonModelSerializer):
|
|||
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)
|
||||
self.push_now = attrs.pop('push_now', False)
|
||||
return super().validate(attrs)
|
||||
|
||||
def create(self, validated_data):
|
||||
from accounts.tasks import push_accounts_to_assets
|
||||
instance = super().create(validated_data)
|
||||
|
@ -240,7 +236,6 @@ class AssetSerializer(BulkOrgResourceModelSerializer, WritableNestedModelSeriali
|
|||
class DetailMixin(serializers.Serializer):
|
||||
accounts = AssetAccountSerializer(many=True, required=False, label=_('Accounts'))
|
||||
|
||||
|
||||
def get_field_names(self, declared_fields, info):
|
||||
names = super().get_field_names(declared_fields, info)
|
||||
names.extend([
|
||||
|
|
Loading…
Reference in New Issue