I added a demo package to illustrate the zope3 / xml schema integration.
You can grab de code over there :
http://svn.nuxeo.org/trac/pub/browser/z3lab/zope/xmlschema/trunk/demo/
The goal of the demo is to get a new content object registred within Zope3 with an "add "and "edit" form driven by an XML Schema definition.
This example is pretty simple but illustrate perfectly the goal of the XML integration and the power of Zope3 ;)
Let's take a look at the xsd we will use in this demo :
It defines a schema having 2 attributs : title and description.
title is required and is a normalizedString data type
description is required as well and is a string data type.
Let's define an interface IFoo and set the "foo" schema above. Note I'm using directly an id in here for the schema (i.e : foo). This is because at this stage the schema will be registred within Zope3 under this name. We will see how just after.
Then define the Foo content object implementing IFoo. It inherits from presistent so that the content object will be persistent but it's not compulsory.
Now the zcml configuration (the glue) (see demo.configure.zcml) to tell Zope what to do whith the definitions above.
Let's take a look at the configure.zcml file for the package as explained below :
Then the one below defines the new content object (notice the set_schema directive.) You can forget about the Zope permission stuffs if you're not familiar with Zope3
Then, we specify the add and edit form for the Foo content object.
Then we need to tell Zope to add an entry in ZMI (Zope Management Interface) menu for us to be able to create a Foo content object through the ZMI.
Now start Zope3 go in the ZMI and click the Add menu entry and ask for the creation of a Foo content object.
You will get the form below :
Then you can try to submit with empty values and since we specified required values on the xml schema the submission is not possible :
Here it is for today.
The 'magic' for the rendering against the schema is done by Zope3 itself. It's not the job of the zope.xmlschema component.
Again, the next step will be to have the rendering done with XForms :) Currently, looking at it. I played today with the mozilla xforms plugin. I'll dig into this whike coming back from holidays at the beginning of September.
Time to sleep...
You can grab de code over there :
http://svn.nuxeo.org/trac/pub/browser/z3lab/zope/xmlschema/trunk/demo/
The goal of the demo is to get a new content object registred within Zope3 with an "add "and "edit" form driven by an XML Schema definition.
This example is pretty simple but illustrate perfectly the goal of the XML integration and the power of Zope3 ;)
Let's take a look at the xsd we will use in this demo :
<?xml version="1.0" encoding="UTF-8"?>(demo.foo.xs)
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:attribute name="title" use="required" type="xs:normalizedString" />
<xs:attribute name="description" use="required" type="xs:string"/>
</xs:schema>
It defines a schema having 2 attributs : title and description.
title is required and is a normalizedString data type
description is required as well and is a string data type.
Let's define an interface IFoo and set the "foo" schema above. Note I'm using directly an id in here for the schema (i.e : foo). This is because at this stage the schema will be registred within Zope3 under this name. We will see how just after.
import zope.interface(demo.interfaces.py)
import zope.xmlschema
class IFoo(zope.interface.Interface):
zope.xmlschema.set('foo')
Then define the Foo content object implementing IFoo. It inherits from presistent so that the content object will be persistent but it's not compulsory.
import persistent(demo.foo.py)
import zope.interface
from zope.xmlschema.demo.interfaces import IFoo
class Foo(persistent.Persistent):
zope.interface.implements(IFoo)
def __init__(self, title='', description=''):
self.title = title
self.description = description
Now the zcml configuration (the glue) (see demo.configure.zcml) to tell Zope what to do whith the definitions above.
Let's take a look at the configure.zcml file for the package as explained below :
<xmlschemaThis directive registers the xsd foo.xs under the name of 'foo'
id = "foo"
document ="foo.xs"
>
</xmlschema>
Then the one below defines the new content object (notice the set_schema directive.) You can forget about the Zope permission stuffs if you're not familiar with Zope3
<content class=".foo.Foo">
<implements
interface="zope.app.annotation.IAttributeAnnotatable" />
<require permission="zope.View"
interface=".interfaces.IFoo" />
<require permission="zope.ManageContent"
set_schema=".interfaces.IFoo" />
</content>
Then, we specify the add and edit form for the Foo content object.
<browser:addform
schema=".interfaces.IFoo"
label="Add a Foo content object"
content_factory=".foo.Foo"
arguments="title description"
name="addFoo.html"
permission="zope.ManageContent"
/>
<browser:editform
schema=".interfaces.IFoo"
label="Edit Foo"
name="edit.html"
menu="zmi_views" title="Edit"
permission="zope.ManageContent"
/>
Then we need to tell Zope to add an entry in ZMI (Zope Management Interface) menu for us to be able to create a Foo content object through the ZMI.
<browser:addMenuItem
class=".foo.Foo"
title="Foo"
permission="zope.ManageContent"
view="addFoo.html"
/>
Now start Zope3 go in the ZMI and click the Add menu entry and ask for the creation of a Foo content object.
You will get the form below :
Then you can try to submit with empty values and since we specified required values on the xml schema the submission is not possible :
Here it is for today.
The 'magic' for the rendering against the schema is done by Zope3 itself. It's not the job of the zope.xmlschema component.
Again, the next step will be to have the rendering done with XForms :) Currently, looking at it. I played today with the mozilla xforms plugin. I'll dig into this whike coming back from holidays at the beginning of September.
Time to sleep...
(Post originally written by Julien Anguenot on the old Nuxeo blogs.)
Comments