From 972115ffebc1b129dcb8d0a7634e77a75ba4d3cd Mon Sep 17 00:00:00 2001
From: anima
Date: Thu, 3 Jun 2021 18:29:15 +0200
Subject: [PATCH] create active event handling
---
timeTrack.py | 151 ++++++++++++++++++++++++++++++++++++---------------
1 file changed, 106 insertions(+), 45 deletions(-)
diff --git a/timeTrack.py b/timeTrack.py
index 282fcf1..0198bd8 100644
--- a/timeTrack.py
+++ b/timeTrack.py
@@ -3,13 +3,14 @@
#
# timeTrack.py
# by 4nima
-# v.0.2
+# v.0.3.0
#
#########################
# simple time tracking with database
#########################
import datetime
+from sqlite3.dbapi2 import Cursor
import time
import sqlite3
import json
@@ -197,10 +198,10 @@ class TimeTrack:
logging.debug('User database read out successfully')
connect.commit()
- row = cursor.fetchall()
+ data = cursor.fetchall()
output = []
- for data in row:
- output.append(data)
+ for row in data:
+ output.append(row)
connect.close()
return output
@@ -213,56 +214,116 @@ class TimeTrack:
self.create_user()
data = self.get_users(UID=self.USERID)
- try:
- self.USERNAME = data[0][1]
- except IndexError:
+ if data == []:
logging.error('User ID was not found')
+ else:
+ self.USERNAME = data[0][1]
- def time_start(self):
- starttime = datetime.datetime.now()
- time.sleep(10)
- endtime = datetime.datetime.now()
- print('Start: {}'.format(starttime))
- print('Ende: {}'.format(endtime))
- print('Diff: {}'.format(endtime - starttime))
- sql = """
- INSERT INTO time_entries
- ( starttime, endtime, user_id )
- VALUES ( ?, ?, ?)
- """
- connect = sqlite3.connect(self.DATABASE)
+ ## Time handling
+ ### Creates an active event if none exists for the user
+ def set_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 ( ?, ? )"
+
+ try:
+ cursor.execute(sql, [TIME, self.USERID])
+ except:
+ logging.error('Event could not be created')
+ logging.debug(sql)
+ print('Event konnte nicht gespeichert werden.')
+ else:
+ logging.info('Event was created successfully')
+ connect.commit()
+
+ connect.close()
+ else:
+ logging.warning('Active events found for the user, new event could not be created')
+
+ ### Deletes an active event based on a user or event ID
+ def delete_event(self, ENTRYID='', USERID=''):
+ if not ENTRYID == '':
+ logging.info('Deletes event based on eventid: {}'.format(USERID))
+ sql = "DELETE FROM active_events WHERE id = ?"
+ data = ENTRYID
+ elif not USERID == '':
+ logging.info('Deletes events based on userid: {}'.format(USERID))
+ sql = "DELETE FROM active_events WHERE user_id = ?"
+ data = USERID
+ else:
+ logging.warning('No indication of what should be deleted')
+ 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()
- cursor.execute(sql, (starttime, endtime, self.USERID))
- logging.debug(sql)
-
- connect.commit()
+
+ try:
+ cursor.execute(sql, [data])
+ except:
+ logging.error('Event could not be deleted')
+ logging.debug(sql)
+ 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=''):
+ if not ENTRYID == '':
+ logging.debug('Search event based on eventid: {}'.format(ENTRYID))
+ sql = "SELECT * FROM active_events WHERE id = ?"
+ data = ENTRYID
+ elif not USERID == '':
+ logging.debug('Search events based on userid: {}'.format(USERID))
+ sql = "SELECT * FROM active_events WHERE user_id = ?"
+ data = USERID
+ else:
+ sql = "SELECT * FROM active_events"
+ data = ''
+
+ connect = sqlite3.connect(self.DATABASE, detect_types=sqlite3.PARSE_DECLTYPES | sqlite3.PARSE_COLNAMES)
+ cursor = connect.cursor()
+ try:
+ if data:
+ logging.debug('Search event')
+ cursor.execute(sql, [data])
+ else:
+ logging.debug('Get all Events')
+ cursor.execute(sql)
+ except:
+ logging.error('Events could not be read')
+ logging.debug(sql)
+ 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
+ else:
+ logging.debug('{} events found'.format(len(data)))
+ return data[0]
+####################################################################################################
+####################################################################################################
+####################################################################################################
+####################################################################################################
+ def time_start(self):
+ pass
+
def time_end(self):
pass
-
def get_time(self):
- connect = sqlite3.connect(self.DATABASE, detect_types=sqlite3.PARSE_DECLTYPES | sqlite3.PARSE_COLNAMES)
- cursor = connect.cursor()
-
- connect.commit()
-
- sql = """SELECT * FROM time_entries"""
- cursor.execute(sql)
-
- row = cursor.fetchall()
- for i in row:
- print(i)
- print('Start: {}'.format(i[1]))
- print('End: {}'.format(i[2]))
- print('Diff: {}'.format(i[2] - i[1]))
- endtime = i[2]
-
- connect.close()
-
+ pass
test = TimeTrack()
-test.time_start()
-test.get_time()
\ No newline at end of file
+#test.delete_event(USERID=1)
+#test.set_event()
+test.get_event()
\ No newline at end of file