Commit c0a15003 authored by ibuler's avatar ibuler

[Update] 统一url地址

parent 49ccba85
# coding:utf-8 # coding:utf-8
# #
from django.urls import path from django.urls import path, re_path
from rest_framework_bulk.routes import BulkRouter from rest_framework_bulk.routes import BulkRouter
from common import api as capi
from .. import api from .. import api
app_name = 'applications' app_name = 'applications'
router = BulkRouter() router = BulkRouter()
router.register(r'remote-app', api.RemoteAppViewSet, 'remote-app') router.register(r'remote-apps', api.RemoteAppViewSet, 'remote-app')
urlpatterns = [ urlpatterns = [
path('remote-apps/<uuid:pk>/connection-info/', path('remote-apps/<uuid:pk>/connection-info/',
api.RemoteAppConnectionInfoApi.as_view(), api.RemoteAppConnectionInfoApi.as_view(),
name='remote-app-connection-info') name='remote-app-connection-info')
] ]
old_version_urlpatterns = [
re_path('(?P<resource>remote-app)/.*', capi.redirect_plural_name_api)
]
urlpatterns += router.urls urlpatterns += router.urls + old_version_urlpatterns
...@@ -31,9 +31,9 @@ from ..utils import LabelFilter ...@@ -31,9 +31,9 @@ from ..utils import LabelFilter
logger = get_logger(__file__) logger = get_logger(__file__)
__all__ = [ __all__ = [
'AssetViewSet', 'AssetListUpdateApi', 'AssetViewSet',
'AssetRefreshHardwareApi', 'AssetAdminUserTestApi', 'AssetRefreshHardwareApi', 'AssetAdminUserTestApi',
'AssetGatewayApi', 'AssetBulkUpdateSelectAPI' 'AssetGatewayApi',
] ]
...@@ -102,29 +102,6 @@ class AssetViewSet(LabelFilter, OrgBulkModelViewSet): ...@@ -102,29 +102,6 @@ class AssetViewSet(LabelFilter, OrgBulkModelViewSet):
return queryset return queryset
class AssetListUpdateApi(IDInCacheFilterMixin, ListBulkCreateUpdateDestroyAPIView):
"""
Asset bulk update api
"""
queryset = Asset.objects.all()
serializer_class = serializers.AssetSerializer
permission_classes = (IsOrgAdmin,)
class AssetBulkUpdateSelectAPI(APIView):
permission_classes = (IsOrgAdmin,)
def post(self, request, *args, **kwargs):
assets_id = request.data.get('assets_id', '')
if assets_id:
spm = uuid.uuid4().hex
key = CACHE_KEY_ASSET_BULK_UPDATE_ID_PREFIX.format(spm)
cache.set(key, assets_id, 300)
url = reverse_lazy('assets:asset-bulk-update') + '?spm=%s' % spm
return Response({'url': url})
error = _('Please select assets that need to be updated')
return Response({'error': error}, status=400)
class AssetRefreshHardwareApi(generics.RetrieveAPIView): class AssetRefreshHardwareApi(generics.RetrieveAPIView):
""" """
......
...@@ -2,11 +2,12 @@ ...@@ -2,11 +2,12 @@
# #
from rest_framework.response import Response from rest_framework.response import Response
from rest_framework import viewsets, status, generics from rest_framework import generics
from rest_framework.pagination import LimitOffsetPagination from rest_framework.pagination import LimitOffsetPagination
from rest_framework import filters from rest_framework import filters
from rest_framework_bulk import BulkModelViewSet from rest_framework_bulk import BulkModelViewSet
from django.shortcuts import get_object_or_404 from django.shortcuts import get_object_or_404
from django.http import Http404
from common.permissions import IsOrgAdminOrAppUser, NeedMFAVerify from common.permissions import IsOrgAdminOrAppUser, NeedMFAVerify
from common.utils import get_object_or_none, get_logger from common.utils import get_object_or_none, get_logger
...@@ -115,14 +116,6 @@ class AssetUserAuthInfoApi(generics.RetrieveAPIView): ...@@ -115,14 +116,6 @@ class AssetUserAuthInfoApi(generics.RetrieveAPIView):
serializer_class = serializers.AssetUserAuthInfoSerializer serializer_class = serializers.AssetUserAuthInfoSerializer
permission_classes = [IsOrgAdminOrAppUser, NeedMFAVerify] permission_classes = [IsOrgAdminOrAppUser, NeedMFAVerify]
def retrieve(self, request, *args, **kwargs):
instance = self.get_object()
serializer = self.get_serializer(instance)
status_code = status.HTTP_200_OK
if not instance:
status_code = status.HTTP_400_BAD_REQUEST
return Response(serializer.data, status=status_code)
def get_object(self): def get_object(self):
query_params = self.request.query_params query_params = self.request.query_params
username = query_params.get('username') username = query_params.get('username')
...@@ -133,8 +126,7 @@ class AssetUserAuthInfoApi(generics.RetrieveAPIView): ...@@ -133,8 +126,7 @@ class AssetUserAuthInfoApi(generics.RetrieveAPIView):
manger = AssetUserManager() manger = AssetUserManager()
instance = manger.get(username, asset, prefer=prefer) instance = manger.get(username, asset, prefer=prefer)
except Exception as e: except Exception as e:
logger.error(e, exc_info=True) raise Http404("Not found")
return None
else: else:
return instance return instance
......
...@@ -205,6 +205,7 @@ class AssetsAmountMixin: ...@@ -205,6 +205,7 @@ class AssetsAmountMixin:
获取节点下所有资产数量速度太慢,所以需要重写,使用cache等方案 获取节点下所有资产数量速度太慢,所以需要重写,使用cache等方案
:return: :return:
""" """
return 0
if self._assets_amount is not None: if self._assets_amount is not None:
return self._assets_amount return self._assets_amount
cache_key = self._assets_amount_cache_key.format(self.key) cache_key = self._assets_amount_cache_key.format(self.key)
...@@ -254,7 +255,7 @@ class Node(OrgModelMixin, FamilyMixin, FullValueMixin, AssetsAmountMixin): ...@@ -254,7 +255,7 @@ class Node(OrgModelMixin, FamilyMixin, FullValueMixin, AssetsAmountMixin):
ordering = ['key'] ordering = ['key']
def __str__(self): def __str__(self):
return self.full_value return self.value
def __eq__(self, other): def __eq__(self, other):
if not other: if not other:
......
# coding:utf-8 # coding:utf-8
from django.urls import path from django.urls import path, re_path
from rest_framework_nested import routers from rest_framework_nested import routers
# from rest_framework.routers import DefaultRouter # from rest_framework.routers import DefaultRouter
from rest_framework_bulk.routes import BulkRouter from rest_framework_bulk.routes import BulkRouter
from common import api as capi
from .. import api from .. import api
app_name = 'assets' app_name = 'assets'
router = BulkRouter() router = BulkRouter()
router.register(r'assets', api.AssetViewSet, 'asset') router.register(r'assets', api.AssetViewSet, 'asset')
router.register(r'admin-user', api.AdminUserViewSet, 'admin-user') router.register(r'admin-users', api.AdminUserViewSet, 'admin-user')
router.register(r'system-user', api.SystemUserViewSet, 'system-user') router.register(r'system-users', api.SystemUserViewSet, 'system-user')
router.register(r'labels', api.LabelViewSet, 'label') router.register(r'labels', api.LabelViewSet, 'label')
router.register(r'nodes', api.NodeViewSet, 'node') router.register(r'nodes', api.NodeViewSet, 'node')
router.register(r'domain', api.DomainViewSet, 'domain') router.register(r'domains', api.DomainViewSet, 'domain')
router.register(r'gateway', api.GatewayViewSet, 'gateway') router.register(r'gateways', api.GatewayViewSet, 'gateway')
router.register(r'cmd-filter', api.CommandFilterViewSet, 'cmd-filter') router.register(r'cmd-filters', api.CommandFilterViewSet, 'cmd-filter')
router.register(r'asset-user', api.AssetUserViewSet, 'asset-user') router.register(r'asset-users', api.AssetUserViewSet, 'asset-user')
router.register(r'asset-user-info', api.AssetUserExportViewSet, 'asset-user-info') router.register(r'asset-user-info', api.AssetUserExportViewSet, 'asset-user-info')
cmd_filter_router = routers.NestedDefaultRouter(router, r'cmd-filter', lookup='filter') cmd_filter_router = routers.NestedDefaultRouter(router, r'cmd-filters', lookup='filter')
cmd_filter_router.register(r'rules', api.CommandFilterRuleViewSet, 'cmd-filter-rule') cmd_filter_router.register(r'rules', api.CommandFilterRuleViewSet, 'cmd-filter-rule')
urlpatterns = [ urlpatterns = [
path('assets-bulk/', api.AssetListUpdateApi.as_view(), name='asset-bulk-update'),
path('asset/update/select/',
api.AssetBulkUpdateSelectAPI.as_view(), name='asset-bulk-update-select'),
path('assets/<uuid:pk>/refresh/', path('assets/<uuid:pk>/refresh/',
api.AssetRefreshHardwareApi.as_view(), name='asset-refresh'), api.AssetRefreshHardwareApi.as_view(), name='asset-refresh'),
path('assets/<uuid:pk>/alive/', path('assets/<uuid:pk>/alive/',
...@@ -35,36 +34,36 @@ urlpatterns = [ ...@@ -35,36 +34,36 @@ urlpatterns = [
path('assets/<uuid:pk>/gateway/', path('assets/<uuid:pk>/gateway/',
api.AssetGatewayApi.as_view(), name='asset-gateway'), api.AssetGatewayApi.as_view(), name='asset-gateway'),
path('asset-user/auth-info/', path('asset-users/auth-info/',
api.AssetUserAuthInfoApi.as_view(), name='asset-user-auth-info'), api.AssetUserAuthInfoApi.as_view(), name='asset-user-auth-info'),
path('asset-user/test-connective/', path('asset-users/test-connective/',
api.AssetUserTestConnectiveApi.as_view(), name='asset-user-connective'), api.AssetUserTestConnectiveApi.as_view(), name='asset-user-connective'),
path('admin-user/<uuid:pk>/nodes/', path('admin-users/<uuid:pk>/nodes/',
api.ReplaceNodesAdminUserApi.as_view(), name='replace-nodes-admin-user'), api.ReplaceNodesAdminUserApi.as_view(), name='replace-nodes-admin-user'),
path('admin-user/<uuid:pk>/auth/', path('admin-users/<uuid:pk>/auth/',
api.AdminUserAuthApi.as_view(), name='admin-user-auth'), api.AdminUserAuthApi.as_view(), name='admin-user-auth'),
path('admin-user/<uuid:pk>/connective/', path('admin-users/<uuid:pk>/connective/',
api.AdminUserTestConnectiveApi.as_view(), name='admin-user-connective'), api.AdminUserTestConnectiveApi.as_view(), name='admin-user-connective'),
path('admin-user/<uuid:pk>/assets/', path('admin-users/<uuid:pk>/assets/',
api.AdminUserAssetsListView.as_view(), name='admin-user-assets'), api.AdminUserAssetsListView.as_view(), name='admin-user-assets'),
path('system-user/<uuid:pk>/auth-info/', path('system-users/<uuid:pk>/auth-info/',
api.SystemUserAuthInfoApi.as_view(), name='system-user-auth-info'), api.SystemUserAuthInfoApi.as_view(), name='system-user-auth-info'),
path('system-user/<uuid:pk>/asset/<uuid:aid>/auth-info/', path('system-users/<uuid:pk>/asset/<uuid:aid>/auth-info/',
api.SystemUserAssetAuthInfoApi.as_view(), name='system-user-asset-auth-info'), api.SystemUserAssetAuthInfoApi.as_view(), name='system-user-asset-auth-info'),
path('system-user/<uuid:pk>/assets/', path('system-users/<uuid:pk>/assets/',
api.SystemUserAssetsListView.as_view(), name='system-user-assets'), api.SystemUserAssetsListView.as_view(), name='system-user-assets'),
path('system-user/<uuid:pk>/push/', path('system-users/<uuid:pk>/push/',
api.SystemUserPushApi.as_view(), name='system-user-push'), api.SystemUserPushApi.as_view(), name='system-user-push'),
path('system-user/<uuid:pk>/asset/<uuid:aid>/push/', path('system-users/<uuid:pk>/asset/<uuid:aid>/push/',
api.SystemUserPushToAssetApi.as_view(), name='system-user-push-to-asset'), api.SystemUserPushToAssetApi.as_view(), name='system-user-push-to-asset'),
path('system-user/<uuid:pk>/asset/<uuid:aid>/test/', path('system-users/<uuid:pk>/asset/<uuid:aid>/test/',
api.SystemUserTestAssetConnectivityApi.as_view(), name='system-user-test-to-asset'), api.SystemUserTestAssetConnectivityApi.as_view(), name='system-user-test-to-asset'),
path('system-user/<uuid:pk>/connective/', path('system-users/<uuid:pk>/connective/',
api.SystemUserTestConnectiveApi.as_view(), name='system-user-connective'), api.SystemUserTestConnectiveApi.as_view(), name='system-user-connective'),
path('system-user/<uuid:pk>/cmd-filter-rules/', path('system-users/<uuid:pk>/cmd-filter-rules/',
api.SystemUserCommandFilterRuleListApi.as_view(), name='system-user-cmd-filter-rule-list'), api.SystemUserCommandFilterRuleListApi.as_view(), name='system-user-cmd-filter-rule-list'),
path('nodes/tree/', api.NodeListAsTreeApi.as_view(), name='node-tree'), path('nodes/tree/', api.NodeListAsTreeApi.as_view(), name='node-tree'),
...@@ -89,10 +88,16 @@ urlpatterns = [ ...@@ -89,10 +88,16 @@ urlpatterns = [
path('nodes/refresh-assets-amount/', path('nodes/refresh-assets-amount/',
api.RefreshAssetsAmount.as_view(), name='refresh-assets-amount'), api.RefreshAssetsAmount.as_view(), name='refresh-assets-amount'),
path('gateway/<uuid:pk>/test-connective/', path('gateways/<uuid:pk>/test-connective/',
api.GatewayTestConnectionApi.as_view(), name='test-gateway-connective'), api.GatewayTestConnectionApi.as_view(), name='test-gateway-connective'),
] ]
old_version_urlpatterns = [
re_path('(?P<resource>admin_user|system_user|domain|gateway|cmd-filter|asset-user)/.*', capi.redirect_plural_name_api)
]
urlpatterns += router.urls + old_version_urlpatterns
urlpatterns += router.urls + cmd_filter_router.urls urlpatterns += router.urls + cmd_filter_router.urls
...@@ -202,9 +202,11 @@ class GrantAssetsMixin(LabelFilterMixin): ...@@ -202,9 +202,11 @@ class GrantAssetsMixin(LabelFilterMixin):
data.append(asset) data.append(asset)
return data return data
def get_serializer(self, queryset_list, many=True): def get_serializer(self, data=None, many=True):
data = self.get_serializer_queryset(queryset_list) if data is None:
return super().get_serializer(data, many=True) data = []
data = self.get_serializer_queryset(data)
return super().get_serializer(data=data, many=many)
def filter_queryset_by_id(self, assets_items): def filter_queryset_by_id(self, assets_items):
i = self.request.query_params.get("id") i = self.request.query_params.get("id")
......
...@@ -140,9 +140,11 @@ class UserGrantedNodesApi(UserPermissionCacheMixin, NodesWithUngroupMixin, ListA ...@@ -140,9 +140,11 @@ class UserGrantedNodesApi(UserPermissionCacheMixin, NodesWithUngroupMixin, ListA
_nodes.append(node) _nodes.append(node)
return _nodes return _nodes
def get_serializer(self, nodes_with_assets, many=True): def get_serializer(self, data=None, many=True):
nodes = self.get_nodes(nodes_with_assets) if data is None:
return super().get_serializer(nodes, many=True) data = []
nodes = self.get_nodes(data)
return super().get_serializer(data=nodes, many=True)
def get_queryset(self): def get_queryset(self):
user = self.get_object() user = self.get_object()
...@@ -160,8 +162,10 @@ class UserGrantedNodesAsTreeApi(UserGrantedNodesApi): ...@@ -160,8 +162,10 @@ class UserGrantedNodesAsTreeApi(UserGrantedNodesApi):
serializer_class = TreeNodeSerializer serializer_class = TreeNodeSerializer
only_fields = ParserNode.nodes_only_fields only_fields = ParserNode.nodes_only_fields
def get_serializer(self, nodes_with_assets, many=True): def get_serializer(self, data=None, many=True):
nodes = self.get_nodes(nodes_with_assets) if data is None:
data = []
nodes = self.get_nodes(data)
queryset = [] queryset = []
for node in nodes: for node in nodes:
data = ParserNode.parse_node_to_tree_node(node) data = ParserNode.parse_node_to_tree_node(node)
...@@ -263,9 +267,11 @@ class UserGrantedNodesWithAssetsApi(UserPermissionCacheMixin, NodesWithUngroupMi ...@@ -263,9 +267,11 @@ class UserGrantedNodesWithAssetsApi(UserPermissionCacheMixin, NodesWithUngroupMi
queryset.append(node) queryset.append(node)
return queryset return queryset
def get_serializer(self, nodes_items, many=True): def get_serializer(self, data=None, many=True):
queryset = self.get_serializer_queryset(nodes_items) if data is None:
return super().get_serializer(queryset, many=many) data = []
queryset = self.get_serializer_queryset(data)
return super().get_serializer(data=queryset, many=many)
def get_queryset(self): def get_queryset(self):
user = self.get_object() user = self.get_object()
...@@ -292,8 +298,10 @@ class UserGrantedNodesWithAssetsAsTreeApi(UserGrantedNodesWithAssetsApi): ...@@ -292,8 +298,10 @@ class UserGrantedNodesWithAssetsAsTreeApi(UserGrantedNodesWithAssetsApi):
assets_only_fields = ParserNode.assets_only_fields assets_only_fields = ParserNode.assets_only_fields
system_users_only_fields = ParserNode.system_users_only_fields system_users_only_fields = ParserNode.system_users_only_fields
def get_serializer(self, nodes_items, many=True): def get_serializer(self, data=None, many=True):
_queryset = super().get_serializer_queryset(nodes_items) if data is None:
data = []
_queryset = super().get_serializer_queryset(data)
queryset = [] queryset = []
for node in _queryset: for node in _queryset:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment