示例#1
0
    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
示例#2
0
 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))
示例#3
0
    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
示例#4
0
    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)
示例#5
0
    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
示例#6
0
    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)
示例#7
0
    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'
示例#8
0
    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'
示例#9
0
 def test_get_value(self):
     assert fields.get_value('foo', {'foo': 42}) == 42
示例#10
0
 def output(self, key, data):
     code = base.get_value(key if self.attribute is None
                           else self.attribute, data)
     return self.format(code)
示例#11
0
 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(self):
     assert_equal(fields.get_value('foo', {'foo': 42}), 42)
示例#14
0
 def test_get_value_obj(self):
     assert_equal(fields.get_value('foo', Mock(foo=42)), 42)
示例#15
0
 def test_get_value_no_value(self):
     assert_equal(fields.get_value("foo", {'foo': 42}), 42)
示例#16
0
 def test_get_value_no_value(self):
     assert fields.get_value("foo", {'foo': 42}) == 42
示例#17
0
 def test_get_value_obj(self, mocker):
     assert fields.get_value('foo', mocker.Mock(foo=42)) == 42
 def test_get_value_obj(self):
     assert_equal(fields.get_value('foo', Mock(foo=42)), 42)
示例#19
0
文件: fields.py 项目: chrnux/cerebrum
 def output(self, key, data):
     code = base.get_value(
         key if self.attribute is None else self.attribute, data)
     return self.format(code)
示例#20
0
 def test_get_value(self):
     assert fields.get_value('foo', {'foo': 42}) == 42
示例#21
0
 def test_get_value_no_value(self):
     assert fields.get_value("foo", {'foo': 42}) == 42
示例#22
0
 def test_get_value_obj(self, mocker):
     assert fields.get_value('foo', mocker.Mock(foo=42)) == 42
示例#23
0
 def output(self, key, obj, **kwargs):
     value = fields.get_value('appointments', obj)
     if value is None:
         value = []
     return len(value)
示例#24
0
 def output(self, key, obj, **kwargs):
     code = base.get_value(
         key if self.attribute is None else self.attribute, obj)
     return self.format(code)