From 36a80afa6912b6bcd3d72ef3c44fc18bb67df39b Mon Sep 17 00:00:00 2001
From: Nicolas Delaby <nicolas@nexedi.com>
Date: Fri, 28 May 2010 16:00:33 +0000
Subject: [PATCH] Merge base_convertable_and_file and base_convertable into
 BaseConvertableFileMixin Update path to new Mixin BaseConvertableFileMixin

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@35747 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 product/ERP5/Document/TextDocument.py         |  8 +--
 product/ERP5/mixin/base_convertable.py        | 56 ++++++++++++++----
 .../ERP5/mixin/base_convertable_and_file.py   | 59 -------------------
 product/ERP5OOo/Document/OOoDocument.py       |  8 +--
 4 files changed, 52 insertions(+), 79 deletions(-)
 delete mode 100644 product/ERP5/mixin/base_convertable_and_file.py

diff --git a/product/ERP5/Document/TextDocument.py b/product/ERP5/Document/TextDocument.py
index f8125b715b..1fbe74ba4e 100644
--- a/product/ERP5/Document/TextDocument.py
+++ b/product/ERP5/Document/TextDocument.py
@@ -45,8 +45,7 @@ import cStringIO
 
 # Mixin Import
 from Products.ERP5.mixin.cached_convertable import CachedConvertableMixin
-from Products.ERP5.mixin.base_convertable import BaseConvertableMixin
-from Products.ERP5.mixin.base_convertable_and_file import BaseConvertableAndFileMixin
+from Products.ERP5.mixin.base_convertable import BaseConvertableFileMixin
 try:
   from string import Template
 except ImportError:
@@ -55,9 +54,8 @@ except ImportError:
 DEFAULT_CONTENT_TYPE = 'text/html'
 _MARKER = []
 
-class TextDocument(BaseConvertableAndFileMixin, CachedConvertableMixin,
-                   CachedConvertableMixin, BaseConvertableMixin, TextContent,
-                   File):
+class TextDocument(CachedConvertableMixin, BaseConvertableFileMixin,
+                                                            TextContent, File):
     """A TextDocument impletents IDocument, IFile, IBaseConvertable, ICachedconvertable
     and ITextConvertable
     """
diff --git a/product/ERP5/mixin/base_convertable.py b/product/ERP5/mixin/base_convertable.py
index 225ebc0f9f..3e81f46c99 100644
--- a/product/ERP5/mixin/base_convertable.py
+++ b/product/ERP5/mixin/base_convertable.py
@@ -26,13 +26,23 @@
 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 #
 ##############################################################################
-from Products.ERP5Type.Base import WorkflowMethod
+from Products.CMFCore.utils import getToolByName
 from AccessControl import ClassSecurityInfo
 from Products.ERP5Type import Permissions
+from OFS.Image import Pdata
+from cStringIO import StringIO
+_MARKER = []
 
-class BaseConvertableMixin:
+class BaseConvertableFileMixin:
   """
   This class provides a generic implementation of IBaseConvertable.
+  This Mixin combine BaseConvertable  and Files documents.
+    getBaseData is overrided to serialise Pdata into string
+    _setBaseData is overrided to wrapp data into Pdata
+
+  - updateBaseMetadata is not implemented in this mixin and must be
+  explicitely overrided if needed.
+
   """
 
   security = ClassSecurityInfo()
@@ -40,23 +50,28 @@ class BaseConvertableMixin:
   security.declareProtected(Permissions.ModifyPortalContent, 'convertToBaseFormat')
   def convertToBaseFormat(self):
     """
+    1 - Check if convertable data is not empty.
+    2 - Call implementation of conversion to Base Format
+    3 - Call convertFile form processing_status_workflow
+        to inform user in case of failures by writing
+        error message in transition's comment.
     """
     if not self.hasData():
       # Empty document cannot be converted
       return
-    try:
-      message = self._convertToBaseFormat() # Call implemetation method
-      if message is None:
-        message = self.Base_translateString('Converted to ${mime_type}.',
-                              mapping={'mime_type': self.getBaseContentType()})
+    message = self._convertToBaseFormat() # Call implemetation method
+    if message is None:
+      message = self.Base_translateString('Converted to ${mime_type}.',
+                            mapping={'mime_type': self.getBaseContentType()})
+    # if processing_status_workflow is associated
+    workflow_tool = getToolByName(self.getPortalObject(), 'portal_workflow')
+    if workflow_tool.isTransitionPossible(self, 'convert_file'):
       self.convertFile(comment=message) # Invoke workflow method
-    except NotImplementedError:
-      message = ''
     return message
 
   security.declareProtected(Permissions.ModifyPortalContent, 'updateBaseMetadata')
   def updateBaseMetadata(self, **kw):
-    """
+    """This Method must be defined explicitely.
     """
     raise NotImplementedError
 
@@ -67,3 +82,24 @@ class BaseConvertableMixin:
     to processing_status_workflow like TempObject.
     """
   convertFile = WorkflowMethod(convertFile)
+
+  security.declareProtected(Permissions.AccessContentsInformation,
+                                                                 'getBaseData')
+  def getBaseData(self, default=_MARKER):
+    """Serialise Pdata into string
+    """
+    self._checkConversionFormatPermission(None)
+    if default is _MARKER:
+      base_data = self._baseGetBaseData()
+    else:
+      base_data = self._baseGetBaseData(default)
+    if base_data is None:
+      return None
+    else:
+      return str(base_data)
+
+  security.declareProtected(Permissions.ModifyPortalContent, '_setBaseData')
+  def _setBaseData(self, data):
+    """Wrap value into Pdata
+    """
+
diff --git a/product/ERP5/mixin/base_convertable_and_file.py b/product/ERP5/mixin/base_convertable_and_file.py
deleted file mode 100644
index 765747986c..0000000000
--- a/product/ERP5/mixin/base_convertable_and_file.py
+++ /dev/null
@@ -1,59 +0,0 @@
-# -*- coding: utf-8 -*-
-##############################################################################
-#
-# Copyright (c) 2010 Nexedi SA and Contributors. All Rights Reserved.
-#                    Nicolas Delaby <nicolas@nexedi.com>
-#
-# WARNING: This program as such is intended to be used by professional
-# programmers who take the whole responsability 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
-# garantees 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
-#
-##############################################################################
-from AccessControl import ClassSecurityInfo
-from Products.ERP5Type import Permissions
-from OFS.Image import Pdata
-import cStringIO
-
-_MARKER = []
-class BaseConvertableAndFileMixin:
-  security = ClassSecurityInfo()
-
-  security.declareProtected(Permissions.AccessContentsInformation,
-                                                                 'getBaseData')
-  def getBaseData(self, default=_MARKER):
-    """return BaseData as str."""
-    if default is _MARKER:
-      base_data = self._baseGetBaseData()
-    else:
-      base_data = self._baseGetBaseData(default)
-    if base_data is None:
-      return None
-    else:
-      return str(base_data)
-
-  security.declareProtected(Permissions.ModifyPortalContent, '_setBaseData')
-  def _setBaseData(self, data):
-    """
-      This is a method which combine base_convertable interface and File API
-    """
-    if not isinstance(data, Pdata) and data is not None:
-      file = cStringIO.StringIO(data)
-      data, size = self._read_data(file)
-    self._baseSetBaseData(data)
diff --git a/product/ERP5OOo/Document/OOoDocument.py b/product/ERP5OOo/Document/OOoDocument.py
index 7ec679ea43..df539d35b0 100644
--- a/product/ERP5OOo/Document/OOoDocument.py
+++ b/product/ERP5OOo/Document/OOoDocument.py
@@ -50,10 +50,8 @@ VALID_IMAGE_FORMAT_LIST, ConversionError, NotConvertedError
 from zLOG import LOG, ERROR
 
 # Mixin Import
-from Products.ERP5.mixin.base_convertable import BaseConvertableMixin
+from Products.ERP5.mixin.base_convertable import BaseConvertableFileMixin
 from Products.ERP5.mixin.text_convertable import TextConvertableMixin
-from Products.ERP5.mixin.base_convertable_and_file import\
-                                                    BaseConvertableAndFileMixin
 
 enc=base64.encodestring
 dec=base64.decodestring
@@ -89,8 +87,8 @@ class TimeoutTransport(SafeTransport):
     return SafeTransport.make_connection(self, h)
 
 
-class OOoDocument(PermanentURLMixIn, BaseConvertableAndFileMixin, File,
-                  BaseConvertableMixin, TextConvertableMixin, Document):
+class OOoDocument(PermanentURLMixIn, BaseConvertableFileMixin, File,
+                                               TextConvertableMixin, Document):
   """
     A file document able to convert OOo compatible files to
     any OOo supported format, to capture metadata and to
-- 
2.30.9