From f08accf4aa287ca0c012c57442e49ba897b62ee0 Mon Sep 17 00:00:00 2001 From: zmsdev Date: Tue, 27 Jun 2023 20:43:21 +0200 Subject: [PATCH] ++rest_api PoC --- .vscode/settings.json | 4 +++ Products/zms/_globals.py | 31 +++++++++------------- Products/zms/_pathhandler.py | 41 ++++++++++++++++------------- Products/zms/rest_api.py | 51 ++++++++++++++++++++++++++++++++++++ Products/zms/standard.py | 7 +---- tests/test_standard.py | 3 --- 6 files changed, 92 insertions(+), 45 deletions(-) create mode 100644 Products/zms/rest_api.py diff --git a/.vscode/settings.json b/.vscode/settings.json index e53d604c1..447a35067 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -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 diff --git a/Products/zms/_globals.py b/Products/zms/_globals.py index 755f699fd..f8b0c827b 100644 --- a/Products/zms/_globals.py +++ b/Products/zms/_globals.py @@ -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 = { @@ -65,7 +63,6 @@ u'ч': 'ch', u'ш': 'sh', u'щ': 'sch', - u'ь': "'", u'ы': 'y', u'ь': "'", u'э': 'e', @@ -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. diff --git a/Products/zms/_pathhandler.py b/Products/zms/_pathhandler.py index c042a93d4..e13de63ef 100644 --- a/Products/zms/_pathhandler.py +++ b/Products/zms/_pathhandler.py @@ -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 @@ -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 # ------------------------------------------------------------------------------ @@ -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. @@ -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) diff --git a/Products/zms/rest_api.py b/Products/zms/rest_api.py new file mode 100644 index 000000000..d095bd2f6 --- /dev/null +++ b/Products/zms/rest_api.py @@ -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 \ No newline at end of file diff --git a/Products/zms/standard.py b/Products/zms/standard.py index f46885751..2746ba3a4 100644 --- a/Products/zms/standard.py +++ b/Products/zms/standard.py @@ -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) @@ -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() diff --git a/tests/test_standard.py b/tests/test_standard.py index e8bce87b6..16dfa7567 100644 --- a/tests/test_standard.py +++ b/tests/test_standard.py @@ -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'