[Zope3-Users] Views for adapted components

Lorenzo Gil Sanchez lgs at sicem.biz
Mon Mar 26 06:43:21 EDT 2007


Hi everybody,

I have several components and now I want to add ImportExport
funcionality to some of them.

So I created a simple interface called IImportExport:

class IImportExport(Interface):

  def importFromFile(filename):
    pass

  def exportToFile(filename):
    pass

Now, all I have to do is write an adapter for each component I want to
make importable/exportable. Piece of cake:

class ImportExportAdapter1(object):
  implements(IImportExport)
  __used_for__ = IMyComponent

 def importFromFile(self, filename):
   pass

 def exportToFile(self, filename):
   pass

and in the zcml:

<adapter
  for=".interfaces.IMyComponent"
  provides=".interfaces.IImportExport"
  factory=".adapters.ImportExportAdapter1"
  />

So far so good.

Now the problem: I have a view for IImportExport objects. It just lets
the user choose a name for the file and execute the import or export
method. I want this view to appears in the ZMI tabs for every object
that has an adapter to IImportExport.

If I use implements(IImportExport) in MyComponent class even when it
does not implements IImportExport directly but trough an adapter I can
see the view for objects of type IMyComponent *but* instead of doing

IImportExport(object).exportFromFile(filename)

I have to write

adapter = zope.component.getAdapter(object, IImportExport)
adapter.exportFromFile(filename)

This is not so bad. The real problem is that I have to modify the
original IMyCompoment component to tell zope it implements IImportExport
even when this is false. The adapter does it, not the compoment. But I
don't know other way to make my view work with my components.

I hope I made myself clear. Best regards.

Lorenzo



More information about the Zope3-users mailing list