From: Bin Yang Date: Fri, 18 Nov 2022 03:33:25 +0000 (+0800) Subject: Fix filter issue to support value with space X-Git-Tag: 2.0.0-rc2~9 X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=commitdiff_plain;h=356dc5488966c011e8a49fc87a8b824fba39b18b;p=pti%2Fo2.git Fix filter issue to support value with space Issue-ID: INF-365 Signed-off-by: Bin Yang Change-Id: I3d567107d6dbc05bc7a8f5272fbd8efd6c5fc226 --- diff --git a/o2common/domain/filter.py b/o2common/domain/filter.py index 80fe322..de89cea 100644 --- a/o2common/domain/filter.py +++ b/o2common/domain/filter.py @@ -22,45 +22,46 @@ logger = o2logging.get_logger(__name__) def gen_orm_filter(obj: ColumnElement, filter_str: str): if not filter_str: return [] - filter_without_space = filter_str.replace(" ", "") + # filter_without_space = filter_str.replace(" ", "") + filter_without_space = filter_str.strip(' ()') items = filter_without_space.split(';') filter_list = list() for i in items: - if '(' in i: - i = i.replace("(", "") - if ')' in i: - i = i.replace(")", "") + # if '(' in i: + # i = i.replace("(", "") + # if ')' in i: + # i = i.replace(")", "") filter_expr = i.split(',') if len(filter_expr) < 3: continue - filter_op = filter_expr[0] - filter_key = filter_expr[1] + filter_op = filter_expr[0].strip() + filter_key = filter_expr[1].strip() filter_vals = filter_expr[2:] filter_list.extend(toFilterArgs( filter_op, obj, filter_key, filter_vals)) - logger.info('Filter list length: %d' % len(filter_list)) + logger.debug('Filter list length: %d' % len(filter_list)) return filter_list def toFilterArgs(operation: str, obj: ColumnElement, key: str, values: list): - if not hasattr(obj, key): - logger.warning('Filter attrName %s not in Object %s.' % - (key, str(obj))) - raise KeyError( - 'Filter attrName {} not in the Object'.format(key)) + # if not hasattr(obj, key): + # logger.warning('Filter attrName %s not in Object %s' % + # (key, str(obj))) + # raise KeyError( + # 'Filter attrName {} not in the Object'.format(key)) - if operation in ['eq', 'neq', 'gt', 'lt', 'gte', 'lte']: - if len(values) != 1: - raise KeyError( - 'Filter operation one {} is only support one value.'. - format(operation)) - elif operation in ['in', 'nin', 'cont', 'ncont']: - if len(values) == 0: - raise KeyError('Filter operation {} value is needed.'. - format(operation)) - else: - raise KeyError('Filter operation {} not support.'.format(operation)) + # if operation in ['eq', 'neq', 'gt', 'lt', 'gte', 'lte']: + # if len(values) != 1: + # raise KeyError( + # 'Filter operation one {} is only support one value'. + # format(operation)) + # elif operation in ['in', 'nin', 'cont', 'ncont']: + # if len(values) == 0: + # raise KeyError('Filter operation {} value is needed'. + # format(operation)) + # else: + # raise KeyError('Filter operation {} not support'.format(operation)) ll = list() if operation == 'eq': diff --git a/o2common/views/pagination_view.py b/o2common/views/pagination_view.py index 9f282f2..2ea68b4 100644 --- a/o2common/views/pagination_view.py +++ b/o2common/views/pagination_view.py @@ -38,7 +38,7 @@ class Pagination: def get_result(self, ret: Tuple[int, List[Serializer]]): count = ret[0] - logger.info('List count: {}'.format(count)) + logger.debug('List count: {}'.format(count)) ret_list = ret[1] page_total = int(math.ceil(count/self.limit) ) if count > self.limit else 1 diff --git a/o2common/views/route.py b/o2common/views/route.py index 9366a32..c6fc56e 100644 --- a/o2common/views/route.py +++ b/o2common/views/route.py @@ -130,7 +130,7 @@ class o2_marshal_with(marshal_with): mask_val = '' if 'all_fields' in kwargs: all_fields_without_space = kwargs['all_fields'].replace(" ", "") - logger.info('all_fields selector value is {}'.format( + logger.debug('all_fields selector value is {}'.format( all_fields_without_space)) # all_fields = all_fields_without_space.lower() # if 'true' == all_fields: diff --git a/o2common/views/view.py b/o2common/views/view.py index 2b05027..d390634 100644 --- a/o2common/views/view.py +++ b/o2common/views/view.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -import re +# import re from sqlalchemy.sql.elements import ColumnElement from o2common.views.route_exception import BadRequestException @@ -44,33 +44,49 @@ def gen_filter(obj: ColumnElement, filter_str: str): def check_filter(obj: ColumnElement, filter_str: str): if not filter_str: return - pattern = r'^(\((eq|neq|gt|lt|gte|lte){1},\w+,[\w -\.]+\)\;?|' +\ - r'\((in|nin|cont|ncont){1},\w*(,[\w -\.]*)*\)\;?)+' - result = re.match(pattern, filter_str) - logger.warning('filter: {} match result is {}'.format(filter_str, result)) - if not result: - raise BadRequestException( - 'filter value formater not correct.') + # pattern = r'^(\((eq|neq|gt|lt|gte|lte){1},\w+,[\w -\.]+\)\;?|' +\ + # r'\((in|nin|cont|ncont){1},\w*(,[\w -\.]*)*\)\;?)+' + # result = re.match(pattern, filter_str) + # logger.debug('filter: {} match result is {}'.format(filter_str, result)) + # if not result: + # raise BadRequestException( + # 'filter value format is invalid') check_filter_attribute(obj, filter_str) def check_filter_attribute(obj: ColumnElement, filter_str: str): - filter_without_space = filter_str.replace(" ", "") + # filter_without_space = filter_str.replace(" ", "") + filter_without_space = filter_str.strip(' ()') + logger.debug( + f"filter_str: {filter_str}, stripped: {filter_without_space}") items = filter_without_space.split(';') for i in items: - if '(' in i: - i = i.replace("(", "") - if ')' in i: - i = i.replace(")", "") + # if '(' in i: + # i = i.replace("(", "") + # if ')' in i: + # i = i.replace(")", "") filter_expr = i.split(',') if len(filter_expr) < 3: raise BadRequestException( - 'Filter {} formater not correct.'.format(i)) + 'ignore invalid filter {}'.format(i)) continue - # filter_op = filter_expr[0] - filter_key = filter_expr[1] - # filter_vals = filter_expr[2:] + filter_op = filter_expr[0].strip() + filter_key = filter_expr[1].strip() + filter_vals = filter_expr[2:] + if filter_op in ["eq", "neq", "gt", "lt", "gte", "lte"]: + if len(filter_vals) != 1: + raise BadRequestException( + "Found {} values: {} while only single value" + " is allowed for operation {}".format( + len(filter_vals), filter_vals, filter_op) + ) + elif filter_op not in ["in", "nin", "cont", "ncont"]: + raise BadRequestException( + 'Filter operation {} is invalid'.format(filter_op) + ) + else: + pass if not hasattr(obj, filter_key): raise BadRequestException( - 'Filter attrName {} not in the Object'.format(filter_key)) + 'Filter attrName {} is invalid'.format(filter_key))