def miseAJour(self): u"""Liste les absences pouvant donner lieu à un email de rappel""" self._ui.cbAbsence.clear() # Vérification des mails à envoyer req = QSqlQuery() sql = MailSQL.construitRequeteComptage(self.__conf["duree"]) if req.exec_(sql): req.next() nbMails = req.record().value(0) else: # TODO log print req.lastError().text() print req.lastQuery() print "Erreur de requête" return label = str(nbMails) + " absence" if nbMails == 0: label += " :" self._ui.lAbsence.setText(label) self._ui.leSujet.setText("") self._ui.teCorps.setText("") self.__activerUi(False) self._ui.pbEnvoyer.setText("Envoyer") return else: self.__activerUi(True) if nbMails > 1: label += "s" label += " :" self._ui.lAbsence.setText(label) sql = MailSQL.construitRequeteListe(self.__conf["duree"]) if not req.exec_(sql): print req.lastError().text() print req.lastQuery() print "Erreur de requête" else: self.__absences = [] while (req.next()): absence = {} rec = req.record() absence = {} absence["id"] = rec.value(0) absence["date"] = QDate.fromString(rec.value(1), Qt.ISODate) absence["nom"] = rec.value(2) absence["adresse"] = rec.value(3) self.__absences.append(absence) item = absence["nom"] + " le " item += absence["date"].toString(Qt.SystemLocaleLongDate) self._ui.cbAbsence.addItem(item)
def __resulatEnvoi(self, errCode): u"""Slot notifié quand l'envoi du mail est fini errCode indique le succès, ou l'échec (avec la raison) de l'envoi du mail """ if errCode == MailSender.MAIL_ERROR_NONE: # Mail envoyé, mise à jour de la base self.notification.emit(u"Email envoyé", MailUI.DUREE_MESSAGE) index = self._ui.cbAbsence.currentIndex() sql = MailSQL.mailEnvoye(str(self.__absences[index]["id"])) req = QSqlQuery() if not req.exec_(sql): QMessageBox.critical(self, u"Erreur de base de données", u"Le mail a été envoyé mais impossible de <br />" + u"l'enregistrer dans la base.") # TODO logger print "SQL error" print str(req.lastError().text().toUtf8()) print req.lastQuery() else: self.majBdd.emit() elif (errCode == MailSender.MAIL_ERROR_TIMEOUT or errCode == MailSender.MAIL_ERROR_CONNECTION): message = u"Email non envoyé - " if errCode == MailSender.MAIL_ERROR_TIMEOUT: message += u"Durée dépassée" else: message += u"Erreur de connexion" self.notification.emit(message, MailUI.DUREE_MESSAGE) QMessageBox.critical(self, "Erreur de connection", u"Impossible de contacter le serveur.<br />" + u"Veuillez vérifier la connexion à internet, <br />" + u"ainsi que l'adresse du serveur de messagerie.") elif errCode == MailSender.MAIL_ERROR_AUTHENTICATION: message = u"Email non envoyé - Erreur d'authentification" self.notification.emit(message, MailUI.DUREE_MESSAGE) QMessageBox.critical(self, "Erreur d'authentification", "Indentifiants incorrects.<br />(login " + self.__conf["email"] + ")") del self.__password else: # MailSender.MAIL_ERROR_OTHER: message = u"Email non envoyé - Erreur inconnue" self.notification.emit(message, MailUI.DUREE_MESSAGE) QMessageBox.critical(self, "Erreur inconnue", "Une erreur inconnue s'est produite.<br />(login '" + self.__conf["email"] + "')") # TODO logger l'erreur réelle à la levée de l'exception self.majBdd.emit()
def update(self): self.defenses = {} query = QSqlQuery(self.parent.db) query.prepare("SELECT planetuid, itemuid, amount, description, structure FROM planets_defense LEFT JOIN static_defenses ON static_defenses.id=planets_defense.itemuid WHERE amount != 0") if not query.exec_(): self.log.warning(query.lastError()) if query.size() > 0 : #query.first() while query.next() : planetuid = int(query.value(0)) itemuid = int(query.value(1)) amount = int(query.value(2)) description = str(query.value(3)) structure = str(query.value(4)) if not planetuid in self.defenses: self.defenses[planetuid] = defense(planetuid, self.parent) self.defenses[planetuid].addDefense(itemuid, amount, description, structure)
def save(layout): """"Saves the state of the program and moves the save file to specified place""" # Create a file if there is no previous save # else move db to current folder to save if not os.path.isfile(paths.savePath): # Create txt and write down the first day of usage savefile = open(paths.savePath, mode='w') savefile.write(str(global_vars.DAYFIRST.toordinal())) savefile.close() filename = os.path.basename(paths.savePath)[:-8] # Saving tasks widget block database.setConnection(filename) database.truncate() groups = layout.taskwidget.getGroups() for group in groups: query = QSqlQuery() query.prepare("INSERT INTO status (name) VALUES (:name)") query.bindValue(":name", group.getName()) query.exec_() query = QSqlQuery("SELECT rowid FROM status WHERE name = '" + group.getName() + "' ") query.next() id_ = query.value(0) for task in group: query = QSqlQuery() query.prepare("INSERT INTO tasks (name, status) VALUES (:name, :id)") query.bindValue(":name", task.getTask()) query.bindValue(":id", id_) query.exec_() # Saving maintable for weeknum in layout.tab.notsaved: table = layout.tab.getWidgetFromWeeknum(weeknum) weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] for weekday in weekdays: tasks = table.getTasks(weekday) if tasks: fields = '' values = '' for time in tasks: query = QSqlQuery("SELECT rowid FROM tasks WHERE name ='" + tasks[time] + "'") query.next() tasks[time] = query.value(0) fields += '"' + time + '"' + ',' values += str(tasks[time]) + ',' fields = 'weekday,' + fields[:-1] values = '"' + weekday + '_' + str(weeknum) + '"' + ', ' + values[:-1] request = 'INSERT INTO main (' + fields + ') VALUES (' + values + ')' query = QSqlQuery() query.exec_(request) layout.tab.notsaved = [] # Saving evaluation values for week in global_vars.EVAL_VALUES.keys(): val = str(week) + ', ' for value in global_vars.EVAL_VALUES[week]: val += str(value) + ', ' request = 'INSERT INTO evaluation VALUES(' + val[:-2] + ')' query = QSqlQuery() query.exec_(request) # Saving neural network weights parent = layout.parentWidget() exists = True try: parent.network except (AttributeError, NameError): exists = False if exists: hdw = parent.network.hiddenweights otpw = parent.network.outputweights request = 'INSERT INTO weights (input, output, value) ' for i in range(len(hdw)): for j in range(len(hdw[i])): if (i == 0) and (j == 0): request += 'SELECT ' + '"in_' + str(i) + '", "hidden_' + str(j) + '", ' + str(hdw[i][j]) else: request += ' UNION ALL' request += ' SELECT ' + '"in_' + str(i) + '", "hidden_' + str(j) + '", ' + str(hdw[i][j]) for i in range(len(otpw)): for j in range(len(otpw[i])): request += ' UNION ALL' request += ' SELECT ' + '"hidden_' + str(i) + '", "out_' + str(j) + '", ' + str(hdw[i][j]) print(request) query = QSqlQuery() query.exec_(request) print(query.lastError()) print('Saved') # Restoring the original state database.dropConnection()
def save(layout): """"Saves the state of the program and moves the save file to specified place""" # Create a file if there is no previous save # else move db to current folder to save if not os.path.isfile(paths.savePath): # Create txt and write down the first day of usage savefile = open(paths.savePath, mode='w') savefile.write(str(global_vars.DAYFIRST.toordinal())) savefile.close() filename = os.path.basename(paths.savePath)[:-8] # Saving tasks widget block database.setConnection(filename) database.truncate() groups = layout.taskwidget.getGroups() for group in groups: query = QSqlQuery() query.prepare("INSERT INTO status (name) VALUES (:name)") query.bindValue(":name", group.getName()) query.exec_() query = QSqlQuery("SELECT rowid FROM status WHERE name = '" + group.getName() + "' ") query.next() id_ = query.value(0) for task in group: query = QSqlQuery() query.prepare( "INSERT INTO tasks (name, status) VALUES (:name, :id)") query.bindValue(":name", task.getTask()) query.bindValue(":id", id_) query.exec_() # Saving maintable for weeknum in layout.tab.notsaved: table = layout.tab.getWidgetFromWeeknum(weeknum) weekdays = [ 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday' ] for weekday in weekdays: tasks = table.getTasks(weekday) if tasks: fields = '' values = '' for time in tasks: query = QSqlQuery("SELECT rowid FROM tasks WHERE name ='" + tasks[time] + "'") query.next() tasks[time] = query.value(0) fields += '"' + time + '"' + ',' values += str(tasks[time]) + ',' fields = 'weekday,' + fields[:-1] values = '"' + weekday + '_' + str( weeknum) + '"' + ', ' + values[:-1] request = 'INSERT INTO main (' + fields + ') VALUES (' + values + ')' query = QSqlQuery() query.exec_(request) layout.tab.notsaved = [] # Saving evaluation values for week in global_vars.EVAL_VALUES.keys(): val = str(week) + ', ' for value in global_vars.EVAL_VALUES[week]: val += str(value) + ', ' request = 'INSERT INTO evaluation VALUES(' + val[:-2] + ')' query = QSqlQuery() query.exec_(request) # Saving neural network weights parent = layout.parentWidget() exists = True try: parent.network except (AttributeError, NameError): exists = False if exists: hdw = parent.network.hiddenweights otpw = parent.network.outputweights request = 'INSERT INTO weights (input, output, value) ' for i in range(len(hdw)): for j in range(len(hdw[i])): if (i == 0) and (j == 0): request += 'SELECT ' + '"in_' + str( i) + '", "hidden_' + str(j) + '", ' + str(hdw[i][j]) else: request += ' UNION ALL' request += ' SELECT ' + '"in_' + str( i) + '", "hidden_' + str(j) + '", ' + str(hdw[i][j]) for i in range(len(otpw)): for j in range(len(otpw[i])): request += ' UNION ALL' request += ' SELECT ' + '"hidden_' + str(i) + '", "out_' + str( j) + '", ' + str(hdw[i][j]) print(request) query = QSqlQuery() query.exec_(request) print(query.lastError()) print('Saved') # Restoring the original state database.dropConnection()
def __resulatEnvoi(self, errCode): u"""Slot notifié quand l'envoi du mail est fini errCode indique le succès, ou l'échec (avec la raison) de l'envoi du mail """ if errCode == MailSender.MAIL_ERROR_NONE: # Mail envoyé, mise à jour de la base self.notification.emit(u"Email envoyé", MailUI.DUREE_MESSAGE) index = self._ui.cbAbsence.currentIndex() sql = MailSQL.mailEnvoye(str(self.__absences[index]["id"])) req = QSqlQuery() if not req.exec_(sql): QMessageBox.critical( self, u"Erreur de base de données", u"Le mail a été envoyé mais impossible de <br />" + u"l'enregistrer dans la base.") # TODO logger print "SQL error" print str(req.lastError().text().toUtf8()) print req.lastQuery() else: self.majBdd.emit() elif (errCode == MailSender.MAIL_ERROR_TIMEOUT or errCode == MailSender.MAIL_ERROR_CONNECTION): message = u"Email non envoyé - " if errCode == MailSender.MAIL_ERROR_TIMEOUT: message += u"Durée dépassée" else: message += u"Erreur de connexion" self.notification.emit(message, MailUI.DUREE_MESSAGE) QMessageBox.critical( self, "Erreur de connection", u"Impossible de contacter le serveur.<br />" + u"Veuillez vérifier la connexion à internet, <br />" + u"ainsi que l'adresse du serveur de messagerie.") elif errCode == MailSender.MAIL_ERROR_AUTHENTICATION: message = u"Email non envoyé - Erreur d'authentification" self.notification.emit(message, MailUI.DUREE_MESSAGE) QMessageBox.critical( self, "Erreur d'authentification", "Indentifiants incorrects.<br />(login " + self.__conf["email"] + ")") del self.__password else: # MailSender.MAIL_ERROR_OTHER: message = u"Email non envoyé - Erreur inconnue" self.notification.emit(message, MailUI.DUREE_MESSAGE) QMessageBox.critical( self, "Erreur inconnue", "Une erreur inconnue s'est produite.<br />(login '" + self.__conf["email"] + "')") # TODO logger l'erreur réelle à la levée de l'exception self.majBdd.emit()