jumpserver/apps/perms/serializers.py

67 lines
1.7 KiB
Python
Raw Normal View History

2016-10-19 11:30:55 +00:00
# -*- coding: utf-8 -*-
#
2016-11-10 08:59:50 +00:00
from django.utils.translation import ugettext_lazy as _
from rest_framework import serializers
from common.utils import get_object_or_none
2016-11-04 10:33:16 +00:00
from .models import AssetPermission
2016-11-10 08:59:50 +00:00
from .hands import User
2016-11-04 10:33:16 +00:00
class AssetPermissionSerializer(serializers.ModelSerializer):
assets_ = serializers.SerializerMethodField()
asset_groups_ = serializers.SerializerMethodField()
users_ = serializers.SerializerMethodField()
user_groups_ = serializers.SerializerMethodField()
system_users_ = serializers.SerializerMethodField()
2016-11-04 10:33:16 +00:00
class Meta:
model = AssetPermission
2017-01-20 05:59:53 +00:00
fields = '__all__'
2016-11-10 08:59:50 +00:00
@staticmethod
def get_assets_(obj):
return [asset.hostname for asset in obj.assets.all()]
@staticmethod
def get_asset_groups_(obj):
return [group.name for group in obj.asset_groups.all()]
@staticmethod
def get_users_(obj):
return [user.username for user in obj.users.all()]
@staticmethod
def get_user_groups_(obj):
return [group.name for group in obj.user_groups.all()]
@staticmethod
def get_system_users_(obj):
return [user.username for user in obj.system_users.all()]
class AssetPermissionUpdateUserSerializer(serializers.ModelSerializer):
class Meta:
model = AssetPermission
fields = ['id', 'users']
class AssetPermissionUpdateAssetSerializer(serializers.ModelSerializer):
class Meta:
model = AssetPermission
fields = ['id', 'assets']
2016-11-10 08:59:50 +00:00
class UserAssetPermissionSerializer(AssetPermissionSerializer):
is_inherited = serializers.SerializerMethodField()
@staticmethod
def get_is_inherited(obj):
if getattr(obj, 'inherited', ''):
return True
else:
return False