[Zope3-Users] want to use ObjectWidget and default add form

john saponara john at saponara.net
Sat Dec 29 11:21:10 EST 2007


hi,

please point me to an example showing how to use ObjectWidget with a 
default add form.  in case there is no example, perhaps my failing 
attempt below could serve as one, once it's modified to work.

########################################
# interfaces.py

from zope.interface import Interface
from zope.schema import Field, Text, TextLine, Choice, Int, Bool, Date, 
Datetime, Object
from zope.app.container.constraints import ContainerTypesConstraint
from zope.app.container.constraints import ItemTypePrecondition
from zope.app.container.interfaces import IContained, IContainer

class ICar(Interface):
   model = TextLine(
     title = u'model',
     default = u'',
     required = False)
   nPassengers = Int(
     title = u'nPassengers',
     default = 0,
     required = False)

class IDriver(Interface):
   car = Object(
     title = u'car',
     default = None,
     schema=ICar,
     required = False)

class ILimoservice(IContainer):
   '''container for items of type ICar, IDriver'''
   name = TextLine(
     title=u'Limoservice',
     description=u'a Limoservice container',
     default=u'',
     required=True)
   def __setitem__(name, obj): pass
   __setitem__.precondition = ItemTypePrecondition(ICar, IDriver)

class ILimoserviceContained(IContained):
   '''for types that can only be contained in a Limoservice'''
   __parent__ = Field(constraint = ContainerTypesConstraint(ILimoservice))

########################################
# classes.py

from zope.interface import implements
from zope.app.container.btree import BTreeContainer
from zope.app.container.contained import Contained

from limoService.interfaces import ICar, IDriver, ILimoservice, 
ILimoserviceContained

class Car(Contained):
   implements(ICar,ILimoserviceContained)
   model = u''
   nPassengers = u'0'

class Driver(Contained):
   implements(IDriver,ILimoserviceContained)

class Limoservice(BTreeContainer):
   implements(ILimoservice)
   name = "u''"

and configure.zcml as listed below.  in the zmi, i add a Limoservice ok, 
then i enter that container and see car and driver in my add menu, and i 
add a car ok, but when i try to add a driver, i get this error:

2007-12-29T10:36:00 ERROR SiteError 
http://localhost:2020/mylimo/@@+/action.html
Traceback (most recent call last):
   File "C:\Python24\Lib\site-packages\zope\publisher\publish.py", line 
133, in publish
     result = publication.callObject(request, obj)
   File 
"C:\Python24\Lib\site-packages\zope\app\publication\zopepublication.py", 
line 161, in callObject
     return mapply(ob, request.getPositionalArguments(), request)
   File "C:\Python24\Lib\site-packages\zope\publisher\publish.py", line 
108, in mapply
     return debug_call(obj, args)
    - __traceback_info__: <bound method +.action of 
<zope.app.publisher.browser.viewmeta.+ object at 0x03584130>>
   File "C:\Python24\Lib\site-packages\zope\publisher\publish.py", line 
114, in debug_call
     return obj(*args)
   File 
"C:\Python24\Lib\site-packages\zope\app\container\browser\adding.py", 
line 124, in action
     name=view_name) is not None:
   File "C:\Python24\Lib\site-packages\zope\component\_api.py", line 
114, in queryMultiAdapter
     return sitemanager.queryMultiAdapter(objects, interface, name, default)
   File "C:\Python24\Lib\site-packages\zope\component\registry.py", line 
206, in queryMultiAdapter
     return self.adapters.queryMultiAdapter(
   File "C:\Python24\Lib\site-packages\zope\interface\adapter.py", line 
482, in queryMultiAdapter
     result = factory(*objects)
   File 
"C:\Python24\Lib\site-packages\zope\app\form\browser\editview.py", line 
62, in __init__
     self._setUpWidgets()
   File "C:\Python24\Lib\site-packages\zope\app\form\browser\add.py", 
line 48, in _setUpWidgets
     setUpWidgets(self, self.schema, IInputWidget, names=self.fieldNames)
   File "C:\Python24\Lib\site-packages\zope\app\form\utility.py", line 
153, in setUpWidgets
     context=context)
   File "C:\Python24\Lib\site-packages\zope\app\form\utility.py", line 
97, in setUpWidget
     widget = _createWidget(context, field, viewType, view.request)
   File "C:\Python24\Lib\site-packages\zope\app\form\utility.py", line 
65, in _createWidget
     return zapi.getMultiAdapter((field, request), viewType)
   File "C:\Python24\Lib\site-packages\zope\component\_api.py", line 
103, in getMultiAdapter
     raise ComponentLookupError(objects, interface, name)
ComponentLookupError: ((<zope.schema._field.Object object at 
0x03584090>, <zope.publisher.browser.BrowserRequest instance 
URL=http://localhost:2020/mylimo/@@+/action.html>), <InterfaceClass 
zope.app.form.interfaces.IInputWidget>, u'') 127.0.0.1 - - 
[29/Dec/2007:10:36:00 -0400] "GET 
/mylimo/@@+/action.html?type_name=addDriver.html HTTP/1.1" 500 84 
"http://localhost:2020/mylimo/@@contents.html" "Mozilla/5.0 (Windows; U; 
Windows NT 5.1; en-US; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11"

i'm running zope-3.3.1 under winxpprosp2.

is there an example of using ObjectWidget somewhere?

thanks!

########################################
# configure.zcml

<configure
     xmlns="http://namespaces.zope.org/zope"
     xmlns:browser="http://namespaces.zope.org/browser"
   i18n_domain="limoService"
   >

   <interface
       interface=".interfaces.ICar"
       type="zope.app.content.interfaces.IContentType"
       />

   <class class=".classes.Car">
     <implements
         interface="zope.annotation.interfaces.IAttributeAnnotatable"
         />
     <factory
         id="limoService.classes.Car"
         description="a car"
         />
     <require
         permission="zope.ManageContent"
         interface=".interfaces.ICar"
         />
     <require
         permission="zope.ManageContent"
         set_schema=".interfaces.ICar"
         />
   </class>

   <browser:addMenuItem
       class=".classes.Car"
       title="a car"
       permission="zope.ManageContent"
       description='add Car'
       view='addCar.html'
   />
   <browser:addform
       name="addCar.html"
       label="add Car"
       schema="limoService.interfaces.ICar"
       content_factory="limoService.classes.Car"
       fields='model nPassengers'
       permission="zope.ManageContent"
       />
   <browser:editform
       name="edit.html"
       label="edit Car"
       schema="limoService.interfaces.ICar"
       for="limoService.interfaces.ICar"
       fields='model nPassengers'
       permission="zope.ManageContent"
       menu="zmi_views" title="Edit"
       />

   <interface
       interface=".interfaces.IDriver"
       type="zope.app.content.interfaces.IContentType"
       />

   <class class=".classes.Driver">
     <implements
         interface="zope.annotation.interfaces.IAttributeAnnotatable"
         />
     <factory
         id="limoService.classes.Driver"
         description="a driver"
         />
     <require
         permission="zope.ManageContent"
         interface=".interfaces.IDriver"
         />
     <require
         permission="zope.ManageContent"
         set_schema=".interfaces.IDriver"
         />
   </class>

   <browser:addMenuItem
       class=".classes.Driver"
       title="a driver"
       permission="zope.ManageContent"
       description='add Driver'
       view='addDriver.html'
   />
   <browser:addform
       name="addDriver.html"
       label="add Driver"
       schema="limoService.interfaces.IDriver"
       content_factory="limoService.classes.Driver"
       fields='car'
       permission="zope.ManageContent"
       />
   <browser:editform
       name="edit.html"
       label="edit Driver"
       schema="limoService.interfaces.IDriver"
       for="limoService.interfaces.IDriver"
       fields='car'
       permission="zope.ManageContent"
       menu="zmi_views" title="Edit"
       />

   <interface
       interface=".interfaces.ILimoservice"
       type="zope.app.content.interfaces.IContentType"
       />

   <class class=".classes.Limoservice">
     <implements
         interface="zope.app.annotation.interfaces.IAttributeAnnotatable"
         />
     <implements
         interface="zope.app.container.interfaces.IContentContainer"
         />
     <factory
         id="limoService.classes.Limoservice"
         description="Limoservice"
         />
     <require
         permission="zope.ManageContent"
         interface=".interfaces.ILimoservice"
         />
     <require
         permission="zope.ManageContent"
         set_schema=".interfaces.ILimoservice"
         />
   </class>

   <browser:addMenuItem
       class=".classes.Limoservice"
       title="Limoservice"
       permission="zope.ManageContent"
   />

   <browser:containerViews
       for="limoService.interfaces.ILimoservice"
       index="zope.View"
       contents="zope.View"
       add="zope.ManageContent"
       />

   </configure>



More information about the Zope3-users mailing list