2019-12-05 07:09:25 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2020-03-16 11:15:29 +00:00
|
|
|
from collections import defaultdict
|
2021-09-09 08:04:54 +00:00
|
|
|
from django.db.models import F, Value, Model
|
2020-03-16 11:15:29 +00:00
|
|
|
from django.db.models.signals import m2m_changed
|
2019-12-05 07:09:25 +00:00
|
|
|
from django.db.models.functions import Concat
|
|
|
|
|
2021-07-08 06:23:18 +00:00
|
|
|
from common.utils import get_logger
|
2019-12-05 07:09:25 +00:00
|
|
|
from orgs.mixins.api import OrgBulkModelViewSet
|
|
|
|
from orgs.utils import current_org
|
|
|
|
from .. import models, serializers
|
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
__all__ = [
|
|
|
|
'SystemUserAssetRelationViewSet', 'SystemUserNodeRelationViewSet',
|
2021-09-09 08:04:54 +00:00
|
|
|
'SystemUserUserRelationViewSet', 'BaseRelationViewSet',
|
2020-03-12 08:24:38 +00:00
|
|
|
]
|
2019-12-05 07:09:25 +00:00
|
|
|
|
2021-07-08 06:23:18 +00:00
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
2019-12-05 07:09:25 +00:00
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
class RelationMixin:
|
2021-09-09 08:04:54 +00:00
|
|
|
model: Model
|
|
|
|
|
2019-12-05 07:09:25 +00:00
|
|
|
def get_queryset(self):
|
|
|
|
queryset = self.model.objects.all()
|
2021-03-18 08:44:32 +00:00
|
|
|
if not current_org.is_root():
|
|
|
|
org_id = current_org.org_id()
|
2019-12-05 07:09:25 +00:00
|
|
|
queryset = queryset.filter(systemuser__org_id=org_id)
|
2021-03-18 08:44:32 +00:00
|
|
|
|
2019-12-05 07:09:25 +00:00
|
|
|
queryset = queryset.annotate(systemuser_display=Concat(
|
2021-07-08 06:23:18 +00:00
|
|
|
F('systemuser__name'), Value('('),
|
|
|
|
F('systemuser__username'), Value(')')
|
2019-12-05 07:09:25 +00:00
|
|
|
))
|
|
|
|
return queryset
|
|
|
|
|
2020-03-16 11:15:29 +00:00
|
|
|
def send_post_add_signal(self, instance):
|
|
|
|
if not isinstance(instance, list):
|
|
|
|
instance = [instance]
|
|
|
|
|
|
|
|
system_users_objects_map = defaultdict(list)
|
|
|
|
model, object_field = self.get_objects_attr()
|
|
|
|
|
|
|
|
for i in instance:
|
|
|
|
_id = getattr(i, object_field).id
|
|
|
|
system_users_objects_map[i.systemuser].append(_id)
|
|
|
|
|
|
|
|
sender = self.get_sender()
|
2021-07-08 06:23:18 +00:00
|
|
|
for system_user, object_ids in system_users_objects_map.items():
|
|
|
|
logger.debug('System user relation changed, send m2m_changed signals')
|
2020-03-16 11:15:29 +00:00
|
|
|
m2m_changed.send(
|
|
|
|
sender=sender, instance=system_user, action='post_add',
|
2021-07-08 06:23:18 +00:00
|
|
|
reverse=False, model=model, pk_set=set(object_ids)
|
2020-03-16 11:15:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def get_sender(self):
|
|
|
|
return self.model
|
|
|
|
|
|
|
|
def get_objects_attr(self):
|
|
|
|
return models.Asset, 'asset'
|
|
|
|
|
|
|
|
def perform_create(self, serializer):
|
|
|
|
instance = serializer.save()
|
|
|
|
self.send_post_add_signal(instance)
|
|
|
|
|
2019-12-05 07:09:25 +00:00
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
class BaseRelationViewSet(RelationMixin, OrgBulkModelViewSet):
|
2022-03-07 06:12:32 +00:00
|
|
|
perm_model = models.SystemUser
|
2020-03-12 08:24:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SystemUserAssetRelationViewSet(BaseRelationViewSet):
|
2022-03-17 09:47:57 +00:00
|
|
|
perm_model = models.AuthBook
|
2019-12-05 07:09:25 +00:00
|
|
|
serializer_class = serializers.SystemUserAssetRelationSerializer
|
|
|
|
model = models.SystemUser.assets.through
|
2021-01-07 02:53:10 +00:00
|
|
|
filterset_fields = [
|
2019-12-05 07:09:25 +00:00
|
|
|
'id', 'asset', 'systemuser',
|
|
|
|
]
|
|
|
|
search_fields = [
|
|
|
|
"id", "asset__hostname", "asset__ip",
|
2021-07-12 12:11:53 +00:00
|
|
|
"systemuser__name", "systemuser__username",
|
2019-12-05 07:09:25 +00:00
|
|
|
]
|
|
|
|
|
2020-03-16 11:15:29 +00:00
|
|
|
def get_objects_attr(self):
|
|
|
|
return models.Asset, 'asset'
|
|
|
|
|
2019-12-05 07:09:25 +00:00
|
|
|
def get_queryset(self):
|
|
|
|
queryset = super().get_queryset()
|
|
|
|
queryset = queryset.annotate(
|
|
|
|
asset_display=Concat(
|
|
|
|
F('asset__hostname'), Value('('),
|
|
|
|
F('asset__ip'), Value(')')
|
|
|
|
)
|
|
|
|
)
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
class SystemUserNodeRelationViewSet(BaseRelationViewSet):
|
2019-12-05 07:09:25 +00:00
|
|
|
serializer_class = serializers.SystemUserNodeRelationSerializer
|
|
|
|
model = models.SystemUser.nodes.through
|
2021-01-07 02:53:10 +00:00
|
|
|
filterset_fields = [
|
2019-12-05 07:09:25 +00:00
|
|
|
'id', 'node', 'systemuser',
|
|
|
|
]
|
|
|
|
search_fields = [
|
2020-11-03 04:22:13 +00:00
|
|
|
"node__value", "systemuser__name", "systemuser__username"
|
2019-12-05 07:09:25 +00:00
|
|
|
]
|
|
|
|
|
2020-03-16 11:15:29 +00:00
|
|
|
def get_objects_attr(self):
|
|
|
|
return models.Node, 'node'
|
|
|
|
|
2019-12-05 07:09:25 +00:00
|
|
|
def get_queryset(self):
|
|
|
|
queryset = super().get_queryset()
|
|
|
|
queryset = queryset \
|
|
|
|
.annotate(node_key=F('node__key'))
|
|
|
|
return queryset
|
2020-03-12 08:24:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SystemUserUserRelationViewSet(BaseRelationViewSet):
|
|
|
|
serializer_class = serializers.SystemUserUserRelationSerializer
|
|
|
|
model = models.SystemUser.users.through
|
2021-01-07 02:53:10 +00:00
|
|
|
filterset_fields = [
|
2020-03-12 08:24:38 +00:00
|
|
|
'id', 'user', 'systemuser',
|
|
|
|
]
|
|
|
|
search_fields = [
|
|
|
|
"user__username", "user__name",
|
|
|
|
"systemuser__name", "systemuser__username",
|
|
|
|
]
|
|
|
|
|
2020-03-16 11:15:29 +00:00
|
|
|
def get_objects_attr(self):
|
|
|
|
from users.models import User
|
|
|
|
return User, 'user'
|
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
def get_queryset(self):
|
|
|
|
queryset = super().get_queryset()
|
|
|
|
queryset = queryset.annotate(
|
|
|
|
user_display=Concat(
|
|
|
|
F('user__name'), Value('('),
|
|
|
|
F('user__username'), Value(')')
|
|
|
|
)
|
|
|
|
)
|
|
|
|
return queryset
|