Back to index

plone3  3.1.7
Functions
Archetypes.Schema.factory Namespace Reference

Functions

def instanceSchemaFactory

Function Documentation

Default schema adapter factory.

In BaseObject, the Schema() method will do 'schema = ISchema(self)'. This
adapter factory is the default, meaning that setting a 'schema' class
attribute pointing to a schema is the default way of supplying a schema.

You may override this behaviour by supplying a different adapter. Most
likely, this will adapt some marker interface you apply to your own
content type and provide ISchema, e.g.:

  @implementer(ISchema)
  @adapter(IMyObject)
  def mySchemaFactory(context):
      return ...
      
The challenge is that the accessors and mutators of the returned schema
must be available as methods on 'context', with appropriate security
declarations. When the schema is set in a 'schema' class-attribute, this
is taken care of by ClassGen. However, if you wish to provide the schema
with a different adapter, you have three choices:

 1. Provide the accessor and mutator methods explicitly in the class. This
 probably means you will be properly implementing a particular interface,
 which is never a bad thing.
 
 2. Run Products.Archetypes.VariableSchemaSupport.VarClassGen on the class.
 This will generate the missing methods. However, this may be slow, so
 you may need to implement a marker to ensure it only happens once. (The
 VariableSchemaSupport class attempts to do this, but does it badly and is
 probably not to be relied on). Also note that this effectively precludes
 any site-local or per-instance semantics, since it modifies the global
 class dict.
 
 3. Add the methods per-instance yourself. This is what the ContentFlavors
 product does, so you may be better off using that.

Definition at line 7 of file factory.py.

00007 
00008 def instanceSchemaFactory(context):
00009     """Default schema adapter factory.
00010     
00011     In BaseObject, the Schema() method will do 'schema = ISchema(self)'. This
00012     adapter factory is the default, meaning that setting a 'schema' class
00013     attribute pointing to a schema is the default way of supplying a schema.
00014     
00015     You may override this behaviour by supplying a different adapter. Most
00016     likely, this will adapt some marker interface you apply to your own
00017     content type and provide ISchema, e.g.:
00018     
00019       @implementer(ISchema)
00020       @adapter(IMyObject)
00021       def mySchemaFactory(context):
00022           return ...
00023           
00024     The challenge is that the accessors and mutators of the returned schema
00025     must be available as methods on 'context', with appropriate security
00026     declarations. When the schema is set in a 'schema' class-attribute, this
00027     is taken care of by ClassGen. However, if you wish to provide the schema
00028     with a different adapter, you have three choices:
00029     
00030      1. Provide the accessor and mutator methods explicitly in the class. This
00031      probably means you will be properly implementing a particular interface,
00032      which is never a bad thing.
00033      
00034      2. Run Products.Archetypes.VariableSchemaSupport.VarClassGen on the class.
00035      This will generate the missing methods. However, this may be slow, so
00036      you may need to implement a marker to ensure it only happens once. (The
00037      VariableSchemaSupport class attempts to do this, but does it badly and is
00038      probably not to be relied on). Also note that this effectively precludes
00039      any site-local or per-instance semantics, since it modifies the global
00040      class dict.
00041      
00042      3. Add the methods per-instance yourself. This is what the ContentFlavors
00043      product does, so you may be better off using that.
00044     """
00045     return context.schema
00046 
00047 
00048 
00049 
00050