jumpserver/apps/assets/serializers/account/template.py

39 lines
1.4 KiB
Python
Raw Normal View History

2022-08-24 08:36:42 +00:00
from django.utils.translation import ugettext_lazy as _
from rest_framework import serializers
from assets.models import AccountTemplate
from orgs.mixins.serializers import BulkOrgResourceModelSerializer
2022-09-06 11:57:03 +00:00
from assets.serializers.base import AuthValidateMixin
from .common import AccountFieldsSerializerMixin
2022-08-24 08:36:42 +00:00
2022-09-06 11:57:03 +00:00
class AccountTemplateSerializer(AuthValidateMixin, BulkOrgResourceModelSerializer):
2022-08-24 08:36:42 +00:00
class Meta:
model = AccountTemplate
2022-09-15 13:20:56 +00:00
fields_mini = ['id', 'name', 'username', 'privileged']
2022-09-06 11:57:03 +00:00
fields_write_only = AccountFieldsSerializerMixin.Meta.fields_write_only
fields_other = AccountFieldsSerializerMixin.Meta.fields_other
2022-08-24 08:36:42 +00:00
fields = fields_mini + fields_write_only + fields_other
extra_kwargs = {
'username': {'required': True},
2022-09-15 13:20:56 +00:00
'name': {'required': True},
2022-08-24 08:36:42 +00:00
'private_key': {'write_only': True},
'public_key': {'write_only': True},
}
def validate(self, attrs):
attrs = self._validate_gen_key(attrs)
return attrs
@classmethod
def validate_required(cls, attrs):
required_field_dict = {}
error = _('This field is required.')
for k, v in cls().fields.items():
if v.required and k not in attrs:
required_field_dict[k] = error
if not required_field_dict:
return
raise serializers.ValidationError(required_field_dict)