create active event handling
This commit is contained in:
151
timeTrack.py
151
timeTrack.py
@@ -3,13 +3,14 @@
|
|||||||
#
|
#
|
||||||
# timeTrack.py
|
# timeTrack.py
|
||||||
# by 4nima
|
# by 4nima
|
||||||
# v.0.2
|
# v.0.3.0
|
||||||
#
|
#
|
||||||
#########################
|
#########################
|
||||||
# simple time tracking with database
|
# simple time tracking with database
|
||||||
#########################
|
#########################
|
||||||
|
|
||||||
import datetime
|
import datetime
|
||||||
|
from sqlite3.dbapi2 import Cursor
|
||||||
import time
|
import time
|
||||||
import sqlite3
|
import sqlite3
|
||||||
import json
|
import json
|
||||||
@@ -197,10 +198,10 @@ class TimeTrack:
|
|||||||
logging.debug('User database read out successfully')
|
logging.debug('User database read out successfully')
|
||||||
connect.commit()
|
connect.commit()
|
||||||
|
|
||||||
row = cursor.fetchall()
|
data = cursor.fetchall()
|
||||||
output = []
|
output = []
|
||||||
for data in row:
|
for row in data:
|
||||||
output.append(data)
|
output.append(row)
|
||||||
connect.close()
|
connect.close()
|
||||||
return output
|
return output
|
||||||
|
|
||||||
@@ -213,56 +214,116 @@ class TimeTrack:
|
|||||||
self.create_user()
|
self.create_user()
|
||||||
|
|
||||||
data = self.get_users(UID=self.USERID)
|
data = self.get_users(UID=self.USERID)
|
||||||
try:
|
if data == []:
|
||||||
self.USERNAME = data[0][1]
|
|
||||||
except IndexError:
|
|
||||||
logging.error('User ID was not found')
|
logging.error('User ID was not found')
|
||||||
|
else:
|
||||||
|
self.USERNAME = data[0][1]
|
||||||
|
|
||||||
def time_start(self):
|
## Time handling
|
||||||
starttime = datetime.datetime.now()
|
### Creates an active event if none exists for the user
|
||||||
time.sleep(10)
|
def set_event(self, TIME=datetime.datetime.now()):
|
||||||
endtime = datetime.datetime.now()
|
if not self.get_event(USERID=self.USERID):
|
||||||
print('Start: {}'.format(starttime))
|
logging.debug('No active events found for the user: {}'.format(self.USERID))
|
||||||
print('Ende: {}'.format(endtime))
|
connect = sqlite3.connect(self.DATABASE)
|
||||||
print('Diff: {}'.format(endtime - starttime))
|
cursor = connect.cursor()
|
||||||
sql = """
|
sql = "INSERT INTO active_events ( starttime, user_id ) VALUES ( ?, ? )"
|
||||||
INSERT INTO time_entries
|
|
||||||
( starttime, endtime, user_id )
|
try:
|
||||||
VALUES ( ?, ?, ?)
|
cursor.execute(sql, [TIME, self.USERID])
|
||||||
"""
|
except:
|
||||||
connect = sqlite3.connect(self.DATABASE)
|
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 = connect.cursor()
|
||||||
cursor.execute(sql, (starttime, endtime, self.USERID))
|
|
||||||
logging.debug(sql)
|
try:
|
||||||
|
cursor.execute(sql, [data])
|
||||||
connect.commit()
|
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()
|
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):
|
def time_end(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def get_time(self):
|
def get_time(self):
|
||||||
connect = sqlite3.connect(self.DATABASE, detect_types=sqlite3.PARSE_DECLTYPES | sqlite3.PARSE_COLNAMES)
|
pass
|
||||||
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()
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
test = TimeTrack()
|
test = TimeTrack()
|
||||||
test.time_start()
|
#test.delete_event(USERID=1)
|
||||||
test.get_time()
|
#test.set_event()
|
||||||
|
test.get_event()
|
||||||
Reference in New Issue
Block a user