[Zope3-Users] Re: NameChooser functionality for IContainerNamesContainer

Philipp von Weitershausen philipp at weitershausen.de
Mon Jan 16 19:43:54 EST 2006


Joel Moxley wrote:
> I successfully added a name chooser for objects added through an add
> form to my container.  This involved implementing
> IContainerNamesContainer on my container, INameChooser on my
> NameChooser, registering each, and being sure to set my set_before_add
> fields in the addform registration.
> 
> What is The Right Way (tm) to use this NameChooser functionality
> outside of the addform?  In other words, I have a method that defines
> the container from a file as follows:
> 
> self[name] = obj
> 
> This obviously would overwrite duplicates, etc.  I would prefer to
> have something as follows which would use the NameChooser machinery
> automatically (ie, without having to get the NameChooser adapter in my
> method).  Is there a way I can do something along the lines of this?
> 
> self.add(obj)

You could implement this method on the container itself, but
traditionally such an 'add' method is on the IAdding view for
containers. See zope.app.container.interfaces.IAdding. addforms usually
are views on the adding view (+), not on the container:

  http://localhost:8080/some_folder/+/worldcookery.Recipe

The addform then says self.context.add(obj), whereas self.context refers
to the IAdding view. IAdding specifies the add() method you're looking for.

So, you could either register whatever you're doing for the IAdding
view, or you look up IAdding manually somewhere:

  adding_view = zapi.getAdapter((folder, request), name="+")
  adding_view.add(obj)

Philipp



More information about the Zope3-users mailing list