DMSFile.py 8.55 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 30 31 32 33 34 35 36 37

##############################################################################
#
# Copyright (c) 2002-2006 Nexedi SARL and Contributors. All Rights Reserved.
#
# 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.CMFCore.WorkflowCore import WorkflowMethod
from Products.ERP5Type import Permissions, PropertySheet, Constraint, Interface
from Products.ERP5Type.Cache import CachingMethod
from Products.ERP5.Document.File import File
from Products.ERP5Type.XMLObject import XMLObject
# to overwrite WebDAV methods
from Products.CMFDefault.File import File as CMFFile

38 39
import mimetypes, re
from DateTime import DateTime
40 41 42
mimetypes.init()


43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
rs=[]
rs.append(re.compile('<!.*>'))
rs.append(re.compile('<HEAD>.*</HEAD>',re.DOTALL|re.MULTILINE|re.IGNORECASE))
rs.append(re.compile('<.?(HTML|BODY)[^>]*>',re.DOTALL|re.MULTILINE|re.IGNORECASE))

def stripHtml(txt):
  for r in rs:
    txt=r.sub('',txt)
  return txt


class CachingMixin:
  # time of generation of various formats
  cached_time={}
  # generated files (cache)
  cached_data={}
  # mime types for cached formats XXX to be refactored
  cached_mime={}

  # Declarative security
  security = ClassSecurityInfo()
  security.declareObjectProtected(Permissions.AccessContentsInformation)

  security.declareProtected(Permissions.ModifyPortalContent,'clearCache')
  def clearCache(self):
    """
    Clear cache (invoked by interaction workflow upon file upload
    needed here to overwrite class attribute with instance attrs
    """
    self.cached_time={}
    self.cached_data={}
    self.cached_mime={}

  security.declareProtected(Permissions.View,'hasFileCache')
  def hasFileCache(self,format):
    """
    Checks whether we have a version in this format
    """
    return self.cached_data.has_key(format)

  def getCacheTime(self,format):
    """
    Checks when if ever was the file produced
    """
    return self.cached_time.get(format,0)

  def cacheUpdate(self,format):
      self.cached_time[format]=DateTime()

  def cacheSet(self,format,mime=None,data=None):
    if mime is not None:
      self.cached_mime[format]=mime
    if data is not None:
      self.cached_data[format]=data

  def cacheGet(self,format):
    '''
    we could be much cooler here - pass testing and updating methods to this function
    so that it does it all by itself; this'd eliminate the need for cacheSet public method
    '''
    return self.cached_mime.get(format,''),self.cached_data.get(format,'')

  security.declareProtected(Permissions.View,'getCacheInfo')
  def getCacheInfo(self):
    """
    Get cache details as string (for debugging)
    """
    s='CACHE INFO:<br/><table><tr><td>format</td><td>size</td><td>time</td><td>is changed</td></tr>'
    #self.log('getCacheInfo',self.cached_time)
    #self.log('getCacheInfo',self.cached_data)
    for f in self.cached_time.keys():
      t=self.cached_time[f]
      data=self.cached_data.get(f)
      if data:
        if isinstance(data,str):
          ln=len(data)
        else:
          ln=0
          while data is not None:
            ln+=len(data.data)
            data=data.next
      else:
        ln='no data!!!'
      s+='<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>' % (f,str(ln),str(t),str(self.isFileChanged(f)))
    s+='</table>'
    return s

130 131 132 133
class DMSFile(XMLObject,File):
  """
  Special base class, different from File only in that it can contain things 
  (like Role Definition, for example)
134
  will be merged with File when WebDAV issues are solved
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
  """
  # CMF Type Definition
  meta_type = 'ERP5 DMS File'
  portal_type = 'DMS File'
  isPortalContent = 1
  isRADContent = 1

  # Declarative security
  security = ClassSecurityInfo()
  security.declareObjectProtected(Permissions.AccessContentsInformation)

  # Default Properties
  property_sheets = ( PropertySheet.Base
                    , PropertySheet.CategoryCore
                    , PropertySheet.DublinCore
                    , PropertySheet.Version
                    , PropertySheet.Reference
                    , PropertySheet.DMSFile
                    )


  # make sure to call the right edit methods
  _edit=File._edit
  edit=File.edit

160
  searchable_attrs=('title','description','id','reference','version',
161
      'short_title','keywords','subject','source_reference','source_project_title')
162

163 164 165
  ### Content indexing methods
  security.declareProtected(Permissions.View, 'getSearchableText')
  def getSearchableText(self, md=None):
166
    """
167 168
    Used by the catalog for basic full text indexing
    """
169
    searchable_text = ' '.join(map(lambda x: self.getProperty(x) or ' ',self.searchable_attrs))
170 171
    return searchable_text

172 173 174 175 176 177 178 179 180 181 182 183 184 185
  security.declarePrivate('_unpackData')
  def _unpackData(self,data):
    """
    Unpack Pdata into string
    """
    if isinstance(data,str):
      return data
    else:
      data_list=[]
      while data is not None:
        data_list.append(data.data)
        data=data.next
      return ''.join(data_list)

186 187
  SearchableText=getSearchableText

188 189 190 191 192 193 194
  security.declareProtected(Permissions.ModifyPortalContent, 'guessMimeType')
  def guessMimeType(self,fname=''):
    '''get mime type from file name'''
    if fname=='':fname=self.getOriginalFilename()
    if fname:
      content_type,enc=mimetypes.guess_type(fname)
      if content_type is not None:
195 196
        self.content_type=content_type
    return content_type
197

198 199 200 201 202 203 204 205 206 207 208 209 210
  security.declareProtected(Permissions.ModifyPortalContent, 'setPropertiesFromFilename')
  def setPropertiesFromFilename(self,fname):
    rx_parse=re.compile(self.portal_preferences.getPreferredDmsFilenameRegexp())
    if rx_parse is None:
      self.setReference(fname)
      return
    m=rx_parse.match(fname)
    if m is None:
      self.setReference(fname)
      return
    for k,v in m.groupdict().items():
      self.setProperty(k,v)

211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
  security.declareProtected(Permissions.View, 'getWikiSuccessorReferenceList')
  def getWikiSuccessorReferenceList(self):
    '''
    find references in text_content, return matches
    with this we can then find objects
    '''
    rx_search=re.compile(self.portal_preferences.getPreferredDmsReferenceRegexp())
    try:
      res=rx_search.finditer(self.getTextContent())
    except AttributeError:
      return []
    res=[(r.group(),r.groupdict()) for r in res]
    return res

  security.declareProtected(Permissions.View, 'getWikiSuccessorValueList')
  def getWikiSuccessorValueList(self):
    '''
    getWikiSuccessorValueList - the way to find objects is on 
    implementation level
    '''
    lst=[]
    for ref in self.getWikiSuccessorReferenceList():
      res=self.DMS_findDocument(ref)
      if len(res)>0:
        lst.append(res[0].getObject())
    return lst

  security.declareProtected(Permissions.View, 'getWikiPredecessorValueList')
  def getWikiPredecessorValueList(self):
    '''
    it is mostly implementation level - depends on what parameters we use to identify
    document, and on how a doc must reference me to be my predecessor (reference only,
    or with a language, etc
    '''
    lst=self.DMS_findPredecessors()
    lst=[r.getObject() for r in lst]
    di=dict.fromkeys(lst) # make it unique
    ref=self.getReference()
    return [o for o in di.keys() if o.getReference()!=ref] # every object has its own reference in SearchableText
250

251 252 253 254 255 256 257 258 259 260 261
  # BG copied from File in case
  index_html = CMFFile.index_html
  PUT = CMFFile.PUT
  security.declareProtected('FTP access', 'manage_FTPget', 'manage_FTPstat', 'manage_FTPlist')
  manage_FTPget = CMFFile.manage_FTPget
  manage_FTPlist = CMFFile.manage_FTPlist
  manage_FTPstat = CMFFile.manage_FTPstat


# vim: syntax=python shiftwidth=2