From 4427a1fff4273d426574ee9c0b4815b449f49c5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9rome=20Perrin?= <jerome@nexedi.com> Date: Mon, 25 Sep 2017 03:26:14 +0000 Subject: [PATCH] ERP5Type: move history access from component to a dedicated mixin --- product/ERP5Type/Core/DocumentComponent.py | 3 +- product/ERP5Type/Core/ExtensionComponent.py | 3 +- product/ERP5Type/Core/TestComponent.py | 3 +- product/ERP5Type/mixin/component.py | 44 ----------- .../ERP5Type/mixin/text_content_history.py | 79 +++++++++++++++++++ 5 files changed, 85 insertions(+), 47 deletions(-) create mode 100644 product/ERP5Type/mixin/text_content_history.py diff --git a/product/ERP5Type/Core/DocumentComponent.py b/product/ERP5Type/Core/DocumentComponent.py index 6235ebb5c0..55e21ba189 100644 --- a/product/ERP5Type/Core/DocumentComponent.py +++ b/product/ERP5Type/Core/DocumentComponent.py @@ -28,13 +28,14 @@ ############################################################################## from Products.ERP5Type.mixin.component import ComponentMixin +from Products.ERP5Type.mixin.text_content_history import TextContentHistoryMixin from AccessControl import ClassSecurityInfo from Products.ERP5Type import Permissions import zope.interface from Products.ERP5Type.interfaces.component import IComponent -class DocumentComponent(ComponentMixin): +class DocumentComponent(ComponentMixin, TextContentHistoryMixin): """ ZODB Component for Documents in bt5 only for now (which used to be installed in INSTANCE_HOME/Document) but this will also be used later on for Documents diff --git a/product/ERP5Type/Core/ExtensionComponent.py b/product/ERP5Type/Core/ExtensionComponent.py index b2b2472219..a448499161 100644 --- a/product/ERP5Type/Core/ExtensionComponent.py +++ b/product/ERP5Type/Core/ExtensionComponent.py @@ -28,13 +28,14 @@ ############################################################################## from Products.ERP5Type.mixin.component import ComponentMixin +from Products.ERP5Type.mixin.text_content_history import TextContentHistoryMixin from AccessControl import ClassSecurityInfo from Products.ERP5Type import Permissions import zope.interface from Products.ERP5Type.interfaces.component import IComponent -class ExtensionComponent(ComponentMixin): +class ExtensionComponent(ComponentMixin, TextContentHistoryMixin): """ ZODB Component for Extensions previously defined in the bt5 and installed in INSTANCE_HOME/Extensions diff --git a/product/ERP5Type/Core/TestComponent.py b/product/ERP5Type/Core/TestComponent.py index bf02cc2968..733a1115e7 100644 --- a/product/ERP5Type/Core/TestComponent.py +++ b/product/ERP5Type/Core/TestComponent.py @@ -28,13 +28,14 @@ ############################################################################## from Products.ERP5Type.mixin.component import ComponentMixin +from Products.ERP5Type.mixin.text_content_history import TextContentHistoryMixin from AccessControl import ClassSecurityInfo from Products.ERP5Type import Permissions import zope.interface from Products.ERP5Type.interfaces.component import IComponent -class TestComponent(ComponentMixin): +class TestComponent(ComponentMixin, TextContentHistoryMixin): """ ZODB Component for Live Tests only (previously defined in the bt5 and installed in INSTANCE_HOME/tests) as other kind of Tests should be diff --git a/product/ERP5Type/mixin/component.py b/product/ERP5Type/mixin/component.py index 5cf6561897..5d47d365aa 100644 --- a/product/ERP5Type/mixin/component.py +++ b/product/ERP5Type/mixin/component.py @@ -385,48 +385,4 @@ class ComponentMixin(PropertyRecordableMixin, Base): return new_component - security.declareProtected(Permissions.ModifyPortalContent, - 'getTextContentHistoryRevisionDictList') - def getTextContentHistoryRevisionDictList(self, limit=100): - """ - TODO - """ - history_dict_list = self._p_jar.db().history(self._p_oid, size=limit) - if history_dict_list is None: - # Storage doesn't support history - return () - - from struct import unpack - from OFS.History import historicalRevision - - previous_text_content = None - result = [] - for history_dict in history_dict_list: - text_content = historicalRevision(self, history_dict['tid']).getTextContent() - if text_content and text_content != previous_text_content: - history_dict['time'] = history_dict['time'] - history_dict['user_name'] = history_dict['user_name'].strip() - history_dict['key'] = '.'.join(map(str, unpack(">HHHH", history_dict['tid']))) - del history_dict['tid'] - del history_dict['size'] - - result.append(history_dict) - previous_text_content = text_content - - return result - - security.declareProtected(Permissions.ModifyPortalContent, - 'getTextContentHistory') - def getTextContentHistory(self, key): - """ - TODO - """ - from struct import pack - from OFS.History import historicalRevision - - serial = apply(pack, ('>HHHH',) + tuple(map(int, key.split('.')))) - rev = historicalRevision(self, serial) - - return rev.getTextContent() - InitializeClass(ComponentMixin) diff --git a/product/ERP5Type/mixin/text_content_history.py b/product/ERP5Type/mixin/text_content_history.py new file mode 100644 index 0000000000..21212ef9dc --- /dev/null +++ b/product/ERP5Type/mixin/text_content_history.py @@ -0,0 +1,79 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# Copyright (c) 2017 Nexedi SA and Contributors. All Rights Reserved. +# +# WARNING: This program as such is intended to be used by professional +# programmers who take the whole responsibility of assessing all potential +# consequences resulting from its eventual inadequacies and bugs +# End users who are looking for a ready-to-use solution with commercial +# guarantees and support are strongly adviced to contract a Free Software +# Service Company +# +# 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., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. +# +############################################################################## + +from AccessControl import ClassSecurityInfo +from Products.ERP5Type.Globals import InitializeClass +from Products.ERP5Type import Permissions + + +class TextContentHistoryMixin: + security = ClassSecurityInfo() + + security.declareProtected(Permissions.ModifyPortalContent, + 'getTextContentHistoryRevisionDictList') + def getTextContentHistoryRevisionDictList(self, limit=100): + """TODO + """ + history_dict_list = self._p_jar.db().history(self._p_oid, size=limit) + if history_dict_list is None: + # Storage doesn't support history + return () + + from struct import unpack + from OFS.History import historicalRevision + + previous_text_content = None + result = [] + for history_dict in history_dict_list: + text_content = historicalRevision(self, history_dict['tid']).getTextContent() + if text_content and text_content != previous_text_content: + history_dict['time'] = history_dict['time'] + history_dict['user_name'] = history_dict['user_name'].strip() + history_dict['key'] = '.'.join(map(str, unpack(">HHHH", history_dict['tid']))) + del history_dict['tid'] + del history_dict['size'] + + result.append(history_dict) + previous_text_content = text_content + + return result + + security.declareProtected(Permissions.ModifyPortalContent, + 'getTextContentHistory') + def getTextContentHistory(self, key): + """TODO + """ + from struct import pack + from OFS.History import historicalRevision + + serial = apply(pack, ('>HHHH',) + tuple(map(int, key.split('.')))) + rev = historicalRevision(self, serial) + + return rev.getTextContent() + +InitializeClass(TextContentHistoryMixin) -- 2.30.9