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