Improve GET packages API call

Add 'search' and 'type' parameters

Allows to search any text occurrence in db
Support several params with 3 kinds od delimeters: space, comma and semicolon

Type allows to filter packages only by specified type

Partially-implements blueprint murano-repository-api-v2
Closes-Bug: #1304344

Change-Id: I4edef62dec2a3cf14506bede2a7482ed957c9a7e
This commit is contained in:
Ekaterina Fedorova 2014-04-08 14:45:50 +04:00
parent d000b7b1f8
commit b5681a22bf
2 changed files with 30 additions and 7 deletions

View File

@ -17,14 +17,10 @@ from muranoapi.db import session as db_session
stats = None
SUPPORTED_PARAMS = ('order_by', 'category', 'marker', 'tag', 'class_name',
'limit', 'type', 'fqn', 'category', 'owned')
LIST_PARAMS = ('category', 'tag', 'class_name', 'order_by')
SUPPORTED_PARAMS = ('order_by', 'category', 'marker', 'tag', 'class',
'limit', 'type', 'fqn', 'category', 'owned', 'search')
LIST_PARAMS = ('category', 'tag', 'class', 'order_by')
ORDER_VALUES = ('fqn', 'name', 'created')
SEARCH_MAPPING = {'fqn': 'fully_qualified_name',
'name': 'name',
'created': 'created'
}
PKG_PARAMS_MAP = {'display_name': 'name',
'full_name': 'fully_qualified_name',
'raw_ui': 'ui_definition',

View File

@ -13,6 +13,7 @@
# under the License.
from sqlalchemy import or_
from sqlalchemy.orm import attributes
from sqlalchemy import sql
from webob import exc
@ -262,6 +263,8 @@ def package_search(filters, context):
else:
query = session.query(pkg).filter(or_((pkg.is_public & pkg.enabled),
pkg.owner_id == context.tenant))
if 'type' in filters.keys():
query = query.filter(pkg.type == filters['type'].title())
if 'category' in filters.keys():
query = query.filter(pkg.categories.any(
@ -275,6 +278,30 @@ def package_search(filters, context):
if 'fqn' in filters.keys():
query = query.filter(pkg.fully_qualified_name == filters['fqn'])
if 'search' in filters.keys():
fk_fields = {'categories': 'Category',
'tags': 'Tag',
'class_definition': 'Class'}
conditions = []
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:
conditions.append(getattr(pkg, attr).like(_word))
query = query.filter(or_(*conditions))
sort_keys = filters.get('order_by', []) or ['created']
for key in sort_keys:
query = query.order_by(get_pkg_attr(pkg, key))