2016-10-19 11:30:55 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
|
2021-07-30 11:13:47 +00:00
|
|
|
from django.db.models import Q
|
2022-11-11 07:04:31 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
from rest_framework import serializers
|
2021-04-29 11:10:45 +00:00
|
|
|
|
2022-08-17 03:54:18 +00:00
|
|
|
from assets.models import Asset, Node
|
2022-11-11 07:04:31 +00:00
|
|
|
from common.drf.fields import BitChoicesField, ObjectRelatedField
|
|
|
|
from orgs.mixins.serializers import BulkOrgResourceModelSerializer
|
|
|
|
from perms.models import ActionChoices, AssetPermission
|
2021-02-08 06:59:20 +00:00
|
|
|
from users.models import User, UserGroup
|
2022-09-07 09:35:23 +00:00
|
|
|
|
2022-11-11 07:04:31 +00:00
|
|
|
__all__ = ["AssetPermissionSerializer", "ActionChoicesField"]
|
2022-09-07 09:35:23 +00:00
|
|
|
|
|
|
|
|
2022-11-11 07:04:31 +00:00
|
|
|
class ActionChoicesField(BitChoicesField):
|
2022-09-07 09:35:23 +00:00
|
|
|
def __init__(self, **kwargs):
|
2022-11-15 06:59:22 +00:00
|
|
|
super().__init__(choice_cls=ActionChoices, **kwargs)
|
2019-06-30 12:10:34 +00:00
|
|
|
|
2022-09-07 09:35:23 +00:00
|
|
|
|
|
|
|
class AssetPermissionSerializer(BulkOrgResourceModelSerializer):
|
2022-12-26 08:15:44 +00:00
|
|
|
users = ObjectRelatedField(queryset=User.objects, many=True, required=False, label=_('User'))
|
2022-11-11 07:04:31 +00:00
|
|
|
user_groups = ObjectRelatedField(
|
2022-12-26 08:15:44 +00:00
|
|
|
queryset=UserGroup.objects, many=True, required=False, label=_('User group')
|
2022-11-11 07:04:31 +00:00
|
|
|
)
|
2022-12-26 08:15:44 +00:00
|
|
|
assets = ObjectRelatedField(queryset=Asset.objects, many=True, required=False, label=_('Asset'))
|
|
|
|
nodes = ObjectRelatedField(queryset=Node.objects, many=True, required=False, label=_('Node'))
|
2022-11-11 07:04:31 +00:00
|
|
|
actions = ActionChoicesField(required=False, allow_null=True, label=_("Actions"))
|
2021-07-30 11:13:47 +00:00
|
|
|
is_valid = serializers.BooleanField(read_only=True, label=_("Is valid"))
|
2022-11-11 07:04:31 +00:00
|
|
|
is_expired = serializers.BooleanField(read_only=True, label=_("Is expired"))
|
|
|
|
accounts = serializers.ListField(label=_("Accounts"), required=False)
|
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
|
2022-11-11 07:04:31 +00:00
|
|
|
fields_mini = ["id", "name"]
|
2021-04-28 08:42:54 +00:00
|
|
|
fields_small = fields_mini + [
|
2022-11-11 07:04:31 +00:00
|
|
|
"accounts",
|
|
|
|
"is_active",
|
|
|
|
"is_expired",
|
|
|
|
"is_valid",
|
|
|
|
"actions",
|
|
|
|
"created_by",
|
|
|
|
"date_created",
|
|
|
|
"date_expired",
|
|
|
|
"date_start",
|
|
|
|
"comment",
|
|
|
|
"from_ticket",
|
2020-05-09 06:51:19 +00:00
|
|
|
]
|
2021-04-28 08:42:54 +00:00
|
|
|
fields_m2m = [
|
2022-11-11 07:04:31 +00:00
|
|
|
"users",
|
|
|
|
"user_groups",
|
|
|
|
"assets",
|
|
|
|
"nodes",
|
2020-05-09 06:51:19 +00:00
|
|
|
]
|
2021-04-28 08:42:54 +00:00
|
|
|
fields = fields_small + fields_m2m
|
2022-11-11 07:04:31 +00:00
|
|
|
read_only_fields = ["created_by", "date_created", "from_ticket"]
|
2020-11-11 02:27:18 +00:00
|
|
|
extra_kwargs = {
|
2022-11-11 07:04:31 +00:00
|
|
|
"actions": {"label": _("Actions")},
|
|
|
|
"is_expired": {"label": _("Is expired")},
|
|
|
|
"is_valid": {"label": _("Is valid")},
|
2020-11-11 02:27:18 +00:00
|
|
|
}
|
2020-05-09 06:51:19 +00:00
|
|
|
|
2022-09-29 12:41:40 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.set_actions_field()
|
|
|
|
|
|
|
|
def set_actions_field(self):
|
2022-11-11 07:04:31 +00:00
|
|
|
actions = self.fields.get("actions")
|
2022-09-29 12:41:40 +00:00
|
|
|
if not actions:
|
|
|
|
return
|
|
|
|
choices = actions._choices
|
|
|
|
actions._choices = choices
|
|
|
|
actions.default = list(choices.keys())
|
|
|
|
|
2020-05-09 06:51:19 +00:00
|
|
|
@classmethod
|
|
|
|
def setup_eager_loading(cls, queryset):
|
2022-11-11 07:04:31 +00:00
|
|
|
"""Perform necessary eager loading of data."""
|
2021-07-30 11:13:47 +00:00
|
|
|
queryset = queryset.prefetch_related(
|
2022-11-11 07:04:31 +00:00
|
|
|
"users",
|
|
|
|
"user_groups",
|
|
|
|
"assets",
|
|
|
|
"nodes",
|
2021-07-30 11:13:47 +00:00
|
|
|
)
|
2020-05-09 06:51:19 +00:00
|
|
|
return queryset
|
2021-04-28 08:42:54 +00:00
|
|
|
|
2021-07-30 11:13:47 +00:00
|
|
|
@staticmethod
|
|
|
|
def perform_display_create(instance, **kwargs):
|
2021-04-28 08:42:54 +00:00
|
|
|
# 用户
|
|
|
|
users_to_set = User.objects.filter(
|
2022-11-11 07:04:31 +00:00
|
|
|
Q(name__in=kwargs.get("users_display"))
|
|
|
|
| Q(username__in=kwargs.get("users_display"))
|
2021-04-28 08:42:54 +00:00
|
|
|
).distinct()
|
2021-04-30 03:32:33 +00:00
|
|
|
instance.users.add(*users_to_set)
|
2021-04-28 08:42:54 +00:00
|
|
|
# 用户组
|
2021-07-30 11:13:47 +00:00
|
|
|
user_groups_to_set = UserGroup.objects.filter(
|
2022-11-11 07:04:31 +00:00
|
|
|
name__in=kwargs.get("user_groups_display")
|
2021-07-30 11:13:47 +00:00
|
|
|
).distinct()
|
2021-04-30 03:32:33 +00:00
|
|
|
instance.user_groups.add(*user_groups_to_set)
|
2021-04-28 08:42:54 +00:00
|
|
|
# 资产
|
|
|
|
assets_to_set = Asset.objects.filter(
|
2022-11-11 07:04:31 +00:00
|
|
|
Q(address__in=kwargs.get("assets_display"))
|
|
|
|
| Q(name__in=kwargs.get("assets_display"))
|
2021-04-28 08:42:54 +00:00
|
|
|
).distinct()
|
2021-04-30 03:32:33 +00:00
|
|
|
instance.assets.add(*assets_to_set)
|
2021-04-28 08:42:54 +00:00
|
|
|
# 节点
|
2021-07-30 11:13:47 +00:00
|
|
|
nodes_to_set = Node.objects.filter(
|
2022-11-11 07:04:31 +00:00
|
|
|
full_value__in=kwargs.get("nodes_display")
|
2021-07-30 11:13:47 +00:00
|
|
|
).distinct()
|
2021-04-30 03:32:33 +00:00
|
|
|
instance.nodes.add(*nodes_to_set)
|
2021-04-28 08:42:54 +00:00
|
|
|
|
|
|
|
def create(self, validated_data):
|
|
|
|
display = {
|
2022-11-11 07:04:31 +00:00
|
|
|
"users_display": validated_data.pop("users_display", ""),
|
|
|
|
"user_groups_display": validated_data.pop("user_groups_display", ""),
|
|
|
|
"assets_display": validated_data.pop("assets_display", ""),
|
|
|
|
"nodes_display": validated_data.pop("nodes_display", ""),
|
2021-04-28 08:42:54 +00:00
|
|
|
}
|
|
|
|
instance = super().create(validated_data)
|
|
|
|
self.perform_display_create(instance, **display)
|
|
|
|
return instance
|