def __call__(self): self.request.response.setHeader("Content-type", "application/pdf") self.request.response.setHeader( "Content-disposition", 'inline;filename="' + removeSecurityProxy(self.report.report_type) + "_" + removeSecurityProxy(self.report.start_date).strftime("%Y-%m-%d") + '.pdf"') session = Session() report = session.query(domain.Report).get(self.report.report_id) d = dict([(f.file_title, f.file_data) for f in report.attached_files]) if "pdf" not in d.keys(): params = {} params["body_text"] = self.cleanupText() openofficepath = getUtility(IOpenOfficeConfig).getPath() renderer = Renderer(self.odt_file, params, self.tempFileName, pythonWithUnoPath=openofficepath) renderer.run() f = open(self.tempFileName, "rb") doc = f.read() f.close() os.remove(self.tempFileName) attached_file = domain.AttachedFile() attached_file.file_title = "pdf" attached_file.file_data = doc attached_file.language = report.language report.attached_files.append(attached_file) notify(ObjectCreatedEvent(attached_file)) session.add(report) session.commit() return doc else: return d["pdf"].__str__()
def __call__(self): self.request.response.setHeader( 'Content-type', 'application/vnd.oasis.opendocument.text') self.request.response.setHeader( 'Content-disposition', 'inline;filename="' + removeSecurityProxy(self.report.report_type) + "_" + removeSecurityProxy(self.report.start_date).strftime('%Y-%m-%d') + '.odt"') session = Session() report = session.query(domain.Report).get(self.report.report_id) d = dict([(f.file_title, f.file_data) for f in report.attached_files]) if "odt" not in d.keys(): params = {} params['body_text'] = self.cleanupText() renderer = Renderer(self.odt_file, params, self.tempFileName) renderer.run() f = open(self.tempFileName, 'rb') doc = f.read() f.close() os.remove(self.tempFileName) attached_file = domain.AttachedFile() attached_file.file_title = "odt" attached_file.file_data = doc attached_file.language = report.language report.attached_files.append(attached_file) notify(ObjectCreatedEvent(attached_file)) session.add(report) session.commit() return doc else: return d["odt"].__str__()
def documentData(self, cached=False): """Either generate ODT/PDF doc or retrieve from attached files of the content item. Cached should only be True for content items that are immutable eg. reports.""" #TODO : Either generate a hash of a mutable content item and store it # with the odt/pdf doc or track changes to a doc # Add caching by state. items in terminal states do not change tempFileName = os.path.dirname( __file__) + "/tmp/%f.%s" % (time.time(), self.document_type) if cached: session = Session() d = [f.file_title for f in self.document.attached_files] if self.document_type not in d: file_type = session.query(domain.AttachedFileType) \ .filter(domain.AttachedFileType \ .attached_file_type_name == "system") \ .first() if file_type is None: file_type = domain.AttachedFileType() file_type.attached_file_type_name = "system" file_type.language = self.document.language session.add(file_type) session.flush() attached_file = domain.AttachedFile() attached_file.file_title = self.document_type attached_file.file_data = self.generateDoc() attached_file.language = self.document.language attached_file.type = file_type self.document.attached_files.append(attached_file) session.add(self.document) session.flush() #!+ REPORTS(miano, apr-2011) Anonymous users may prompt #the storage of a report if it hasn't been stored before. #Actions that are executed when an objectcreatedevent #is triggered may require a principal in the #request eg. auditing. Report attachments are not displayed in #listings or any other place so not triggering the event #shouldn't do any harm. #notify(ObjectCreatedEvent(attached_file)) for f in self.document.attached_files: if f.file_title == self.document_type: self.setHeader(self.document_type) return f.file_data.__str__() #If file is not found try: return self.error_template() except ComponentLookupError: return u"An error occured during ODT/PDF generation." else: return self.generateDoc()
def documentData(self, cached=False): """Either generate ODT/PDF doc or retrieve from attached files of the content item. Cached should only be True for content items that are immutable eg. reports.""" #TODO : Either generate a hash of a mutable content item and store it # with the odt/pdf doc or track changes to a doc tempFileName = os.path.dirname( __file__) + "/tmp/%f.%s" % (time.time(), self.document_type) if cached: session = Session() d = [f.file_title for f in self.document.attached_files] if self.document_type not in d: file_type = session.query(domain.AttachedFileType) \ .filter(domain.AttachedFileType \ .attached_file_type_name == "system") \ .first() if file_type is None: file_type = domain.AttachedFileType() file_type.attached_file_type_name = "system" file_type.language = self.document.language session.add(file_type) session.flush() attached_file = domain.AttachedFile() attached_file.file_title = self.document_type attached_file.file_data = self.generateDoc() attached_file.language = self.document.language attached_file.type = file_type self.document.attached_files.append(attached_file) session.add(self.document) session.commit() notify(ObjectCreatedEvent(attached_file)) for f in self.document.attached_files: if f.file_title == self.document_type: self.setHeader(self.document_type) return f.file_data.__str__() #If file is not found return self.error_template() else: self.setHeader(self.document_type) return self.generateDoc()