from objectbroker import Persistence,NoPersistence,Aquisition,AquisitionProxy
import hserver
class WebCallable:
def __init__(self, method=None, instance=None):
self._method = method
self._instance = instance
def __call__(self,request, o=None):
if (hasattr(self,"_method")):
m = getattr( self, "_method")
i = getattr( self, "_instance", o)
if i is None:
i = o
m(i,request, o=o)
else:
request.getContentFile().write("
No ContentSorry, no content here!")
class WebObject(WebCallable,Aquisition):
WO_Accepts = None
def wo_accepts(self):
woa = getattr(self,"WO_Accepts",None)
if woa is None:
if (self.__class__ == WebObject) or (self._aq_parent is None):
return hserver.api.ListKnownWebObjects()
else:
return self._aq_parent.wo_accepts()
return woa
def __init__(self):
self.__default = None
def __getitem__(self,name):
try:
return getattr( self, name )
except:
raise IndexError("%s not found in %s" % (name,self))
def __setitem__(self,name,value):
setattr( self, name, value )
def __delitem__(self,name):
delattr( self, name )
def __contains__(self,name):
o = getattr(self,name,None)
return not o is None
def setDefault(self,default):
self.__default = default
def setChild(self,name,child):
setattr( self, name, child )
def children(self):
ls = []
for an in dir(self):
if isinstance( self[an], WebObject ):
ls.append( an )
return ls
def walk(self,request):
next, stack = request.pathwalker().walk()
no = getattr(self, next, None)
if no is None:
raise hserver.HttpException(404,"%s in /%s" % (next,request.pathwalker().walked(-1)))
if (len(stack)==0) or (not isinstance(no,WebObject)):
if (isinstance(no,WebCallable)):
no(request, o=self)
else:
raise hserver.HttpException(404,"%s in /%s" % (next,request.pathwalker().walked(-1)))
else:
no.walk(request)
def __call__(self,request,o = None):
if not self.__default is None:
request.redirect( request.pathwalker().walked() + "/" + self.__default )
elif "index_html" in self:
request.redirect( request.pathwalker().walked() + "/index_html" )
request.getContentFile().write("No ContentSorry, no content here!")
def attribute(self,name,default):
if not hasattr(self,name):
setattr( self, name, default )
return getattr( self, name )
def __public_types(self):
if hasattr(self,"_public_types"):
return self._public_types
return {}
def __public_names(self):
if hasattr(self,"_public_names"):
return self._public_names
return {}
def update(self,request):
if not request is None:
f = request.getForm()
else:
f = None
for n in self.__public_names():
if n in self.__public_types():
t = self.__public_types()[n]
else:
t = str
if (not f is None) and (n in f):
setattr(self, n, t(f[n].value()))
elif not hasattr(self, n):
setattr( self, n, t() )
class WebFolder(WebObject, Persistence):
pass
class NavWebObject(WebObject):
def __init__(self,title = None):
WebObject.__init__(self)
self.title = title
def title(self):
t = getattr(self,"title",None)
if t is None:
return self.__aq_name
return t
def navigation(self,levels = 1):
nav = {}
nav["title"] = self.title
nav["children"] = {}
if (levels > 0):
for child in self.children():
if isinstance(self[child],NavWebObject):
nav["children"][child] = self[child].navigation(levels-1)
return nav