zope.py
上传用户:ah_jiwei
上传日期:2022-07-24
资源大小:54044k
文件大小:6k
源码类别:

数据库编程

开发平台:

Visual C++

  1. #!/usr/bin/env python
  2. """
  3. FCKeditor - The text editor for Internet - http://www.fckeditor.net
  4. Copyright (C) 2003-2007 Frederico Caldeira Knabben
  5. == BEGIN LICENSE ==
  6. Licensed under the terms of any of the following licenses at your
  7. choice:
  8. - GNU General Public License Version 2 or later (the "GPL")
  9. http://www.gnu.org/licenses/gpl.html
  10. - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
  11. http://www.gnu.org/licenses/lgpl.html
  12. - Mozilla Public License Version 1.1 or later (the "MPL")
  13. http://www.mozilla.org/MPL/MPL-1.1.html
  14. == END LICENSE ==
  15. Connector for Python and Zope.
  16. This code was not tested at all.
  17. It just was ported from pre 2.5 release, so for further reference see 
  18. editorfilemanagerbrowserdefaultconnectorspyconnector.py in previous 
  19. releases.
  20. """
  21. from fckutil import *
  22. from connector import *
  23. import config as Config
  24. class FCKeditorConnectorZope(FCKeditorConnector):
  25. """
  26. Zope versiof FCKeditorConnector
  27. """
  28. # Allow access (Zope)
  29. __allow_access_to_unprotected_subobjects__ = 1
  30. def __init__(self, context=None):
  31. """
  32. Constructor
  33. """
  34. FCKeditorConnector.__init__(self, environ=None) # call superclass constructor
  35. # Instance Attributes
  36. self.context = context
  37. self.request = FCKeditorRequest(context)
  38. def getZopeRootContext(self):
  39. if self.zopeRootContext is None:
  40. self.zopeRootContext = self.context.getPhysicalRoot()
  41. return self.zopeRootContext
  42. def getZopeUploadContext(self):
  43. if self.zopeUploadContext is None:
  44. folderNames = self.userFilesFolder.split("/")
  45. c = self.getZopeRootContext()
  46. for folderName in folderNames:
  47. if (folderName <> ""):
  48. c = c[folderName]
  49. self.zopeUploadContext = c
  50. return self.zopeUploadContext
  51. def setHeader(self, key, value):
  52. self.context.REQUEST.RESPONSE.setHeader(key, value)
  53. def getFolders(self, resourceType, currentFolder):
  54. # Open the folders node
  55. s = ""
  56. s += """<Folders>"""
  57. zopeFolder = self.findZopeFolder(resourceType, currentFolder)
  58. for (name, o) in zopeFolder.objectItems(["Folder"]):
  59. s += """<Folder name="%s" />""" % (
  60. convertToXmlAttribute(name)
  61. )
  62. # Close the folders node
  63. s += """</Folders>"""
  64. return s
  65. def getZopeFoldersAndFiles(self, resourceType, currentFolder):
  66. folders = self.getZopeFolders(resourceType, currentFolder)
  67. files = self.getZopeFiles(resourceType, currentFolder)
  68. s = folders + files
  69. return s
  70. def getZopeFiles(self, resourceType, currentFolder):
  71. # Open the files node
  72. s = ""
  73. s += """<Files>"""
  74. zopeFolder = self.findZopeFolder(resourceType, currentFolder)
  75. for (name, o) in zopeFolder.objectItems(["File","Image"]):
  76. s += """<File name="%s" size="%s" />""" % (
  77. convertToXmlAttribute(name),
  78. ((o.get_size() / 1024) + 1)
  79. )
  80. # Close the files node
  81. s += """</Files>"""
  82. return s
  83. def findZopeFolder(self, resourceType, folderName):
  84. # returns the context of the resource / folder
  85. zopeFolder = self.getZopeUploadContext()
  86. folderName = self.removeFromStart(folderName, "/")
  87. folderName = self.removeFromEnd(folderName, "/")
  88. if (resourceType <> ""):
  89. try:
  90. zopeFolder = zopeFolder[resourceType]
  91. except:
  92. zopeFolder.manage_addProduct["OFSP"].manage_addFolder(id=resourceType, title=resourceType)
  93. zopeFolder = zopeFolder[resourceType]
  94. if (folderName <> ""):
  95. folderNames = folderName.split("/")
  96. for folderName in folderNames:
  97. zopeFolder = zopeFolder[folderName]
  98. return zopeFolder
  99. def createFolder(self, resourceType, currentFolder):
  100. # Find out where we are
  101. zopeFolder = self.findZopeFolder(resourceType, currentFolder)
  102. errorNo = 0
  103. errorMsg = ""
  104. if self.request.has_key("NewFolderName"):
  105. newFolder = self.request.get("NewFolderName", None)
  106. zopeFolder.manage_addProduct["OFSP"].manage_addFolder(id=newFolder, title=newFolder)
  107. else:
  108. errorNo = 102
  109. return self.sendErrorNode ( errorNo, errorMsg )
  110. def uploadFile(self, resourceType, currentFolder, count=None):
  111. zopeFolder = self.findZopeFolder(resourceType, currentFolder)
  112. file = self.request.get("NewFile", None)
  113. fileName = self.getFileName(file.filename)
  114. fileNameOnly = self.removeExtension(fileName)
  115. fileExtension = self.getExtension(fileName).lower()
  116. if (count):
  117. nid = "%s.%s.%s" % (fileNameOnly, count, fileExtension)
  118. else:
  119. nid = fileName
  120. title = nid
  121. try:
  122. zopeFolder.manage_addProduct['OFSP'].manage_addFile(
  123. id=nid,
  124. title=title,
  125. file=file.read()
  126. )
  127. except:
  128. if (count):
  129. count += 1
  130. else:
  131. count = 1
  132. return self.zopeFileUpload(resourceType, currentFolder, count)
  133. return self.sendUploadResults( 0 )
  134. class FCKeditorRequest(object):
  135. "A wrapper around the request object"
  136. def __init__(self, context=None):
  137. r = context.REQUEST
  138. self.request = r
  139. def has_key(self, key):
  140. return self.request.has_key(key)
  141. def get(self, key, default=None):
  142. return self.request.get(key, default)
  143. """
  144. Running from zope, you will need to modify this connector.
  145. If you have uploaded the FCKeditor into Zope (like me), you need to
  146. move this connector out of Zope, and replace the "connector" with an
  147. alias as below.  The key to it is to pass the Zope context in, as
  148. we then have a like to the Zope context.
  149. ## Script (Python) "connector.py"
  150. ##bind container=container
  151. ##bind context=context
  152. ##bind namespace=
  153. ##bind script=script
  154. ##bind subpath=traverse_subpath
  155. ##parameters=*args, **kws
  156. ##title=ALIAS
  157. ##
  158. import Products.zope as connector
  159. return connector.FCKeditorConnectorZope(context=context).doResponse()
  160. """