document.py 4.98 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 29
# -*- 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 Products.CMFCore.utils import getToolByName
Nicolas Delaby's avatar
Nicolas Delaby committed
30
from AccessControl import ClassSecurityInfo, Unauthorized
31 32 33
from Products.ERP5Type import Permissions
from OFS.Image import Pdata
from cStringIO import StringIO
34 35
from Products.ERP5Type.TransactionalVariable import getTransactionalVariable

36
_MARKER = []
37
LOCK_PERMISSION_KEY = 'TRANSACTIONAL_VARIABLE_FORMAT_PERMISSION_LOCK_FLAG'
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

class DocumentMixin:
  """
  Implementation of IDocument interface
   convert must not be overloaded as it checks conversion
   format permission

   isSupportBaseDataConversion can be overriden, if base_conversion
   is supported (eg. OOoDocuments, TextDocument).


  """
  security = ClassSecurityInfo()

  security.declareProtected(Permissions.AccessContentsInformation, 'convert')
  def convert(self, format, **kw):
    """
      Main content conversion function, returns result which should
      be returned and stored in cache.

      If format is not allowed for download, Unauthorized exception is raised.

      format - the format specied in the form of an extension
      string (ex. jpeg, html, text, txt, etc.)
      **kw can be various things - e.g. resolution
    """
64 65 66 67 68 69 70 71
    transaction_variable = getTransactionalVariable(self.getPortalObject())
    if LOCK_PERMISSION_KEY in transaction_variable:
      del transaction_variable[LOCK_PERMISSION_KEY]
    self._checkConversionFormatPermission(format, lock_checking=True, **kw)
    result = self._convert(format, **kw)
    if LOCK_PERMISSION_KEY in transaction_variable:
      del transaction_variable[LOCK_PERMISSION_KEY]
    return result
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91

  def _convert(self, format, **kw):
    """Private method which make the transformation.
    Must be overriden !!!
    """
    raise NotImplementedError

  security.declareProtected(Permissions.AccessContentsInformation,
                                             'checkConversionFormatPermission')
  def checkConversionFormatPermission(self, format, **kw):
    """Public version of _checkConversionFormatPermission
    Without raising return just True or False.
    """
    try:
      self._checkConversionFormatPermission(format, **kw)
    except Unauthorized:
      return False
    else:
      return True

92
  def _checkConversionFormatPermission(self, format, lock_checking=False, **kw):
93 94 95
    """Private method to check permission when access specified format.
    This method raises
    """
96 97 98 99 100
    transaction_variable = getTransactionalVariable(self.getPortalObject())
    if transaction_variable.get(LOCK_PERMISSION_KEY, False):
      # Permission already checked in convert with final format,
      # do not check permission for intermediate formats
      return True
101 102 103
    # XXX cache result in TV
    method = self._getTypeBasedMethod('checkConversionFormatPermission',
                 fallback_script_id='Document_checkConversionFormatPermission')
Nicolas Delaby's avatar
Nicolas Delaby committed
104
    if '**' not in method.params():
105 106 107 108 109 110 111
      # Backward compatibility code:
      # Existing Type Based Method doesn't support new **kw argument
      # in their signature.
      is_allowed = method(format=format)
    else:
      is_allowed = method(format=format, **kw)
    if not is_allowed:
112 113 114
      raise Unauthorized('Document: user does not have enough permission'\
                         ' to access document in %s format' %\
                                                        (format or 'original'))
115
    transaction_variable[LOCK_PERMISSION_KEY] = lock_checking
116 117 118 119 120 121 122 123

  security.declareProtected(Permissions.AccessContentsInformation,
                                                 'isSupportBaseDataConversion')
  def isSupportBaseDataConversion(self):
    """Tell if document implement IBaseConvertable Interface.
    By default it doens't
    """
    return False