ContributionTool.py 13.4 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 cStringIO
import pdb
31 32
import re
import string
33

Bartek Górny's avatar
Bartek Górny committed
34
from AccessControl import ClassSecurityInfo, getSecurityManager
35 36 37 38 39 40 41 42 43 44 45
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'
46 47 48
TEMP_NEW_OBJECT_KEY = '_v_new_object'

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

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'
71
  allowed_types = ('File', 'Image', 'Text') # XXX Is this really needed ?
72 73 74 75 76 77 78 79

  # Declarative Security
  security = ClassSecurityInfo()

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

  security.declarePrivate('findTypeName')
80
  def findTypeName(self, file_name, ob):
81 82 83 84
    """
      Finds the appropriate portal type based on the file name
      or if necessary the content of ob
    """
85
    portal_type = None
86 87 88 89 90 91 92 93
    # 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
94 95 96 97
    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]
98 99 100 101 102 103

    # 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)
104 105 106 107 108
      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
109 110 111 112
      else:
        # if not found but the candidate list is there, return the first
        if len(portal_type_list)>0:
          return portal_type_list[0]
113 114 115 116

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

    if portal_type not in valid_portal_type_list:
      # We will not be able to migrate ob to portal_type
121 122
      #return ob.portal_type
      return None
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

    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
149 150
    file_name = None
    # check if file was provided
151 152
    file = kw.get('file', None)
    if file is not None:
Jean-Paul Smets's avatar
Jean-Paul Smets committed
153 154
      file_name = file.filename
    else:
155 156 157 158 159 160 161 162 163
      # some channels supply data and file name separately
      # we have to build an object
      data = kw.get('data', None)
      if data is not None:
        file_name = kw.get('file_name', None)
        if file_name is not None:
          file = cStringIO.StringIO()
          file.write(data)
          file.seek(0)
164 165

    # If the portal_type was provided, we can go faster
166
    if portal_type is not None and portal_type != '':
167 168 169 170 171
      # 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
172 173
      document = module.newContent(portal_type=portal_type, **kw)
      if discover_metadata: document.discoverMetadata(file_name=file_name, user_login=user_login)
174 175 176 177 178 179 180
      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
181
    # with PUT_factory
182 183
    ob = self.PUT_factory( file_name, None, None )

Jean-Paul Smets's avatar
Jean-Paul Smets committed
184 185 186 187 188
    # 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"

189
    # Then put the file inside ourselves for a short while
Jean-Paul Smets's avatar
Jean-Paul Smets committed
190 191 192 193 194 195
    BaseTool._setObject(self, file_name, ob)
    document = self[file_name]

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

196
    # Remove the object from ourselves
Jean-Paul Smets's avatar
Jean-Paul Smets committed
197
    BaseTool._delObject(self, file_name)
198

Jean-Paul Smets's avatar
Jean-Paul Smets committed
199
    # Move the document to where it belongs
200 201
    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
202
    document = self._setObject(file_name, ob)
203

Jean-Paul Smets's avatar
Jean-Paul Smets committed
204 205 206 207 208 209
    # 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
210 211 212 213
    # XXX seems we have to commit now, otherwise it is not reindexed properly later
    # dunno why
    get_transaction().commit()
    document.reindexObject()
214 215
    return document

216
  security.declareProtected( Permissions.AddPortalContent, 'newXML' )
217 218 219 220 221 222 223 224
  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')
225
  def getPropertyDictFromFileName(self, file_name):
226 227 228 229
    """
      Gets properties from filename. File name is parsed with a regular expression
      set in preferences. The regexp should contain named groups.
    """
230 231 232
    if file_name is None:
      return {}
    property_dict = {}
233
    rx_src = self.portal_preferences.getPreferredDocumentFileNameRegularExpression()
234
    if rx_src:
235
      rx_parse = re.compile(rx_src)
236 237 238 239 240
      if rx_parse is not None:
        try:
          property_dict = rx_parse.match(file_name).groupdict()
        except AttributeError: # no match
          pass
241 242
    method = self._getTypeBasedMethod('getPropertyDictFromFileName', 
        fallback_script_id = 'ContributionTool_getPropertyDictFromFileName')
243
    property_dict = method(file_name, property_dict)
244 245 246 247 248
    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:
249 250 251 252 253 254 255 256 257 258
      # 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

259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
  # 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))
285 286
    if portal_type is None:
      raise TypeError, "Unable to determine portal type"
287 288 289 290 291 292 293 294 295 296 297 298 299
    
    # 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)
300
    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
301 302 303 304 305

    # Keep the document close to us
    if not hasattr(self, '_v_document_cache'):
      self._v_document_cache = {}
    self._v_document_cache[name] = document.getRelativeUrl()
306 307 308 309

    # Return document to newContent method
    return document
    
310 311 312 313 314
  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
315 316 317 318 319 320 321 322 323
    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)
324 325 326 327 328

  def _delOb(self, id):
    """
    We don't need to delete, since we never set here
    """
Jean-Paul Smets's avatar
Jean-Paul Smets committed
329 330 331 332 333 334 335 336
    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
337

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

Bartek Górny's avatar
Bartek Górny committed
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
  def listDAVObjects(self):
   """
     Get all docs contributed by the current user
     XXX you can only list them this way, but they're not accessible
     to make it fully usable we should set their id's with module name
     and possibly something nicer to display
   """
   sm = getSecurityManager()
   u = sm.getUser()
   kw = {}
   res = self.portal_catalog(portal_type=self.getPortalDocumentTypeList())
   res = [r.getObject() for r in res]
   res = [o for o in res if u.allowed(o, ('Owner',))] # XXX terrible - needs to use portal_catalog
   return res

355
InitializeClass(ContributionTool)
356 357

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