Skip to content

Commit

Permalink
prepare decorators for openapi-endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
zmsdev committed Jul 25, 2023
1 parent 7c8e7cf commit b65fd74
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 52 deletions.
167 changes: 116 additions & 51 deletions Products/zms/rest_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,38 @@
from Products.zms import _blobfields
from Products.zms import standard

class api(object):

def __init__(self, **kwargs):
"""
If there are decorator arguments, the function
to be decorated is not passed to the constructor!
"""
#--print("Inside __init__()", kwargs)
self.kwargs = kwargs

def __call__(self, f):
"""
If there are decorator arguments, __call__() is only called
once, as part of the decoration process! You can only give
it a single argument, which is the function object.
"""
#--print("Inside __call__()")
def wrapped_f(*args):
#--print("Inside wrapped_f()")
#--print("Decorator arguments:", self.kwargs)
data = f(*args)
#--print("After f(*args)")
return self.kwargs['content_type'], data
return wrapped_f


def get_lang_context(self):
langs = self.context.getLangIds()
monolang = self.ids and self.ids[-1] in langs
langs = self.ids[-1:] if monolang else langs
return monolang, langs


def get_meta_data(node):
data = {}
Expand Down Expand Up @@ -56,7 +88,8 @@ def get_attr(node, id):
return value


def get_attrs(node, langs, monolang, REQUEST):
def get_attrs(self, node, REQUEST):
monolang, langs = get_lang_context(self)
data = get_meta_data(node)
for lang in langs:
REQUEST.set('lang',lang)
Expand Down Expand Up @@ -114,55 +147,87 @@ def __bobo_traverse__(self, TraversalRequest, name):
def __call__(self, REQUEST=None, **kw):
""""""
if self.method == 'GET':
data = {}
context = self.context
if context.meta_type == 'ZMSIndex':
data = []
catalog = context.get_catalog()
q = {k:v for k,v in REQUEST.form.items() if v != ''}
l = catalog(q)
data = [{item_name:r[item_name] for item_name in catalog.schema()} for r in l]
elif context.meta_type == 'ZMSMetamodelProvider':
data = {}
for id in context.getMetaobjIds():
d = {}
d['icon_clazz'] = context.aq_parent.zmi_icon(id)
data[id] = d
elif self.ids == ['get_body_content']:
REQUEST.RESPONSE.setHeader('Content-Type','text/html')
return context.getBodyContent(REQUEST,forced=False)
content_type, data = 'text/plain', {}
if self.context.meta_type == 'ZMSIndex':
content_type, data = self.zmsindex(self.context, REQUEST)
elif self.context.meta_type == 'ZMSMetamodelProvider':
content_type, data = self.metaobj_manager(self.context, REQUEST)
elif self.ids and self.ids[0] == 'get_body_content':
content_type, self.get_body_content(self.context, REQUEST)
elif self.ids and self.ids[0] == 'list_parent_nodes':
content_type, data = self.list_parent_nodes(self.context, REQUEST)
elif self.ids and self.ids[0] == 'list_child_nodes':
content_type, data = self.list_child_nodes(self.context, REQUEST)
elif self.ids and self.ids[0] == 'list_tree_nodes':
content_type, data = self.list_tree_nodes(self.context, REQUEST)
elif self.ids and self.ids[0] == 'get_parent_nodes':
content_type, data = self.get_parent_nodes(self.context, REQUEST)
elif self.ids and self.ids[0] == 'get_child_nodes':
content_type, data = self.get_child_nodes(self.context, REQUEST)
elif self.ids and self.ids[0] == 'get_tree_nodes':
content_type, data = self.get_tree_nodes(self.context, REQUEST)
else:
langs = context.getLangIds()
monolang = self.ids and self.ids[-1] in langs
langs = self.ids if monolang else langs
if self.ids == ['list_parent_nodes']:
id_prefix = REQUEST.get('id_prefix','e')
nodes = context.breadcrumbs_obj_path()
data = [get_meta_data(x) for x in nodes]
elif self.ids == ['list_child_nodes']:
id_prefix = REQUEST.get('id_prefix','e')
nodes = context.getObjChildren(id_prefix,REQUEST)
data = [get_meta_data(x) for x in nodes]
if context.meta_type == 'ZMS':
nodes.extend(context.getPortalClients())
elif self.ids == ['list_tree_nodes']:
nodes = context.getTreeNodes(REQUEST)
data = [get_meta_data(x) for x in nodes]
elif self.ids and self.ids[0] == 'get_parent_nodes':
id_prefix = REQUEST.get('id_prefix','e')
nodes = context.breadcrumbs_obj_path()
data = [get_attrs(x, langs, monolang, REQUEST) for x in nodes]
elif self.ids and self.ids[0] == 'get_child_nodes':
id_prefix = REQUEST.get('id_prefix','e')
nodes = context.getObjChildren(id_prefix,REQUEST)
if context.meta_type == 'ZMS':
nodes.extend(context.getPortalClients())
data = [get_attrs(x, langs, monolang, REQUEST) for x in nodes]
elif self.ids and self.ids[0] == 'get_tree_nodes':
nodes = context.getTreeNodes(REQUEST)
data = [get_attrs(x, langs, monolang, REQUEST) for x in nodes]
else:
data = get_attrs(context, langs, monolang, REQUEST)
REQUEST.RESPONSE.setHeader('Content-Type','application/json')
content_type, data = self.get(self.context, REQUEST)
REQUEST.RESPONSE.setHeader('Content-Type',content_type)
return json.dumps(data)
return None
return None

@api(tag="zmsindex", pattern="/zmsindex", content_type="application/json")
def zmsindex(self, context, REQUEST):
catalog = context.get_catalog()
q = {k:v for k,v in REQUEST.form.items() if v != ''}
l = catalog(q)
return [{item_name:r[item_name] for item_name in catalog.schema()} for r in l]

@api(tag="metamodel", pattern="/metaobj_manager", content_type="application/json")
def metaobj_manager(self, context, REQUEST):
data = {}
for id in context.getMetaobjIds():
d = {}
d['icon_clazz'] = context.aq_parent.zmi_icon(id)
data[id] = d
return data

@api(tag="content", pattern="/{path}", method="GET", content_type="application/json")
def get(self, context, REQUEST):
return get_attrs(self, context, REQUEST)

@api(tag="content", pattern="/{path}/get_body_content", method="GET", content_type="text/html")
def get_body_content(self, context, REQUEST):
return context.getBodyContent(REQUEST,forced=False)

@api(tag="navigation", pattern="/{path}/list_parent_nodes", method="GET", content_type="application/json")
def list_parent_nodes(self, context, REQUEST):
nodes = context.breadcrumbs_obj_path()
return [get_meta_data(x) for x in nodes]

@api(tag="navigation", pattern="/{path}/list_child_nodes", method="GET", content_type="application/json")
def list_child_nodes(self, context, REQUEST):
id_prefix = REQUEST.get('id_prefix','e')
nodes = context.getObjChildren(id_prefix,REQUEST)
if context.meta_type == 'ZMS':
nodes.extend(context.getPortalClients())
return [get_meta_data(x) for x in nodes]

@api(tag="navigation", pattern="/{path}/list_tree_nodes", method="GET", content_type="application/json")
def list_tree_nodes(self, context, REQUEST):
nodes = context.getTreeNodes(REQUEST)
return [get_meta_data(x) for x in nodes]

@api(tag="navigation", pattern="/{path}/get_parent_nodes", method="GET", content_type="application/json")
def get_parent_nodes(self, context, REQUEST):
nodes = context.breadcrumbs_obj_path()
return [get_attrs(self, x, REQUEST) for x in nodes]

@api(tag="navigation", pattern="/{path}/get_child_nodes", method="GET", content_type="application/json")
def get_child_nodes(self, context, REQUEST):
id_prefix = REQUEST.get('id_prefix','e')
nodes = context.getObjChildren(id_prefix,REQUEST)
if context.meta_type == 'ZMS':
nodes.extend(context.getPortalClients())
return [get_attrs(self, x, REQUEST) for x in nodes]

@api(tag="navigation", pattern="/{path}/get_tree_nodes", method="GET", content_type="application/json")
def get_tree_nodes(self, context, REQUEST):
nodes = context.getTreeNodes(REQUEST)
return [get_attrs(self, x, REQUEST) for x in nodes]
2 changes: 1 addition & 1 deletion tests/test_rest_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def test_get(self):
self.assertTrue( 'title' in actual)
self.assertFalse( 'title_%s'%self.lang in actual)
# list_parent_nodes
request = MockHTTPRequest({'REQUEST_METHOD':'GET','TraversalRequestNameStack':path_to_handle+['list_child_nodes'],'path_to_handle':path_to_handle+['list_parent_nodes']})
request = MockHTTPRequest({'REQUEST_METHOD':'GET','TraversalRequestNameStack':path_to_handle+['list_parent_nodes'],'path_to_handle':path_to_handle+['list_parent_nodes']})
print("path_to_handle", request.get('path_to_handle'))
actual = json.loads( self.context.__bobo_traverse__(request, name)(request))
print(json.dumps(actual))
Expand Down

0 comments on commit b65fd74

Please sign in to comment.