db handling optimized
This commit is contained in:
107
timeTrack.py
107
timeTrack.py
@@ -3,7 +3,7 @@
|
||||
#
|
||||
# timeTrack.py
|
||||
# by 4nima
|
||||
# v.2.0.2
|
||||
# v.2.0.3
|
||||
#
|
||||
#########################
|
||||
# simple time tracking with database
|
||||
@@ -24,6 +24,7 @@ class TimeTrack:
|
||||
self.USERNAME = ''
|
||||
self.OLDEVENT = 2
|
||||
self.LOGFILE = 'timetrack.log'
|
||||
self.DBCON = sqlite3.connect(self.DATABASE, detect_types=sqlite3.PARSE_DECLTYPES | sqlite3.PARSE_COLNAMES)
|
||||
logging.basicConfig(
|
||||
filename=self.LOGFILE,
|
||||
level=logging.DEBUG,
|
||||
@@ -33,6 +34,9 @@ class TimeTrack:
|
||||
self.db_setup()
|
||||
self.load_config()
|
||||
|
||||
def __del__(self):
|
||||
self.DBCON.close()
|
||||
|
||||
## Prepartation
|
||||
### Check OS and clear screen
|
||||
def clear_screen(self):
|
||||
@@ -95,23 +99,20 @@ class TimeTrack:
|
||||
)
|
||||
""")
|
||||
|
||||
connect = sqlite3.connect(self.DATABASE)
|
||||
cursor = connect.cursor()
|
||||
logging.debug('Create initial database tables')
|
||||
for SQL in sql:
|
||||
try:
|
||||
cursor.execute(SQL)
|
||||
except:
|
||||
with self.DBCON as con:
|
||||
for SQL in sql:
|
||||
con.execute(SQL)
|
||||
except sqlite3.Error as err:
|
||||
logging.error('Table could not be created')
|
||||
logging.debug(SQL)
|
||||
logging.debug(sql)
|
||||
logging.error('SQLError: {}'.format(err))
|
||||
print('TimeTrack wird beendet: Fehler bei Datanbank Setup')
|
||||
quit()
|
||||
else:
|
||||
logging.debug('Table was created successfully or already exists')
|
||||
|
||||
connect.commit()
|
||||
connect.close()
|
||||
|
||||
### Loads or creates a config file
|
||||
def load_config(self):
|
||||
if os.path.isfile(self.CONFIG):
|
||||
@@ -160,21 +161,16 @@ class TimeTrack:
|
||||
|
||||
logging.debug('Accepted username: {}'.format(username))
|
||||
|
||||
connect = sqlite3.connect(self.DATABASE)
|
||||
cursor = connect.cursor()
|
||||
|
||||
sql = "INSERT INTO users ( name ) values ( ? )"
|
||||
|
||||
try:
|
||||
cursor.execute(sql, [username])
|
||||
except:
|
||||
with self.DBCON as con:
|
||||
con.execute(sql, [username])
|
||||
except sqlite3.Error as err:
|
||||
logging.error('User could not be saved in database')
|
||||
logging.debug(sql)
|
||||
logging.error('SQLError: {}'.format(err))
|
||||
else:
|
||||
logging.info('User was saved successfully')
|
||||
connect.commit()
|
||||
|
||||
connect.close()
|
||||
|
||||
### Outputs existing users from the DB
|
||||
def get_users(self, UID=0, NAME=''):
|
||||
@@ -191,25 +187,23 @@ class TimeTrack:
|
||||
data = ''
|
||||
sql = "SELECT * FROM users"
|
||||
|
||||
connect = sqlite3.connect(self.DATABASE)
|
||||
cursor = connect.cursor()
|
||||
|
||||
try:
|
||||
with self.DBCON as con:
|
||||
cur = con.cursor()
|
||||
if data:
|
||||
cursor.execute(sql, [data])
|
||||
cur.execute(sql, [data])
|
||||
else:
|
||||
cursor.execute(sql)
|
||||
except:
|
||||
cur.execute(sql)
|
||||
data = cur.fetchall()
|
||||
except sqlite3.Error as err:
|
||||
logging.error('Could not get user')
|
||||
logging.debug(sql)
|
||||
logging.error('SQLError: {}'.format(err))
|
||||
print('Fehler beim Zugriff auf die Benutzer Datenbank')
|
||||
return 1
|
||||
else:
|
||||
logging.debug('User database read out successfully')
|
||||
connect.commit()
|
||||
|
||||
data = cursor.fetchall()
|
||||
connect.close()
|
||||
return data
|
||||
|
||||
### Defines a user for the session
|
||||
@@ -231,23 +225,22 @@ class TimeTrack:
|
||||
def save_event(self, TIME=datetime.datetime.now()):
|
||||
if not self.get_event(USERID=self.USERID):
|
||||
logging.debug('No active events found for the user: {}'.format(self.USERID))
|
||||
connect = sqlite3.connect(self.DATABASE)
|
||||
cursor = connect.cursor()
|
||||
sql = "INSERT INTO active_events ( starttime, user_id ) VALUES ( ?, ? )"
|
||||
|
||||
sql = "INSERT INTO active_events ( starttime, user_id ) VALUES ( ?, ? )"
|
||||
try:
|
||||
cursor.execute(sql, [TIME, self.USERID])
|
||||
except:
|
||||
with self.DBCON as con:
|
||||
con.execute(sql, [TIME, self.USERID])
|
||||
except sqlite3.Error as err:
|
||||
logging.error('Event could not be created')
|
||||
logging.debug(sql)
|
||||
logging.error('SQLError: {}'.format(err))
|
||||
print('Event konnte nicht gespeichert werden.')
|
||||
return False
|
||||
else:
|
||||
logging.info('Event was created successfully')
|
||||
connect.commit()
|
||||
|
||||
connect.close()
|
||||
return True
|
||||
|
||||
else:
|
||||
logging.warning('Active events found for the user, new event could not be created')
|
||||
return False
|
||||
@@ -267,20 +260,16 @@ class TimeTrack:
|
||||
print('Keine angabe was gelöscht werden soll')
|
||||
return 0
|
||||
|
||||
connect = sqlite3.connect(self.DATABASE, detect_types=sqlite3.PARSE_DECLTYPES | sqlite3.PARSE_COLNAMES)
|
||||
cursor = connect.cursor()
|
||||
|
||||
try:
|
||||
cursor.execute(sql, [data])
|
||||
except:
|
||||
with self.DBCON as con:
|
||||
con.execute(sql, [data])
|
||||
except sqlite3.Error as err:
|
||||
logging.error('Event could not be deleted')
|
||||
logging.debug(sql)
|
||||
logging.error('SQLError: {}'.format(err))
|
||||
print('Fehler beim löschen des Events.')
|
||||
else:
|
||||
logging.debug('Event was successfully deleted')
|
||||
connect.commit()
|
||||
|
||||
connect.close()
|
||||
|
||||
### Get an active event based on a user or event ID
|
||||
def get_event(self, ENTRYID='', USERID=''):
|
||||
@@ -296,23 +285,24 @@ class TimeTrack:
|
||||
sql = "SELECT * FROM active_events"
|
||||
data = ''
|
||||
|
||||
connect = sqlite3.connect(self.DATABASE, detect_types=sqlite3.PARSE_DECLTYPES | sqlite3.PARSE_COLNAMES)
|
||||
cursor = connect.cursor()
|
||||
try:
|
||||
with self.DBCON as con:
|
||||
cur = con.cursor()
|
||||
if data:
|
||||
logging.debug('Search event')
|
||||
cursor.execute(sql, [data])
|
||||
cur.execute(sql, [data])
|
||||
else:
|
||||
logging.debug('Get all Events')
|
||||
cursor.execute(sql)
|
||||
except:
|
||||
cur.execute(sql)
|
||||
data = cur.fetchall()
|
||||
except sqlite3.Error as err:
|
||||
logging.error('Events could not be read')
|
||||
logging.debug(sql)
|
||||
logging.error('SQLError: {}'.format(err))
|
||||
print('Fehler beim auslesen der aktiven Events')
|
||||
else:
|
||||
logging.debug('Events could be read out successfully')
|
||||
data = cursor.fetchall()
|
||||
connect.close()
|
||||
|
||||
if data == []:
|
||||
logging.debug('No active events found')
|
||||
return 0
|
||||
@@ -421,20 +411,20 @@ class TimeTrack:
|
||||
|
||||
endtime = datetime.datetime.now()
|
||||
logging.debug('Event end process start at {}'.format(endtime))
|
||||
connect = sqlite3.connect(self.DATABASE)
|
||||
cursor = connect.cursor()
|
||||
sql = "INSERT INTO time_entries ( starttime, endtime, user_id, activity ) VALUES ( ?, ?, ?, ? )"
|
||||
|
||||
sql = "INSERT INTO time_entries ( starttime, endtime, user_id, activity ) VALUES ( ?, ?, ?, ? )"
|
||||
try:
|
||||
cursor.execute(sql, [data[1], endtime, self.USERID, action])
|
||||
except:
|
||||
with self.DBCON as con:
|
||||
con.execute(sql, [data[1], endtime, self.USERID, action])
|
||||
except sqlite3.Error as err:
|
||||
logging.error('Time entry could not be created')
|
||||
logging.debug(sql)
|
||||
logging.error('SQLError: {}'.format(err))
|
||||
print('Zeiteintrag konnte nicht gespeichert werden.')
|
||||
return False
|
||||
else:
|
||||
logging.info('Time entry was created successfully')
|
||||
connect.commit()
|
||||
|
||||
self.delete_event(data[0])
|
||||
self.print_time_entry(STARTTIME=data[1], ENDTIME=endtime, ACTIVITY=action)
|
||||
print('Zeiteintrag wurde gespeichert.')
|
||||
@@ -457,9 +447,6 @@ class TimeTrack:
|
||||
logging.debug('Terminated by the user')
|
||||
exit()
|
||||
|
||||
|
||||
connect.close()
|
||||
|
||||
elif userinput == "2":
|
||||
logging.info('Event should be deleted (eventid: {})'.format(data[0]))
|
||||
self.delete_event(data[0])
|
||||
@@ -484,5 +471,7 @@ class TimeTrack:
|
||||
print(ACTIVITY)
|
||||
print(50*"-")
|
||||
|
||||
if __name__ == "__main__":
|
||||
test = TimeTrack()
|
||||
test.time_start()
|
||||
test.DBCON.close()
|
||||
Reference in New Issue
Block a user