| 1 | ################################################################################
|
|---|
| 2 | # _importable.py
|
|---|
| 3 | #
|
|---|
| 4 | # This program is free software; you can redistribute it and/or
|
|---|
| 5 | # modify it under the terms of the GNU General Public License
|
|---|
| 6 | # as published by the Free Software Foundation; either version 2
|
|---|
| 7 | # of the License, or (at your option) any later version.
|
|---|
| 8 | #
|
|---|
| 9 | # This program is distributed in the hope that it will be useful,
|
|---|
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 12 | # GNU General Public License for more details.
|
|---|
| 13 | #
|
|---|
| 14 | # You should have received a copy of the GNU General Public License
|
|---|
| 15 | # along with this program; if not, write to the Free Software
|
|---|
| 16 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|---|
| 17 | ################################################################################
|
|---|
| 18 |
|
|---|
| 19 | # Imports.
|
|---|
| 20 | from App.Common import package_home
|
|---|
| 21 | from App.special_dtml import HTMLFile
|
|---|
| 22 | from cStringIO import StringIO
|
|---|
| 23 | import ZPublisher.HTTPRequest
|
|---|
| 24 | import os
|
|---|
| 25 | import sys
|
|---|
| 26 | import tempfile
|
|---|
| 27 | import time
|
|---|
| 28 | import transaction
|
|---|
| 29 | import urllib
|
|---|
| 30 | import zExceptions
|
|---|
| 31 | # Product Imports.
|
|---|
| 32 | import _fileutil
|
|---|
| 33 | import _filtermanager
|
|---|
| 34 | import _globals
|
|---|
| 35 | from _blobfields import recurse_uploadRessources, uploadRessources
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 | # ------------------------------------------------------------------------------
|
|---|
| 39 | # _importable.recurse_importContent:
|
|---|
| 40 | #
|
|---|
| 41 | # Process objects after import.
|
|---|
| 42 | # ------------------------------------------------------------------------------
|
|---|
| 43 | def recurse_importContent(self, folder):
|
|---|
| 44 | _globals.writeBlock( self, '[recurse_importContent]')
|
|---|
| 45 | message = ''
|
|---|
| 46 |
|
|---|
| 47 | # Cleanup.
|
|---|
| 48 | for key in ['oRoot','oRootNode','oCurrNode','oParent','dTagStack','dValueStack']:
|
|---|
| 49 | try: delattr(self,key)
|
|---|
| 50 | except: pass
|
|---|
| 51 |
|
|---|
| 52 | # Upload ressources.
|
|---|
| 53 | uploadRessources(self,folder)
|
|---|
| 54 |
|
|---|
| 55 | # Commit object.
|
|---|
| 56 | self.onChangeObj( self.REQUEST, forced=1)
|
|---|
| 57 | transaction.commit()
|
|---|
| 58 |
|
|---|
| 59 | # Process children.
|
|---|
| 60 | for ob in self.getChildNodes():
|
|---|
| 61 | recurse_importContent(ob,folder)
|
|---|
| 62 |
|
|---|
| 63 | # Return with message.
|
|---|
| 64 | return message
|
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 | # ------------------------------------------------------------------------------
|
|---|
| 68 | # _importable.importContent
|
|---|
| 69 | # ------------------------------------------------------------------------------
|
|---|
| 70 | def importContent(self, file):
|
|---|
| 71 | message = ''
|
|---|
| 72 |
|
|---|
| 73 | # Setup.
|
|---|
| 74 | catalog_awareness = self.getConfProperty('ZMS.CatalogAwareness.active',1)
|
|---|
| 75 | self.setConfProperty('ZMS.CatalogAwareness.active',0)
|
|---|
| 76 | self.dTagStack = _globals.MyStack()
|
|---|
| 77 | self.dValueStack = _globals.MyStack()
|
|---|
| 78 | self.oParent = self.getParentNode()
|
|---|
| 79 |
|
|---|
| 80 | # Parse XML-file.
|
|---|
| 81 | ob = self.parse(StringIO(file.read()),self,1)
|
|---|
| 82 |
|
|---|
| 83 | # Process objects after import
|
|---|
| 84 | message += recurse_importContent(ob,_fileutil.getFilePath(file.name))
|
|---|
| 85 |
|
|---|
| 86 | # Cleanup.
|
|---|
| 87 | self.setConfProperty('ZMS.CatalogAwareness.active',catalog_awareness)
|
|---|
| 88 |
|
|---|
| 89 | # Return with message.
|
|---|
| 90 | return message
|
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 | # ------------------------------------------------------------------------------
|
|---|
| 94 | # _importable.importFile
|
|---|
| 95 | # ------------------------------------------------------------------------------
|
|---|
| 96 | def importFile(self, file, REQUEST, handler):
|
|---|
| 97 | message = ''
|
|---|
| 98 |
|
|---|
| 99 | # Get filename.
|
|---|
| 100 | if isinstance(file,ZPublisher.HTTPRequest.FileUpload):
|
|---|
| 101 | filename = file.filename
|
|---|
| 102 | else:
|
|---|
| 103 | filename = file.name
|
|---|
| 104 |
|
|---|
| 105 | # Create temporary folder.
|
|---|
| 106 | folder = tempfile.mktemp()
|
|---|
| 107 | os.mkdir(folder)
|
|---|
| 108 |
|
|---|
| 109 | # Save to temporary file.
|
|---|
| 110 | filename = _fileutil.getOSPath('%s/%s'%(folder,_fileutil.extractFilename(filename)))
|
|---|
| 111 | _fileutil.exportObj(file,filename)
|
|---|
| 112 |
|
|---|
| 113 | # Find XML-file.
|
|---|
| 114 | if _fileutil.extractFileExt(filename) == 'zip':
|
|---|
| 115 | _fileutil.extractZipArchive(filename)
|
|---|
| 116 | filename = None
|
|---|
| 117 | for deep in [0,1]:
|
|---|
| 118 | for ext in ['xml', 'htm', 'html' ]:
|
|---|
| 119 | if filename is None:
|
|---|
| 120 | filename = _fileutil.findExtension(ext, folder, deep)
|
|---|
| 121 | break
|
|---|
| 122 | if filename is None:
|
|---|
| 123 | raise zExceptions.InternalError('XML-File not found!')
|
|---|
| 124 |
|
|---|
| 125 | # Import Filter.
|
|---|
| 126 | if REQUEST.get('filter','') in self.getFilterIds():
|
|---|
| 127 | filename = _filtermanager.importFilter(self, filename, REQUEST.get('filter',''), REQUEST)
|
|---|
| 128 |
|
|---|
| 129 | # Import XML-file.
|
|---|
| 130 | f = open(filename, 'r')
|
|---|
| 131 | message += handler(self, f)
|
|---|
| 132 | f.close()
|
|---|
| 133 |
|
|---|
| 134 | # Remove temporary files.
|
|---|
| 135 | _fileutil.remove(folder, deep=1)
|
|---|
| 136 |
|
|---|
| 137 | # Return with message.
|
|---|
| 138 | message += self.getZMILangStr('MSG_IMPORTED')%('<i>%s</i>'%_fileutil.extractFilename(filename))
|
|---|
| 139 | return message
|
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 142 | ################################################################################
|
|---|
| 143 | ################################################################################
|
|---|
| 144 | ###
|
|---|
| 145 | ### class Importable
|
|---|
| 146 | ###
|
|---|
| 147 | ################################################################################
|
|---|
| 148 | ################################################################################
|
|---|
| 149 | class Importable:
|
|---|
| 150 |
|
|---|
| 151 | ##############################################################################
|
|---|
| 152 | # Importable.manage_import:
|
|---|
| 153 | #
|
|---|
| 154 | # Import XML-file.
|
|---|
| 155 | ##############################################################################
|
|---|
| 156 | def manage_import(self, file, lang, REQUEST, RESPONSE=None):
|
|---|
| 157 | """ Importable.manage_import """
|
|---|
| 158 | message = ''
|
|---|
| 159 | t0 = time.time()
|
|---|
| 160 |
|
|---|
| 161 | # Import XML.
|
|---|
| 162 | message += importFile(self, file, REQUEST, importContent)
|
|---|
| 163 |
|
|---|
| 164 | # Return with message.
|
|---|
| 165 | if RESPONSE:
|
|---|
| 166 | message += ' (in '+str(int((time.time()-t0)*100.0)/100.0)+' secs.)'
|
|---|
| 167 | return REQUEST.RESPONSE.redirect('manage_main?lang=%s&manage_tabs_message=%s'%(lang,urllib.quote(message)))
|
|---|
| 168 |
|
|---|
| 169 |
|
|---|
| 170 | ################################################################################
|
|---|