From 36f1165d1bd924174be0445b75d2ae521bed421c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=81=E5=B9=BF?= Date: Wed, 25 Jul 2018 02:05:28 -0500 Subject: [PATCH] Tmp org (#1583) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [Update] 修改一些内容 * [Update] 修改datatable 支持process * [Bugfix] 修复asset queryset 没有valid方法的bug --- apps/assets/api/asset.py | 53 +++++++++++--------- apps/assets/models/asset.py | 6 +-- apps/assets/serializers/asset.py | 2 +- apps/assets/templates/assets/asset_list.html | 19 ++----- apps/audits/models.py | 4 +- apps/common/permissions.py | 3 ++ apps/jumpserver/views.py | 5 +- apps/orgs/api.py | 4 +- apps/orgs/mixins.py | 7 +-- apps/static/js/jumpserver.js | 12 ++--- 10 files changed, 54 insertions(+), 61 deletions(-) diff --git a/apps/assets/api/asset.py b/apps/assets/api/asset.py index 2d25fb4b2..e2ce1b62a 100644 --- a/apps/assets/api/asset.py +++ b/apps/assets/api/asset.py @@ -42,37 +42,40 @@ class AssetViewSet(IDInFilterMixin, LabelFilter, BulkModelViewSet): pagination_class = LimitOffsetPagination permission_classes = (permissions.AllowAny,) - def get_queryset(self): - time.sleep(3) - queryset = super().get_queryset()\ - .prefetch_related('labels', 'nodes')\ - .select_related('admin_user') - admin_user_id = self.request.query_params.get('admin_user_id') + def filter_node(self): node_id = self.request.query_params.get("node_id") + if not node_id: + return + + node = get_object_or_404(Node, id=node_id) show_current_asset = self.request.query_params.get("show_current_asset") - if admin_user_id: - admin_user = get_object_or_404(AdminUser, id=admin_user_id) - queryset = queryset.filter(admin_user=admin_user) - - if node_id and show_current_asset: - node = get_object_or_404(Node, id=node_id) - if node.is_root(): - queryset = queryset.filter( + if node.is_root(): + if show_current_asset: + self.queryset = self.queryset.filter( Q(nodes=node_id) | Q(nodes__isnull=True) ).distinct() - else: - queryset = queryset.filter(nodes=node).distinct() + return + if show_current_asset: + self.queryset = self.queryset.filter(nodes=node).distinct() + else: + self.queryset = self.queryset.filter( + nodes__key__regex='^{}(:[0-9]+)*$'.format(node.key), + ).distinct() - if node_id and not show_current_asset: - node = get_object_or_404(Node, id=node_id) - if node.is_root(): - queryset = Asset.objects.all() - else: - queryset = queryset.filter( - nodes__key__regex='^{}(:[0-9]+)*$'.format(node.key), - ).distinct() - return queryset + def filter_admin_user_id(self): + admin_user_id = self.request.query_params.get('admin_user_id') + if admin_user_id: + admin_user = get_object_or_404(AdminUser, id=admin_user_id) + self.queryset = self.queryset.filter(admin_user=admin_user) + + def get_queryset(self): + self.queryset = super().get_queryset()\ + .prefetch_related('labels', 'nodes')\ + .select_related('admin_user') + self.filter_admin_user_id() + self.filter_node() + return self.queryset class AssetListUpdateApi(IDInFilterMixin, ListBulkCreateUpdateDestroyAPIView): diff --git a/apps/assets/models/asset.py b/apps/assets/models/asset.py index c44239f55..4a3f49d99 100644 --- a/apps/assets/models/asset.py +++ b/apps/assets/models/asset.py @@ -45,10 +45,6 @@ class AssetQuerySet(models.QuerySet): return self.active() -class AssetManager(OrgManager): - pass - - class Asset(OrgModelMixin): # Important PLATFORM_CHOICES = ( @@ -131,7 +127,7 @@ class Asset(OrgModelMixin): comment = models.TextField(max_length=128, default='', blank=True, verbose_name=_('Comment')) - objects = AssetManager() + objects = OrgManager.from_queryset(AssetQuerySet)() def __str__(self): return '{0.hostname}({0.ip})'.format(self) diff --git a/apps/assets/serializers/asset.py b/apps/assets/serializers/asset.py index e63735794..dba6df3cf 100644 --- a/apps/assets/serializers/asset.py +++ b/apps/assets/serializers/asset.py @@ -20,7 +20,7 @@ class AssetSerializer(BulkSerializerMixin, serializers.ModelSerializer): model = Asset list_serializer_class = BulkListSerializer fields = '__all__' - validators = [] # If not set to [], partial bulk update will be error + # validators = [] # If not set to [], partial bulk update will be error def get_field_names(self, declared_fields, info): fields = super().get_field_names(declared_fields, info) diff --git a/apps/assets/templates/assets/asset_list.html b/apps/assets/templates/assets/asset_list.html index 9756d1e9d..23d7fe001 100644 --- a/apps/assets/templates/assets/asset_list.html +++ b/apps/assets/templates/assets/asset_list.html @@ -10,6 +10,7 @@ {% block custom_head_css_js %} +{# #} {% endblock %} diff --git a/apps/audits/models.py b/apps/audits/models.py index 667087e18..b8d46586d 100644 --- a/apps/audits/models.py +++ b/apps/audits/models.py @@ -3,8 +3,10 @@ import uuid from django.db import models from django.utils.translation import ugettext_lazy as _ +from orgs.mixins import OrgModelMixin -class FTPLog(models.Model): + +class FTPLog(OrgModelMixin): id = models.UUIDField(default=uuid.uuid4, primary_key=True) user = models.CharField(max_length=128, verbose_name=_('User')) remote_addr = models.CharField(max_length=15, verbose_name=_("Remote addr"), blank=True, null=True) diff --git a/apps/common/permissions.py b/apps/common/permissions.py index c9015844c..9d01031bc 100644 --- a/apps/common/permissions.py +++ b/apps/common/permissions.py @@ -80,6 +80,9 @@ class AdminUserRequiredMixin(UserPassesTestMixin): def dispatch(self, request, *args, **kwargs): print("Current org: {}".format(current_org)) + if not request.user.is_authenticated: + return super().dispatch(request, *args, **kwargs) + if not current_org: return redirect('orgs:switch-a-org') diff --git a/apps/jumpserver/views.py b/apps/jumpserver/views.py index 4dc3a3dc7..d6d74a497 100644 --- a/apps/jumpserver/views.py +++ b/apps/jumpserver/views.py @@ -175,4 +175,7 @@ class LunaView(View): Luna是单独部署的一个程序,你需要部署luna,coco,配置nginx做url分发, 如果你看到了这个页面,证明你访问的不是nginx监听的端口,祝你好运 """ - return HttpResponse(msg) \ No newline at end of file + return HttpResponse(msg) + + + diff --git a/apps/orgs/api.py b/apps/orgs/api.py index 14d837841..7da77cfce 100644 --- a/apps/orgs/api.py +++ b/apps/orgs/api.py @@ -3,7 +3,7 @@ from rest_framework import viewsets -from common.permissions import IsOrgAdminOrAppUser +from common.permissions import IsSuperUserOrAppUser from .models import Organization from .serializers import OrgSerializer @@ -11,4 +11,4 @@ from .serializers import OrgSerializer class OrgViewSet(viewsets.ModelViewSet): queryset = Organization.objects.all() serializer_class = OrgSerializer - permission_classes = (IsOrgAdminOrAppUser,) + permission_classes = (IsSuperUserOrAppUser,) diff --git a/apps/orgs/mixins.py b/apps/orgs/mixins.py index 873b25fa3..1dd08c5c5 100644 --- a/apps/orgs/mixins.py +++ b/apps/orgs/mixins.py @@ -23,10 +23,10 @@ __all__ = [ class OrgManager(models.Manager): def get_queryset(self): + queryset = super(OrgManager, self).get_queryset() kwargs = {} if not hasattr(tl, 'times'): tl.times = 0 - print("[{}]>>>>>>>>>> Get query set".format(tl.times)) print(current_org) if not current_org: @@ -34,11 +34,8 @@ class OrgManager(models.Manager): elif current_org.is_real(): kwargs['org_id'] = current_org.id elif current_org.is_default(): - kwargs['org_id'] = None - queryset = super(OrgManager, self).get_queryset() + queryset = queryset.filter(org_id="").filter(org_id__isnull=True) queryset = queryset.filter(**kwargs) - # print(kwargs) - # print(queryset.query) tl.times += 1 return queryset diff --git a/apps/static/js/jumpserver.js b/apps/static/js/jumpserver.js index 4cc0a3e1e..d96c8c3d6 100644 --- a/apps/static/js/jumpserver.js +++ b/apps/static/js/jumpserver.js @@ -386,11 +386,10 @@ jumpserver.initServerSideDataTable = function (options) { selector: 'td:first-child' }; var table = ele.DataTable({ - // pageLength: options.pageLength || 15, - // dom: options.dom || '<"#uc.pull-left">flt<"row m-t"<"col-md-8"<"#op.col-md-6"><"col-md-6 text-center"i>><"col-md-4"p>>', - // order: options.order || [], - // select: options.select || 'multi', - // buttons: [], + pageLength: options.pageLength || 15, + dom: options.dom || '<"#uc.pull-left">fltr<"row m-t"<"col-md-8"<"#op.col-md-6"><"col-md-6 text-center"i>><"col-md-4"p>>', + order: options.order || [], + buttons: [], columnDefs: columnDefs, serverSide: true, processing: true, @@ -475,8 +474,7 @@ jumpserver.initServerSideDataTable = function (options) { } }) } - }). - on('draw', function(){ + }).on('draw', function(){ $('#op').html(options.op_html || ''); $('#uc').html(options.uc_html || ''); var table_data = [];