Johan "EasyPublisher" Carlsson wrote:
Garito wrote:
Hi all
I have some code like these:
def __bobo_traverse__(self, REQUEST, name): obj = getattr(self, name, None) if obj is not None: return obj else: return 'cocohuaha'
But zope raise an The object at http://myserver:8080/TestingZope/Test is not publishable.
Why?
__bobo_traverse__ returning a string instead of a publishable object. Replace 'cocohuaha' with a publishable object for which its index_html returns 'cocohuaha'.
Johan
Hi Johan and Paul I would __bobo_traverse__ returns a string instead an object because this function generates the response dynamically (is for that reason that the function returns a string) How can I return a string? Do I need to create an object obligatorily? Thank you!!!
Garito wrote:
Hi Johan and Paul I would __bobo_traverse__ returns a string instead an object because this function generates the response dynamically (is for that reason that the function returns a string) How can I return a string? Do I need to create an object obligatorily?
Well, you could return the object declaring the __bobo_traverse__ and let that objects index_html/__call__ return the string, but first setting the string in a _v_attribute or in the REQUEST. Or you could create an object on-the-fly that: class TempPublishable(some base classes: Aquisition.Implicit maybe??): def __init__(self, the_string): self.the_string=the_string def index_html(self, REQUEST=None): """return the string""" return self.the_string I'm not sure exactly what base classes and security settings that needs to be use on the TempPublishable? Also you might want to peek att ZSQLMethods which uses traverse to publish databse rows as objects. Regards, Johan Carlsson
On Pungenday, Bureaucracy 24, 3170 YOLD, Johan Carlsson wrote:
Or you could create an object on-the-fly that:
class TempPublishable(some base classes: Aquisition.Implicit maybe??): def __init__(self, the_string): self.the_string=the_string
def index_html(self, REQUEST=None): """return the string""" return self.the_string
I'm not sure exactly what base classes and security settings that needs to be use on the TempPublishable?
Here is a real-life example of class created dynamically from SQL (cut just to relevant fragments). from Acquisition import Implicit from AccessControl import ClassSecurityInfo from Globals import InitializeClass class theBasket(Implicit): """The basket""" id=meta_type = "Basket" title = "The basket" security = ClassSecurityInfo() security.declareObjectPublic() security.setDefaultAccess("allow") def __init__(self, id): ... ... InitializeClass(theBasket) class BasketGenerator(Something): """Basket Generator product kept in ZODB""" def __getitem__(self, id): """Return theBasket instance""" rv = theBasket(self, id) return rv.__of__(self) Works just fine, including security declarations and acquisition. -- __ Maciek Pasternacki <maciekp@japhy.fnord.org> [ http://japhy.fnord.org/ ] `| _ |_\ / { 2.718281828459045235360287471352662497757247093699959574966967 ,|{-}|}| }\/ 62772407663035354759457138217852516642742746639193200305992181741 \/ |____/ 359662904357290033429526059563073813232862794349076... ( e ) -><-
Hi, Maciek Pasternacki wrote:
On Pungenday, Bureaucracy 24, 3170 YOLD, Johan Carlsson wrote:
Or you could create an object on-the-fly that: [snip]
Here is a real-life example of class created dynamically from SQL (cut just to relevant fragments).
from Acquisition import Implicit from AccessControl import ClassSecurityInfo from Globals import InitializeClass
class theBasket(Implicit): """The basket"""
id=meta_type = "Basket" title = "The basket"
security = ClassSecurityInfo() security.declareObjectPublic() security.setDefaultAccess("allow")
def __init__(self, id): ...
...
InitializeClass(theBasket)
class BasketGenerator(Something): """Basket Generator product kept in ZODB""" def __getitem__(self, id): """Return theBasket instance""" rv = theBasket(self, id) return rv.__of__(self)
I'm unable to understand what BasketGenerator is doing. Can you please explain what __getitem__ is upto? - Raja
On Pungenday, Bureaucracy 24, 3170 YOLD, Raja Subramanian wrote:
class BasketGenerator(Something): """Basket Generator product kept in ZODB""" def __getitem__(self, id): """Return theBasket instance""" rv = theBasket(self, id) return rv.__of__(self)
I'm unable to understand what BasketGenerator is doing. Can you please explain what __getitem__ is upto?
It generates theBasket instance from given id (real data are taken from SQL database) and returns it as its own subobject (it acts like a folder). So when I want to access theBasket with id 23, I go to http://server/BasketGenerator/23 (I use it instead of __bobo_traverse__, because I use generator also from Python and dictionary-like access is easier than calling __bobo_traverse__ by hand, and I don't need request passed as argument). __of__ method of theBasket is inherited from Acquisition.Implicit and sets returned instance's acquisition parent so that it can acquire methods and attributes as if it was part of ZODB tree. I omitted standard inheritance, __init__ etc. of normal Zope product -- it's almost copy-and-paste from DevGuide, nothing special. It's there of course but it's __getitem__ that does the magic. -- __ Maciek Pasternacki <maciekp@japhy.fnord.org> [ http://japhy.fnord.org/ ] `| _ |_\ / { I tell you this, no eternal reward will forgive us now ,|{-}|}| }\/ for wasting the dawn! } \/ |____/ ( Jim Morrison ) -><-
Maciek Pasternacki wrote:
On Pungenday, Bureaucracy 24, 3170 YOLD, Raja Subramanian wrote:
class BasketGenerator(Something): """Basket Generator product kept in ZODB""" def __getitem__(self, id): """Return theBasket instance""" rv = theBasket(self, id) return rv.__of__(self)
I'm unable to understand what BasketGenerator is doing. Can you please explain what __getitem__ is upto?
It generates theBasket instance from given id (real data are taken from SQL database) and returns it as its own subobject (it acts like a folder). So when I want to access theBasket with id 23, I go to http://server/BasketGenerator/23 (I use it instead of __bobo_traverse__, because I use generator also from Python and dictionary-like access is easier than calling __bobo_traverse__ by hand, and I don't need request passed as argument).
__of__ method of theBasket is inherited from Acquisition.Implicit and sets returned instance's acquisition parent so that it can acquire methods and attributes as if it was part of ZODB tree.
I omitted standard inheritance, __init__ etc. of normal Zope product -- it's almost copy-and-paste from DevGuide, nothing special. It's there of course but it's __getitem__ that does the magic.
Thanks all
participants (4)
-
Garito -
Johan Carlsson -
Maciek Pasternacki -
Raja Subramanian