From 292b046b629ae9ad0be0e5f460aa02901cd69083 Mon Sep 17 00:00:00 2001 From: Dennis Potter Date: Sun, 3 Feb 2019 12:51:44 +0100 Subject: [PATCH] Data is now actually refreshed in refresh() method Before this commit, the MySQL connection stayed open during the whole lifetime of the Admidio object. Because of that, new data in the database could not be acquired by the refresh() method. Furthermore, the object would crash when the connection got lost. Now, the connection is created at the start of refresh() and disconnected after refresh(). --- admidio.py | 41 ++++++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/admidio.py b/admidio.py index c175d9f..4e4458a 100644 --- a/admidio.py +++ b/admidio.py @@ -13,14 +13,21 @@ class Admidio: def __init__(self, mysql_host, mysql_user, mysql_pass, mysql_db, db_prefix): - self.db = MySQLdb.connect( - host=mysql_host, user=mysql_user, passwd=mysql_pass, db=mysql_db) - self.cursor = self.db.cursor() + self.mysql_host = mysql_host + self.mysql_user = mysql_user + self.mysql_pass = mysql_pass + self.mysql_db = mysql_db + self.prefix = db_prefix + self.members = dict() self.groups = dict() self.events = dict() + self.event_confirmation_id = None + + self.connect_db() + sql = f"SELECT usf_id,usf_name_intern FROM {self.prefix}_user_fields" self.cursor.execute(sql) self.userfields = dict(self.cursor.fetchall()) @@ -34,25 +41,37 @@ class Admidio: self.cursor.execute(sql) self.event_confirmation_id = self.cursor.fetchone()[0] - self.initMembers() - self.initGroups() - self.initEvents() - self.ready = True + self.db.close() + + self.refresh() def __del__(self): - self.db.close() + pass + + def connect_db(self): + self.db = MySQLdb.connect( + host=self.mysql_host, + user=self.mysql_user, + passwd=self.mysql_pass, + db=self.mysql_db) + + self.cursor = self.db.cursor() def refresh(self): """Refresh all users, groups and events that are stored""" + self.connect_db() + self.ready = False - self.members = dict() - self.groups = dict() - self.events = dict() + self.members.clear() + self.groups.clear() + self.events.clear() self.initMembers() self.initGroups() self.initEvents() self.ready = True + self.db.close() + def initMembers(self): """Initialises all members in the Admidio installation"""