cybertools/composer/schema
helmutm 32c52c7b31 provide schema factory adapter
git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@2079 fd906abe-77d9-0310-91a1-e0d9ade77398
2007-09-30 16:00:55 +00:00
..
browser composer.schema ready for form/dialog handling 2007-09-22 14:37:48 +00:00
__init__.py more on cybertools.reporter: control via the field's renderFactory 2007-05-21 17:28:16 +00:00
client.py work in progress: service managment with composer.schema for tumsm 2007-07-31 09:15:48 +00:00
configure.zcml provide schema factory adapter 2007-09-30 16:00:55 +00:00
factory.py provide schema factory adapter 2007-09-30 16:00:55 +00:00
field.py composer.schema ready for form/dialog handling 2007-09-22 14:37:48 +00:00
instance.py composer.schema ready for form/dialog handling 2007-09-22 14:37:48 +00:00
interfaces.py provide schema factory adapter 2007-09-30 16:00:55 +00:00
README.txt provide schema factory adapter 2007-09-30 16:00:55 +00:00
schema.py rearrangement of schema_macros; added field instance class for number fields 2007-08-30 10:56:28 +00:00
tests.py removed Instance; added schema package 2007-05-15 09:36:02 +00:00

===========================
Schema and Field Management
===========================

  ($Id$)

  >>> from cybertools.composer.schema import Schema
  >>> from cybertools.composer.schema import Field


Working with predefined schemas
===============================

We start with setting up a schema with fields.

  >>> serviceSchema = Schema(
  ...     Field(u'title', renderFactory=None),
  ...     Field(u'description'),
  ...     Field(u'start'),
  ...     Field(u'end'),
  ...     Field(u'capacity'),
  ... )

For using a schema we need some class that we can use for creating
objects.

  >>> class Service(object):
  ...     pass

The schema will be connected with an object via an instance adapter.
In addition, we need a field instance adapter that cares for  the
correct conversion of input data to context attributes.

  >>> from cybertools.composer.schema.instance import Editor
  >>> from cybertools.composer.schema.field import FieldInstance
  >>> from zope import component
  >>> component.provideAdapter(Editor, (Service,), name="service.edit")
  >>> component.provideAdapter(FieldInstance)

  >>> srv = Service()
  >>> inst = component.getAdapter(srv, name='service.edit')
  >>> inst.template = serviceSchema
  >>> inst.applyTemplate(data=dict(title='Service', capacity='30'))
  <...FormState object ...>

  >>> srv.title, srv.description, srv.capacity
  (u'Service', u'', u'30')


Creating a schema from an interface
===================================

  >>> from zope.interface import Interface, implements
  >>> import zope.schema
  >>> from cybertools.composer.schema.factory import SchemaFactory
  >>> component.provideAdapter(SchemaFactory)

  >>> class IPerson(Interface):
  ...    firstName = zope.schema.TextLine(title=u'First name')
  ...    lastName = zope.schema.TextLine(title=u'Last name')
  ...    age = zope.schema.Int(title=u'Age')

  >>> class Person(object):
  ...     implements(IPerson)

  >>> from cybertools.composer.schema.interfaces import ISchemaFactory
  >>> factory = ISchemaFactory(Person())

  >>> schema = factory(IPerson)
  >>> for f in schema.fields:
  ...     print f.name, f.title, f.fieldType
  firstName First name textline
  lastName Last name textline
  age Age number

Using a more specialized schema factory
---------------------------------------

  >>> class PersonSchemaFactory(SchemaFactory):
  ...     def __call__(self, manager=None):
  ...         schema = super(PersonSchemaFactory, self).__call__(manager)
  ...         del schema.fields['firstName']  # don't show first name
  ...         return schema
  >>> component.provideAdapter(PersonSchemaFactory, (IPerson,))

  >>> factory = ISchemaFactory(Person())
  >>> schema = factory(IPerson)
  >>> for f in schema.fields:
  ...     print f.name, f.title, f.fieldType
  lastName Last name textline
  age Age number