def removeWorkerNameDataBase(name, firstName, fileWorkerName) : """ remove worker off the database. :param name: the worker's name. :param firstName: the worker's firstname. :param fileWOrkerName: the path to the file to get name and firstname's worker. :rtype: str :return: message to check the removing. """ if name == "" : return "name" elif firstName == "" : return "firstname" else : res = "" remove = False lines = gl.getLinesOfFile(fileWorkerName) for l in lines : line = l.split(";") nameFile = line[0] firstNameFile = line[1] if not(name == nameFile and firstName == firstNameFile) : #if not the worker ti remove, write the line. res += l else : #else, do nothing to not write the line. remove = True gl.writeInFile(fileWorkerName, res) if remove : return "okRemoveWorker" else : return "remove"
def hoursDataBase(workerFile, hourFile, cssFile, show=True) : """ write an html file with all work hours of all workers. :param workerFile: the path to the file with work hours. :param hourFile: the path to the html file, to write on it. :param cssFile: the path to the css, to write it in the html file. (when the html file is sended by email, we don't need to send the css file). :param show: default, true, to show the html file. (false when the html file is sended by email). """ lines = gl.getLinesOfFile(workerFile) res = "<!doctype html><html lang='fr'><head><meta charset='utf-8'><title>Heure des ouvriers du mois de" + gl.getMonth() + "</title>" #read the css file to write it on the html file. f = open(cssFile, "r") css = f.read() f.close() res += "<style>" + css + "</style>" res += "</head><body>" #show columns num_col = 0 number_of_columns = 2 cols = ['gauche', 'droite'] for l in lines : line = l.split(";") name = line[0] firstName = line[1] #show columns res += "<div id='" + cols[num_col%number_of_columns] + "'>" num_col += 1 res += "<p id='worker'>" + name + " " + firstName + "</p><table><tbody><tr><td id='date'>Date</td><td id='hour'>Nombre d'heure</td><td id='mg'>MG</td></tr>" sumHour = 0 sumMG = 0 lineLen = len(line) for i in range(2,lineLen) : try : construction = line[i].split(":") d = construction[0] year = d[0:4] month = d[5:7] day = d[8:10] nbHour = construction[1] mg = construction[2] sumHour += float(nbHour) sumMG += float(mg) res += "<tr><td>" + day + "/" + month + "/" + year + "</td><td>" + nbHour + "</td><td>" + mg + "</td></tr>" except : None res += "<tr><td>Totaux</td><td>" + str(sumHour) + "</td><td>" + str(sumMG) + "</td></tr>" res += "</tbody></table>" res += "</div>" #show columns res += "</body></html>" gl.writeInFile(hourFile, res) if show : webbrowser.open(hourFile)
def emptyHour(hourFile, fileNameWorker) : """ to empty the file with work hours at the end of the month, and write the worker's name and firstname. :param hourFile: the path to the file with work hours. :param fileNameWorker: the path to the file with name and firstname workers. """ lines = gl.getLinesOfFile(fileNameWorker) res = "" for l in lines : line = l.split(";") res += line[0] + ";" + line[1] + ";\n" fileSendHour = open(hourFile, "w") fileSendHour.write(res) fileSendHour.close()
def takeValuesCombobox(i, f) : """ take the line of the file to put it in the combobox. :param i: the index to take the name (0) or the firstname (1). :param f: the file to read the name and firstname's workers. :rtype: list :return: the list of all names or all firstnames. """ res = [] lines = gl.getLinesOfFile(f) for l in lines : worker = l.split(";") workerValue = worker[i] res.append(workerValue) return res
def takeMG(c, f): """ take the MG to the associeted city. :param c: the name of the city. :param f: the file includes the city and this MG. :rtype: float :return: the MG associted at the city. """ lines = genLib.getLinesOfFile(f) mg = -1 #if not found city, mg = -1 to show error for l in lines: res = l.split(";") if res[0] == c: mg = res[1] return float(mg)
def addWorkerNameDataBase(name, firstName, fileWorkerName, fileWorkerHour) : """ add the worker at the databse. :param name: the worker's name. :param firstName: the worker's firstname. :param fileWOrkerName: the path to the file to get name and firstname's worker. :param fileWorkerHour: the path to the fileto get work hours, to add the name on this file. :rtype: str :return: message to check the adding. """ if name == "" : return "name" elif firstName == "" : return "firstname" else : res = "" add = False lines = gl.getLinesOfFile(fileWorkerName) if lines == [] : #if the file is empty, add name and firstname directly. #for name gl.writeInFile(fileWorkerName, name + ";" + firstName + ";\n") #for hour gl.writeInFile(fileWorkerHour, name + ";" + firstName + ";\n") return "okAddWorker" for l in lines : #else, add them in alphabetical order. line = l.split(";") if not add and line[0] == name and line[1] == firstName : #same name and firstname. return "double" if not add and line[0] > name : # res += name + ";" + firstName + ";\n" + l add = True elif not add and line[0] == name : if line[1] > firstName : res += name + ";" + firstName + ";\n" + l add = True else : res += l else : res += l if not add : res += name + ";" + firstName + ";\n" #for name gl.writeInFile(fileWorkerName, res) #for hour fileWorker = open(fileWorkerHour, "a") fileWorker.write(name + ";" + firstName + ";\n") fileWorker.close() return "okAddWorker"
def addCityDataBase(city, mg, f) : """ add city and this MG in databse. :param city: the name of the city. :pram mg: the MG. :param f: the work hours file. :rtype: str :return: message to check the adding. """ if city == "" : #empty city. return "city" elif mg == "" : #empty MG. return "mg" elif "," in mg : #MG with comma. return "mg," else : try : res = "" add = False mg = float(mg) print(mg) lines = gl.getLinesOfFile(f) for l in lines : line = l.split(";") if not add and line[0] > city : #not add, and alphabetical order res += city + ";" + str(mg) + "\n" + l add = True else : res += l if not add : res += city + ";" + str(mg) + "\n" gl.writeInFile(f,res) return "okAddCity" #city added. except : print("error") return "errorMG" #invalid MG.
def watchEveryHour(userFile, fileToSend, workerFile, hourFile, cssFile, fileNameWorker): """ watch every hour if we are into 00:00 and 01:00 at the 1 of the month. If it is, send an email with all work hours of all workers. :param userFile: path of the userFile (includes password, and email). :param fileToSend: the path of the file to send. This is the file with all work hours of all workers. :param workerFile: the path to the file with work hours. :param hourFile: the path to the html file, to write on it. :param cssFile: the path to the css file, to write it in the html file. (when the html file is sended by email, we don't need to send the css file). :param fileNameWorker: the path to the file includes all names and firstname worker. Used to empty workerFile, and write on it the name and forstname's workers. """ while True: now = datetime.now() date = now.strftime("%d;%H") splitDate = date.split(";") day = int(splitDate[0]) hour = int(splitDate[1]) if day == 1 and hour <= 1: mh.hoursDataBase(workerFile, hourFile, cssFile, False) mail = gl.getLinesOfFile(userFile)[1] if sendMail(mail, fileToSend): mh.emptyHour(hourFile, fileNameWorker) time.sleep(3600)
def checkParams(workerName, workerFirstName, workerArrivingTime, workerDepartureTime, workerMeal, fileWorkerHourPath): """ check params, to correct them. :param workerName: the worker's name. :param workerFirstName: the worker's firstname. :parzm workerArrivingTime: the arriving time. :param workerDepartureTime: the departure time. :param workerMeal: the meal time. :param fileWorkerHourPath: the path to the file includes the work hours of workers. :rtype: tuple (float, list), or str if was an error. :return: tuple of work hours of the day and the lines of the file given, or message error. """ if workerName == "": return "name" elif workerFirstName == "": return "firstname" elif workerArrivingTime == "": return "arriving" elif workerDepartureTime == "": return "departure" elif workerMeal == "": return "meal" else: hourBegin = getHourInInt(workerArrivingTime) if hourBegin == "errorHour" or hourBegin == "errorMinute": return "errorHourBegin" hourEnd = getHourInInt(workerDepartureTime) if hourEnd == "errorHour" or hourEnd == "errorMinute": return "errorHourEnd" minuteMeal = getMinuteInInt(workerMeal) if minuteMeal == "errorMinute": return "errorMinute" workerHour = hourEnd - hourBegin - minuteMeal lines = genLib.getLinesOfFile(fileWorkerHourPath) return workerHour, lines
def __init__(self): self.width = 1900 self.height = 950 self.fileWorkerHourPath = "./dataBase/workerHour.csv" #includes name, first name, date and number of hours work self.fileMGPath = "./dataBase/mg.csv" #includes name of city and this MG (kilometer allowance) self.fileSendHourPath = "./html/sendHour.html" #includes the file to send with worker name and forstname, date, number of hours work and MG (in html) self.fileUserPath = "./user/user.csv" #includes user password and email of client self.scriptPath = "./app/scriptSendMail.py" #the programme that send the file in html by email self.fileCssPath = "./html/style.css" #the cs of html file to send self.fileWorkerNamePath = "./dataBase/workerName.csv" #includes name and firstname of all workers if sys.platform != 'linux': #if os is window, paths are different self.height = 600 self.fileWorkerHourPath = "." + self.fileWorkerHourPath self.fileMGPath = "." + self.fileMGPath self.fileSendHourPath = "." + self.fileSendHourPath self.fileUserPath = "." + self.fileUserPath self.scriptPath = "." + self.scriptPath self.fileCssPath = "." + self.fileCssPath self.fileWorkerNamePath = "." + self.fileWorkerNamePath self.window = tk.Tk(className="Horaires") self.window.geometry(str(self.width) + "x" + str(self.height + 15)) self.listbox = tk.Listbox(self.window) #label and input #worker name self.labelWorkerName = tk.Label(self.window, text="Nom de l'ouvrier : ") self.valueWorkerName = tk.StringVar() self.inputWorkerName = tk.Entry(self.window, textvariable=self.valueWorkerName) #worker first name self.labelWorkerFirstName = tk.Label(self.window, text="Prénom de l'ouvrier : ") self.valueWorkerFirstName = tk.StringVar() self.inputWorkerFirstName = tk.Entry( self.window, textvariable=self.valueWorkerFirstName) #arriving time self.labelArrivingTime = tk.Label(self.window, text="Heure d'arrivée (en heure) : ") self.valueArrivingTime = tk.StringVar() self.inputArrivingTime = tk.Entry(self.window, textvariable=self.valueArrivingTime) #meal self.labelMeal = tk.Label(self.window, text="Repas (en minute) : ") self.valueMeal = tk.StringVar() self.inputMeal = tk.Entry(self.window, textvariable=self.valueMeal) #departure time self.labelDepartureTime = tk.Label( self.window, text="Heure de départ (en heure) : ") self.valueDepartureTime = tk.StringVar() self.inputDepartureTime = tk.Entry( self.window, textvariable=self.valueDepartureTime) #city self.labelCity = tk.Label(self.window, text="Lieu du chantier : ") self.valueCity = tk.StringVar() self.inputCity = tk.Entry(self.window, textvariable=self.valueCity) #MG self.labelMG = tk.Label(self.window, text="MG : ") self.valueMG = tk.StringVar() self.inputMG = tk.Entry(self.window, textvariable=self.valueMG) #date self.labelDate = tk.Label(self.window, text="Date (sous le format aaaa-mm-jj) : ") self.valueDate = tk.StringVar() self.inputDate = tk.Entry(self.window, textvariable=self.valueDate) #combobox to facilitate the choice self.comboWorkerName = ttk.Combobox(self.window) self.comboWorkerFistName = ttk.Combobox(self.window) self.comboCity = ttk.Combobox(self.window) #button self.buttonAddHour = tk.Button(self.window, text="Ajouter les heures", command=self.addHour) self.buttonAddMG = tk.Button(self.window, text="Ajouter la ville et ses MG", command=self.addCity) self.buttonEditHour = tk.Button(self.window, text="Editer les horaires", command=self.editHour) self.closeButton = tk.Button(self.window, text="Fermer", command=self.close) self.buttonAddWorker = tk.Button(self.window, text="Ajouter l'ouvrier", command=self.addWorker) self.buttonRemoveWorker = tk.Button(self.window, text="Supprimer l'ouvrier", command=self.removeWorker) #password button (all actions need password) self.buttonEditPassWord = tk.Button(self.window, text="Changer le mot de passe", command=self.editPassWord) self.buttonCheckPassWordEditPassWord = tk.Button( self.window, text="Valider", command=self.checkPassWordEditPassWord) self.buttonCheckPassWordEditHour = tk.Button( self.window, text="Valider", command=self.checkPassWordEditHour) self.buttonCheckPassWordAddMG = tk.Button( self.window, text="Valider", command=self.checkPassWordAddMG) self.buttonCheckPassWordEditEmail = tk.Button( self.window, text="Valider", command=self.checkPassWordEditEMail) self.buttonEditEmail = tk.Button(self.window, text="Changer le mail", command=self.editEmail) self.buttonCheckPassWordAddWorker = tk.Button( self.window, text="Valider", command=self.checkPassWordAddWorker) self.buttonCheckPassWordRemoveWorker = tk.Button( self.window, text="Valider", command=self.checkPassWordRemoveWorker) #password self.password = gl.getLinesOfFile(self.fileUserPath)[0][0:-1] self.labelPassWord = tk.Label(self.window, text="Mot de passe : ") self.valuePassWord = tk.StringVar() self.inputPassWord = tk.Entry(self.window, textvariable=self.valuePassWord, exportselection=0, show="*") self.inputPassWordWhitoutStars = tk.Entry( self.window, textvariable=self.valuePassWord, exportselection=0) #email self.email = gl.getLinesOfFile(self.fileUserPath)[1] self.labelEmail = tk.Label(self.window, text="Email : ") self.valueEMail = tk.StringVar() self.inputEmail = tk.Entry(self.window, textvariable=self.valueEMail, exportselection=0)