Hi!

I want to define add- and edit-views for objects of objects with
zope.schema.Object and zope.app.form.browser.ObjectWidget.  Roger
Ineichen and Andreas Reuleaux explained (on this list) howto write
custom widgets for objects.  I was trying to define a widget for objects
of objects (objects which's attributes are objects again), but didn't
get it for days.  Tonight I got it!  Since there is not yet a posting on
this problem on the list I decided to post it.  Maybe this is a
zope3-newbie problem, but maybe this list is the right place for these
kinds of problems.

Instead of using the subwidget directive I defined a kind of 'medium'
class which inherits zope.app.form.browser.ObjectWidget and in
which--again--ObjectWidgets are defined. I don't know if this is good
zope3 style but it works fine.  See file widget.py below...

Christian


----------------------------------------------------
file: interfaces.py


from zope.interface import Interface
from zope.schema import TextLine, Int, Object


class IPerson(Interface):

    lastname = TextLine(
        title=u"Lastname",
        description=u"Please give the lastname.",
        required=False)

    firstname = TextLine(
        title=u"Firstname",
        description=u"Please give the firstname.",
        required=False)


class IPages(Interface):

    start = Int(
        title=u"Start page",
        description=u"Please give the start page.",
        required=False)

    end = Int(
        title=u"End page",
        description=u"Please give the end page.",
        required=False)


class IArticle(Interface):

    author = Object(
        schema=IPerson,
        title=u"Author",
        description=u"The author of the article.",
        required=False)

    title = TextLine(
        title=u"Article title",
        description=u"Please give the title of the article.",
        required=False)

    pages = Object(
        schema=IPages,
        title=u"Pages",
        description=u"Start and end page of the article.",
        required=False)

    
class INonsens(Interface):

    article = Object(
        schema=IArticle,
        title=u"Article",
        description=u"A (nonsens) article",
        required=False)
    
----------------------------------------------------
file: article.py


from zope.interface import implements
from objectsofobjects.interfaces import IPerson, IPages, IArticle, INonsens
from zope.schema.fieldproperty import FieldProperty


class Person:
    """The Person object."""

    implements(IPerson)


class Pages:
    """The Pages object."""

    implements(IPages)
    

class Article:
    """The article object."""

    implements(IArticle)


class Nonsens:
    """The nonsens object."""

    article = FieldProperty(INonsens['article'])

    implements(INonsens)

----------------------------------------------------
file: widget.py


from zope.app.form.browser.editview import EditView
from zope.app.form import CustomWidgetFactory
from zope.app.form.browser import ObjectWidget, TextWidget
from zope.schema import TextLine

from objectsofobjects.interfaces import INonsens
from objectsofobjects.article import Person, Pages, Article


class Article_w(ObjectWidget):
    """This is the 'medium' class which inherits ObjectWidget
    and in which ObjectWidgets are defined."""
    
    author_widget = CustomWidgetFactory(ObjectWidget, Person)
    # (Note that you don't have do define a custom widget for the simple
TextLine 'title'!)
    pages_widget = CustomWidgetFactory(ObjectWidget, Pages)


class NonsensEditView(EditView):
    """This is the customized EditView class."""

    __used_for__ = INonsens

    #Give the 'medium' class Article_w as an argument to the contructor
of CustomWidgetFactory.  
    article_widget = CustomWidgetFactory(Article_w, Article)


----------------------------------------------------
file: configure.zcml


<configure
    xmlns='http://namespaces.zope.org/zope'
    xmlns:browser='http://namespaces.zope.org/browser'>

  <content class=".article.Person">
    <require
        permission="zope.View"
    interface=".interfaces.IPerson"
    />
    <require
        permission="zope.ManageContent"
    set_schema=".interfaces.IPerson"
    />
  </content>

  <content class=".article.Pages">
    <require
        permission="zope.View"
    interface=".interfaces.IPages"
    />
    <require
        permission="zope.ManageContent"
    set_schema=".interfaces.IPages"
    />
  </content>

  <content class=".article.Article">
    <require
        permission="zope.View"
    interface=".interfaces.IArticle"
    />
    <require
        permission="zope.ManageContent"
    set_schema=".interfaces.IArticle"
    />
  </content>

  <content class=".article.Nonsens">
    <require
        permission="zope.View"
    interface=".interfaces.INonsens"
    />
    <require
        permission="zope.ManageContent"
    set_schema=".interfaces.INonsens"
    />
  </content>


  <browser:editform
      label="Change nonsens article data"
      name="edit.html"
      schema=".interfaces.INonsens"
      class=".widget.NonsensEditView"
      menu="zmi_views"
      title="Edit"
      permission="zope.ManageContent"
      />

  <browser:addform
      label="Add a nonsens article"
      name="AddNonsensArticle.html"
      schema=".interfaces.INonsens"
      class=".widget.NonsensEditView"
      content_factory=".article.Nonsens"
      permission="zope.ManageContent"
      />

  <browser:addMenuItem
      title="NonsensArticle"
      class=".article.Nonsens"
      permission="zope.ManageContent"
      view="AddNonsensArticle.html"
      />

</configure>

_______________________________________________
Zope3-users mailing list
Zope3-users@zope.org
http://mail.zope.org/mailman/listinfo/zope3-users

Reply via email to