2019-12-05 07:09:25 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
from rest_framework import serializers
|
|
|
|
|
2023-01-16 11:02:09 +00:00
|
|
|
from common.serializers import BulkSerializerMixin
|
2020-10-22 10:13:14 +00:00
|
|
|
from perms.models import AssetPermission
|
2019-12-05 07:09:25 +00:00
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
'AssetPermissionUserRelationSerializer',
|
|
|
|
'AssetPermissionUserGroupRelationSerializer',
|
|
|
|
"AssetPermissionAssetRelationSerializer",
|
|
|
|
'AssetPermissionNodeRelationSerializer',
|
|
|
|
'AssetPermissionAllAssetSerializer',
|
|
|
|
'AssetPermissionAllUserSerializer',
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class CurrentAssetPermission(object):
|
|
|
|
permission = None
|
|
|
|
|
|
|
|
def set_context(self, serializer_field):
|
|
|
|
self.permission = serializer_field.context['permission']
|
|
|
|
|
|
|
|
def __call__(self):
|
|
|
|
return self.permission
|
|
|
|
|
|
|
|
|
|
|
|
class RelationMixin(BulkSerializerMixin, serializers.Serializer):
|
|
|
|
assetpermission_display = serializers.ReadOnlyField()
|
|
|
|
|
|
|
|
def get_field_names(self, declared_fields, info):
|
|
|
|
fields = super().get_field_names(declared_fields, info)
|
|
|
|
fields.extend(['assetpermission', "assetpermission_display"])
|
|
|
|
return fields
|
|
|
|
|
|
|
|
|
|
|
|
class AssetPermissionUserRelationSerializer(RelationMixin, serializers.ModelSerializer):
|
|
|
|
user_display = serializers.ReadOnlyField()
|
|
|
|
|
2021-07-08 06:23:18 +00:00
|
|
|
class Meta:
|
2019-12-05 07:09:25 +00:00
|
|
|
model = AssetPermission.users.through
|
|
|
|
fields = [
|
|
|
|
'id', 'user', 'user_display',
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2019-12-19 07:28:17 +00:00
|
|
|
class AssetPermissionAllUserSerializer(serializers.Serializer):
|
2019-12-05 07:09:25 +00:00
|
|
|
user = serializers.UUIDField(read_only=True, source='id')
|
|
|
|
user_display = serializers.SerializerMethodField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
only_fields = ['id', 'username', 'name']
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_user_display(obj):
|
|
|
|
return str(obj)
|
|
|
|
|
|
|
|
|
|
|
|
class AssetPermissionUserGroupRelationSerializer(RelationMixin, serializers.ModelSerializer):
|
|
|
|
usergroup_display = serializers.ReadOnlyField()
|
|
|
|
|
2021-07-08 06:23:18 +00:00
|
|
|
class Meta:
|
2019-12-05 07:09:25 +00:00
|
|
|
model = AssetPermission.user_groups.through
|
|
|
|
fields = [
|
|
|
|
'id', 'usergroup', "usergroup_display",
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class AssetPermissionAssetRelationSerializer(RelationMixin, serializers.ModelSerializer):
|
|
|
|
asset_display = serializers.ReadOnlyField()
|
|
|
|
|
2021-07-08 06:23:18 +00:00
|
|
|
class Meta:
|
2019-12-05 07:09:25 +00:00
|
|
|
model = AssetPermission.assets.through
|
|
|
|
fields = [
|
|
|
|
'id', "asset", "asset_display",
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2019-12-19 07:28:17 +00:00
|
|
|
class AssetPermissionAllAssetSerializer(serializers.Serializer):
|
2019-12-05 07:09:25 +00:00
|
|
|
asset = serializers.UUIDField(read_only=True, source='id')
|
|
|
|
asset_display = serializers.SerializerMethodField()
|
|
|
|
|
|
|
|
class Meta:
|
2022-09-21 03:17:14 +00:00
|
|
|
only_fields = ['id', 'name', 'address']
|
2019-12-05 07:09:25 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_asset_display(obj):
|
|
|
|
return str(obj)
|
|
|
|
|
|
|
|
|
|
|
|
class AssetPermissionNodeRelationSerializer(RelationMixin, serializers.ModelSerializer):
|
2020-08-16 15:08:58 +00:00
|
|
|
node_display = serializers.CharField(source='node.full_value', read_only=True)
|
2019-12-05 07:09:25 +00:00
|
|
|
|
2021-07-08 06:23:18 +00:00
|
|
|
class Meta:
|
2019-12-05 07:09:25 +00:00
|
|
|
model = AssetPermission.nodes.through
|
|
|
|
fields = [
|
|
|
|
'id', 'node', "node_display",
|
|
|
|
]
|