[Update]优化用户页面的资产标签过滤功能 (#2781)

* [Update] 优化用户页面的资产标签下拉框选项

* [Update]增加用户页面的过滤资产标签功能

* [Update]优化用户页面的资产标签过滤

* [Update]优化用户页面的资产标签过滤代码

* [Update] 优化用户页面的资产标签过滤
pull/2785/head
八千流 2019-06-11 10:09:08 +08:00 committed by 老广
parent 1ba9351957
commit c300f0b549
3 changed files with 32 additions and 1 deletions

View File

@ -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");

View File

@ -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)

View File

@ -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):
"""