def do_export(context, REQUEST): """ Export the document as a html file """ sm = get_site_manager(context) target = sm.queryAdapter(context, IZipExportObject) if target.skip: context.setSessionErrorsTrans("You don't have permission " "to download this document") return REQUEST.RESPONSE.redirect(REQUEST.HTTP_REFERER) data = target.data if isinstance(data, unicode): data = data.encode('utf-8') output = StringIO() output.write(data) set_response_attachment(REQUEST.RESPONSE, target.filename, 'text/html; charset=utf-8', output.len) return output.getvalue()
def do_export(self, REQUEST=None): """ Export the contents of the current folder as a Zip file. Returns an open file object. Caller should close the file to free temporary disk space. """ errors = None if REQUEST is not None: errors = [] if not self.getParentNode().checkPermissionView(): raise Unauthorized my_container = self.getParentNode() temp_file = tempfile.TemporaryFile() zip_file = ZipFile(temp_file, mode='w', allowZip64=True) sm = get_site_manager(my_container) builder = RecursiveZipBuilder(zip_file, errors, sm) zip_adapter = sm.queryAdapter(my_container, IZipExportObject) if zip_adapter is None: REQUEST.RESPONSE.notFoundError() builder.recurse(my_container, zip_adapter.filename) builder.write_index() zip_file.close() temp_file.seek(0) if REQUEST is None: return temp_file if errors: transaction.abort() # TODO use ZODB savepoints self.setSessionErrorsTrans(errors) return REQUEST.RESPONSE.redirect(my_container.absolute_url()) response = REQUEST.RESPONSE response.setHeader('content-type', 'application/zip') response.setHeader( 'content-disposition', 'attachment; filename=%s.zip' % my_container.getId()) return stream_response(REQUEST.RESPONSE, temp_file)
def do_export(self, REQUEST=None): """ Export the contents of the current folder as a Zip file. Returns an open file object. Caller should close the file to free temporary disk space. """ errors = None if REQUEST is not None: errors = [] if not self.getParentNode().checkPermissionView(): raise Unauthorized my_container = self.getParentNode() temp_file = tempfile.TemporaryFile() zip_file = ZipFile(temp_file, 'w') sm = get_site_manager(my_container) builder = RecursiveZipBuilder(zip_file, errors, sm) zip_adapter = sm.queryAdapter(my_container, IZipExportObject) if zip_adapter is None: REQUEST.RESPONSE.notFoundError() builder.recurse(my_container, zip_adapter.filename) builder.write_index() zip_file.close() temp_file.seek(0) if REQUEST is None: return temp_file if errors: transaction.abort() # TODO use ZODB savepoints self.setSessionErrorsTrans(errors) return REQUEST.RESPONSE.redirect(my_container.absolute_url()) response = REQUEST.RESPONSE response.setHeader('content-type', 'application/zip') response.setHeader('content-disposition', 'attachment; filename=%s.zip' % my_container.getId()) return stream_response(REQUEST.RESPONSE, temp_file)