def output(self, key, obj, **kwargs): actual_key = key if self.attribute is None else self.attribute firstname = fields.get_value(actual_key + '.firstname', obj) lastname = fields.get_value(actual_key + '.lastname', obj) value = None if firstname is None else firstname + ' ' + lastname if value is None: default = self._v('default') return self.format(default) if default else default try: data = self.format(value) except fields.MarshallingError as event: msg = 'Unable to marshal field "{0}" value "{1}": {2}'.format(key, value, str(event)) raise fields.MarshallingError(msg) return self.mask.apply(data) if self.mask else data
def url(self, obj): if self._url: return self._url url_data = {} for key in self.url_data: value = get_value(self.url_data[key], obj) if value is None: APP.logger.debug( 'Could not build url because some provided values were none.\n' + 'UrlParam: "%s", ObjectKey: "%s"', key, self.url_data[key]) print(obj) return None url_data[key] = value endpoint = self.endpoint if self.endpoint is not None else request.endpoint o = urlparse(url_for(endpoint, _external=self.absolute, **url_data)) path = '' if o.path.endswith('/'): path = o.path + self.path_variables else: path = o.path + '/' + self.path_variables if self.absolute: scheme = self.scheme if self.scheme is not None else o.scheme return urlunparse((scheme, o.netloc, path, "", "", self.hashtag)) else: return urlunparse(("", "", path, "", "", self.hashtag))
def output(self, key, obj, **kwargs): date = fields.get_value( key if self.attribute is None else self.attribute, obj) duration = fields.get_value( 'duration' if self.duration_attribute is None else self.duration_attribute, obj) if date is None or duration is None: default = self._v('default') return self.format(default) if default else default value = date + timedelta(seconds=duration) try: data = self.format(value) except fields.MarshallingError as error: msg = 'Unable to marshal field "{0}" value "{1}": {2}'.format( key, value, str(error)) raise fields.MarshallingError(msg) return self.mask.apply(data) if self.mask else data
def output(self, key, obj): value = fields.get_value(key if self.attribute is None else self.attribute, obj) if value is None: if self.allow_null: return None elif self.default is not None: return self.default return marshal(value, self.nested)
def output(self, key, obj, orderes=False): data = {} for name in self.embedded_models: key = name if not self.embedded_models[ name].attribute else self.embedded_models[name].attribute value = get_value(key, obj) if value is not None and not (self.embedded_models[name].as_list and (len(value) == 0)): data[name] = marshal(value, self.nested_model(name)) return data
def output(self, key, obj, ordered=False, **kwargs): value = fields.get_value( key if self.attribute is None else self.attribute, obj) if value is None: if self.allow_null: return None elif self.default is not None: return self.default return marshal(value, self.nested, skip_none=self.skip_none, ordered=ordered)
def test_get_value_indexable_object(self): class Test(object): def __init__(self, value): self.value = value def __getitem__(self, n): if type(n) is int: if n < 3: return n raise IndexError raise TypeError obj = Test('hi') assert fields.get_value('value', obj) == 'hi'
def test_get_value(self): assert fields.get_value('foo', {'foo': 42}) == 42
def output(self, key, data): code = base.get_value(key if self.attribute is None else self.attribute, data) return self.format(code)
def test_get_value(self): assert_equal(fields.get_value('foo', {'foo': 42}), 42)
def test_get_value_no_value(self): assert_equal(fields.get_value("foo", {'foo': 42}), 42)
def test_get_value_obj(self): assert_equal(fields.get_value('foo', Mock(foo=42)), 42)
def test_get_value_no_value(self): assert fields.get_value("foo", {'foo': 42}) == 42
def test_get_value_obj(self, mocker): assert fields.get_value('foo', mocker.Mock(foo=42)) == 42
def output(self, key, data): code = base.get_value( key if self.attribute is None else self.attribute, data) return self.format(code)
def output(self, key, obj, **kwargs): value = fields.get_value('appointments', obj) if value is None: value = [] return len(value)
def output(self, key, obj, **kwargs): code = base.get_value( key if self.attribute is None else self.attribute, obj) return self.format(code)