Back to index

moin  1.9.0~rc2
zope.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 """
00004 FCKeditor - The text editor for Internet - http://www.fckeditor.net
00005 Copyright (C) 2003-2009 Frederico Caldeira Knabben
00006 
00007 == BEGIN LICENSE ==
00008 
00009 Licensed under the terms of any of the following licenses at your
00010 choice:
00011 
00012 - GNU General Public License Version 2 or later (the "GPL")
00013 http://www.gnu.org/licenses/gpl.html
00014 
00015 - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
00016 http://www.gnu.org/licenses/lgpl.html
00017 
00018 - Mozilla Public License Version 1.1 or later (the "MPL")
00019 http://www.mozilla.org/MPL/MPL-1.1.html
00020 
00021 == END LICENSE ==
00022 
00023 Connector for Python and Zope.
00024 
00025 This code was not tested at all.
00026 It just was ported from pre 2.5 release, so for further reference see
00027 \editor\filemanager\browser\default\connectors\py\connector.py in previous
00028 releases.
00029 
00030 """
00031 
00032 from fckutil import *
00033 from connector import *
00034 import config as Config
00035 
00036 class FCKeditorConnectorZope(FCKeditorConnector):
00037        """
00038        Zope versiof FCKeditorConnector
00039        """
00040        # Allow access (Zope)
00041        __allow_access_to_unprotected_subobjects__ = 1
00042 
00043        def __init__(self, context=None):
00044               """
00045               Constructor
00046               """
00047               FCKeditorConnector.__init__(self, environ=None) # call superclass constructor
00048               # Instance Attributes
00049               self.context = context
00050               self.request = FCKeditorRequest(context)
00051 
00052        def getZopeRootContext(self):
00053               if self.zopeRootContext is None:
00054                      self.zopeRootContext = self.context.getPhysicalRoot()
00055               return self.zopeRootContext
00056 
00057        def getZopeUploadContext(self):
00058               if self.zopeUploadContext is None:
00059                      folderNames = self.userFilesFolder.split("/")
00060                      c = self.getZopeRootContext()
00061                      for folderName in folderNames:
00062                             if (folderName <> ""):
00063                                    c = c[folderName]
00064                      self.zopeUploadContext = c
00065               return self.zopeUploadContext
00066 
00067        def setHeader(self, key, value):
00068               self.context.REQUEST.RESPONSE.setHeader(key, value)
00069 
00070        def getFolders(self, resourceType, currentFolder):
00071               # Open the folders node
00072               s = ""
00073               s += """<Folders>"""
00074               zopeFolder = self.findZopeFolder(resourceType, currentFolder)
00075               for (name, o) in zopeFolder.objectItems(["Folder"]):
00076                      s += """<Folder name="%s" />""" % (
00077                                    convertToXmlAttribute(name)
00078                                    )
00079               # Close the folders node
00080               s += """</Folders>"""
00081               return s
00082 
00083        def getZopeFoldersAndFiles(self, resourceType, currentFolder):
00084               folders = self.getZopeFolders(resourceType, currentFolder)
00085               files = self.getZopeFiles(resourceType, currentFolder)
00086               s = folders + files
00087               return s
00088 
00089        def getZopeFiles(self, resourceType, currentFolder):
00090               # Open the files node
00091               s = ""
00092               s += """<Files>"""
00093               zopeFolder = self.findZopeFolder(resourceType, currentFolder)
00094               for (name, o) in zopeFolder.objectItems(["File","Image"]):
00095                      s += """<File name="%s" size="%s" />""" % (
00096                                    convertToXmlAttribute(name),
00097                                    ((o.get_size() / 1024) + 1)
00098                                    )
00099               # Close the files node
00100               s += """</Files>"""
00101               return s
00102 
00103        def findZopeFolder(self, resourceType, folderName):
00104               # returns the context of the resource / folder
00105               zopeFolder = self.getZopeUploadContext()
00106               folderName = self.removeFromStart(folderName, "/")
00107               folderName = self.removeFromEnd(folderName, "/")
00108               if (resourceType <> ""):
00109                      try:
00110                             zopeFolder = zopeFolder[resourceType]
00111                      except:
00112                             zopeFolder.manage_addProduct["OFSP"].manage_addFolder(id=resourceType, title=resourceType)
00113                             zopeFolder = zopeFolder[resourceType]
00114               if (folderName <> ""):
00115                      folderNames = folderName.split("/")
00116                      for folderName in folderNames:
00117                             zopeFolder = zopeFolder[folderName]
00118               return zopeFolder
00119 
00120        def createFolder(self, resourceType, currentFolder):
00121               # Find out where we are
00122               zopeFolder = self.findZopeFolder(resourceType, currentFolder)
00123               errorNo = 0
00124               errorMsg = ""
00125               if self.request.has_key("NewFolderName"):
00126                      newFolder = self.request.get("NewFolderName", None)
00127                      zopeFolder.manage_addProduct["OFSP"].manage_addFolder(id=newFolder, title=newFolder)
00128               else:
00129                      errorNo = 102
00130               return self.sendErrorNode ( errorNo, errorMsg )
00131 
00132        def uploadFile(self, resourceType, currentFolder, count=None):
00133               zopeFolder = self.findZopeFolder(resourceType, currentFolder)
00134               file = self.request.get("NewFile", None)
00135               fileName = self.getFileName(file.filename)
00136               fileNameOnly = self.removeExtension(fileName)
00137               fileExtension = self.getExtension(fileName).lower()
00138               if (count):
00139                      nid = "%s.%s.%s" % (fileNameOnly, count, fileExtension)
00140               else:
00141                      nid = fileName
00142               title = nid
00143               try:
00144                      zopeFolder.manage_addProduct['OFSP'].manage_addFile(
00145                                    id=nid,
00146                                    title=title,
00147                                    file=file.read()
00148                                    )
00149               except:
00150                      if (count):
00151                             count += 1
00152                      else:
00153                             count = 1
00154                      return self.zopeFileUpload(resourceType, currentFolder, count)
00155               return self.sendUploadResults( 0 )
00156 
00157 class FCKeditorRequest(object):
00158        "A wrapper around the request object"
00159        def __init__(self, context=None):
00160               r = context.REQUEST
00161               self.request = r
00162 
00163        def has_key(self, key):
00164               return self.request.has_key(key)
00165 
00166        def get(self, key, default=None):
00167               return self.request.get(key, default)
00168 
00169 """
00170 Running from zope, you will need to modify this connector.
00171 If you have uploaded the FCKeditor into Zope (like me), you need to
00172 move this connector out of Zope, and replace the "connector" with an
00173 alias as below.  The key to it is to pass the Zope context in, as
00174 we then have a like to the Zope context.
00175 
00176 ## Script (Python) "connector.py"
00177 ##bind container=container
00178 ##bind context=context
00179 ##bind namespace=
00180 ##bind script=script
00181 ##bind subpath=traverse_subpath
00182 ##parameters=*args, **kws
00183 ##title=ALIAS
00184 ##
00185 
00186 import Products.zope as connector
00187 return connector.FCKeditorConnectorZope(context=context).doResponse()
00188 """