class GenerateSendThread(QtCore.QThread):

    def __init__(self):
        super(GenerateSendThread, self).__init__()

    def __del__(self):
        self.wait()

    def _get_info(self, save_folder, cert_data, clients, responsible):
        self.save_folder = save_folder
        self.cert_data = cert_data
        self.clients = clients
        self.responsible = responsible

    def run(self):
        self.mailer = Mailer()
        self.mailer.connect()
        self.emit(QtCore.SIGNAL("update"),1,0)

        n = 1
        for client in self.clients:
            cursor.execute("SELECT name,email FROM clients WHERE id=?",str(client[1]))
            client_data = cursor.fetchone()

            filepath = os.path.join(unicode(self.save_folder),u''.join(i for i in \
                                                                unicode(client_data[0]) \
                                                                if ord(i)<128)\
                                                                .upper()\
                                                                .replace(" ",""))
            filepath += ".pdf"

            self.cert_data["name"] = unicode(client_data[0]).upper()
            self.cert_data["register"] = unicode(client_data[1])

            self.emit(QtCore.SIGNAL("update"),2,n)
            generate_certificate(self.save_folder, self.cert_data)
            self.mailer.send_certificate(filepath,unicode(client_data[1]))
            n+=1

        filepath = os.path.join(unicode(self.save_folder),u"responsible.pdf")
        self.cert_data["name"] = unicode(self.responsible[1]).upper()
        self.cert_data["register"] = unicode(self.responsible[4]).upper()
        self.emit(QtCore.SIGNAL("update"),3,0)
        generate_certificate_responsible(self.save_folder, self.cert_data)
        self.mailer.send_certificate(unicode(filepath),unicode(self.responsible[3]))

        self.emit(QtCore.SIGNAL("update"),4,0)
        self.mailer.quit()
class GenerateSendThread(QtCore.QThread):
    """
        A thread for dealing with certificates generation and mailing.
    """

    def __init__(self):
        """
            Setup widgets.
        """
        super(GenerateSendThread, self).__init__()

    def __del__(self):
        self.wait()

    def _get_info(self, save_folder, cert_data, clients, responsible):
        """
            Gets info to generate and send certificates.
        """
        self.save_folder = save_folder
        self.cert_data = cert_data
        self.clients = clients
        self.responsible = responsible

    def run(self):
        """
            Connects with the mailer, generates and send the certificates.
        """

        # connects to the mailer and updates progress bar
        self.emit(QtCore.SIGNAL("update"), 1, 0)
        self.mailer = Mailer()
        r = self.mailer.connect()

        # Verifies authentication
        if r == 1:
            n = 1
            for client in self.clients:
                # Get client info
                cursor.execute(
                    "SELECT name,email FROM clients WHERE id=?",
                    str(client[1])
                )
                client_data = cursor.fetchone()

                filepath = os.path.join(
                    unicode(self.save_folder),
                    u''.join(i for i in unicode(client_data[0])
                             if ord(i) < 128)
                    .upper()
                    .replace(" ", "")
                )
                filepath += ".pdf"

                self.cert_data["name"] = unicode(client_data[0]).upper()
                self.cert_data["register"] = unicode(client_data[1])

                # Updates progress bar and generate and send certificate
                self.emit(QtCore.SIGNAL("update"), 2, n)
                generate_certificate(self.save_folder, self.cert_data)
                self.mailer.send_certificate(filepath, unicode(client_data[1]))
                n += 1

            # Gets responsible info
            filepath = os.path.join(
                unicode(self.save_folder),
                u"responsible.pdf"
            )
            self.cert_data["name"] = unicode(self.responsible[1]).upper()
            self.cert_data["register"] = unicode(self.responsible[4]).upper()

            # Updates progress bar and generate and send certificate
            self.emit(QtCore.SIGNAL("update"), 3, 0)
            generate_certificate(self.save_folder, self.cert_data, True)
            self.mailer.send_certificate(
                unicode(filepath),
                unicode(self.responsible[3])
            )

            self.emit(QtCore.SIGNAL("update"), 4, 0)

            # Quits mailer
            self.mailer.quit()
        else:
            self.emit(QtCore.SIGNAL("error_raised()"))
예제 #3
0
class CertificatesWidget(QtGui.QWidget):
    def __init__(self):
        super(CertificatesWidget, self).__init__()
        self.Config = ConfigParser.ConfigParser()
        self.Config.read("institution.ini")

        cursor.execute("SELECT * FROM events")
        self.events = cursor.fetchall()

        cursor.execute("SELECT * FROM signatures")
        self.signatures = cursor.fetchall()

        self.mainLayout = QtGui.QVBoxLayout()
        self.listLayout = QtGui.QHBoxLayout()
        self.btnsLayout = QtGui.QVBoxLayout()
        self.respLayout = QtGui.QVBoxLayout()
        self.instLayout = QtGui.QVBoxLayout()
        self.combosLayoutH = QtGui.QHBoxLayout()
        self.generateBtnsLayout = QtGui.QHBoxLayout()

        self.titleLabel = QtGui.QLabel("Certificados", self)
        self.titleLabel.setFont(titleFont)

        self.addBtn = QtGui.QPushButton("Adicionar")
        self.addBtn.clicked.connect(self.add_client)
        self.removeBtn = QtGui.QPushButton("Remover")
        self.removeBtn.clicked.connect(self.remove_client)
        self.btnsLayout.addWidget(self.addBtn)
        self.btnsLayout.addWidget(self.removeBtn)
        self.btnsLayout.addStretch()

        self.eventsListName = QtGui.QLabel("Selecione um evento:", self)
        self.eventsList = QtGui.QComboBox()
        for event in self.events:
            self.eventsList.addItem(event[1])
        self.eventsList.currentIndexChanged.connect(self.load_list)

        self.subscriptionListName = QtGui.QLabel("Inscritos", self)
        self.subscriptionList = QtGui.QListWidget()
        self.load_list()

        self.responsibleListName = QtGui.QLabel(
            u"Selecione um responsável/ministrante:", self)
        self.responsibleList = QtGui.QComboBox()
        self.institutionListName = QtGui.QLabel(u"Assinatura da instituição:",
                                                self)
        self.institutionList = QtGui.QComboBox()
        for sig in self.signatures:
            self.responsibleList.addItem(str(sig[1]))
            self.institutionList.addItem(str(sig[1]))

        self.errorMsg = QtGui.QLabel("", self)
        self.errorMsg.setStyleSheet("color: red; font-weight: bold;")

        self.generateBtn = QtGui.QPushButton("Gerar!")
        self.generateBtn.clicked.connect(self.generate)
        self.generateSendBtn = QtGui.QPushButton("Gerar e enviar por email!")
        self.generateSendBtn.clicked.connect(self.generate_send)
        self.generateBtnsLayout.addWidget(self.generateBtn)
        self.generateBtnsLayout.addWidget(self.generateSendBtn)

        self.listLayout.addWidget(self.subscriptionList)
        self.listLayout.addLayout(self.btnsLayout)

        self.respLayout.addWidget(self.responsibleListName)
        self.respLayout.addWidget(self.responsibleList)

        self.instLayout.addWidget(self.institutionListName)
        self.instLayout.addWidget(self.institutionList)

        self.combosLayoutH.addLayout(self.respLayout)
        self.combosLayoutH.addLayout(self.instLayout)

        self.mainLayout.addWidget(self.titleLabel)
        self.mainLayout.addWidget(self.eventsListName)
        self.mainLayout.addWidget(self.eventsList)
        self.mainLayout.addWidget(self.subscriptionListName)
        self.mainLayout.addLayout(self.listLayout)
        self.mainLayout.addLayout(self.combosLayoutH)
        self.mainLayout.addWidget(self.errorMsg)
        self.mainLayout.addLayout(self.generateBtnsLayout)

        self.setLayout(self.mainLayout)

    def load_list(self):
        if self.events:
            cursor.execute(
                "SELECT id,client_id FROM subscriptions WHERE event_id=?",
                str(self.events[self.eventsList.currentIndex()][0]))
            self.clients = cursor.fetchall()
        else:
            self.clients = []

        self.subscriptionList.clear()
        for client in self.clients:
            cursor.execute("SELECT name FROM clients WHERE id=?",
                           str(client[1]))
            client_name = cursor.fetchone()[0]
            self.subscriptionList.addItem(str(client_name))

    def add_client(self):
        try:
            event_id = self.events[self.eventsList.currentIndex()][0]
            self.add_client_widget = AddClientDialog(self, event_id)
            self.add_client_widget.show()
        except IndexError:
            self.errorMsg.setText("Selecione um evento existente!")

    def remove_client(self):
        try:
            subscription_id = self.clients[
                self.subscriptionList.currentRow()][0]
            choice = QtGui.QMessageBox.question(
                self, u"Apagar inscrição",
                u"Tem certeza que deseja apagar esta inscrição?",
                QtGui.QMessageBox.Yes | QtGui.QMessageBox.No)
            if choice == QtGui.QMessageBox.Yes:
                cursor.execute("DELETE FROM subscriptions WHERE id=?",
                               str(subscription_id))
                conn.commit()
            else:
                pass
            self.load_list()
        except IndexError:
            self.errorMsg.setText("Selecione um cliente existente!")

    def generate(self):
        if len(self.clients) == 0:
            self.errorMsg.setText("Primeiro inscreva clientes!")
        else:
            self.save_folder = QtGui.QFileDialog.getExistingDirectory(
                None, "Salvar em")

            self.cert_data = {
                "event":
                self.events[self.eventsList.currentIndex()][1].upper(),
                "start_date":
                self.events[self.eventsList.currentIndex()][2],
                "end_date":
                self.events[self.eventsList.currentIndex()][3],
                "hours":
                self.events[self.eventsList.currentIndex()][4],
                "content":
                self.events[self.eventsList.currentIndex()][5].upper(),
                "responsible_sig":
                self.signatures[self.responsibleList.currentIndex()][0],
                "institution_sig":
                self.signatures[self.institutionList.currentIndex()][0],
                "role":
                self.signatures[self.institutionList.currentIndex()][2],
                "institution":
                str(self.Config.get("Main", "Name")).upper(),
                "inst_register":
                str(self.Config.get("Main", "ID"))
            }

            for client in self.clients:
                cursor.execute("SELECT name,register FROM clients WHERE id=?",
                               str(client[1]))
                client_data = cursor.fetchone()
                self.cert_data["name"] = str(client_data[0]).upper()
                self.cert_data["register"] = str(client_data[1])
                generate_certificate(self.save_folder, self.cert_data)

            cursor.execute(
                "SELECT * FROM signatures WHERE id=?",
                str(self.signatures[self.responsibleList.currentIndex()][0]))
            responsible = cursor.fetchone()
            self.cert_data["name"] = str(responsible[1]).upper()
            self.cert_data["register"] = str(responsible[4]).upper()
            generate_certificate_responsible(self.save_folder, self.cert_data)

    def generate_send(self):
        if len(self.clients) == 0:
            self.errorMsg.setText("Primeiro inscreva clientes!")
        else:
            self.generate()
            self.mailer = Mailer()
            self.mailer.connect()
            for client in self.clients:
                cursor.execute("SELECT name,email FROM clients WHERE id=?",
                               str(client[1]))
                client_data = cursor.fetchone()

                filepath = self.save_folder + "/" + client_data[0].replace(
                    " ", "").upper() + ".pdf"
                self.mailer.send_certificate(filepath, client_data[1])

            cursor.execute(
                "SELECT name,email FROM signatures WHERE id=?",
                self.signatures[self.responsibleList.currentIndex()][0])
            responsible = cursor.fetchone()
            filepath = self.save_folder + "/responsible.pdf"
            self.mailer.send_certificate(filepath, responsible[3])

            self.mailer.quit()
예제 #4
0
class GenerateSendThread(QtCore.QThread):
    """
        A thread for dealing with certificates generation and mailing.
    """

    def __init__(self):
        """
            Setup widgets.
        """
        super(GenerateSendThread, self).__init__()

    def __del__(self):
        self.wait()

    def _get_info(self, save_folder, cert_data, clients, responsible):
        """
            Gets info to generate and send certificates.
        """
        self.save_folder = save_folder
        self.cert_data = cert_data
        self.clients = clients
        self.responsible = responsible

    def run(self):
        """
            Connects with the mailer, generates and send the certificates.
        """

        # connects to the mailer and updates progress bar
        self.emit(QtCore.SIGNAL("update"), 1, 0)
        self.mailer = Mailer()
        r = self.mailer.connect()

        # Verifies authentication
        if r == 1:
            n = 1
            for client in self.clients:
                # Get client info
                cursor.execute(
                    "SELECT name,email FROM clients WHERE id=?",
                    (str(client[1]),)
                )
                client_data = cursor.fetchone()

                filepath = os.path.join(
                    unicode(self.save_folder),
                    u''.join(i for i in unicode(client_data[0])
                             if ord(i) < 128)
                    .upper()
                    .replace(" ", "")
                )
                filepath += ".pdf"

                self.cert_data["name"] = unicode(client_data[0]).upper()
                self.cert_data["register"] = unicode(client_data[1])

                # Updates progress bar and generate and send certificate
                self.emit(QtCore.SIGNAL("update"), 2, n)
                generate_certificate(self.save_folder, self.cert_data)
                self.mailer.send_certificate(filepath, unicode(client_data[1]))
                n += 1

            # Gets responsible info
            filepath = os.path.join(
                unicode(self.save_folder),
                u"responsible.pdf"
            )
            self.cert_data["name"] = unicode(self.responsible[1]).upper()
            self.cert_data["register"] = unicode(self.responsible[4]).upper()

            # Updates progress bar and generate and send certificate
            self.emit(QtCore.SIGNAL("update"), 3, 0)
            generate_certificate(self.save_folder, self.cert_data, True)
            self.mailer.send_certificate(
                unicode(filepath),
                unicode(self.responsible[3])
            )

            self.emit(QtCore.SIGNAL("update"), 4, 0)

            # Quits mailer
            self.mailer.quit()
        else:
            self.emit(QtCore.SIGNAL("error_raised()"))
class CertificatesWidget(QtGui.QWidget):

    def __init__(self):
        super(CertificatesWidget, self).__init__()
        self.Config = ConfigParser.ConfigParser()
        self.Config.read("institution.ini")

        cursor.execute("SELECT * FROM events")
        self.events = cursor.fetchall()

        cursor.execute("SELECT * FROM signatures")
        self.signatures = cursor.fetchall()

        self.mainLayout = QtGui.QVBoxLayout()
        self.listLayout = QtGui.QHBoxLayout()
        self.btnsLayout = QtGui.QVBoxLayout()
        self.respLayout = QtGui.QVBoxLayout()
        self.instLayout = QtGui.QVBoxLayout()
        self.combosLayoutH = QtGui.QHBoxLayout()
        self.generateBtnsLayout = QtGui.QHBoxLayout()

        self.titleLabel = QtGui.QLabel(u"Certificados",self)
        self.titleLabel.setFont(titleFont)

        self.addBtn = QtGui.QPushButton(u"Adicionar")
        self.addBtn.clicked.connect(self.add_client)
        self.removeBtn = QtGui.QPushButton(u"Remover")
        self.removeBtn.clicked.connect(self.remove_client)
        self.btnsLayout.addWidget(self.addBtn)
        self.btnsLayout.addWidget(self.removeBtn)
        self.btnsLayout.addStretch()

        self.eventsListName = QtGui.QLabel(u"Selecione um evento:", self)
        self.eventsList = QtGui.QComboBox()
        for event in self.events:
            self.eventsList.addItem(unicode(event[1]))
        self.eventsList.currentIndexChanged.connect(self.load_list)

        self.subscriptionListName = QtGui.QLabel(u"Inscritos", self)
        self.subscriptionList = QtGui.QListWidget()
        self.load_list()

        self.responsibleListName = QtGui.QLabel(u"Selecione um responsável/ministrante:",self)
        self.responsibleList = QtGui.QComboBox()
        self.institutionListName = QtGui.QLabel(u"Assinatura da instituição:", self)
        self.institutionList = QtGui.QComboBox()
        for sig in self.signatures:
            self.responsibleList.addItem(unicode(sig[1]))
            self.institutionList.addItem(unicode(sig[1]))

        self.errorMsg = QtGui.QLabel(u"",self)
        self.errorMsg.setStyleSheet("color: red; font-weight: bold;")

        self.generateBtn = QtGui.QPushButton(u"Gerar!")
        self.generateBtn.clicked.connect(self.generate)
        self.generateSendBtn = QtGui.QPushButton(u"Gerar e enviar por email!")
        self.generateSendBtn.clicked.connect(self.generate_send)
        self.generateBtnsLayout.addWidget(self.generateBtn)
        self.generateBtnsLayout.addWidget(self.generateSendBtn)

        self.listLayout.addWidget(self.subscriptionList)
        self.listLayout.addLayout(self.btnsLayout)

        self.respLayout.addWidget(self.responsibleListName)
        self.respLayout.addWidget(self.responsibleList)

        self.instLayout.addWidget(self.institutionListName)
        self.instLayout.addWidget(self.institutionList)

        self.combosLayoutH.addLayout(self.respLayout)
        self.combosLayoutH.addLayout(self.instLayout)

        self.mainLayout.addWidget(self.titleLabel)
        self.mainLayout.addWidget(self.eventsListName)
        self.mainLayout.addWidget(self.eventsList)
        self.mainLayout.addWidget(self.subscriptionListName)
        self.mainLayout.addLayout(self.listLayout)
        self.mainLayout.addLayout(self.combosLayoutH)
        self.mainLayout.addWidget(self.errorMsg)
        self.mainLayout.addLayout(self.generateBtnsLayout)

        self.setLayout(self.mainLayout)


    def load_list(self):
        if self.events:
            cursor.execute("SELECT id,client_id FROM subscriptions WHERE event_id=?",
                            str(self.events[self.eventsList.currentIndex()][0]))
            self.clients = cursor.fetchall()
        else:
            self.clients = []

        self.subscriptionList.clear()
        for client in self.clients:
            cursor.execute("SELECT name FROM clients WHERE id=?",str(client[1]))
            client_name = unicode(cursor.fetchone()[0])
            self.subscriptionList.addItem(client_name)

    def add_client(self):
        try:
            event_id = self.events[self.eventsList.currentIndex()][0]
            self.add_client_widget = AddClientDialog(self, event_id)
            self.add_client_widget.show()
        except IndexError:
            self.errorMsg.setText(u"Selecione um evento existente!")

    def remove_client(self):
        try:
            subscription_id = self.clients[self.subscriptionList.currentRow()][0]
            choice = QtGui.QMessageBox.question(self, u"Apagar inscrição",
                                                u"Tem certeza que deseja apagar esta inscrição?",
                                                QtGui.QMessageBox.Yes |
                                                QtGui.QMessageBox.No)
            if choice == QtGui.QMessageBox.Yes:
                cursor.execute("DELETE FROM subscriptions WHERE id=?",
                                str(subscription_id))
                conn.commit()
            else:
                pass
            self.load_list()
        except IndexError:
            self.errorMsg.setText(u"Selecione um cliente existente!")

    def generate(self):
        if len(self.clients) == 0:
            self.errorMsg.setText(u"Primeiro inscreva clientes!")
        else:
            self.save_folder = QtGui.QFileDialog.getExistingDirectory(None,
                                                                u"Salvar em")

            self.cert_data = {"event": unicode(self.events[self.eventsList.currentIndex()][1]).upper(),
                         "start_date": unicode(self.events[self.eventsList.currentIndex()][2]),
                         "end_date": unicode(self.events[self.eventsList.currentIndex()][3]),
                         "hours": unicode(self.events[self.eventsList.currentIndex()][4]),
                         "content": unicode(self.events[self.eventsList.currentIndex()][5]).upper(),
                         "responsible_sig": unicode(self.signatures[self.responsibleList.currentIndex()][0]),
                         "institution_sig": unicode(self.signatures[self.institutionList.currentIndex()][0]),
                         "role": unicode(self.signatures[self.institutionList.currentIndex()][2]),
                         "institution": unicode(self.Config.get("Main","Name")).upper(),
                         "inst_register": unicode(self.Config.get("Main","ID"))}

            for client in self.clients:
                cursor.execute("SELECT name,register FROM clients WHERE id=?",str(client[1]))
                client_data = cursor.fetchone()
                self.cert_data["name"] = unicode(client_data[0]).upper()
                self.cert_data["register"] = unicode(client_data[1])
                generate_certificate(self.save_folder, self.cert_data)

            cursor.execute("SELECT * FROM signatures WHERE id=?",
                           str(self.signatures[self.responsibleList.currentIndex()][0]))
            responsible = cursor.fetchone()
            self.cert_data["name"] = unicode(responsible[1]).upper()
            self.cert_data["register"] = unicode(responsible[4]).upper()
            generate_certificate_responsible(self.save_folder, self.cert_data)

    def generate_send(self):
        if len(self.clients) == 0:
            self.errorMsg.setText(u"Primeiro inscreva clientes!")
        else:
            self.generate()
            self.mailer = Mailer()
            self.mailer.connect()
            for client in self.clients:
                cursor.execute("SELECT name,email FROM clients WHERE id=?",str(client[1]))
                client_data = cursor.fetchone()

                filepath = self.save_folder+"/"
                filepath += ''.join(i for i in unicode(client_data[0]) if ord(i)<128).upper()
                filepath += ".pdf"
                filepath.replace(" ","")
                self.mailer.send_certificate(filepath,unicode(client_data[1]))

            cursor.execute("SELECT name,email FROM signatures WHERE id=?",
                           str(self.signatures[self.responsibleList.currentIndex()][0]))
            responsible = cursor.fetchone()
            filepath = self.save_folder+"/responsible.pdf"
            self.mailer.send_certificate(filepath,unicode(responsible[1]))

            self.mailer.quit()