X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=o2common%2Fviews%2Froute.py;h=c6fc56e974c40612cbbca4c1e78069aa6491aaf2;hb=356dc5488966c011e8a49fc87a8b824fba39b18b;hp=2c93d775b871762e57592b0be040d2de3a1f48e1;hpb=c532f7fbf1934c58e79c01dc36cd3eeaf9656b99;p=pti%2Fo2.git diff --git a/o2common/views/route.py b/o2common/views/route.py index 2c93d77..c6fc56e 100644 --- a/o2common/views/route.py +++ b/o2common/views/route.py @@ -30,6 +30,8 @@ from flask_restx.model import Model from flask_restx.fields import List, Nested, String from flask_restx.utils import unpack +from o2common.views.route_exception import BadRequestException + from o2common.helper import o2logging logger = o2logging.get_logger(__name__) @@ -96,8 +98,8 @@ class o2_marshal_with(marshal_with): req_args = request.args mask = self._gen_mask_from_selector(**req_args) - - # mask = self.mask + if mask == '': + mask = self.mask # if has_request_context(): # mask_header = current_app.config["RESTX_MASK_HEADER"] @@ -128,9 +130,13 @@ class o2_marshal_with(marshal_with): mask_val = '' if 'all_fields' in kwargs: all_fields_without_space = kwargs['all_fields'].replace(" ", "") - all_fields = all_fields_without_space.lower() - if 'true' == all_fields: - mask_val = '' + logger.debug('all_fields selector value is {}'.format( + all_fields_without_space)) + # all_fields = all_fields_without_space.lower() + # if 'true' == all_fields: + selector = self.__gen_selector_from_model_with_value( + self.fields) + mask_val = self.__gen_mask_from_selector(selector) elif 'fields' in kwargs and kwargs['fields'] != '': fields_without_space = kwargs['fields'].replace(" ", "") @@ -148,6 +154,7 @@ class o2_marshal_with(marshal_with): selector = {} self.__update_selector_value(selector, fields_without_space, True) + self.__set_default_mask(selector) mask_val = self.__gen_mask_from_selector(selector) @@ -160,6 +167,7 @@ class o2_marshal_with(marshal_with): self.__update_selector_value( selector, exclude_fields_without_space, False) + self.__set_default_mask(selector) mask_val = self.__gen_mask_from_selector(selector) elif 'exclude_default' in kwargs and kwargs['exclude_default'] != '': @@ -200,14 +208,17 @@ class o2_marshal_with(marshal_with): selector[i] = default_val return selector - def __update_selector_value(self, default_selector: dict, filter: str, + def __update_selector_value(self, selector: dict, filter: str, val: bool): fields = filter.split(',') for f in fields: if '/' in f: - self.__update_selector_tree_value(default_selector, f, val) + self.__update_selector_tree_value(selector, f, val) continue - default_selector[f] = val + if f not in self.fields: + raise BadRequestException( + 'Selector attribute {} not found'.format(f)) + selector[f] = val def __update_selector_tree_value(self, m: dict, filter: str, val: bool): filter_list = filter.split('/', 1) @@ -230,3 +241,7 @@ class o2_marshal_with(marshal_with): mask_li.append(k) return '{%s}' % ','.join(mask_li) + + def __set_default_mask(self, selector: dict, val: bool = True): + default_selector = str(getattr(self.fields, "__mask__"))[1:-1] + self.__update_selector_value(selector, default_selector, val)