def test_primitive(): class SerializableClass(SerializableObject): def serializable_export(self, config): return {'test': 'export'} eq_(primitive(SerializableClass()), {'test': 'export'}) eq_(primitive(datetime(2010, 01, 01, 04, 20)), { '#type': 'inyoka.datetime', 'value': '2010-01-01T04:20:00Z' }) eq_(primitive(Locale('en', 'US')), u'en_US') eq_( primitive({ 'dt': datetime(2010, 01, 04, 20, 04), 'str': u'some string' }), { 'dt': { '#type': 'inyoka.datetime', 'value': '2010-01-04T20:04:00Z' }, 'str': u'some string' }) eq_(primitive([1, 2, 3, 4]), [1, 2, 3, 4]) eq_(primitive([1, 2, 3, datetime(2010, 01, 04, 20, 04)]), [ 1, 2, 3, { '#type': 'inyoka.datetime', 'value': '2010-01-04T20:04:00Z' } ])
def __call__(self, field, **kwargs): input_html = super(TokenInput, self).__call__(field, **kwargs) tags = field.data or [] serializer, mime = get_serializer("json") ro = primitive(tags, config={"show_type": False, Tag.object_type: ("id", "name")}) tags_json = serializer(ro) js = TOKEN_INPUT % (field.id, href("api/core/get_tags", format="json"), tags_json) return HTMLString(input_html + js)
def test_serializable_object(): class SerializableClass(SerializableObject): object_type = u'_test.class' public_fields = ('id', 'value') id = 1 value = u'some value' def get_value(self): return u'got value' obj = SerializableClass() eq_(primitive(obj), { '#type': u'_test.class', 'id': 1, 'value': u'some value' }) # test various possibilities to apply an external configuration eq_(primitive(obj, config={'show_type': False}), { 'id': 1, 'value': u'some value' }) eq_( primitive(obj, config={ '_test.class': ('id', ), '_test.klass': ('id', 'value') }), { 'id': 1, '#type': u'_test.class' }) # test special values such as callables and tuples as values eq_( primitive( obj, config={'_test.class': ('id', 'get_value', ('aliased', 'value'))}), { 'id': 1, '#type': u'_test.class', 'get_value': u'got value', 'aliased': u'some value' })
def test_primitive(): class SerializableClass(SerializableObject): def serializable_export(self, config): return {'test': 'export'} eq_(primitive(SerializableClass()), {'test': 'export'}) eq_(primitive(datetime(2010, 01, 01, 04, 20)), {'#type': 'inyoka.datetime', 'value': '2010-01-01T04:20:00Z'}) eq_(primitive(Locale('en', 'US')), u'en_US') eq_(primitive({'dt': datetime(2010, 01, 04, 20, 04), 'str': u'some string'}), {'dt': {'#type': 'inyoka.datetime', 'value': '2010-01-04T20:04:00Z'}, 'str': u'some string'}) eq_(primitive([1,2,3,4]), [1,2,3,4]) eq_(primitive([1,2,3, datetime(2010, 01, 04, 20, 04)]), [1, 2, 3, {'#type': 'inyoka.datetime', 'value': '2010-01-04T20:04:00Z'}])
def __call__(self, field, **kwargs): input_html = super(TokenInput, self).__call__(field, **kwargs) tags = field.data or [] serializer, mime = get_serializer('json') ro = primitive(tags, config={ 'show_type': False, Tag.object_type: ('id', 'name') }) tags_json = serializer(ro) js = TOKEN_INPUT % (field.id, href('api/core/get_tags', format='json'), tags_json) return HTMLString(input_html + js)
def test_serializable_object(): class SerializableClass(SerializableObject): object_type = u'_test.class' public_fields = ('id', 'value') id = 1 value = u'some value' def get_value(self): return u'got value' obj = SerializableClass() eq_(primitive(obj), {'#type': u'_test.class', 'id': 1, 'value': u'some value'}) # test various possibilities to apply an external configuration eq_(primitive(obj, config={'show_type': False}), {'id': 1, 'value': u'some value'}) eq_(primitive(obj, config={'_test.class': ('id',), '_test.klass': ('id', 'value')}), {'id': 1, '#type': u'_test.class'}) # test special values such as callables and tuples as values eq_(primitive(obj, config={ '_test.class': ('id', 'get_value', ('aliased', 'value')) }), {'id': 1, '#type': u'_test.class', 'get_value': u'got value', 'aliased': u'some value'})
def test_doomed_serializable_object(): class SerializableClass(SerializableObject): object_type = u'_test.class' public_fields = None primitive(SerializableClass())