diff --git a/apps/common/drf/api.py b/apps/common/drf/api.py index 3d5b67b34..692d567f5 100644 --- a/apps/common/drf/api.py +++ b/apps/common/drf/api.py @@ -1,4 +1,5 @@ from rest_framework.viewsets import GenericViewSet, ModelViewSet +from rest_framework_bulk import BulkModelViewSet from ..mixins.api import ( SerializerMixin2, QuerySetMixin, ExtraFilterFieldsMixin, PaginatedResponseMixin @@ -19,3 +20,11 @@ class JMSModelViewSet(SerializerMixin2, PaginatedResponseMixin, ModelViewSet): pass + + +class JMSBulkModelViewSet(SerializerMixin2, + QuerySetMixin, + ExtraFilterFieldsMixin, + PaginatedResponseMixin, + BulkModelViewSet): + pass diff --git a/apps/common/drf/serializers.py b/apps/common/drf/serializers.py index bd92415a1..e767c32aa 100644 --- a/apps/common/drf/serializers.py +++ b/apps/common/drf/serializers.py @@ -1,5 +1,25 @@ from rest_framework.serializers import Serializer +from rest_framework.serializers import ModelSerializer +from rest_framework import serializers +from rest_framework_bulk.serializers import BulkListSerializer + +from common.mixins.serializers import BulkSerializerMixin +from common.mixins import BulkListSerializerMixin + +__all__ = ['EmptySerializer', 'BulkModelSerializer'] class EmptySerializer(Serializer): pass + + +class BulkModelSerializer(BulkSerializerMixin, ModelSerializer): + pass + + +class AdaptedBulkListSerializer(BulkListSerializerMixin, BulkListSerializer): + pass + + +class CeleryTaskSerializer(serializers.Serializer): + task = serializers.CharField(read_only=True) diff --git a/apps/common/exceptions.py b/apps/common/exceptions.py index e95cc2801..4b3718837 100644 --- a/apps/common/exceptions.py +++ b/apps/common/exceptions.py @@ -1,7 +1,8 @@ # -*- coding: utf-8 -*- # from rest_framework.exceptions import APIException +from rest_framework import status class JMSException(APIException): - pass + status_code = status.HTTP_400_BAD_REQUEST diff --git a/apps/common/serializers.py b/apps/common/serializers.py index eb657b4cc..971060641 100644 --- a/apps/common/serializers.py +++ b/apps/common/serializers.py @@ -1,14 +1,6 @@ -# -*- coding: utf-8 -*- -# +""" +老的代码统一到 `apps/common/drf/serializers.py` 中, +之后此文件废弃 +""" -from rest_framework_bulk.serializers import BulkListSerializer -from rest_framework import serializers -from .mixins import BulkListSerializerMixin - - -class AdaptedBulkListSerializer(BulkListSerializerMixin, BulkListSerializer): - pass - - -class CeleryTaskSerializer(serializers.Serializer): - task = serializers.CharField(read_only=True) +from common.drf.serializers import AdaptedBulkListSerializer, CeleryTaskSerializer diff --git a/apps/locale/zh/LC_MESSAGES/django.mo b/apps/locale/zh/LC_MESSAGES/django.mo index 646925643..8599cd7c5 100644 Binary files a/apps/locale/zh/LC_MESSAGES/django.mo and b/apps/locale/zh/LC_MESSAGES/django.mo differ diff --git a/apps/locale/zh/LC_MESSAGES/django.po b/apps/locale/zh/LC_MESSAGES/django.po index 03a50defd..eb34c4400 100644 --- a/apps/locale/zh/LC_MESSAGES/django.po +++ b/apps/locale/zh/LC_MESSAGES/django.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: JumpServer 0.3.3\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2020-07-28 11:25+0800\n" +"POT-Creation-Date: 2020-07-29 15:03+0800\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: ibuler \n" "Language-Team: JumpServer team\n" @@ -2419,6 +2419,10 @@ msgstr "会话" msgid "Risk level" msgstr "风险等级" +#: terminal/exceptions.py:8 +msgid "Bulk create not support" +msgstr "不支持批量创建" + #: terminal/models.py:27 msgid "Remote Address" msgstr "远端地址" @@ -2479,40 +2483,40 @@ msgstr "结束日期" msgid "Args" msgstr "参数" -#: tickets/api/request_asset_perm.py:43 +#: tickets/api/request_asset_perm.py:42 msgid "Ticket closed" msgstr "工单已关闭" -#: tickets/api/request_asset_perm.py:46 +#: tickets/api/request_asset_perm.py:45 #, python-format msgid "Ticket has %s" msgstr "工单已%s" -#: tickets/api/request_asset_perm.py:93 +#: tickets/api/request_asset_perm.py:90 msgid "Confirm assets first" msgstr "请先确认资产" -#: tickets/api/request_asset_perm.py:96 +#: tickets/api/request_asset_perm.py:93 msgid "Confirmed assets changed" msgstr "确认的资产变更了" -#: tickets/api/request_asset_perm.py:100 +#: tickets/api/request_asset_perm.py:97 msgid "Confirm system-user first" msgstr "请先确认系统用户" -#: tickets/api/request_asset_perm.py:104 +#: tickets/api/request_asset_perm.py:101 msgid "Confirmed system-user changed" msgstr "确认的系统用户变更了" -#: tickets/api/request_asset_perm.py:107 xpack/plugins/cloud/models.py:202 +#: tickets/api/request_asset_perm.py:104 xpack/plugins/cloud/models.py:202 msgid "Succeed" msgstr "成功" -#: tickets/api/request_asset_perm.py:114 +#: tickets/api/request_asset_perm.py:111 msgid "From request ticket: {} {}" msgstr "来自工单申请: {} {}" -#: tickets/api/request_asset_perm.py:116 +#: tickets/api/request_asset_perm.py:113 msgid "{} request assets, approved by {}" msgstr "{} 申请资产,通过人 {}" @@ -2592,11 +2596,11 @@ msgstr "确认的系统用户" msgid "Invalid `org_id`" msgstr "无效的 `org_id`" -#: tickets/serializers/request_asset_perm.py:93 +#: tickets/serializers/request_asset_perm.py:92 msgid "Field `assignees` must be organization admin or superuser" msgstr "字段 assignees 必须是组织管理员或者超级管理员" -#: tickets/serializers/request_asset_perm.py:143 +#: tickets/serializers/request_asset_perm.py:142 #, python-brace-format msgid "" "\n" diff --git a/apps/terminal/api/terminal.py b/apps/terminal/api/terminal.py index 705848fe0..2ee353e3e 100644 --- a/apps/terminal/api/terminal.py +++ b/apps/terminal/api/terminal.py @@ -5,17 +5,17 @@ import logging import uuid from django.core.cache import cache -from django.shortcuts import get_object_or_404, redirect -from django.utils import timezone +from django.shortcuts import get_object_or_404 from rest_framework import viewsets from rest_framework.views import APIView, Response from rest_framework.permissions import AllowAny - +from common.drf.api import JMSBulkModelViewSet from common.utils import get_object_or_none from common.permissions import IsAppUser, IsOrgAdminOrAppUser, IsSuperUser from ..models import Terminal, Status, Session from .. import serializers +from .. import exceptions __all__ = [ 'TerminalViewSet', 'TerminalTokenApi', 'StatusViewSet', 'TerminalConfig', @@ -23,13 +23,16 @@ __all__ = [ logger = logging.getLogger(__file__) -class TerminalViewSet(viewsets.ModelViewSet): +class TerminalViewSet(JMSBulkModelViewSet): queryset = Terminal.objects.filter(is_deleted=False) serializer_class = serializers.TerminalSerializer permission_classes = (IsSuperUser,) filter_fields = ['name', 'remote_addr'] def create(self, request, *args, **kwargs): + if isinstance(request.data, list): + raise exceptions.BulkCreateNotSupport() + name = request.data.get('name') remote_ip = request.META.get('REMOTE_ADDR') x_real_ip = request.META.get('X-Real-IP') diff --git a/apps/terminal/exceptions.py b/apps/terminal/exceptions.py new file mode 100644 index 000000000..a3b63a3e9 --- /dev/null +++ b/apps/terminal/exceptions.py @@ -0,0 +1,8 @@ +from django.utils.translation import ugettext_lazy as _ + +from common.exceptions import JMSException + + +class BulkCreateNotSupport(JMSException): + default_code = 'bulk_create_not_support' + default_detail = _('Bulk create not support') diff --git a/apps/terminal/serializers/terminal.py b/apps/terminal/serializers/terminal.py index c7a91d009..bfe992b19 100644 --- a/apps/terminal/serializers/terminal.py +++ b/apps/terminal/serializers/terminal.py @@ -1,18 +1,18 @@ from rest_framework import serializers -from common.mixins import BulkSerializerMixin -from common.serializers import AdaptedBulkListSerializer +from common.drf.serializers import BulkModelSerializer, AdaptedBulkListSerializer from ..models import ( Terminal, Status, Session, Task ) -class TerminalSerializer(serializers.ModelSerializer): +class TerminalSerializer(BulkModelSerializer): session_online = serializers.SerializerMethodField() is_alive = serializers.BooleanField(read_only=True) class Meta: model = Terminal + list_serializer_class = AdaptedBulkListSerializer fields = [ 'id', 'name', 'remote_addr', 'http_port', 'ssh_port', 'comment', 'is_accepted', "is_active", 'session_online', @@ -30,7 +30,7 @@ class StatusSerializer(serializers.ModelSerializer): model = Status -class TaskSerializer(BulkSerializerMixin, serializers.ModelSerializer): +class TaskSerializer(BulkModelSerializer): class Meta: fields = '__all__' diff --git a/apps/terminal/urls/api_urls.py b/apps/terminal/urls/api_urls.py index d82a5ca5a..c60f97603 100644 --- a/apps/terminal/urls/api_urls.py +++ b/apps/terminal/urls/api_urls.py @@ -2,7 +2,7 @@ # -*- coding: utf-8 -*- # -from django.urls import path, include, re_path +from django.urls import path, re_path from rest_framework_bulk.routes import BulkRouter from common import api as capi