1 # Copyright (C) 2021-2022 Wind River Systems, Inc.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # -*- coding: utf-8 -*-
16 from __future__ import unicode_literals
18 # from collections import OrderedDict
19 from functools import wraps
20 # from six import iteritems
22 from flask import request
24 from flask_restx import Namespace
25 from flask_restx._http import HTTPStatus
26 from flask_restx.marshalling import marshal_with, marshal
27 from flask_restx.utils import merge
28 from flask_restx.mask import Mask # , apply as apply_mask
29 from flask_restx.model import Model
30 from flask_restx.fields import List, Nested, String
31 from flask_restx.utils import unpack
33 from o2common.helper import o2logging
34 logger = o2logging.get_logger(__name__)
37 class O2Namespace(Namespace):
39 def __init__(self, name, description=None, path=None, decorators=None,
40 validate=None, authorizations=None, ordered=False, **kwargs):
41 super().__init__(name, description, path, decorators,
42 validate, authorizations, ordered, **kwargs)
45 self, fields, as_list=False, code=HTTPStatus.OK, description=None,
49 A decorator specifying the fields to use for serialization.
51 :param bool as_list: Indicate that the return type is a list \
52 (for the documentation)
53 :param int code: Optionally give the expected HTTP response \
54 code if its different from 200
61 str(code): (description, [fields], kwargs)
63 else (description, fields, kwargs)
65 "__mask__": kwargs.get(
67 ), # Mask values can't be determined outside app context
69 func.__apidoc__ = merge(getattr(func, "__apidoc__", {}), doc)
70 return o2_marshal_with(fields, ordered=self.ordered,
76 class o2_marshal_with(marshal_with):
78 self, fields, envelope=None, skip_none=False, mask=None, ordered=False
81 :param fields: a dict of whose keys will make up the final
82 serialized response output
83 :param envelope: optional key that will be used to envelop the
87 self.envelope = envelope
88 self.skip_none = skip_none
89 self.ordered = ordered
90 self.mask = Mask(mask, skip=True)
92 def __call__(self, f):
94 def wrapper(*args, **kwargs):
95 resp = f(*args, **kwargs)
97 req_args = request.args
98 mask = self._gen_mask_from_selector(**req_args)
102 # if has_request_context():
103 # mask_header = current_app.config["RESTX_MASK_HEADER"]
104 # mask = request.headers.get(mask_header) or mask
105 if isinstance(resp, tuple):
106 data, code, headers = unpack(resp)
121 resp, self.fields, self.envelope, self.skip_none, mask,
127 def _gen_mask_from_selector(self, **kwargs) -> str:
129 if 'all_fields' in kwargs:
130 all_fields_without_space = kwargs['all_fields'].replace(" ", "")
131 all_fields = all_fields_without_space.lower()
132 if 'true' == all_fields:
135 elif 'fields' in kwargs and kwargs['fields'] != '':
136 fields_without_space = kwargs['fields'].replace(" ", "")
138 # filters = fields_without_space.split(',')
143 # a = self.__gen_mask_tree(f)
144 # mask_val_list.append(a)
146 # mask_val_list.append(f)
147 # mask_val = '{%s}' % ','.join(mask_val_list)
150 self.__update_selector_value(selector, fields_without_space, True)
152 mask_val = self.__gen_mask_from_selector(selector)
154 elif 'exclude_fields' in kwargs and kwargs['exclude_fields'] != '':
155 exclude_fields_without_space = kwargs['exclude_fields'].replace(
158 selector = self.__gen_selector_from_model_with_value(
161 self.__update_selector_value(
162 selector, exclude_fields_without_space, False)
164 mask_val = self.__gen_mask_from_selector(selector)
165 elif 'exclude_default' in kwargs and kwargs['exclude_default'] != '':
166 exclude_default_without_space = kwargs['exclude_default'].replace(
168 exclude_default = exclude_default_without_space.lower()
169 if 'true' == exclude_default:
177 def __gen_mask_tree(self, field: str) -> str:
179 f = field.split('/', 1)
181 s = self.__gen_mask_tree(f[1])
182 return '%s%s' % (f[0], s)
186 def __gen_selector_from_model_with_value(
187 self, model: Model, default_val: bool = True) -> dict:
190 if type(model[i]) is List:
191 if type(model[i].container) is String:
192 selector[i] = default_val
194 selector[i] = self.__gen_selector_from_model_with_value(
195 model[i].container.model, default_val)
197 elif type(model[i]) is Nested:
198 selector[i] = self.__gen_selector_from_model_with_value(
199 model[i].model, default_val)
200 selector[i] = default_val
203 def __update_selector_value(self, default_selector: dict, filter: str,
205 fields = filter.split(',')
208 self.__update_selector_tree_value(default_selector, f, val)
210 default_selector[f] = val
212 def __update_selector_tree_value(self, m: dict, filter: str, val: bool):
213 filter_list = filter.split('/', 1)
214 if filter_list[0] not in m:
215 m[filter_list[0]] = dict()
216 if len(filter_list) > 1:
217 self.__update_selector_tree_value(
218 m[filter_list[0]], filter_list[1], val)
220 m[filter_list[0]] = val
222 def __gen_mask_from_selector(self, fields: dict) -> str:
224 for k, v in fields.items():
226 s = self.__gen_mask_from_selector(v)
227 mask_li.append('%s%s' % (k, s))
232 return '{%s}' % ','.join(mask_li)