def test_OFS_ObjectManager__importObjectFromFile_xml(self): from OFS.DTMLMethod import DTMLMethod from OFS.Folder import Folder from OFS.XMLExportImport import exportXML connection, app = self._makeJarAndRoot() dm = DTMLMethod('test') dm._setId('test') dm.munge(_LONG_DTML) app._setObject('test', dm) sub = Folder('sub') app._setObject('sub', sub) transaction.savepoint(optimistic=True) # need an OID! oid = dm._p_oid sub = app._getOb('sub') handle, path = tempfile.mkstemp(suffix='.xml') try: ostream = os.fdopen(handle,'wb') data = exportXML(connection, oid, ostream) ostream.close() sub._importObjectFromFile(path, 0, 0) finally: # if this operaiton fails with a 'Permission Denied' error, # then comment it out as it's probably masking a failure in # the block above. os.remove(path)
def test_exportXML(self): from OFS.Folder import Folder from OFS.Image import Image from OFS.XMLExportImport import exportXML connection, app = self._makeJarAndRoot() data = open(imagedata, 'rb') sub = Folder('sub') app._setObject('sub', sub) img = Image('image', '', data, 'image/gif') sub._setObject('image', img) img._setProperty('prop1', 3.14159265359, 'float') img._setProperty('prop2', 1, 'int') img._setProperty('prop3', 2L**31-1, 'long') img._setProperty('prop4', 'xxx', 'string') img._setProperty('prop5', ['xxx', 'zzz'], 'lines') img._setProperty('prop6', u'xxx', 'unicode') img._setProperty('prop7', [u'xxx', u'zzz'], 'ulines') img._setProperty('prop8', '<&>', 'string') img._setProperty('prop9', u'<&>', 'unicode') img._setProperty('prop10', '<]]>', 'string') img._setProperty('prop11', u'<]]>', 'unicode') img._setProperty('prop12', u'£', 'unicode') transaction.savepoint(optimistic=True) oid = sub._p_oid handle, path = tempfile.mkstemp(suffix='.xml') try: ostream = os.fdopen(handle,'wb') data = exportXML(connection, oid, ostream) ostream.close() finally: os.remove(path)
def test_export_import_as_file_idempotent(self): from OFS.DTMLMethod import DTMLMethod from OFS.XMLExportImport import exportXML from OFS.XMLExportImport import importXML connection, app = self._makeJarAndRoot() dm = DTMLMethod('test') dm.munge(_LONG_DTML) app._setObject('test', dm) transaction.savepoint(optimistic=True) # need an OID! oid = dm._p_oid handle, path = tempfile.mkstemp(suffix='.xml') try: ostream = os.fdopen(handle,'wb') data = exportXML(connection, oid, ostream) ostream.close() newobj = importXML(connection, path) self.assertTrue(isinstance(newobj, DTMLMethod)) self.assertEqual(newobj.read(), dm.read()) finally: # if this operaiton fails with a 'Permission Denied' error, # then comment it out as it's probably masking a failure in # the block above. os.remove(path)
def manage_exportObject(self, id='', download=None, toxml=None, RESPONSE=None, REQUEST=None): """Exports an object to a file and returns that file.""" if not id: # can't use getId() here (breaks on "old" exported objects) id = self.id if hasattr(id, 'im_func'): id = id() ob = self else: ob = self._getOb(id) suffix = toxml and 'xml' or 'zexp' if download: f = StringIO() if toxml: exportXML(ob._p_jar, ob._p_oid, f) else: ob._p_jar.exportFile(ob._p_oid, f) if RESPONSE is not None: RESPONSE.setHeader('Content-type', 'application/data') RESPONSE.setHeader('Content-Disposition', 'inline;filename=%s.%s' % (id, suffix)) return f.getvalue() cfg = getConfiguration() f = os.path.join(cfg.clienthome, '%s.%s' % (id, suffix)) if toxml: exportXML(ob._p_jar, ob._p_oid, f) else: ob._p_jar.exportFile(ob._p_oid, f) if REQUEST is not None: return self.manage_main( self, REQUEST, manage_tabs_message= '<em>%s</em> successfully exported to <em>%s</em>' % (id, f), title='Object exported')
def manage_exportObject(self, id='', download=None, toxml=None, RESPONSE=None,REQUEST=None): """Exports an object to a file and returns that file.""" if not id: # can't use getId() here (breaks on "old" exported objects) id=self.id if hasattr(id, 'im_func'): id=id() ob=self else: ob=self._getOb(id) suffix=toxml and 'xml' or 'zexp' if download: f=StringIO() if toxml: exportXML(ob._p_jar, ob._p_oid, f) else: ob._p_jar.exportFile(ob._p_oid, f) if RESPONSE is not None: RESPONSE.setHeader('Content-type','application/data') RESPONSE.setHeader('Content-Disposition', 'inline;filename=%s.%s' % (id, suffix)) return f.getvalue() cfg = getConfiguration() f = os.path.join(cfg.clienthome, '%s.%s' % (id, suffix)) if toxml: exportXML(ob._p_jar, ob._p_oid, f) else: ob._p_jar.exportFile(ob._p_oid, f) if REQUEST is not None: return self.manage_main(self, REQUEST, manage_tabs_message= '<em>%s</em> successfully exported to <em>%s</em>' % (id,f), title = 'Object exported')
def test_export_import_as_string_idempotent(self): from OFS.DTMLMethod import DTMLMethod from OFS.XMLExportImport import exportXML from OFS.XMLExportImport import importXML connection, app = self._makeJarAndRoot() dm = DTMLMethod('test') dm.munge(_LONG_DTML) app._setObject('test', dm) transaction.savepoint(optimistic=True) # need an OID! oid = dm._p_oid stream = StringIO() data = exportXML(connection, oid, stream) stream.seek(0) newobj = importXML(connection, stream) self.assertTrue(isinstance(newobj, DTMLMethod)) self.assertEqual(newobj.read(), dm.read())