def update(self): ''' Computing the halting time ''' halt = datetime.now().timestamp() + self.timeout ''' Retrieving the users ''' students = self.database.selectAllStudent(halt) students['nom'] = students['nom'].apply( lambda x: Tools.normalizeString(x, 'ascii')) students['prenom'] = students['prenom'].apply( lambda x: Tools.normalizeString(x, 'ascii')) self.students = students if students is None: _Error.raiseError( "Erreur : base de données locale vide\nAucun élève à sélectionner" ) #external_data =self.externalBD.getAllUsers(halt) #external_data = self.externalBD.processUsers(students) #external_data.to_csv('students_oze_info.csv') external_data = pandas.read_csv('students_oze_info.csv') if external_data is None: _Error.raiseError( "Erreur : aucun élève accessible depuis la base de données externe" ) #merge data users, unmatch = self.matcheStudent(halt, students, external_data) if unmatch is not None: print("\n---------------------------------") print( "Les étudiants suivant n'ont pas été trouvés sur oZe (leurs informations ne seront donc pas mises à jour)" ) print("\n") print(unmatch[['nom', 'prenom', 'id_BD_local']]) print("\n----------------------------------\n") ''' process data ''' self.updateStudentBan(unmatch, halt) self.updateStudentExit(users, halt) return None
def __init__(self, externalBDInstance, path_to_settings_file, path_tmp_folder_database_file): self.externalBD = externalBDInstance self.readXmlFile = Tools.ReadXmlFile(path_to_settings_file) self.START_SCHOOL = self.readXmlFile.getStartSchool() self.END_SCHOOL = self.readXmlFile.getEndSchool() self.PATH_SETTINGS_FILE = path_to_settings_file self.PATH_TMP_FOLDER_DATABASE_FILE = path_tmp_folder_database_file self.database = Database(path_to_settings_file, path_tmp_folder_database_file) self.students = None
def matcheStudent(self, halt, internal_data, external_dataframe, process_unmacth=True): """ Into Oze each student have a specific id which is independant from the one use into the local database. Thus, we need to get all the student information (last name, first name and used id) Then matches the table id (from the local database) and the used id (from oze) """ local = internal_data extern = external_dataframe if extern is None: _Error.raiseError("Erreur : aucun élève accessible depuis Oze") extern['nom'] = extern['nom'].apply( lambda x: Tools.normalizeString(x, 'ascii')) extern['prenom'] = extern['prenom'].apply( lambda x: Tools.normalizeString(x, 'ascii')) #matches table id and used id columns = ['nom', 'prenom'] users = pandas.merge(local, extern, on=columns).drop_duplicates(subset=columns) unmatch = None if process_unmacth: unmatch = local[(~local['nom'].isin(users['nom'])) & (~local['prenom'].isin(users['prenom']))] if len(unmatch) == 0: unmatch = None users = deque(users.to_dict('records')) return users, unmatch
def __init__(self, path_to_settings_file, path_temp_database_folder): self.ReadXmlFile = Tools.ReadXmlFile(path_to_settings_file) self.path_temp_database_folder = path_temp_database_folder self.credential = self.ReadXmlFile.getDatabaseCredential() self.COMPUTER_TABLE_ID = getComputerTableId( datetime.now().timestamp() + Database.timeout, self.credential, Database.table['agent'], Database.COMPUTER_AUTHENTIFICATION_ID) self.c_exit = { "id": "Id eleve", "start": "Heure debut", "end": "Heure fin", "verif": "verification", "reason": "Motif" } self.exit_array = pandas.DataFrame(columns=[ 'None', self.c_exit['id'], self.c_exit['start'], self.c_exit['end'], self.c_exit['verif'], self.c_exit['reason'] ]) """ When we update student informations, we don't save the modification into the database for each student we wait until we process all the student from the database, then save it into a csv file And then we load all the informations from that file into the database (after deleting all the data into the corresponding table) To make the script easier the columnz into the file are ordered as thez are into the database (when a column into the database can't not be update or not need to be update we just create a None column that is empty) """ self.c_ban = { "type": "Type table", "id": "Id table", "start": "Heure debut", "end": "Heure fin", "reason": "Motif", "agent": "Id agent" } self.ban_array = pandas.DataFrame(columns=[ 'None', self.c_ban['type'], self.c_ban['id'], self.c_ban['start'], self.c_ban['end'], self.c_ban['reason'], self.c_ban['agent'] ])
def getExitRegimes(self, timeout): msg = None list_regime = [] while datetime.now().timestamp() < timeout: msg = None connection = None try: ''' get all exit regime label and id from the database ''' connection = pyodbc.connect(**self.credential) sql = "SELECT Label, SortieFinJournee, Id_regime From " + self.table[ 'RegimeLabel'] res = pandas.read_sql(sql, connection) #create list of regime for index, row in res.iterrows(): regime = Tools.ExitRegime(label=row[0], exitEndOfDay=row[1], tableId=row[2]) list_regime.append(regime) ''' get all authorizations associated to the regimes ''' index = 0 for regime in list_regime: #check data format if regime is None: list_regime.pop(index) continue table_id = regime.Id if table_id == -1: list_regime.pop(index) continue #get authorizations sql = "SELECT P.Label, P.Periode, P.Debut, P.Fin, P.Id_permission FROM " + self.table[ 'RegimeRelation'] + " R, " + self.table[ 'RegimeAuthorization'] + " P WHERE P.Id_permission = R.Id_permission AND R.Id_regime={0}".format( table_id) res = pandas.read_sql(sql, connection) for index, row in res.iterrows(): label = row[0] period = Tools.Timer.timeToSecond( Tools.Timer.stringToTime(row[1])) start = Tools.Timer.timeToSecond( Tools.Timer.stringToTime(row[2])) end = Tools.Timer.timeToSecond( Tools.Timer.stringToTime(row[3])) table_id = row[4] authorization = Tools.ExitRegimeAuthorization( label, period, start, end, table_id) regime.addAuthorization(authorization) index = index + 1 return list_regime except Exception as e: msg = "Impossible d'accèder aux régimes de sortie dans la base de données" msg += "\n" + str(type(e)) msg += "\n" + str(e.args) msg += "\n" + str(e) finally: ''' Closing the connection ''' if connection is not None: connection.close() if not (msg is None): _Error.raiseError(msg)