class EventModel(): """Class to perform all queries related to the event table in the database""" def __init__(self): # Create a instance of the connection class to acces the database self.db = Connection() def show_events(self, date): """method to display all the events of the day """ sql = "SELECT * FROM events WHERE date = %s;" self.db.initialize_connection() self.db.cursor.execute(sql, (date, )) events = self.db.cursor.fetchall() self.db.close_connection() for key, value in enumerate(events): events[key] = Event(value) return events def display_one_event(self, date, time): """method to dislay one event """ sql = "SELECT * FROM events WHERE date = %s AND time = %s;" self.db.initialize_connection() self.db.cursor.execute(sql, (date, time)) event = self.db.cursor.fetchone() self.db.close_connection() if event: return Event(event) return False def add_event(self, event): """ """ sql = "INSERT INTO events(title, date, time, description) VALUES(%s, %s, %s, %s);" percent_s = (event.title, event.date, event.time, event.description) self.db.initialize_connection() self.db.cursor.execute(sql, percent_s) self.db.connection.commit() self.db.close_connection() def delete_event(self, date, time): """ """ sql = "DELETE FROM events WHERE date = %s AND time = %s;" percent_s = (date, time) self.db.initialize_connection() self.db.cursor.execute(sql, percent_s) self.db.connection.commit() self.db.close_connection() def update_event(self, title, date, time, description, actual_date, actual_time): """ """ sql = "UPDATE events SET title=%s, date=%s, time=%s, description = %s WHERE date = %s AND time = %s;" percent_s = (event.title, event.date, event.time, event.description, actual_date, actual_time) self.db.initialize_connection() self.db.cursor.execute(sql, percent_s) self.db.connection.commit() self.db.close_connection()
class MessageModel(): """class to create model message and connect to database """ def __init__(self): self.db = Connection() def display_message(self): """method to display messages """ self.db.initialize_connection() self.db.cursor.execute("SELECT * FROM message;") view=self.db.cursor.fetchall() self.db.close_connection() return view def write_message(self,content, author): """ """ self.db.initialize_connection() self.db.cursor.execute("INSERT INTO message(content, publishing_date, author) VALUES (%s, now(), %s);",(content,author)) self.db.connection.commit() self.db.close_connection()
class Conferencier(): def __init__(self): self.db = Connection() def add_conferencier(self, prenom, nom, description, profession): """method adding conferencier""" argument=(prenom, nom, description, profession) sql="""INSERT INTO conferenciers (prenom, nom, description, profession) VALUES (%s,%s,%s,%s);""" self.db.initialize_connection() self.db.cursor.execute(sql, argument) self.db.connection.commit() self.db.close_connection() def delete_conferencier(self, id): """method deleting conferencier""" sql="""DELETE FROM conferenciers WHERE id=%s;""" self.db.initialize_connection() self.db.cursor.execute(sql, (id,)) self.db.connection.commit() self.db.close_connection() def display_conferencier(self): '''method selecting all conferenciers that have actif status''' self.db.initialize_connection() self.db.cursor.execute("SELECT * FROM conferenciers WHERE statut_actif=True ;") actif_conferenciers = self.db.cursor.fetchall() for key, value in enumerate(actif_conferenciers): actif_conferenciers[key]=HydConferencier(value) self.db.close_connection() return actif_conferenciers
class View_model(): def __init__(self): self.db = Connection() def add_rdv(self, title, date, hour, description): sql = """INSERT INTO calendar (title, date, hour, description) VALUES (%s, %s, %s, %s );""" arguments = (title, date, hour, description) self.db.initialize_connection() self.db.cursor.execute(sql, arguments) self.db.connection.commit() self.db.close_connection() def del_rdv(self, date, hour): sql = "DELETE title, date, hour, description FROM calendar WHERE date = %s and hour = %s);" self.db.initialize_connection() self.db.cursor.execute(sql) self.db.connection.commit() self.db.close_connection() def get_all_rdv(self, date): sql = "SELECT * FROM calendar WHERE date = %s;" self.db.initialize_connection() self.db.cursor.execute(sql, (date, )) rdv = self.db.cursor.fetchall() self.db.close_connection() for key, value in enumerate(rdv): rdv[key] = Rdv(value) return rdv def get_rdv(self, date, hour): sql = "SELECT * FROM calendar WHERE date = %s and hour=%s;" self.db.initialize_connection() self.db.cursor.execute(sql, (date, hour)) rdv = self.db.cursor.fetchone() self.db.close_connection() return rdv
class Conference(): def __init__(self): self.db = Connection() def add_conference(self, titre, resume, date, heure, date_creation, id_conferencier): """method adding conference""" argument = (titre, resume, date, heure, date_creation, id_conferencier) sql = """INSERT INTO conferences (titre, resume, date, heure, date_creation, id_conferencier) VALUES (%s,%s,%s,%s,now(),%s);""" self.db.initialize_connection() self.db.cursor.execute(sql, argument) self.db.connection.commit() self.db.close_connection() def delete_conference(self, id): """method deleting conference""" sql = """DELETE FROM conferences WHERE id=%s;""" self.db.initialize_connection() self.db.cursor.execute(sql, (id, )) self.db.connection.commit() self.db.close_connection() def display_conference(self): '''method selecting all conferences with the nom prenom of conferencier having the id_conferencier valable''' SQL = " SELECT conferences.*, conferenciers.prenom, conferenciers.nom " \ " FROM conferences INNER JOIN conferenciers" \ " ON conferences.id_conferencier=conferenciers.id " \ " ORDER BY conferences.date;" """SELECT a.*, b.prenom, b.prenom From conferences as a INNER JOIN conferenciers as b ON a.id_conferencier=b.id;""" self.db.initialize_connection() self.db.cursor.execute(SQL) all_conferences = self.db.cursor.fetchall() print(all_conferences) self.db.close_connection() for key, value in enumerate(all_conferences): all_conferences[key] = HydConference(value) return all_conferences