def onGenerate(self): """ Slot raised to initiate the certificate generation process. """ self._notifBar.clear() #Validate records records = self.personFKMapper.entities() if records == None: self._notifBar.insertErrorNotification(QApplication.translate("PersonDocumentGenerator", \ "Please select at least one person record")) return if self._docTemplatePath == "": self._notifBar.insertErrorNotification(QApplication.translate("PersonDocumentGenerator", \ "Please select a document template to use")) return documentNamingAttrs = self.lstDocNaming.selectedMappings() if self.chkUseOutputFolder.checkState() == Qt.Checked and len(documentNamingAttrs) == 0: self._notifBar.insertErrorNotification(QApplication.translate("PersonDocumentGenerator", \ "Please select at least one field for naming the output document.")) return #Set output file properties if self.rbExpImage.isChecked(): outputMode = DocumentGenerator.Image fileExtension = self.cboImageType.currentText() saveAsText = "Image File" else: outputMode = DocumentGenerator.PDF fileExtension = "pdf" saveAsText = "PDF File" #Show save file dialog if not using output folder if self.chkUseOutputFolder.checkState() == Qt.Unchecked: docDir = "" if self._outputFilePath != "": fileInfo = QFileInfo(self._outputFilePath) docDir = fileInfo.dir().path() self._outputFilePath = QFileDialog.getSaveFileName(self,QApplication.translate("PersonDocumentGenerator", \ "Save Document"), \ docDir, \ "{0} (*.{1})".format(QApplication.translate("PersonDocumentGenerator", \ saveAsText), \ fileExtension)) if self._outputFilePath == "": self._notifBar.insertErrorNotification(QApplication.translate("PersonDocumentGenerator", \ "Process aborted. No output file was specified.")) return #Include extension in file name self._outputFilePath = self._outputFilePath #+ "." + fileExtension else: #Multiple files to be generated. pass docGenerator = DocumentGenerator(self._iface,self) #Apply cell formatters for naming output files docGenerator.setAttrValueFormatters(self.personFKMapper.cellFormatters()) entityFieldName = "id" #Iterate through the selected records progressDlg = QProgressDialog(self) progressDlg.setMaximum(len(records)) try: for i,record in enumerate(records): progressDlg.setValue(i) if progressDlg.wasCanceled(): break #User-defined location if self.chkUseOutputFolder.checkState() == Qt.Unchecked: status,msg = docGenerator.run(self._docTemplatePath,entityFieldName,record.id,outputMode, \ filePath = self._outputFilePath) #Output folder location using custom naming else: status,msg = docGenerator.run(self._docTemplatePath,entityFieldName,record.id,outputMode, \ dataFields = documentNamingAttrs,fileExtension = fileExtension, \ dbmodel = self._dbModel) if not status: result = QMessageBox.warning(self, QApplication.translate("PersonDocumentGenerator","Document Generate Error"), msg, QMessageBox.Ignore|QMessageBox.Abort) if result == QMessageBox.Abort: progressDlg.close() return else: progressDlg.setValue(len(records)) QMessageBox.information(self, QApplication.translate("PersonDocumentGenerator","Document Generation Complete"), QApplication.translate("PersonDocumentGenerator","Document generation has successfully completed.") ) except Exception as ex: QMessageBox.information(self,"STDM",QApplication.translate("PersonDocumentGenerator","Error Generating documents %s"%(str(ex.message)))) return #Reset UI self.reset()
def onGenerate(self): """ Slot raised to initiate the certificate generation process. """ self._notifBar.clear() #Validate records records = self.personFKMapper.entities() if records == None: self._notifBar.insertErrorNotification(QApplication.translate("PersonDocumentGenerator", \ "Please select at least one person record")) return if self._docTemplatePath == "": self._notifBar.insertErrorNotification(QApplication.translate("PersonDocumentGenerator", \ "Please select a document template to use")) return documentNamingAttrs = self.lstDocNaming.selectedMappings() if self.chkUseOutputFolder.checkState() == Qt.Checked and len(documentNamingAttrs) == 0: self._notifBar.insertErrorNotification(QApplication.translate("PersonDocumentGenerator", \ "Please select at least one field for naming the output document.")) return #Set output file properties if self.rbExpImage.isChecked(): outputMode = DocumentGenerator.Image fileExtension = self.cboImageType.currentText() saveAsText = "Image File" else: outputMode = DocumentGenerator.PDF fileExtension = "pdf" saveAsText = "PDF File" #Show save file dialog if not using output folder if self.chkUseOutputFolder.checkState() == Qt.Unchecked: docDir = "" if self._outputFilePath != "": fileInfo = QFileInfo(self._outputFilePath) docDir = fileInfo.dir().path() self._outputFilePath = QFileDialog.getSaveFileName(self,QApplication.translate("PersonDocumentGenerator", \ "Save Document"), \ docDir, \ "{0} (*.{1})".format(QApplication.translate("PersonDocumentGenerator", \ saveAsText), \ fileExtension)) if self._outputFilePath == "": self._notifBar.insertErrorNotification(QApplication.translate("PersonDocumentGenerator", \ "Process aborted. No output file was specified.")) return #Include extension in file name self._outputFilePath = self._outputFilePath #+ "." + fileExtension else: #Multiple files to be generated. pass docGenerator = DocumentGenerator(self._iface,self) #Apply cell formatters for naming output files docGenerator.setAttrValueFormatters(self.personFKMapper.cellFormatters()) entityFieldName = "id" #Iterate through the selected records progressDlg = QProgressDialog(self) progressDlg.setMaximum(len(records)) for i,record in enumerate(records): progressDlg.setValue(i) if progressDlg.wasCanceled(): break #User-defined location if self.chkUseOutputFolder.checkState() == Qt.Unchecked: status,msg = docGenerator.run(self._docTemplatePath,entityFieldName,record.id,outputMode, \ filePath = self._outputFilePath) #Output folder location using custom naming else: status,msg = docGenerator.run(self._docTemplatePath,entityFieldName,record.id,outputMode, \ dataFields = documentNamingAttrs,fileExtension = fileExtension, \ dbmodel = Farmer) if not status: result = QMessageBox.warning(self, QApplication.translate("PersonDocumentGenerator","Document Generate Error"), msg, QMessageBox.Ignore|QMessageBox.Abort) if result == QMessageBox.Abort: progressDlg.close() return else: progressDlg.setValue(len(records)) QMessageBox.information(self, QApplication.translate("PersonDocumentGenerator","Document Generation Complete"), QApplication.translate("PersonDocumentGenerator","Document generation has successfully completed.") ) #Reset UI self.reset()