FormEncode


SQLSchema

SQLSchema objects are FormEncode schemas that are attached to specific instances or classes.

In .from_python(object) these schemas serialize SQLObject instances to dictionaries of values, or to empty dictionaries (when serializing a class). The object passed in should either be None (new or default object) or the object to edit.

In .to_python these either create new objects (when no id field is present) or edit an object by the included id. The returned value is the created object.

SQLObject validators are applied to the input, as is notNone restrictions. Also column restrictions and defaults are applied. Note that you can add extra fields to this schema, and they will be applied before the SQLObject validators and restrictions. This means you can use, for instance, validators.DateConverter() (assigning it to the same name as the SQLObject class's date column) to have this serialize date columns to/from strings.

You can override update_object to change the actual instantiation.

The basic idea is that a SQLSchema 'wraps' a class or instance (most typically a class). So it would look like:

class PersonSchema(SQLSchema):
    wrap = Person

ps = PersonSchema()
form_defaults = ps.from_python(None)
new_object = ps.to_python(form_input)
form_defaults = ps.from_python(aPerson)
edited_person = ps.to_python(edited_form_input)

To override the encoding and decoding, use update_object and get_current. In this example, lets say that we take a single name field instead of a first_name and last_name (which is what the database has):

class PersonSchema(SQLSchema):
    wrap = Person

    def update_object(self, columns, extra, state):
        name = extra.pop('name')
        fname, lname = name.split(None, 1)
        columns['first_name'] = fname
        columns['last_name'] = lname
        return super(PersonSchema).update_object(
            columns, extra, state)

    def get_current(self, obj, state):
        value = super(PersonSchema).get_current(obj, state)
        value['name'] = '%(first_name)s %(last_name)s' % value
        del value['first_name']
        del value['last_name']

Messages

badID:
The id %(value)r did not match the expected id
badType:
The input must be a string (not a %(type)s: %(value)r)
empty:
Please enter a value
invalidID:
The id is not valid: %(error)s
missingValue:
Missing value
noneType:
The input must be a string (not None)
notExpected:
The input field %(name)s was not expected.
notNone:
You may not provide None for that value

Attributes

a allow_edit

True

a strip

False

a wrap

None

a accept_python

True

a instance

<bound class method SQLSchema.instance>

a secret

None

a __singletonmethods__

('to_python', 'from_python')

a add_field

<bound class method SQLSchema.add_field>

a compound

True

a sign_id

False

a not_empty

False

a add_chained_validator

<bound class method SQLSchema.add_chained_validator>

a object

<bound class method SQLSchema.object>

a add_pre_validator

<bound class method SQLSchema.add_pre_validator>

a fields

{}

a __mutableattributes__

('fields', 'chained_validators', 'pre_validators')

a repeating

False

a order

[]

Methods

f __classinit__(cls, new_attrs) ...

f __initargs__(self, new_attrs) ...

f is_empty(self, value) ...

f __init__(self, *args, **kw) ...

f __call__(self, *args, **kw) ...

f __sourcerepr__(self, source, binding=None) ...

f update_object(self, columns, extra_fields, state) ...

Actually do the action, like create or update an object.

f __classsourcerepr__(cls, source, binding=None) ...

f get_current(self, obj, state) ...

f get_defaults(self, soClass, state) ...

See the source for more information.