• BaiJiangJie's avatar
    [Update] 添加用户授权资产列表页的分页,搜索,排序 (#1963) · 82d866db
    BaiJiangJie authored
    * [Update] 分页获取用户授权资产
    
    * [Update] 修改前端-用户授权资产分页
    
    * [Update] 用户授权资产支持搜索
    
    * [Update] 用户授权资产支持排序
    
    * [Update] 用户授权的节点with资产Api,对资产进行排序
    
    * [Update] 获取用户授权的节点下的资产的api,进行分页、排序、查询
    
    * [Update] 抽象用户授权资产列表的查询,排序
    
    * [Update] 优化小细节
    
    * [Update] 删除无用导入
    
    * [Update] 修改AssetFilterMixins目录从common到perms
    
    * [Update] 资产授权规则列表: 添加分页、搜索
    
    * [Update] 添加管理用户,系统用户列表分页、搜索
    
    * [Update] 用户组列表添加分页,搜索
    
    * [Update] 资产标签列表添加分页、搜索
    
    * [Update] 网域网关列表添加分页、搜索
    
    * [Update] 命令过滤列表添加分页、搜索,修改翻译小细节
    
    * [Update] 删除前端注释initDataTable
    
    * [Update] 修改文案,资产组-节点
    
    * [Update] 普通用户资产列表添加分页、搜索
    82d866db
label.py 1.61 KB
# ~*~ coding: utf-8 ~*~
# Copyright (C) 2014-2018 Beijing DuiZhan Technology Co.,Ltd. All Rights Reserved.
#
# Licensed under the GNU General Public License v2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.gnu.org/licenses/gpl-2.0.html
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from rest_framework_bulk import BulkModelViewSet
from rest_framework.pagination import LimitOffsetPagination
from django.db.models import Count

from common.utils import get_logger
from ..hands import IsOrgAdmin
from ..models import Label
from .. import serializers


logger = get_logger(__file__)
__all__ = ['LabelViewSet']


class LabelViewSet(BulkModelViewSet):
    filter_fields = ("name", "value")
    search_fields = filter_fields
    permission_classes = (IsOrgAdmin,)
    serializer_class = serializers.LabelSerializer
    pagination_class = LimitOffsetPagination

    def list(self, request, *args, **kwargs):
        if request.query_params.get("distinct"):
            self.serializer_class = serializers.LabelDistinctSerializer
            self.queryset = self.queryset.values("name").distinct()
        return super().list(request, *args, **kwargs)

    def get_queryset(self):
        self.queryset = Label.objects.annotate(asset_count=Count("assets"))
        return self.queryset