def getSpreadsheet(file): ooo_parser = OOoParser() # Extract tables from the speadsheet file if file is None: return {} elif hasattr(file, 'headers'): # if the file is not an open office format, try to convert it using oood content_type = file.headers.get('Content-Type', '') if not (content_type.startswith('application/vnd.sun.xml') or content_type.startswith('application/vnd.oasis.opendocument')): from Products.ERP5Type.Document import newTempOOoDocument tmp_ooo = newTempOOoDocument(context, file.filename) tmp_ooo.edit(data=file.read(), content_type=content_type) tmp_ooo.convertToBaseFormat() ignored, import_file_content = tmp_ooo.convert('ods') ooo_parser.openFromString(str(import_file_content)) else: ooo_parser.openFile(file) else: ooo_parser.openFromString(file) return ooo_parser.getSpreadsheetsMapping()
def checkConversionToolAvailability(self): """ Check conversion tool (oood) is available for erp5. This script convert an odt document into HTML and try to read the returned string and find out expected string """ portal = self.getPortalObject() document_id = 'P-ERP5-TEST.Conversion.Tool.Availability-001-en.odt' document_path = 'portal_skins/erp5_administration/%s' % (document_id,) document_file = portal.restrictedTraverse(document_path) message = None severity = 0 try: temp_document = newTempOOoDocument(self, document_id, data=document_file.data, source_reference=document_id) temp_document.convertToBaseFormat() _, html_result = temp_document.convert(format='html') except ConflictError: raise except: #Which Errors should we catch ? #Transformation failed message = 'Conversion tool got unexpected error:\n%s' % ''.join(ExceptionFormatter.format_exception(*sys.exc_info())) else: #Everything goes fine, Check that expected string is present in HTML conversion if 'AZERTYUIOPMQ' not in html_result: message = 'Conversion to HTML Failed:\n%s' % (html_result,) active_process = self.newActiveProcess() result = ActiveResult() if message: severity = 1 result.edit(detail=message) result.edit(severity=severity) active_process.postResult(result)
def checkConversionToolAvailability(self): """ Check conversion tool (oood) is available for erp5. This script convert an odt document into HTML and try to read the returned string and find out expected string """ portal = self.getPortalObject() document_id = 'P-ERP5-TEST.Conversion.Tool.Availability-001-en.odt' document_path = 'portal_skins/erp5_administration/%s' % (document_id,) document_file = portal.restrictedTraverse(document_path) message = None severity = 0 try: temp_document = newTempOOoDocument(self, document_id, data=document_file.data, source_reference=document_id) temp_document.convertToBaseFormat() _, html_result = temp_document.convert(format='html') except ConflictError: raise except: #Which Errors should we catch ? #Transformation failed message = 'Conversion tool got unexpected error:\n%s' % ''.join(ExceptionFormatter.format_exception(*sys.exc_info())) else: #Everything goes fine, Check that expected string is present in HTML conversion if 'AZERTYUIOPMQ' not in html_result: message = 'Conversion to HTML Failed:\n%s' % (html_result,) active_process = self.newActiveProcess() result = ActiveResult() if message: severity = 1 result.edit(detail=message) result.edit(severity=severity) active_process.activateResult(result)
def _oooConvertByFormat(self, printout, content_type, extra_context, REQUEST, format, batch_mode): """ Convert the ODF document into the given format. Keyword arguments: printout -- ODF document content_type -- the content type of the printout extra_context -- extra_context including a format REQUEST -- Request object format -- requested output format batch_mode -- Disable headers overriding """ if REQUEST is not None and not format: format = REQUEST.get("format", None) filename = self.getProperty("filename") # Call refresh through cloudooo # XXX This is a temporary implementation: # Calling a webservice must be done through a WebServiceMethod # and a WebServiceConnection from Products.ERP5OOo.Document.OOoDocument import OOoServerProxy, enc, dec server_proxy = OOoServerProxy(self) extension = guess_extension(content_type).strip(".") printout = dec( server_proxy.convertFile( enc(printout), extension, extension, False, True # source_format # destination_format # zip ) ) # refresh # End of temporary implementation if not format: if REQUEST is not None and not batch_mode: REQUEST.RESPONSE.setHeader("Content-Length", len(printout)) REQUEST.RESPONSE.setHeader("Content-Type", "%s" % content_type) REQUEST.RESPONSE.setHeader( "Content-disposition", 'inline;filename="%s%s"' % (filename, guess_extension(content_type) or "") ) return printout from Products.ERP5Type.Document import newTempOOoDocument tmp_ooo = newTempOOoDocument(self, self.title_or_id()) tmp_ooo.edit( data=printout, base_data=printout, filename=self.title_or_id(), content_type=content_type, base_content_type=content_type, ) mime, data = tmp_ooo.convert(format) if REQUEST is not None and not batch_mode: REQUEST.RESPONSE.setHeader("Content-Length", len(data)) REQUEST.RESPONSE.setHeader("Content-type", mime) REQUEST.RESPONSE.setHeader("Content-disposition", 'attachment;filename="%s.%s"' % (filename, format)) return str(data)
def pt_render(self, source=0, extra_context={}): # Get request request = extra_context.get('REQUEST', self.REQUEST) # Get parent object (the one to render this template on) here = getattr(self, 'aq_parent', None) if here is None: # This is a system error raise ValueError, 'Can not render a template without a parent acquisition context' # Retrieve master document ooo_document = None # If script is setting, call it if self.ooo_script_name: ooo_script = getattr(here, self.ooo_script_name) ooo_document = ooo_script(self.ooo_stylesheet) else: ooo_document = getattr(here, self.ooo_stylesheet) format = request.get('format') try: # If style is dynamic, call it if getattr(aq_base(ooo_document), '__call__', None) is not None: request.set('format', None) ooo_document = ooo_document() finally: request.set('format', format) # Create a new builder instance ooo_builder = OOoBuilder(ooo_document) # Pass builder instance as extra_context extra_context['ooo_builder'] = ooo_builder # And render page template doc_xml = ZopePageTemplate.pt_render(self, source=source, extra_context=extra_context) if isinstance(doc_xml, unicode): doc_xml = doc_xml.encode('utf-8') # Replace the includes (doc_xml,attachments_dict) = self.renderIncludes(here, doc_xml, extra_context, request) try: default_styles_text = ooo_builder.extract('styles.xml') except AttributeError: default_styles_text = None # Add the associated files for dir_name, document_dict in attachments_dict.iteritems(): # Special case : the document is an OOo one if document_dict['doc_type'].startswith(self._OOo_content_type_root) or \ document_dict['doc_type'].startswith(self._ODF_content_type_root): ooo_builder.addFileEntry(full_path=dir_name, media_type=document_dict['doc_type']) ooo_builder.addFileEntry(full_path=dir_name + '/content.xml', media_type='text/xml', content=document_dict['document']) styles_text = default_styles_text if document_dict.has_key('stylesheet') and document_dict['stylesheet']: styles_text = document_dict['stylesheet'] if styles_text: ooo_builder.addFileEntry(full_path=dir_name + '/styles.xml', media_type='text/xml', content=styles_text) else: # Generic case ooo_builder.addFileEntry(full_path=dir_name, media_type=document_dict['doc_type'], content=document_dict['document']) # Replace content.xml in master openoffice template ooo_builder.replace(self.ooo_xml_file_id, doc_xml) # Old templates correction try: self.OLE_documents_zipstring except AttributeError: self.OLE_documents_zipstring = None # Convert if necessary opts = extra_context.get("options", dict()) # Get batch_mode batch_mode = opts.get('batch_mode', None) # If the file has embedded OLE documents, restore it if self.OLE_documents_zipstring: additional_builder = OOoBuilder( self.OLE_documents_zipstring ) for name in additional_builder.getNameList(): ooo_builder.replace(name, additional_builder.extract(name) ) # Update the META informations ooo_builder.updateManifest() # Produce final result if batch_mode: ooo = ooo_builder.render() else: ooo = ooo_builder.render(name=self.title or self.id, source=source) if DevelopmentMode: # Validate XML in development mode from Products.ERP5OOo.tests.utils import Validator err_list = Validator().validate(ooo) if err_list: LOG('ERP5OOo', PROBLEM, 'Validation of %s failed:\n%s' % (self.getId(), ''.join(err_list))) extension = None mimetype = ooo_builder.getMimeType() mimetypes_registry = self.getPortalObject().mimetypes_registry mimetype_object_list = mimetypes_registry.lookup(mimetype) for mimetype_object in mimetype_object_list: if mimetype_object.extensions: extension = mimetype_object.extensions[0] break elif mimetype_object.globs: extension = mimetype_object.globs.strip('*.') break if extension: filename = '%s.%s' % (self._getFileName(), extension) else: filename = self._getFileName() from Products.ERP5Type.Document import newTempOOoDocument tmp_ooo = newTempOOoDocument(self, self.title_or_id()) tmp_ooo.edit(data=ooo, filename=filename, content_type=mimetype,) format = opts.get('format', request.get('format', None)) if format: # Performance improvement: # We already have OOo format data, so we do not need to call # convertToBaseFormat(), but just copy it into base_data property. tmp_ooo.setBaseData(ooo) tmp_ooo.setBaseContentType(mimetype) if request is not None and not batch_mode and not source: return tmp_ooo.index_html(REQUEST=request, RESPONSE=request.RESPONSE, format=format) return tmp_ooo.convert(format)[1]
clean_id = clean_id[1:] while len(clean_id) > 0 and not clean_id[-1].isalnum(): clean_id = clean_id[:-1] return clean_id # if the file is not an open office format, try to convert it using oood # FIXME: use portal_transforms content_type = 'unknown' if hasattr(import_file, 'headers'): content_type = import_file.headers.get('Content-Type', '') if not (content_type.startswith('application/vnd.sun.xml') or content_type.startswith('application/vnd.oasis.opendocument')): from Products.ERP5Type.Document import newTempOOoDocument tmp_ooo = newTempOOoDocument(context, "_") tmp_ooo.edit(data=import_file.read(), content_type=content_type) tmp_ooo.convertToBaseFormat() ignored, import_file_content = tmp_ooo.convert('ods') parser.openFromString(str(import_file_content)) else: parser.openFile(import_file) # Extract tables from the speadsheet file filename = parser.getFilename() spreadsheet_list = parser.getSpreadsheetsMapping(no_empty_lines=True) for table_name in spreadsheet_list.keys(): sheet = spreadsheet_list[table_name] if not sheet: continue
'''Returns all possible document conversions from the `base_content_type`. ''' from Products.ERP5Type.Document import newTempOOoDocument td = newTempOOoDocument(context, 'testOOo') td.edit(base_content_type=base_content_type, base_data='not empty') return [('', '')] + td.getTargetFormatItemList()
def pt_render(self, source=0, extra_context={}): if source: return ZopePageTemplate.pt_render(self, source=source, extra_context=extra_context) # Get request request = extra_context.get('REQUEST', self.REQUEST) # Get parent object (the one to render this template on) here = getattr(self, 'aq_parent', None) if here is None: # This is a system error raise ValueError, 'Can not render a template without a parent acquisition context' # Retrieve master document ooo_document = None # If script is setting, call it if self.ooo_script_name: ooo_script = getattr(here, self.ooo_script_name) ooo_document = ooo_script(self.ooo_stylesheet) else: ooo_document = getattr(here, self.ooo_stylesheet) format = request.get('format') try: # If style is dynamic, call it if getattr(aq_base(ooo_document), '__call__', None) is not None: request.set('format', None) ooo_document = ooo_document() finally: request.set('format', format) # Create a new builder instance ooo_builder = OOoBuilder(ooo_document) # Pass builder instance as extra_context extra_context['ooo_builder'] = ooo_builder # And render page template doc_xml = ZopePageTemplate.pt_render(self, source=source, extra_context=extra_context) if isinstance(doc_xml, unicode): doc_xml = doc_xml.encode('utf-8') # Replace the includes (doc_xml, attachments_dict) = self.renderIncludes(here, doc_xml, extra_context, request) try: default_styles_text = ooo_builder.extract('styles.xml') except AttributeError: default_styles_text = None # Add the associated files for dir_name, document_dict in attachments_dict.iteritems(): # Special case : the document is an OOo one if document_dict['doc_type'].startswith(self._OOo_content_type_root) or \ document_dict['doc_type'].startswith(self._ODF_content_type_root): ooo_builder.addFileEntry(full_path=dir_name, media_type=document_dict['doc_type']) ooo_builder.addFileEntry(full_path=dir_name + '/content.xml', media_type='text/xml', content=document_dict['document']) styles_text = default_styles_text if document_dict.has_key( 'stylesheet') and document_dict['stylesheet']: styles_text = document_dict['stylesheet'] if styles_text: ooo_builder.addFileEntry(full_path=dir_name + '/styles.xml', media_type='text/xml', content=styles_text) else: # Generic case ooo_builder.addFileEntry(full_path=dir_name, media_type=document_dict['doc_type'], content=document_dict['document']) # Replace content.xml in master openoffice template ooo_builder.replace(self.ooo_xml_file_id, doc_xml) # Old templates correction try: self.OLE_documents_zipstring except AttributeError: self.OLE_documents_zipstring = None # Convert if necessary opts = extra_context.get("options", {}) # Get batch_mode batch_mode = opts.get('batch_mode', None) # If the file has embedded OLE documents, restore them if self.OLE_documents_zipstring: additional_builder = OOoBuilder(self.OLE_documents_zipstring) for name in additional_builder.getNameList(): if name not in ('META-INF/manifest.xml', ): # We don't replace manifest ooo_builder.replace(name, additional_builder.extract(name)) # Update the META information ooo_builder.updateManifest() # Produce final result if batch_mode: ooo = ooo_builder.render() else: ooo = ooo_builder.render(name=self.title or self.id, source=source) extension = None mimetype = ooo_builder.getMimeType() mimetypes_registry = self.getPortalObject().mimetypes_registry mimetype_object_list = mimetypes_registry.lookup(mimetype) for mimetype_object in mimetype_object_list: if mimetype_object.extensions: extension = mimetype_object.extensions[0] break elif mimetype_object.globs: extension = mimetype_object.globs.strip('*.') break if extension: filename = '%s.%s' % (self._getFileName(), extension) else: filename = self._getFileName() from Products.ERP5Type.Document import newTempOOoDocument tmp_ooo = newTempOOoDocument(self, self.title_or_id()) tmp_ooo.edit( data=ooo, filename=filename, content_type=mimetype, ) format = opts.get('format', request.get('format', None)) if format: # Performance improvement: # We already have OOo format data, so we do not need to call # convertToBaseFormat(), but just copy it into base_data property. tmp_ooo.setBaseData(ooo) tmp_ooo.setBaseContentType(mimetype) if request is not None and not batch_mode and not source: return tmp_ooo.index_html(REQUEST=request, RESPONSE=request.RESPONSE, format=format) return tmp_ooo.convert(format)[1]
while len(clean_id) > 0 and not clean_id[0].isalnum(): clean_id = clean_id[1:] while len(clean_id) > 0 and not clean_id[-1].isalnum(): clean_id = clean_id[:-1] return clean_id # if the file is not an open office format, try to convert it using oood # FIXME: use portal_transforms content_type = 'unknown' if hasattr(import_file, 'headers'): content_type = import_file.headers.get('Content-Type', '') if not (content_type.startswith('application/vnd.sun.xml') or content_type.startswith('application/vnd.oasis.opendocument')): from Products.ERP5Type.Document import newTempOOoDocument tmp_ooo = newTempOOoDocument(context, "_") tmp_ooo.edit(data=import_file.read(), content_type=content_type) tmp_ooo.convertToBaseFormat() ignored, import_file_content = tmp_ooo.convert('ods') parser.openFromString(str(import_file_content)) else: parser.openFile(import_file) # Extract tables from the speadsheet file filename = parser.getFilename() spreadsheet_list = parser.getSpreadsheetsMapping(no_empty_lines=True) for table_name in spreadsheet_list.keys(): sheet = spreadsheet_list[table_name]