From b9ecbbe50da86852fb767dd6e20202a5d59040d7 Mon Sep 17 00:00:00 2001 From: Timur Sufiev Date: Thu, 24 Apr 2014 15:26:19 +0400 Subject: [PATCH] Revert "Remove hardcoded foreign keys from db.catalog.api" It uses SqlAlchemy 0.8, which doesn't fit global requirement sqlalchemy>=0.7.8. This reverts commit f960c88209c78bc4075de70a10231552748dbff9. Change-Id: I97ca02519856e2c183af6c8e0b7f4731b94490f9 --- muranoapi/db/catalog/api.py | 39 +++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/muranoapi/db/catalog/api.py b/muranoapi/db/catalog/api.py index 47ed157d..9a122ec9 100644 --- a/muranoapi/db/catalog/api.py +++ b/muranoapi/db/catalog/api.py @@ -12,7 +12,6 @@ # License for the specific language governing permissions and limitations # under the License. -from sqlalchemy import inspection from sqlalchemy import or_ from sqlalchemy.orm import attributes from sqlalchemy import sql @@ -271,29 +270,27 @@ def package_search(filters, context): query = query.filter(pkg.fully_qualified_name == filters['fqn']) if 'search' in filters.keys(): + fk_fields = {'categories': 'Category', + 'tags': 'Tag', + 'class_definitions': 'Class'} conditions = [] - search_str = filters['search'] - for delimiter in ',;': - search_str = search_str.replace(delimiter, ' ') - key_words = ['%{0}%'.format(word) for word in search_str.split()] - relationships = inspection.inspect(pkg).relationships - searchable_attrs = set([ - 'fully_qualified_name', 'author', 'name', 'description', 'tags', - 'categories', 'class_definitions']) - searchable_pkg_attrs = set(dir(pkg)) & searchable_attrs - - for attr_name in searchable_pkg_attrs: - attr = getattr(pkg, attr_name) - if isinstance(attr, attributes.InstrumentedAttribute): - if attr_name in relationships: - model = relationships[attr_name].mapper.class_ - for key_word in key_words: - condition = attr.any(model.name.like(key_word)) + for attr in dir(pkg): + if attr.startswith('_'): + continue + if isinstance(getattr(pkg, attr), + attributes.InstrumentedAttribute): + search_str = filters['search'] + for delim in ',;': + search_str = search_str.replace(delim, ' ') + for key_word in search_str.split(): + _word = '%{value}%'.format(value=key_word) + if attr in fk_fields.keys(): + condition = getattr(pkg, attr).any( + getattr(models, fk_fields[attr]).name.like(_word)) conditions.append(condition) - else: - for key_word in key_words: - conditions.append(attr.like(key_word)) + else: + conditions.append(getattr(pkg, attr).like(_word)) query = query.filter(or_(*conditions)) sort_keys = filters.get('order_by', []) or ['created']