From 368a03706270978c88c1bec892f5af9a346fd4b8 Mon Sep 17 00:00:00 2001 From: mabashian Date: Wed, 2 May 2018 14:16:08 -0400 Subject: [PATCH 1/2] Remove non-filterable fields from search key and treat searches using those keys like string searches. --- .../shared/smart-search/queryset.service.js | 12 ++++---- .../smart-search/smart-search.controller.js | 28 +++++++++++++++++-- .../smart-search/smart-search.partial.html | 2 +- 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/awx/ui/client/src/shared/smart-search/queryset.service.js b/awx/ui/client/src/shared/smart-search/queryset.service.js index 7d79ac1ec1..7d34a40a4e 100644 --- a/awx/ui/client/src/shared/smart-search/queryset.service.js +++ b/awx/ui/client/src/shared/smart-search/queryset.service.js @@ -364,7 +364,7 @@ function QuerysetService ($q, Rest, ProcessErrors, $rootScope, Wait, DjangoSearc return merged; }, - getSearchInputQueryset (searchInput, isRelatedField = null, isAnsibleFactField = null, singleSearchParam = null) { + getSearchInputQueryset (searchInput, isFilterableBaseField = null, isRelatedField = null, isAnsibleFactField = null, singleSearchParam = null) { // XXX Should find a better approach than passing in the two 'is...Field' callbacks XXX const space = '%20and%20'; let params = {}; @@ -406,12 +406,12 @@ function QuerysetService ($q, Rest, ProcessErrors, $rootScope, Wait, DjangoSearc if (termParts.length === 1) { termParams = searchWithoutKey(term, singleSearchParam); - } else if (isAnsibleFactField && isAnsibleFactField(termParts)) { + } else if ((isAnsibleFactField && isAnsibleFactField(termParts)) || (isFilterableBaseField && isFilterableBaseField(termParts))) { termParams = this.encodeParam({ term, singleSearchParam }); } else if (isRelatedField && isRelatedField(termParts)) { termParams = this.encodeParam({ term, singleSearchParam, related: true }); } else { - termParams = this.encodeParam({ term, singleSearchParam }); + termParams = searchWithoutKey(term, singleSearchParam); } params = _.merge(params, termParams, combineSameSearches); @@ -419,7 +419,7 @@ function QuerysetService ($q, Rest, ProcessErrors, $rootScope, Wait, DjangoSearc return params; }, - removeTermsFromQueryset(queryset, term, isRelatedField = null, singleSearchParam = null) { + removeTermsFromQueryset(queryset, term, isFilterableBaseField, isRelatedField = null, isAnsibleFactField = null, singleSearchParam = null) { const modifiedQueryset = _.cloneDeep(queryset); const removeSingleTermFromQueryset = (value, key) => { @@ -457,10 +457,12 @@ function QuerysetService ($q, Rest, ProcessErrors, $rootScope, Wait, DjangoSearc if (termParts.length === 1) { removed = searchWithoutKey(term, singleSearchParam); + } else if ((isAnsibleFactField && isAnsibleFactField(termParts)) || (isFilterableBaseField && isFilterableBaseField(termParts))) { + removed = this.encodeParam({ term, singleSearchParam }); } else if (isRelatedField && isRelatedField(termParts)) { removed = this.encodeParam({ term, singleSearchParam, related: true }); } else { - removed = this.encodeParam({ term, singleSearchParam }); + removed = searchWithoutKey(term, singleSearchParam); } if (!removed) { diff --git a/awx/ui/client/src/shared/smart-search/smart-search.controller.js b/awx/ui/client/src/shared/smart-search/smart-search.controller.js index 560fbd0c0c..0bab35df39 100644 --- a/awx/ui/client/src/shared/smart-search/smart-search.controller.js +++ b/awx/ui/client/src/shared/smart-search/smart-search.controller.js @@ -52,6 +52,12 @@ function SmartSearchController ( .then((data) => { $scope.models = data.models; $scope.options = data.options.data; + $scope.keyFields = _.reduce(data.models[$scope.djangoModel].base, function(result, value, key) { + if (!(typeof value.filterable === "boolean" && value.filterable === false)) { + result.push(key); + } + return result; + }, []); if ($scope.list) { $scope.$emit(optionsKey, data.options); } @@ -139,6 +145,17 @@ function SmartSearchController ( return rootField === 'ansible_facts'; } + function isFilterableBaseField (termParts) { + const rootField = termParts[0].split('.')[0].replace(/^-/, ''); + const listName = $scope.list.name; + const baseFieldPath = `models.${listName}.base.${rootField}`; + const isBaseField = _.has($scope, `${baseFieldPath}`); + const filterable = _.get($scope, `${baseFieldPath}.filterable`); + const isNotFilterable = typeof filterable === "boolean" && filterable === false; + const isBaseModelRelatedSearchTermField = (_.get($scope, `${baseFieldPath}.type`) === 'field'); + return isBaseField && !isBaseModelRelatedSearchTermField && !isNotFilterable; + } + function isRelatedField (termParts) { const rootField = termParts[0].split('.')[0].replace(/^-/, ''); const listName = $scope.list.name; @@ -154,7 +171,7 @@ function SmartSearchController ( const { singleSearchParam } = $scope; const unmodifiedQueryset = _.clone(queryset); - const searchInputQueryset = qs.getSearchInputQueryset(terms, isRelatedField, isAnsibleFactField, singleSearchParam); + const searchInputQueryset = qs.getSearchInputQueryset(terms, isFilterableBaseField, isRelatedField, isAnsibleFactField, singleSearchParam); queryset = qs.mergeQueryset(queryset, searchInputQueryset, singleSearchParam); // Go back to the first page after a new search @@ -191,7 +208,7 @@ function SmartSearchController ( const { singleSearchParam } = $scope; const [term] = $scope.searchTags.splice(index, 1); - queryset = qs.removeTermsFromQueryset(queryset, term, isRelatedField, singleSearchParam); + queryset = qs.removeTermsFromQueryset(queryset, term, isFilterableBaseField, isRelatedField, isAnsibleFactField, singleSearchParam); if (!$scope.querySet) { $state.go('.', { [searchKey]: queryset }) @@ -199,7 +216,7 @@ function SmartSearchController ( // for some reason deleting a tag from a list in a modal does not // remove the param from $stateParams. Here we'll manually check to make sure // that that happened and remove it if it didn't. - const clearedParams = qs.removeTermsFromQueryset($stateParams[searchKey], term, isRelatedField, singleSearchParam); + const clearedParams = qs.removeTermsFromQueryset($stateParams[searchKey], term, isFilterableBaseField, isRelatedField, isAnsibleFactField, singleSearchParam); $stateParams[searchKey] = clearedParams; }); } @@ -238,6 +255,11 @@ function SmartSearchController ( $scope.searchTags = qs.stripDefaultParams(queryset, defaults); }; + + $scope.hideUnfilterable = (field) => { + console.log(field); + return !(typeof field.value.filterable === "boolean" && field.value.filterable === false); + }; } SmartSearchController.$inject = [ diff --git a/awx/ui/client/src/shared/smart-search/smart-search.partial.html b/awx/ui/client/src/shared/smart-search/smart-search.partial.html index ce52fea759..a44a90f649 100644 --- a/awx/ui/client/src/shared/smart-search/smart-search.partial.html +++ b/awx/ui/client/src/shared/smart-search/smart-search.partial.html @@ -42,7 +42,7 @@
- FIELDS: {{ key }}, + FIELDS: {{ field }},
RELATED FIELDS: {{ relation }}, From 84c0fdfa4a22a9b7741522c9d9dc53b0bf249518 Mon Sep 17 00:00:00 2001 From: mabashian Date: Wed, 2 May 2018 14:21:43 -0400 Subject: [PATCH 2/2] Removed unused function --- .../src/shared/smart-search/smart-search.controller.js | 5 ----- 1 file changed, 5 deletions(-) diff --git a/awx/ui/client/src/shared/smart-search/smart-search.controller.js b/awx/ui/client/src/shared/smart-search/smart-search.controller.js index 0bab35df39..fb0a4c0701 100644 --- a/awx/ui/client/src/shared/smart-search/smart-search.controller.js +++ b/awx/ui/client/src/shared/smart-search/smart-search.controller.js @@ -255,11 +255,6 @@ function SmartSearchController ( $scope.searchTags = qs.stripDefaultParams(queryset, defaults); }; - - $scope.hideUnfilterable = (field) => { - console.log(field); - return !(typeof field.value.filterable === "boolean" && field.value.filterable === false); - }; } SmartSearchController.$inject = [