def _test_leaf_field(fixture_name, k, f, v):

    ser = serializer_for_field(f)
    assert ser
    verifyObject(ISerializer, ser)

    s1 = ser.dumps(v)
    v1 = ser.loads(s1)
    assert v1 == v
def _test_leaf_field(fixture_name, k, f, v):
    
    ser = serializer_for_field(f)
    assert ser
    verifyObject(ISerializer, ser)

    s1 = ser.dumps(v)
    v1 = ser.loads(s1)
    assert v1 == v
def test_field_textline():

    f = zope.schema.TextLine(title=u'Summary')

    for fmt in ['default']:
        ser = serializer_for_field(f, fmt)
        assert ser
        verifyObject(ISerializer, ser)

        v = u'Καλημέρα Κόσμε'
        s = ser.dumps(v)
        assert s

        v1 = ser.loads(s)
        assert v == v1

    fmt = 'not-existing-format'
    try:
        ser = serializer_for_field(f, fmt)
    except:
        pass
    else:
        assert False, 'Unexpected serialization format'
def test_field_textline():

    f = zope.schema.TextLine(title=u'Summary')
    
    for fmt in ['default']:
        ser = serializer_for_field(f, fmt)
        assert ser 
        verifyObject(ISerializer, ser)

        v = u'Καλημέρα Κόσμε'
        s = ser.dumps(v)
        assert s    

        v1 = ser.loads(s)
        assert v == v1
  
    fmt = 'not-existing-format'
    try:
        ser = serializer_for_field(f, fmt)
    except:
        pass
    else:
        assert False, 'Unexpected serialization format'
def test_field_datetime():

    f = zope.schema.Datetime(title=u'Created')

    ser = serializer_for_field(f)
    assert ser
    verifyObject(ISerializer, ser)

    dt1 = datetime.datetime(2014, 8, 1, 8, 0, 0)

    s = ser.dumps(dt1)
    print s
    assert s == '2014-08-01T08:00:00'

    v = ser.loads(s)
    assert v == dt1
def test_field_datetime():
    
    f = zope.schema.Datetime(title=u'Created')
    
    ser = serializer_for_field(f)
    assert ser 
    verifyObject(ISerializer, ser)

    dt1 = datetime.datetime(2014, 8, 1, 8, 0, 0)

    s = ser.dumps(dt1)
    print s
    assert s == '2014-08-01T08:00:00'   

    v = ser.loads(s)
    assert v == dt1
Ejemplo n.º 7
0
        def _get_field_value(self, v, field):
            '''Get the value of a field considered a leaf.
            Serialize or format (not both!) this value, if requested so.
            '''
            assert v is not None, 'This was supposed to be checked at dictize()'

            # Check if value needs to be serialized

            serializer_name = self.opts.get('serialize-values', False)
            if serializer_name:
                ser = serializer_for_field(field, serializer_name)
                if ser:
                    try:
                        v = ser.dumps(v)
                    except Exception as ex:
                        logger.warn(
                            'Failed to serialize value %r for field %r (%s): %s'
                            %
                            (v, field.__name__, field.__class__.__name__, ex))
                        v = None
                # Return here, no need to do anything more
                return v

            # Check if value needs to be formatted

            format_spec = self.opts.get('format-values', False)
            if format_spec:
                assert isinstance(format_spec, FormatSpec)
                fo = formatter_for_field(field, format_spec.name)
                if fo:
                    fo_opts = format_spec.opts
                    # Fetch any extra field-level extra options
                    fo_conf = formatters.config_for_field(
                        field, format_spec.name)
                    if fo_conf and 'extra-opts' in fo_conf:
                        fo_opts = copy.copy(fo_opts)
                        fo_opts.update(fo_conf.get('extra-opts'))
                    # Try to format
                    try:
                        v = fo.format(v, opts=fo_opts)
                    except Exception as ex:
                        logger.warn(
                            'Failed to format value %r for field %r (%s): %s' %
                            (v, field.__name__, field.__class__.__name__, ex))
                        v = None

            return v
Ejemplo n.º 8
0
        def _create_leaf_field(self, v, field):
            assert v is not None, 'This was supposed to be checked at load()'
            f = None

            ser_name = self.opts.get('unserialize-values', False)
            if ser_name:
                ser = serializer_for_field(field, ser_name)
                if ser:
                    try:
                        f = ser.loads(v)
                    except:
                        logger.warn(
                            'Failed to unserialize value %r for field %r' %
                            (v, field))

            if f is None:
                f = copy.copy(v)  # or simply assign it ??

            return f