[Grok-dev] Success: z3c.form and grok
Fernando Correa Neto
fcdoth at gmail.com
Fri Jul 25 11:30:17 EDT 2008
Hey there,
After tweaking my testing app to setup the layers properly (thanks
JW!), I got a basic add and edit form (z3c.form ones) working in grok.
Here is the app.py
import grok
from zope.interface import Interface, implements
from zope import schema
from z3c.form import button, field, form, widget
from z3c.formui import layout
from z3c.formui import interfaces
from z3c.form.interfaces import IFormLayer
from z3c.layer.pagelet import IPageletBrowserLayer
from persistent import Persistent
class Z3CFormLayer(IFormLayer, IPageletBrowserLayer):
pass
class AggregatedLayer(interfaces.IDivFormLayer, Z3CFormLayer):
pass
class Z3CFormSkin(grok.Skin):
grok.name('IDoubtIt')
grok.layer(AggregatedLayer)
class Z3cformtest(grok.Application, grok.Container):
grok.layer(AggregatedLayer)
pass
class Index(grok.View):
grok.layer(AggregatedLayer)
def items(self):
return self.context.items()
class ISomething(Interface):
something = schema.Text(
title=u'something',
description=u'This is something',
required=True,)
class Something(Persistent):
implements(ISomething)
something = schema.fieldproperty.FieldProperty(ISomething['something'])
class SomethingView(grok.View):
grok.layer(AggregatedLayer)
grok.context(Something)
grok.name('index.html')
def render(self):
return '<h1>I am lazy...create a better view for this</h1>'
class AddForm(form.AddForm, grok.View):
grok.layer(AggregatedLayer)
fields = field.Fields(ISomething)
label = u'This is a add form'
def create(self, data):
return Something(**data)
def add(self, object):
count = 0
while 'something-%i' %count in self.context:
count += 1;
self._name = 'something-%i' %count
self.context[self._name] = object
return object
def nextURL(self):
return self.context[self._name].redirect('index.html')
class EditForm(form.EditForm, grok.View):
grok.context(Something)
grok.layer(AggregatedLayer)
grok.name('edit.html')
form.extends(form.EditForm)
label = u'This is a edit form'
fields = field.Fields(ISomething)
@button.buttonAndHandler(u'Apply and View', name='applyView')
def handleApplyView(self, action):
self.handleApply(self, action)
if not self.widgets.errors:
self.redirect('index.html')
The extra packages that I needed to add to my setup.py as well:
'zope.viewlet',
'z3c.form',
'z3c.formui',
'z3c.macro',
'z3c.template',
'z3c.layer',
After running bin/buildout -N I was good to go.
Regards,
Fernando
More information about the Grok-dev
mailing list