Skip to content

Commit

Permalink
++rest_api PoC
Browse files Browse the repository at this point in the history
  • Loading branch information
zmsdev committed Jun 27, 2023
1 parent 3144409 commit f08accf
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 45 deletions.
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
{
"python.defaultInterpreterPath": "~/vpy38/bin/python3",
"python.linting.enabled": false,
"[python]": {
"editor.defaultFormatter": "ms-python.autopep8"
},
"python.formatting.provider": "none",

// "python.testing.pytestEnabled": false,
// "python.testing.unittestEnabled": true
Expand Down
31 changes: 13 additions & 18 deletions Products/zms/_globals.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@
################################################################################

# Imports.
from Products.PageTemplates.Expressions import SecureModuleImporter
from Products.PageTemplates.PageTemplateFile import PageTemplateFile
import sys
import time
# Product Imports.
from Products.zms import standard
from Products.PageTemplates.Expressions import SecureModuleImporter
from Products.PageTemplates.PageTemplateFile import PageTemplateFile

# Umlauts
umlaut_map = {
Expand Down Expand Up @@ -65,7 +63,6 @@
u'ч': 'ch',
u'ш': 'sh',
u'щ': 'sch',
u'ь': "'",
u'ы': 'y',
u'ь': "'",
u'э': 'e',
Expand Down Expand Up @@ -106,19 +103,17 @@
u'Я': 'JA',}

def sort_item( i):
if isinstance(i, time.struct_time):
i = time.strftime('%Y%m%d%H%M%S',i)
elif isinstance(i, float):
pass
elif i is None or i == '':
i = 0
elif not isinstance(i, int):
i = standard.pystr(i)
mapping = umlaut_map
for key in mapping:
try: i = i.replace(key, mapping[key])
except: pass
return i
if isinstance(i, time.struct_time):
i = time.strftime('%Y%m%d%H%M%S',i)
elif isinstance(i, float):
pass
elif i is None or i == '':
i = 0
elif not isinstance(i, str):
mapping = umlaut_map
for key, value in mapping.items():
i = i.replace(key, value)
return i


# Datatypes.
Expand Down
41 changes: 23 additions & 18 deletions Products/zms/_pathhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import copy
import re
# Product Imports.
from Products.zms import rest_api
from Products.zms import standard
from Products.zms import _blobfields
from Products.zms import _fileutil
Expand All @@ -32,23 +33,23 @@
# Validates id against list of possible declarative id.
# ------------------------------------------------------------------------------
def validateId(self, id, REQUEST):
langs = []
lang = REQUEST.get( 'lang')
if lang is None:
for lang in self.getLanguages():
request = { 'lang': lang, 'preview': REQUEST.get('preview', '')}
decl_id = self.getDeclId(request)
if id == decl_id:
langs.append( lang)
if len( langs) == 1:
self.REQUEST.set( 'lang', langs[0])
else:
decl_id = self.getDeclId( REQUEST)
if id == decl_id:
langs.append( lang)
if len( langs) > 0:
return True
return False
langs = []
lang = REQUEST.get( 'lang')
if lang is None:
for lang in self.getLanguages():
request = { 'lang': lang, 'preview': REQUEST.get('preview', '')}
decl_id = self.getDeclId(request)
if id == decl_id:
langs.append( lang)
if len( langs) == 1:
self.REQUEST.set( 'lang', langs[0])
else:
decl_id = self.getDeclId( REQUEST)
if id == decl_id:
langs.append( lang)
if len( langs) > 0:
return True
return False


# ------------------------------------------------------------------------------
Expand Down Expand Up @@ -125,7 +126,7 @@ def __bobo_traverse__(self, TraversalRequest, name):
if 'path_to_handle' not in TraversalRequest:

# Make a reversed copy of the TraversalRequestNameStack
TraversalRequestNameStackReversed=copy.copy(TraversalRequest['TraversalRequestNameStack'])
TraversalRequestNameStackReversed = copy.copy(TraversalRequest['TraversalRequestNameStack'])
TraversalRequestNameStackReversed.reverse()

# Set path_to_handle in the TraversalRequest.
Expand Down Expand Up @@ -171,6 +172,10 @@ def __bobo_traverse__(self, TraversalRequest, name):
lang = self.getPrimaryLanguage()
request.set('lang', lang)

# Package-Home.
if name == '++rest_api':
return rest_api.RestApiController(self)

# Package-Home.
if name == '$ZMS_HOME':
i = TraversalRequest['path_to_handle'].index(name)
Expand Down
51 changes: 51 additions & 0 deletions Products/zms/rest_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
################################################################################
# rest_api.py
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
################################################################################

# Imports.
import json

class RestApiController(object):
"""
RestApiController
"""

def __init__(self, context):
self.context = context

def __bobo_traverse__(self, TraversalRequest, name):
self.method = TraversalRequest['REQUEST_METHOD']
for id in TraversalRequest['path_to_handle'][1:]:
self.context = getattr(self.context, id)
return self

__call____roles__ = None
def __call__(self, REQUEST=None, **kw):
""""""
if self.method == 'GET':
data = {}
data['id'] = self.context.id
data['meta_id'] = self.context.meta_id
data['uid'] = self.context.get_uid()
obj_attrs = self.context.getObjAttrs()
metaobj_attrs = self.context.getMetaobjManager().getMetaobjAttrs(self.context.meta_id)
for metaobj_attr in metaobj_attrs:
id = metaobj_attr['id']
if id in obj_attrs:
data[id] = self.context.attr(id)
return json.dumps(data)
return None
7 changes: 1 addition & 6 deletions Products/zms/standard.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
security = ModuleSecurityInfo('Products.zms.standard')

security.declarePublic('pystr')
pystr_ = str
def pystr(v, encoding='utf-8', errors='strict'):
if isinstance(v, bytes):
v = v.decode(encoding, errors)
Expand Down Expand Up @@ -1802,11 +1801,7 @@ def sort_list(l, qorder=None, qorderdir='asc', ignorecase=1):
tl = [(_globals.sort_item(x[qorder]), x) for x in l]
if ignorecase and len(tl) > 0 and isinstance(tl[0][0], str):
tl = [(str(x[0]).upper(), x[1]) for x in tl]
try:
tl = sorted(tl,key=lambda x:x[0])
except:
writeError(context, '[sort_list]: mixed datatypes normalized to strings')
tl = sorted(tl,key=lambda x:str(x[0]))
tl = sorted(tl,key=lambda x:x[0])
tl = [x[1] for x in tl]
if qorderdir == 'desc':
tl.reverse()
Expand Down
3 changes: 0 additions & 3 deletions tests/test_standard.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@ class StandardTests(unittest.TestCase):

def test_pystr(self):
self.assertEqual(standard.pystr('ABC'),'ABC')
self.assertEqual(standard.pystr_('ABC'),'ABC')
self.assertEqual(standard.pystr(b'ABC'),'ABC')
self.assertEqual(standard.pystr_(b'ABC'),'b\'ABC\'')
self.assertEqual(standard.pystr(123),'123')
self.assertEqual(standard.pystr_(123),'123')

def test_url_append_params(self):
expected = 'index.html?a=b&c=d&e=1&f:list=1&f:list=2&f:list=3'
Expand Down

0 comments on commit f08accf

Please sign in to comment.