OK, I take my last post back. I figured out a nice way to do it without having to rewrite ANYTHING. I created a product whose sole purpose is to cause zope to load some modules when it starts up. Within the initialize method of that product, it is possible to call 'allow_module' and 'allow_class', which use some kind of introspection to make all members of modules and classes available through the web. You can't access modules by name from a dtml-method, but you can access class instances. Example: In a product called Everest, file called __init__.py: from AccessControl import ModuleSecurityInfo, allow_class, allow_type, allow_module import cv import cv.everest import cv.everest.everest_users def initialize(context): allow_module('cv') allow_module('cv.everest') allow_module('cv.everest.everest_users') allow_class(cv.everest.everest_users.ev_user) allow_class(cv.everest.everest_users.user_acct) allow_class(cv.everest.everest_users.user_campaign) Not that allow_module takes a string and allow_class takes an class as a parameter. Seems inconsistent to me. In a PythonScript called test: import cv.everest.everest_users return cv.everest.everest_users.list_users() which returns a list of ev_user objects defined in the everest_users module In a dtml method: <dtml-in test prefix="user"> <dtml-var expr="user_item.method1()"> <dtml-var expr="user_item.method2()"> </dtml-in> I assume that method1 and method2 of the ev_user class must return a python built-in like dict, list, string, or number, or else must be callable or at least be representable as a string. --sam On Apr 28, 2004, at 12:10 PM, Sam Gendler wrote:
I am answering my own question so the next person who bumps into it might have more luck tracking it down. The following examples are taken from the 'Security' chapter of the Zope Developers Guidde (ZDG). Note that I still wish there were a way to use external objects unchanged. Particularly because I have a class heirarchy, where customer objects create campaignh objects, but I want to to return campaign objects that have zope security info, so I have to create instances of my zopified objects from the non-zope objects...very wasteful and slow.
--sam
# an external method that returns Book instances
from AccessControl import ClassSecurityInfo from Acquistion import Implicit import Globals
class Book(Implicit):
def __init__(self, title): self._title=title
# Create a SecurityInfo for this class security = ClassSecurityInfo() security.declareObjectPublic()
security.declarePublic('getTitle') def getTitle(self): return self._title
Globals.InitializeClass(Book)
# The actual external method def GetBooks(self): books=[] books.append(Book('King Lear').__of__(self)) books.append(Book('Romeo and Juliet').__of__(self)) books.append(Book('The Tempest').__of__(self)) return books
Or, if you can't modify the original object, try this:
# an external method that returns Book instances
from AccessControl import ClassSecurityInfo from Acquisition import Implicit import bookstuff import Globals
class Book(Implicit, bookstuff.Book): security = ClassSecurityInfo() security.declareObjectPublic() security.declarePublic('getTitle')
Globals.InitializeClass(Book)
# The actual external method def GetBooks(self): books=[] books.append(Book('King Lear')) books.append(Book('Romeo and Juliet')) books.append(Book('The Tempest')) return books
On Apr 27, 2004, at 6:26 PM, Sam Gendler wrote:
I used to be a fairly knowledgable zope user many years ago, but things have moved on and I've forgotten most of what I know. I have a largish library of python modules which define a number of classes which have methods that perform work in a product. Currently, much of the system is managed vai commandline tools which utilize the library. We want to ad a web interface, and I figured zope would be a natural, since everything is already in python.
I created external methods which return instances of classes, but if I try to use those instances from within dtml (iterating over a list of them, for instance), I just get prompted for authentication credentials. I seem to recall that there was some way of applying zopishness to external object instances at runtime, but I can't remember what that mechanism is, and I couldn't find it in either the zope book or the zope bible I went out an bought.
For example, if I have a customer class which has name, id, and revenue members, and I create an external method to return a list of customer instances, I cannot do the following
<dtml-in extListCustomers prefix="cust"> <dtml-var expr="cust_item.id"> <dtml-var expr="cust_item.name"> <dtml var expr="cust_item.revenue"> </dtml-in>
Thanks
--sam
_______________________________________________ Zope maillist - Zope@zope.org http://mail.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce http://mail.zope.org/mailman/listinfo/zope-dev )