[Grok-dev] Problem with grok.AddForm, grok.Adapter and widgets
Hannes
hannes.flocke at gmx.li
Fri Jan 29 14:36:18 EST 2010
Hi Grokkers,
I've some problem with a schema.Object field when I wanna use grok.AddForm.
Seems like I've to write an Adapter class...?, but unfortunately I don't
know how.... :-(
thanks.
--------------------------- here's my interfaces.py ------------------------
class IVersionPart1(Interface):
major = Int(
title = u'Major Version Number'
)
minor = Int(
title = u'Minor Version Number'
)
class IVersionPart2(Interface):
v3 = Int(
title = u'V3'
)
v4 = Int(
title = u'V4'
)
v5 = Int(
title = u'V5'
)
class IVersion(IVersionPart1, IVersionPart2):
""" Combines both Version Parts """
class IPlatform(Interface):
name = TextLine(
title = u'The Platform Name'
)
bits = Int(
title = u'32 or 64bit System'
)
class IProduct(Interface):
code = TextLine(
title = u'Product Code'
)
name = TextLine(
title = u'Full Product Name'
)
platform = Object(
title = u'Platform',
schema = IPlatform
)
version = Object(
title = u'Version',
schema = IVersion
)
---------------------------- here's my product.py
-------------------------------------
import grok
from interfaces import IProduct
from zope.app.form import CustomWidgetFactory
from zope.app.form.browser import ObjectWidget
from product_controller import ProductController
from version import Version
from platform import Platform
myVersionWidget = CustomWidgetFactory(ObjectWidget, Version)
myPlatformWidget = CustomWidgetFactory(ObjectWidget, Platform)
class Product(grok.Model):
""" Persistent Product """
grok.implements(IProduct)
def __init__(self, code, name, platformObj, versionObj):
super(Product, self).__init__()
self.code = code
self.name = name
self.platform = platformObj
self.version = versionObj
class Add(grok.AddForm):
""" Adds a product to the ProductController """
grok.context(ProductController)
form_fields = grok.AutoFields(Product)
form_fields['version'].custom_widget = myVersionWidget
form_fields['platform'].custom_widget = myPlatformWidget
@grok.action('Add Product')
def add(self, **data):
platformObj = Platform(data['platform']['name'],
data['platform']['bits'])
versionObj = Version(data['version']['major'],
data['version']['minor'],
data['version']['sp'],
data['version']['fix'],
data['version']['build'])
product = Product(data['code'],
data['name'],
platformObj,
versionObj)
self.context[product.code] = product
self.redirect(url(p))
class Edit(grok.EditForm):
""" Knows how to edit a product """
form_fields = grok.AutoFields(Product)
form_fields['version'].custom_widget = myVersionWidget
form_fields['platform'].custom_widget = myPlatformWidget
class Index(grok.DisplayForm):
""" Knows how to display a product """
#from zope.component.interfaces.IView import IInputWidget
#class VersionWidgetAdapter(grok.Adapter):
#grok.context(Version)
#grok.provides(IInputWidget)
----------------------------------- here's the output from zopectl -------
------
2010-01-26T03:06:23 INFO root -- HTTP:localhost:8080 Server started.
Hostname: localhost
Port: 8080
------
2010-01-26T03:06:23 INFO root Startup time: 2.234 sec real, 2.210 sec CPU
------
2010-01-26T03:06:30 ERROR SiteError http://192.168.1.3:8080/app/products/add
Traceback (most recent call last):
File
"/home/wacky/.buildout/eggs/zope.publisher-3.4.9-py2.5.egg/zope/publisher/publish.py",
line 133, in publish
result = publication.callObject(request, obj)
File
"/home/wacky/.buildout/eggs/grok-1.0-py2.5.egg/grok/publication.py",
line 89, in callObject
return super(ZopePublicationSansProxy, self).callObject(request, ob)
File
"/home/wacky/.buildout/eggs/zope.app.publication-3.4.3-py2.5.egg/zope/app/publication/zopepublication.py",
line 167, in callObject
return mapply(ob, request.getPositionalArguments(), request)
File
"/home/wacky/.buildout/eggs/zope.publisher-3.4.9-py2.5.egg/zope/publisher/publish.py",
line 108, in mapply
return debug_call(obj, args)
- __traceback_info__: <VersionTest.product.Add object at 0x26d27d0>
File
"/home/wacky/.buildout/eggs/zope.publisher-3.4.9-py2.5.egg/zope/publisher/publish.py",
line 114, in debug_call
return obj(*args)
File
"/home/wacky/.buildout/eggs/grokcore.formlib-1.4-py2.5.egg/grokcore/formlib/components.py",
line 90, in __call__
self.update_form()
File
"/home/wacky/.buildout/eggs/grokcore.formlib-1.4-py2.5.egg/grokcore/formlib/components.py",
line 62, in update_form
super(GrokForm, self).update()
File
"/home/wacky/.buildout/eggs/zope.formlib-3.4.0-py2.5.egg/zope/formlib/form.py",
line 740, in update
errors, action = handleSubmit(self.actions, data, self.validate)
File
"/home/wacky/.buildout/eggs/zope.formlib-3.4.0-py2.5.egg/zope/formlib/form.py",
line 683, in handleSubmit
errors = default_validate(action, data)
File
"/home/wacky/.buildout/eggs/zope.formlib-3.4.0-py2.5.egg/zope/formlib/form.py",
line 720, in validate
return (getWidgetsData(self.widgets, self.prefix, data)
File
"/home/wacky/.buildout/eggs/zope.formlib-3.4.0-py2.5.egg/zope/formlib/form.py",
line 344, in getWidgetsData
data[name] = widget.getInputValue()
File
"/home/wacky/.buildout/eggs/zope.app.form-3.4.1-py2.5.egg/zope/app/form/browser/objectwidget.py",
line 134, in getInputValue
content = self.factory()
TypeError: __init__() takes exactly 3 arguments (1 given)
More information about the Grok-dev
mailing list