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
|
2019-08-21 12:27:21 +00:00
|
|
|
from orgs.mixins.serializers import BulkOrgResourceModelSerializer
|
2019-07-02 08:45:26 +00:00
|
|
|
from perms.models import AssetPermission, Action
|
2019-07-12 10:31:14 +00:00
|
|
|
from assets.models import Asset
|
2016-11-04 10:33:16 +00:00
|
|
|
|
2018-12-10 02:11:54 +00:00
|
|
|
__all__ = [
|
|
|
|
'AssetPermissionCreateUpdateSerializer', 'AssetPermissionListSerializer',
|
|
|
|
'AssetPermissionUpdateUserSerializer', 'AssetPermissionUpdateAssetSerializer',
|
2019-07-12 10:31:14 +00:00
|
|
|
'ActionsField', 'AssetPermissionAssetsSerializer',
|
2018-12-10 02:11:54 +00:00
|
|
|
]
|
|
|
|
|
2016-11-04 10:33:16 +00:00
|
|
|
|
2019-07-02 09:51:50 +00:00
|
|
|
class ActionsField(serializers.MultipleChoiceField):
|
2019-06-30 12:10:34 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
2019-07-02 08:45:26 +00:00
|
|
|
kwargs['choices'] = Action.CHOICES
|
2019-06-30 12:10:34 +00:00
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
def to_representation(self, value):
|
2019-07-02 08:45:26 +00:00
|
|
|
return Action.value_to_choices(value)
|
2019-06-30 12:10:34 +00:00
|
|
|
|
|
|
|
def to_internal_value(self, data):
|
2019-07-02 09:51:50 +00:00
|
|
|
if data is None:
|
|
|
|
return data
|
2019-07-02 08:45:26 +00:00
|
|
|
return Action.choices_to_value(data)
|
2019-06-30 12:10:34 +00:00
|
|
|
|
|
|
|
|
2019-07-02 09:51:50 +00:00
|
|
|
class ActionsDisplayField(ActionsField):
|
2019-06-30 12:10:34 +00:00
|
|
|
def to_representation(self, value):
|
|
|
|
values = super().to_representation(value)
|
2019-07-02 08:45:26 +00:00
|
|
|
choices = dict(Action.CHOICES)
|
2019-06-30 12:10:34 +00:00
|
|
|
return [choices.get(i) for i in values]
|
|
|
|
|
|
|
|
|
2019-06-19 08:45:14 +00:00
|
|
|
class AssetPermissionCreateUpdateSerializer(BulkOrgResourceModelSerializer):
|
2019-07-02 09:51:50 +00:00
|
|
|
actions = ActionsField(required=False, allow_null=True)
|
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
|
2018-05-04 06:54:58 +00:00
|
|
|
exclude = ('created_by', 'date_created')
|
2017-12-19 11:38:09 +00:00
|
|
|
|
|
|
|
|
2019-06-19 08:45:14 +00:00
|
|
|
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-02 09:51:50 +00:00
|
|
|
actions = ActionsDisplayField()
|
2019-01-15 11:01:33 +00:00
|
|
|
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__'
|
2017-12-19 11:38:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AssetPermissionUpdateUserSerializer(serializers.ModelSerializer):
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = AssetPermission
|
|
|
|
fields = ['id', 'users']
|
|
|
|
|
|
|
|
|
|
|
|
class AssetPermissionUpdateAssetSerializer(serializers.ModelSerializer):
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = AssetPermission
|
|
|
|
fields = ['id', 'assets']
|
2019-07-12 10:31:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AssetPermissionAssetsSerializer(serializers.ModelSerializer):
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Asset
|
|
|
|
only_fields = ['id', 'hostname', 'ip']
|
|
|
|
fields = tuple(only_fields)
|