X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=o2common%2Fviews%2Fview.py;h=7feaf04278333ae2ab18dc6667065f464c5106a5;hb=c532f7fbf1934c58e79c01dc36cd3eeaf9656b99;hp=b49a2c6380f881262e3eef3223c0471718ee5287;hpb=9b9b1da2e26de1bc639827e10c5965b358d20b9e;p=pti%2Fo2.git diff --git a/o2common/views/view.py b/o2common/views/view.py index b49a2c6..7feaf04 100644 --- a/o2common/views/view.py +++ b/o2common/views/view.py @@ -13,6 +13,7 @@ # limitations under the License. from sqlalchemy.sql.elements import ColumnElement +from sqlalchemy import or_ from o2common.helper import o2logging logger = o2logging.get_logger(__name__) @@ -48,38 +49,50 @@ def toFilterArgs(operation: str, obj: ColumnElement, key: str, values: list): (key, str(obj))) return [] + if operation in ['eq', 'neq', 'gt', 'lt', 'gte', 'lte']: + if len(values) != 1: + raise KeyError('Filter operation one is only support one value.') + elif operation in ['in', 'nin', 'cont', 'ncont']: + if len(values) == 0: + raise KeyError('Filter operation value is needed.') + else: + raise KeyError('Filter operation value not support.') + ll = list() if operation == 'eq': - for val in values: - if val.lower() == 'null': - val = None - ll.append(getattr(obj, key) == val) + val = values[0] + if val.lower() == 'null': + val = None + ll.append(getattr(obj, key) == val) elif operation == 'neq': - for val in values: - if val.lower() == 'null': - val = None - ll.append(getattr(obj, key) != val) + val = values[0] + if val.lower() == 'null': + val = None + ll.append(getattr(obj, key) != val) elif operation == 'gt': - for val in values: - ll.append(getattr(obj, key) > val) + val = values[0] + ll.append(getattr(obj, key) > val) elif operation == 'lt': - for val in values: - ll.append(getattr(obj, key) < val) + val = values[0] + ll.append(getattr(obj, key) < val) elif operation == 'gte': - for val in values: - ll.append(getattr(obj, key) >= val) + val = values[0] + ll.append(getattr(obj, key) >= val) elif operation == 'lte': - for val in values: - ll.append(getattr(obj, key) <= val) + val = values[0] + ll.append(getattr(obj, key) <= val) elif operation == 'in': - pass + ll.append(getattr(obj, key).in_(values)) elif operation == 'nin': - pass - elif operation == 'count': - pass - elif operation == 'ncount': - pass - else: - raise KeyError('Filter operation value not support.') - + ll.append(~getattr(obj, key).in_(values)) + elif operation == 'cont': + val_list = list() + for val in values: + val_list.append(getattr(obj, key).contains(val)) + ll.append(or_(*val_list)) + elif operation == 'ncont': + val_list = list() + for val in values: + val_list.append(getattr(obj, key).contains(val)) + ll.append(~or_(*val_list)) return ll