ContributionTool.py 12.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
##############################################################################
#
# Copyright (c) 2007 Nexedi SARL and Contributors. All Rights Reserved.
#                    Jean-Paul Smets <jp@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.
#
##############################################################################

29 30
import re
import string
31
import pdb
32 33 34 35 36 37 38 39 40 41 42 43 44

from AccessControl import ClassSecurityInfo
from Globals import InitializeClass, DTMLFile
from Products.ERP5Type.Tool.BaseTool import BaseTool
from Products.ERP5Type import Permissions
from Products.ERP5 import _dtmldir
from Products.ERP5.Document.BusinessTemplate import getChainByType
from zLOG import LOG
from DateTime import DateTime
from Acquisition import aq_base

NO_DISCOVER_METADATA_KEY = '_v_no_discover_metadata'
USER_NAME_KEY = '_v_document_user_login'
45 46 47
TEMP_NEW_OBJECT_KEY = '_v_new_object'

_marker = []  # Create a new marker object.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69

class ContributionTool(BaseTool):
  """
    ContributionTool provides an abstraction layer to unify the contribution
    of documents into an ERP5Site.

    ContributionTool is configured in portal_types in
    such way that it can store Text, Spreadsheet, PDF, etc.

    The method to use is portal_contributions.newContent, which should receive
    either a portal type or a file name from which type can be derived or a file from which
    content type can be derived, otherwise it will fail.

    Configuration Scripts:
      - ContributionTool_getPropertyDictFromFileName: receives file name and a 
        dict derived from filename by regular expression, and does any necesary
        operations (e.g. mapping document type id onto a real portal_type).
  """
  title = 'Contribution Tool'
  id = 'portal_contributions'
  meta_type = 'ERP5 Contribution Tool'
  portal_type = 'Contribution Tool'
70
  allowed_types = ('File', 'Image', 'Text') # XXX Is this really needed ?
71 72 73 74 75 76 77 78

  # Declarative Security
  security = ClassSecurityInfo()

  security.declareProtected(Permissions.ManagePortal, 'manage_overview' )
  manage_overview = DTMLFile( 'explainContributionTool', _dtmldir )

  security.declarePrivate('findTypeName')
79
  def findTypeName(self, file_name, ob):
80 81 82 83
    """
      Finds the appropriate portal type based on the file name
      or if necessary the content of ob
    """
84
    portal_type = None
85 86 87 88 89 90 91 92
    # We should only consider those portal_types which share the
    # same meta_type with the current object
    valid_portal_type_list = []
    for pt in self.portal_types.objectValues():
      if pt.meta_type == ob.meta_type:
        valid_portal_type_list.append(pt.id)

    # Check if the filename tells which portal_type this is
93 94 95 96
    portal_type_list = self.getPropertyDictFromFileName(file_name).get('portal_type', [])
    if len(portal_type_list) == 1:
      # if we have only one, then this is it
      return portal_type_list[0]
97 98 99 100 101 102

    # If it is still None, we need to read the document
    # to check which of the candidates is suitable
    if portal_type is None:
      # The document is now responsible of telling all its properties
      portal_type = ob.getPropertyDictFromContent().get('portal_type', None)
103 104 105 106 107
      if portal_type is not None:
        # we check if it matches the candidate list, if there were any
        if len(portal_type_list)>1 and portal_type not in portal_type_list:
          raise TypeError('%s not in the list of %s' % (portal_type, str(portal_type_list)))
        return portal_type
108 109 110 111
      else:
        # if not found but the candidate list is there, return the first
        if len(portal_type_list)>0:
          return portal_type_list[0]
112 113 114 115

    if portal_type is None:
      # We can not do anything anymore
      return ob.portal_type
116
      #return None
117 118 119

    if portal_type not in valid_portal_type_list:
      # We will not be able to migrate ob to portal_type
120 121
      #return ob.portal_type
      return None
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149

    return portal_type

  security.declareProtected(Permissions.AddPortalContent, 'newContent')
  def newContent(self, id=None, portal_type=None,
                       discover_metadata=1, temp_object=0,
                       user_login=None, **kw):
    """
      The newContent method is overriden to implement smart content
      creation by detecting the portal type based on whatever information
      was provided and finding out the most appropriate module to store
      the content.

      user_login is the name under which the content will be created
      XXX - Is this a security hole ?

      NOTE:
        We always generate ID. So, we must prevent using the one
        which we were provided.
    """
    # Temp objects use the standard newContent from Folder
    if temp_object:
      # For temp_object creation, use the standard method
      return BaseTool.newContent(self, id=id, portal_type=portal_type, temp_object=1, **kw)

    # Try to find the file_name
    file = kw.get('file', None)
    if file is not None:
Jean-Paul Smets's avatar
Jean-Paul Smets committed
150 151 152
      file_name = file.filename
    else:
      file_name = None
153 154

    # If the portal_type was provided, we can go faster
155
    if portal_type is not None and portal_type != '':
156 157 158 159 160
      # We know the portal_type, let us find the module
      module = self.getDefaultModule(portal_type)

      # And return a document
      # NOTE: we use the module ID generator rather than the provided ID
161 162
      document = module.newContent(portal_type=portal_type, **kw)
      if discover_metadata: document.discoverMetadata(file_name=file_name, user_login=user_login)
163 164 165 166 167 168 169
      return document

    # From here, there is no hope unless a file was provided    
    if file is None:
      raise ValueError, "could not determine portal type"

    # So we will simulate WebDAV to get an empty object
170
    # with PUT_factory
171 172
    ob = self.PUT_factory( file_name, None, None )

Jean-Paul Smets's avatar
Jean-Paul Smets committed
173 174 175 176 177
    # Raise an error if we could not guess the portal type
    # XXX Maybe we should try to pass the typ param
    if ob is None:
      raise ValueError, "Could not determine the document type"

178
    # Then put the file inside ourselves for a short while
Jean-Paul Smets's avatar
Jean-Paul Smets committed
179 180 181 182 183 184
    BaseTool._setObject(self, file_name, ob)
    document = self[file_name]

    # Then edit the document contents (so that upload can happen)
    document._edit(**kw)

185
    # Remove the object from ourselves
Jean-Paul Smets's avatar
Jean-Paul Smets committed
186
    BaseTool._delObject(self, file_name)
187

Jean-Paul Smets's avatar
Jean-Paul Smets committed
188
    # Move the document to where it belongs
189 190
    if not discover_metadata: setattr(self, NO_DISCOVER_METADATA_KEY, 1)
    setattr(ob, USER_NAME_KEY, user_login)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
191
    document = self._setObject(file_name, ob)
192

Jean-Paul Smets's avatar
Jean-Paul Smets committed
193 194 195 196 197 198
    # Time to empty the cache
    if hasattr(self, '_v_document_cache'):
      if self._v_document_cache.has_key(file_name):
        del self._v_document_cache[file_name]

    # Reindex it and return the document
199 200 201 202
    # XXX seems we have to commit now, otherwise it is not reindexed properly later
    # dunno why
    get_transaction().commit()
    document.reindexObject()
203 204 205 206 207 208 209 210 211 212 213
    return document

  security.declareProtected( Permissions.AddPortalContent, 'fromXML' )
  def newXML(self, xml):
    """
      Create a new content based on XML data. This is intended for contributing
      to ERP5 from another application.
    """
    pass

  security.declareProtected(Permissions.ModifyPortalContent,'getPropertyDictFromFileName')
214
  def getPropertyDictFromFileName(self, file_name):
215 216 217 218
    """
      Gets properties from filename. File name is parsed with a regular expression
      set in preferences. The regexp should contain named groups.
    """
219 220 221
    if file_name is None:
      return {}
    property_dict = {}
222
    rx_src = self.portal_preferences.getPreferredDocumentFileNameRegularExpression()
223
    if rx_src:
224
      rx_parse = re.compile(rx_src)
225 226 227 228 229
      if rx_parse is not None:
        try:
          property_dict = rx_parse.match(file_name).groupdict()
        except AttributeError: # no match
          pass
230 231
    method = self._getTypeBasedMethod('getPropertyDictFromFileName', 
        fallback_script_id = 'ContributionTool_getPropertyDictFromFileName')
232
    property_dict = method(file_name, property_dict)
233 234 235 236 237
    if property_dict.has_key('portal_type'):
      # we have to return portal_type as a tuple
      # because we can allow for having multiple types (candidates)
      property_dict['portal_type'] = (property_dict['portal_type'],)
    else:
238 239 240 241 242 243 244 245 246 247
      # we have to find candidates by file extenstion
      try:
        index = file_name.rfind('.')
        if index != -1:
          ext = file_name[index+1:]
          property_dict['portal_type'] = self.ContributionTool_getCandidateTypeListByExtension(ext)
      except ValueError: # no dot in file name
        pass
    return property_dict

248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
  # WebDAV virtual folder support
  def _setObject(self, name, ob, user_login=None):
    """
      The strategy is to let NullResource.PUT do everything as
      usual and at the last minute put the object in a different
      location with a different portal type. This means that
      NullResource.PUT creates an empty document with PUT_factory
      then upload document data by invoking PUT on the empty
      document and finally sets the object. By overriding _setObject
      we get a chance to fix the portal_type of the document
      (as long as the one we find is compatible) and move the
      document to the appropriate module.

      content_type_registry must be set up so that an appropriate
      portal_type with appropriate meta_type is found for every
      kind of document. However, a different portal_type might
      be used in the end.

      The ContributionTool instance must be configured in such
      way that _verifyObjectPaste will return TRUE.

      Refer to: NullResource.PUT
    """
    # Find the portal type based on file name and content
    # We provide ob in the context of self to make sure scripting is possible
    portal_type = self.findTypeName(name, ob.__of__(self))
274 275
    if portal_type is None:
      raise TypeError, "Unable to determine portal type"
276 277 278 279 280 281 282 283 284 285 286 287 288
    
    # We know the portal_type, let us find the module
    module = self.getDefaultModule(portal_type)

    # Set the object on the module and fix the portal_type and id
    new_id = module.generateNewId()
    ob.portal_type = portal_type
    ob.id = new_id
    module._setObject(new_id, ob)

    # We can now discover metadata unless NO_DISCOVER_METADATA_KEY was set on ob
    document = module[new_id]
    user_login = getattr(self, USER_NAME_KEY, None)
289
    if not getattr(ob, NO_DISCOVER_METADATA_KEY, 0): document.discoverMetadata(file_name=name, user_login=user_login)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
290 291 292 293 294

    # Keep the document close to us
    if not hasattr(self, '_v_document_cache'):
      self._v_document_cache = {}
    self._v_document_cache[name] = document.getRelativeUrl()
295 296 297 298

    # Return document to newContent method
    return document
    
299 300 301 302 303
  def _getOb(self, id, default=_marker):
    """
    Check for volatile temp object info first
    and try to find it
    """
Jean-Paul Smets's avatar
Jean-Paul Smets committed
304 305 306 307 308 309 310 311 312
    if hasattr(self, '_v_document_cache'):
      document_url = self._v_document_cache.get(id, None)
      if document_url is not None:
        return self.getPortalObject().unrestrictedTraverse(document_url)

    if default is _marker:
      return BaseTool._getOb(self, id)
    else:
      return BaseTool._getOb(self, id, default=default)
313 314 315 316 317

  def _delOb(self, id):
    """
    We don't need to delete, since we never set here
    """
Jean-Paul Smets's avatar
Jean-Paul Smets committed
318 319 320 321 322 323 324 325
    if hasattr(self, '_v_document_cache'):
      document_url = self._v_document_cache.get(id, None)
      if document_url is not None:
        document = self.getPortalObject().unrestrictedTraverse(document_url)
        if document is not None:
          document.getParentValue()._delOb(document.getId())
          del self._v_document_cache[id]
          return
326

Jean-Paul Smets's avatar
Jean-Paul Smets committed
327
    return BaseTool._delOb(self, id)
328

329
InitializeClass(ContributionTool)
330 331

# vim: filetype=python syntax=python shiftwidth=2