• 老广's avatar
    Config (#3502) · 4944ac8e
    老广 authored
    * [Update] 修改config
    
    * [Update] 移动存储设置到到terminal中
    
    * [Update] 修改permission 查看
    
    * [Update] pre merge
    
    * [Update] 录像存储
    
    * [Update] 命令存储
    
    * [Update] 添加存储测试可连接性
    
    * [Update] 修改 meta 值的 key 为大写
    
    * [Update] 修改 Terminal 相关 Storage 配置
    
    * [Update] 删除之前获取录像/命令存储的代码
    
    * [Update] 修改导入失败
    
    * [Update] 迁移文件添加default存储
    
    * [Update] 删除之前代码,添加help_text信息
    
    * [Update] 删除之前代码
    
    * [Update] 删除之前代码
    
    * [Update] 抽象命令/录像存储 APIView
    
    * [Update] 抽象命令/录像存储 APIView 1
    
    * [Update] 抽象命令/录像存储 DictField
    
    * [Update] 抽象命令/录像存储列表页面
    
    * [Update] 修复CustomDictField的bug
    
    * [Update] RemoteApp 页面添加 hidden
    
    * [Update] 用户页面添加用户关联授权
    
    * [Update] 修改存储测试可连接性 target
    
    * [Update] 修改配置
    
    * [Update] 修改存储前端 Form 渲染逻辑
    
    * [Update] 修改存储细节
    
    * [Update] 统一存储类型到 const 文件
    
    * [Update] 修改迁移文件及Model,创建默认存储
    
    * [Update] 修改迁移文件及Model初始化默认数据
    
    * [Update] 修改迁移文件
    
    * [Update] 修改迁移文件
    
    * [Update] 修改迁移文件
    
    * [Update] 修改迁移文件
    
    * [Update] 修改迁移文件
    
    * [Update] 修改迁移文件
    
    * [Update] 修改迁移文件
    
    * [Update] 限制删除默认存储配置,只允许创建扩展的存储类型
    
    * [Update] 修改ip字段长度
    
    * [Update] 修改ip字段长度
    
    * [Update] 修改一些css
    
    * [Update] 修改关联
    
    * [Update] 添加操作日志定时清理
    
    * [Update] 修改记录syslog的instance encoder
    
    * [Update] 忽略登录产生的操作日志
    
    * [Update] 限制更新存储时不覆盖原有AK SK 等字段
    
    * [Update] 修改迁移文件添加comment字段
    
    * [Update] 修改迁移文件
    
    * [Update] 添加 comment 字段
    
    * [Update] 修改默认存储no -> null
    
    * [Update] 修改细节
    
    * [Update] 更新翻译(存储配置
    
    * [Update] 修改定时任务注册,修改系统用户资产、节点关系api
    
    * [Update] 添加监控磁盘任务
    
    * [Update] 修改session
    
    * [Update] 拆分serializer
    
    * [Update] 还原setting原来的manager
    4944ac8e
api.py 2.46 KB
# -*- coding: utf-8 -*-
#
from django.shortcuts import get_object_or_404
from rest_framework.viewsets import ModelViewSet
from rest_framework_bulk import BulkModelViewSet
from common.mixins import CommonApiMixin

from ..utils import set_to_root_org, filter_org_queryset
from ..models import Organization

__all__ = [
    'RootOrgViewMixin', 'OrgMembershipModelViewSetMixin', 'OrgModelViewSet',
    'OrgBulkModelViewSet', 'OrgQuerySetMixin',
]


class RootOrgViewMixin:
    def dispatch(self, request, *args, **kwargs):
        set_to_root_org()
        return super().dispatch(request, *args, **kwargs)


class OrgQuerySetMixin:
    def get_queryset(self):
        if hasattr(self, 'model'):
            queryset = self.model.objects.all()
        else:
            assert self.queryset is None, (
                    "'%s' should not include a `queryset` attribute"
                    % self.__class__.__name__
            )
            queryset = super().get_queryset()

        if hasattr(self, 'swagger_fake_view'):
            return queryset[:1]
        if hasattr(self, 'action') and self.action == 'list' and \
            hasattr(self, 'serializer_class') and \
                hasattr(self.serializer_class, 'setup_eager_loading'):
            queryset = self.serializer_class.setup_eager_loading(queryset)
        return queryset


class OrgModelViewSet(CommonApiMixin, OrgQuerySetMixin, ModelViewSet):
    pass


class OrgBulkModelViewSet(CommonApiMixin, OrgQuerySetMixin, BulkModelViewSet):
    def allow_bulk_destroy(self, qs, filtered):
        qs_count = qs.count()
        filtered_count = filtered.count()
        if filtered_count == 1:
            return True
        if qs_count <= filtered_count:
            return False
        if self.request.query_params.get('spm', ''):
            return True
        return False


class OrgMembershipModelViewSetMixin:
    org = None
    membership_class = None
    lookup_field = 'user'
    lookup_url_kwarg = 'user_id'
    http_method_names = ['get', 'post', 'delete', 'head', 'options']

    def dispatch(self, request, *args, **kwargs):
        self.org = get_object_or_404(Organization, pk=kwargs.get('org_id'))
        return super().dispatch(request, *args, **kwargs)

    def get_serializer_context(self):
        context = super().get_serializer_context()
        context['org'] = self.org
        return context

    def get_queryset(self):
        queryset = self.membership_class.objects.filter(organization=self.org)
        return queryset