2018-03-12 20:38:33 +01:00
|
|
|
from objectbroker import NoPersistence,Persistence
|
2018-03-13 08:06:50 +01:00
|
|
|
|
|
|
|
from hserver.api.WebCallable import WebCallable
|
|
|
|
from hserver.api.WebObject import WebObject
|
|
|
|
|
|
|
|
from hserver.templates.Template import Template
|
|
|
|
|
|
|
|
import hserver
|
2018-03-12 20:38:33 +01:00
|
|
|
|
|
|
|
from simplelog import log
|
|
|
|
|
|
|
|
import os
|
|
|
|
import os.path
|
|
|
|
import json
|
|
|
|
|
|
|
|
|
|
|
|
class RootFolder(WebObject,Persistence):
|
|
|
|
|
|
|
|
_hm_frame = Template( base = os.path.dirname( __file__ ), template = "frame.html" )
|
|
|
|
_hm_style = Template( base = os.path.dirname( __file__ ), template = "style.css" )
|
|
|
|
_hm_api = Template( base = os.path.dirname( __file__ ), template = "api.js" )
|
|
|
|
_hm_edit = Template( base = os.path.dirname( __file__ ), template = "edit.html" )
|
|
|
|
_hm_tree = Template( base = os.path.dirname( __file__ ), template = "tree.html" )
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
WebObject.__init__(self)
|
|
|
|
|
|
|
|
|
|
|
|
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.redirect( request.pathwalker().walked() + "/_hm_frame" )
|
|
|
|
|
|
|
|
|
|
|
|
def hm_json(self, request, o = None):
|
|
|
|
reply = { "result": None, "exception": None }
|
|
|
|
|
|
|
|
try:
|
|
|
|
j = json.loads( request.mime().body() )
|
|
|
|
log("Received JSON Request: {0}".format( j ))
|
|
|
|
|
|
|
|
method = j["method"]
|
|
|
|
parameters = j["parameters"]
|
|
|
|
|
|
|
|
m = getattr(self, method)
|
|
|
|
if not m is None:
|
|
|
|
reply["result"] = m(request,**parameters)
|
|
|
|
|
|
|
|
|
|
|
|
except Exception as ex:
|
|
|
|
reply["result"] = None
|
|
|
|
reply["exception"] = repr(ex)
|
|
|
|
|
|
|
|
request.setResponseHeader("Content-Type","application/json")
|
|
|
|
jreply = json.dumps( reply )
|
|
|
|
log("JReply: {0}".format(jreply))
|
|
|
|
request.getContentFile().write( jreply )
|
|
|
|
|
|
|
|
def objCreate(self,request,objTypeName,objName,objPath):
|
|
|
|
if (objTypeName == ""):
|
|
|
|
raise Exception("objTypeName must contain a valid type")
|
|
|
|
|
|
|
|
pObject = self.objPathWalk(request, objPath)
|
|
|
|
if (objName == "") or (objName in pObject):
|
|
|
|
raise Exception("objName must contain a valid and unused name of the new object")
|
|
|
|
|
|
|
|
objClass = hserver.api.findClass( objTypeName.split(".") )
|
|
|
|
o = objClass()
|
|
|
|
|
|
|
|
pObject[ objName ] = o
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
def objRemove(self,request,objPath):
|
|
|
|
if (len(objPath) < 1):
|
|
|
|
raise Exception("Can't delete root object")
|
|
|
|
|
|
|
|
pObject = self.objPathWalk(request, objPath)
|
|
|
|
del pObject._aq_parent[pObject._aq_name]
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
_hm_json = WebCallable( method = hm_json )
|
|
|
|
|
|
|
|
def objPathWalk(self, request, path):
|
|
|
|
o = self
|
|
|
|
for n in path:
|
|
|
|
o = getattr( o, n )
|
|
|
|
return o
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|