[Zope3-dev] Proposal for more widgets cleanup

Garrett Smith garrett at mojave-corp.com
Thu Oct 14 10:26:00 EDT 2004


Janko Hauser wrote:
> Garrett Smith wrote:
> Yeah, this second approach was mentioned at other places and I think I
> have it understood now. But I haven't seen an example yet. What's
> missing in my mental view, is how to keep the number of schemas low. I
> think of an initial schema with ten fields, and I only want to
> overwrite one in a special context. I do not want to replicate the
> complete schema, perhaps this could be reduced by subclassing the
> original one. 

There's been some discussion about 'schema arithmetic' that would let
you transform a schema to another with minimal semantics -- e.g. to
change a field to read only, etc. I don't think we have this fleshed out
in any detail, but there's been talk.

If you're ambitious, you could write a utility for your own purposes and
submit it as part of a small proposal to get schema arithmetic started.
I can help you with the proposal.

Otherwise, subclassing will work well.

> But how do I connect the new schema with the existing content object?
> Especially in ZCML? And do I need to define Attribute-fields for the
> fields the adapted schema provides. My goal is, to have the
> contentobjekt fully described by schema, so that I can export and
> import it into the system.

If you're using the 'editform' directive, it takes a 'schema' attribute,
which will cause the form machinery to adapt the context to provide that
schema. E.g.

  <editform
    for="IYourContent"
    schema="ISomeOtherSchema"
    ... />

But because you're dealing with policies that may change the schema
based, you might want to use views that subclass EditView (in
zope.app.form.browser) and setup your 'schema' attribute in __init__:

  class YourView(EditForm):
    def __init__(self, context, request):
      if some_condition:
        self.schema = ISomeOtherSchema1
      else:
        self.schema = ISomeOtherSchema2
      super(YourView, self).__init__(context, request)

Note that the call to 'super' is last -- it's going to setup the form
widgets using schema, so you need to setup schema first.

> I hope my questions are not convoluted. Is there somewhere a code
> example for the second approach, so I can try to wrap my ahead around
> this?

I don't know of any offhand know of any examples in the core. But the
above snippet should give you a good starting point.

 -- Garrett


More information about the Zope3-dev mailing list