From c300f0b549300dc53ec3fd0db2b13c80435db8db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=85=AB=E5=8D=83=E6=B5=81?= <40739051+jym503558564@users.noreply.github.com> Date: Tue, 11 Jun 2019 10:09:08 +0800 Subject: [PATCH] =?UTF-8?q?[Update]=E4=BC=98=E5=8C=96=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E9=A1=B5=E9=9D=A2=E7=9A=84=E8=B5=84=E4=BA=A7=E6=A0=87=E7=AD=BE?= =?UTF-8?q?=E8=BF=87=E6=BB=A4=E5=8A=9F=E8=83=BD=20(#2781)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [Update] 优化用户页面的资产标签下拉框选项 * [Update]增加用户页面的过滤资产标签功能 * [Update]优化用户页面的资产标签过滤 * [Update]优化用户页面的资产标签过滤代码 * [Update] 优化用户页面的资产标签过滤 --- .../templates/assets/user_asset_list.html | 6 ++++- apps/assets/views/asset.py | 1 + apps/perms/mixins.py | 26 +++++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/apps/assets/templates/assets/user_asset_list.html b/apps/assets/templates/assets/user_asset_list.html index 329f24c72..23d0b34ed 100644 --- a/apps/assets/templates/assets/user_asset_list.html +++ b/apps/assets/templates/assets/user_asset_list.html @@ -153,7 +153,11 @@ function initTree() { $(document).ready(function () { initTree(); initTable(); -}) +}).on('click', '.labels li', function () { + var val = $(this).text(); + $("#user_assets_table_filter input").val(val); + asset_table.search(val).draw(); + }) .on('click', '.asset_detail', function() { var data = asset_table.ajax.json(); var asset_id = this.getAttribute("asset-id"); diff --git a/apps/assets/views/asset.py b/apps/assets/views/asset.py index 653fa12c3..4a3ce2273 100644 --- a/apps/assets/views/asset.py +++ b/apps/assets/views/asset.py @@ -79,6 +79,7 @@ class UserAssetListView(LoginRequiredMixin, TemplateView): def get_context_data(self, **kwargs): context = { 'action': _('My assets'), + 'labels': Label.objects.all().order_by('name'), 'system_users': SystemUser.objects.all(), } kwargs.update(context) diff --git a/apps/perms/mixins.py b/apps/perms/mixins.py index 3adaa6e5b..f302285a6 100644 --- a/apps/perms/mixins.py +++ b/apps/perms/mixins.py @@ -14,6 +14,7 @@ class AssetsFilterMixin(object): def filter_queryset(self, queryset): queryset = self.search_assets(queryset) + queryset = self.filter_labels(queryset) queryset = self.sort_assets(queryset) return queryset @@ -40,6 +41,31 @@ class AssetsFilterMixin(object): queryset = sort_assets(queryset, order_by=order_by, reverse=reverse) return queryset + def filter_labels(self, queryset): + from assets.models import Label + query_keys = self.request.query_params.keys() + all_label_keys = Label.objects.values_list('name', flat=True) + valid_keys = set(all_label_keys) & set(query_keys) + labels_query = {} + for key in valid_keys: + labels_query[key] = self.request.query_params.get(key) + if not labels_query: + return queryset + + labels = set() + for k, v in labels_query.items(): + label = Label.objects.filter(name=k, value=v).first() + if not label: + continue + labels.add(label) + + _queryset = [] + for asset in queryset: + _labels = set(asset.labels.all()) & set(labels) + if _labels and len(_labels) == len(set(labels)): + _queryset.append(asset) + return _queryset + class RemoteAppFilterMixin(object): """