jumpserver/apps/perms/serializers/asset_permission.py

71 lines
2.1 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 rest_framework import serializers
2018-10-30 04:06:39 +00:00
2018-04-10 12:29:06 +00:00
from common.fields import StringManyToManyField
from orgs.mixins import BulkOrgResourceModelSerializer
from perms.models import AssetPermission, ActionFlag
2016-11-04 10:33:16 +00:00
__all__ = [
'AssetPermissionCreateUpdateSerializer', 'AssetPermissionListSerializer',
'AssetPermissionUpdateUserSerializer', 'AssetPermissionUpdateAssetSerializer',
'ActionField',
]
2016-11-04 10:33:16 +00:00
2019-06-30 12:10:34 +00:00
class ActionField(serializers.MultipleChoiceField):
def __init__(self, *args, **kwargs):
kwargs['choices'] = ActionFlag.CHOICES
super().__init__(*args, **kwargs)
def to_representation(self, value):
return ActionFlag.value_to_choices(value)
def to_internal_value(self, data):
return ActionFlag.choices_to_value(data)
class ActionDisplayField(ActionField):
def to_representation(self, value):
values = super().to_representation(value)
choices = dict(ActionFlag.CHOICES)
return [choices.get(i) for i in values]
class AssetPermissionCreateUpdateSerializer(BulkOrgResourceModelSerializer):
2019-07-01 10:22:40 +00:00
actions = ActionField()
2019-06-30 12:10:34 +00:00
2016-11-04 10:33:16 +00:00
class Meta:
2018-04-08 12:02:40 +00:00
model = AssetPermission
exclude = ('created_by', 'date_created')
class AssetPermissionListSerializer(BulkOrgResourceModelSerializer):
2018-04-10 12:29:06 +00:00
users = StringManyToManyField(many=True, read_only=True)
user_groups = StringManyToManyField(many=True, read_only=True)
assets = StringManyToManyField(many=True, read_only=True)
nodes = StringManyToManyField(many=True, read_only=True)
system_users = StringManyToManyField(many=True, read_only=True)
2019-07-01 10:22:40 +00:00
actions = ActionDisplayField()
is_valid = serializers.BooleanField()
is_expired = serializers.BooleanField()
2018-04-10 12:29:06 +00:00
2018-02-01 09:14:15 +00:00
class Meta:
2018-04-08 12:02:40 +00:00
model = AssetPermission
2018-02-01 09:14:15 +00:00
fields = '__all__'
class AssetPermissionUpdateUserSerializer(serializers.ModelSerializer):
class Meta:
model = AssetPermission
fields = ['id', 'users']
class AssetPermissionUpdateAssetSerializer(serializers.ModelSerializer):
class Meta:
model = AssetPermission
fields = ['id', 'assets']