Compare commits
5 Commits
53d0f8d83f
...
f553f75462
| Author | SHA1 | Date | |
|---|---|---|---|
| f553f75462 | |||
| 106d93074d | |||
| e49d0c3ab5 | |||
| c9ac507ab5 | |||
| 33dd249601 |
93
timeTrack.py
93
timeTrack.py
@@ -3,7 +3,7 @@
|
|||||||
#
|
#
|
||||||
# timeTrack.py
|
# timeTrack.py
|
||||||
# by 4nima
|
# by 4nima
|
||||||
# v.2.1.2
|
# v.2.1.5
|
||||||
#
|
#
|
||||||
#########################
|
#########################
|
||||||
# simple time tracking with database
|
# simple time tracking with database
|
||||||
@@ -23,8 +23,10 @@ class TimeTrack:
|
|||||||
self.USERID = 0
|
self.USERID = 0
|
||||||
self.USERNAME = ''
|
self.USERNAME = ''
|
||||||
self.OLDEVENT = 2
|
self.OLDEVENT = 2
|
||||||
|
self.CLIENTS = False
|
||||||
|
self.CATEGORIES = False
|
||||||
|
self.REFERENCES = False
|
||||||
self.LOGFILE = 'timetrack.log'
|
self.LOGFILE = 'timetrack.log'
|
||||||
self.DBCON = sqlite3.connect(self.DATABASE, detect_types=sqlite3.PARSE_DECLTYPES | sqlite3.PARSE_COLNAMES)
|
|
||||||
logging.basicConfig(
|
logging.basicConfig(
|
||||||
filename=self.LOGFILE,
|
filename=self.LOGFILE,
|
||||||
level=logging.DEBUG,
|
level=logging.DEBUG,
|
||||||
@@ -34,9 +36,6 @@ class TimeTrack:
|
|||||||
self.db_setup()
|
self.db_setup()
|
||||||
self.load_config()
|
self.load_config()
|
||||||
|
|
||||||
def __del__(self):
|
|
||||||
self.DBCON.close()
|
|
||||||
|
|
||||||
## Prepartation
|
## Prepartation
|
||||||
#==============
|
#==============
|
||||||
### Check OS and clear screen
|
### Check OS and clear screen
|
||||||
@@ -61,17 +60,23 @@ class TimeTrack:
|
|||||||
quit()
|
quit()
|
||||||
else:
|
else:
|
||||||
logging.info('Config file was loaded successfully')
|
logging.info('Config file was loaded successfully')
|
||||||
self.USERID = data['user']
|
self.USERID = data['userid']
|
||||||
self.OLDEVENT = data['oldevent']
|
self.OLDEVENT = data['oldevent']
|
||||||
logging.debug('UserID {} was used'.format(data['user']))
|
self.CLIENTS = data['clients']
|
||||||
|
self.CATEGORIES = data['categories']
|
||||||
|
self.REFERENCES = data['references']
|
||||||
|
logging.debug('UserID {} was used'.format(data['userid']))
|
||||||
self.set_user()
|
self.set_user()
|
||||||
|
|
||||||
else:
|
else:
|
||||||
logging.warning('Config file not found')
|
logging.warning('Config file not found')
|
||||||
config = {
|
config = {
|
||||||
'default' : 'interactive',
|
'default' : 'interactive',
|
||||||
'user' : 1,
|
'userid' : 1,
|
||||||
'oldevent' : 2
|
'oldevent' : 2,
|
||||||
|
'clients' : False,
|
||||||
|
'categories' : False,
|
||||||
|
'references' : False
|
||||||
}
|
}
|
||||||
with open(self.CONFIG, "w") as outfile:
|
with open(self.CONFIG, "w") as outfile:
|
||||||
json.dump(config, outfile, indent=4, sort_keys=True)
|
json.dump(config, outfile, indent=4, sort_keys=True)
|
||||||
@@ -137,7 +142,7 @@ class TimeTrack:
|
|||||||
|
|
||||||
logging.debug('Create initial database tables')
|
logging.debug('Create initial database tables')
|
||||||
try:
|
try:
|
||||||
with self.DBCON as con:
|
with sqlite3.connect(self.DATABASE, detect_types=sqlite3.PARSE_DECLTYPES | sqlite3.PARSE_COLNAMES) as con:
|
||||||
for SQL in sql:
|
for SQL in sql:
|
||||||
con.execute(SQL)
|
con.execute(SQL)
|
||||||
except sqlite3.Error as err:
|
except sqlite3.Error as err:
|
||||||
@@ -281,8 +286,9 @@ class TimeTrack:
|
|||||||
logging.debug('Accepted username: {}'.format(username))
|
logging.debug('Accepted username: {}'.format(username))
|
||||||
|
|
||||||
sql = "INSERT INTO users ( name ) values ( ? )"
|
sql = "INSERT INTO users ( name ) values ( ? )"
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with self.DBCON as con:
|
with sqlite3.connect(self.DATABASE, detect_types=sqlite3.PARSE_DECLTYPES | sqlite3.PARSE_COLNAMES) as con:
|
||||||
con.execute(sql, [username])
|
con.execute(sql, [username])
|
||||||
except sqlite3.Error as err:
|
except sqlite3.Error as err:
|
||||||
logging.error('User could not be saved in database')
|
logging.error('User could not be saved in database')
|
||||||
@@ -307,7 +313,7 @@ class TimeTrack:
|
|||||||
sql = "SELECT * FROM users"
|
sql = "SELECT * FROM users"
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with self.DBCON as con:
|
with sqlite3.connect(self.DATABASE, detect_types=sqlite3.PARSE_DECLTYPES | sqlite3.PARSE_COLNAMES) as con:
|
||||||
cur = con.cursor()
|
cur = con.cursor()
|
||||||
if data:
|
if data:
|
||||||
cur.execute(sql, [data])
|
cur.execute(sql, [data])
|
||||||
@@ -339,6 +345,23 @@ class TimeTrack:
|
|||||||
else:
|
else:
|
||||||
self.USERNAME = data[0][1]
|
self.USERNAME = data[0][1]
|
||||||
|
|
||||||
|
def delete_user(self, UID=False):
|
||||||
|
if not UID:
|
||||||
|
logging.error('no userid passed for deletion')
|
||||||
|
else:
|
||||||
|
logging.debug('Userid {} will be delete'.format(UID))
|
||||||
|
sql = "DELETE FROM users WHERE id = ?"
|
||||||
|
|
||||||
|
try:
|
||||||
|
with sqlite3.connect(self.DATABASE, detect_types=sqlite3.PARSE_DECLTYPES | sqlite3.PARSE_COLNAMES) as con:
|
||||||
|
con.execute(sql, [UID])
|
||||||
|
except sqlite3.Error as err:
|
||||||
|
logging.error('User could not be delete from database')
|
||||||
|
logging.debug(sql)
|
||||||
|
logging.error('SQLError: {}'.format(err))
|
||||||
|
else:
|
||||||
|
logging.info('User was delete successfully')
|
||||||
|
|
||||||
## Time handling
|
## Time handling
|
||||||
#===============
|
#===============
|
||||||
### Creates an active event if none exists for the user
|
### Creates an active event if none exists for the user
|
||||||
@@ -348,7 +371,7 @@ class TimeTrack:
|
|||||||
|
|
||||||
sql = "INSERT INTO active_events ( starttime, user_id ) VALUES ( ?, ? )"
|
sql = "INSERT INTO active_events ( starttime, user_id ) VALUES ( ?, ? )"
|
||||||
try:
|
try:
|
||||||
with self.DBCON as con:
|
with sqlite3.connect(self.DATABASE, detect_types=sqlite3.PARSE_DECLTYPES | sqlite3.PARSE_COLNAMES) as con:
|
||||||
con.execute(sql, [TIME, self.USERID])
|
con.execute(sql, [TIME, self.USERID])
|
||||||
except sqlite3.Error as err:
|
except sqlite3.Error as err:
|
||||||
logging.error('Event could not be created')
|
logging.error('Event could not be created')
|
||||||
@@ -381,7 +404,7 @@ class TimeTrack:
|
|||||||
return 0
|
return 0
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with self.DBCON as con:
|
with sqlite3.connect(self.DATABASE, detect_types=sqlite3.PARSE_DECLTYPES | sqlite3.PARSE_COLNAMES) as con:
|
||||||
con.execute(sql, [data])
|
con.execute(sql, [data])
|
||||||
except sqlite3.Error as err:
|
except sqlite3.Error as err:
|
||||||
logging.error('Event could not be deleted')
|
logging.error('Event could not be deleted')
|
||||||
@@ -484,7 +507,7 @@ class TimeTrack:
|
|||||||
|
|
||||||
sql = "INSERT INTO time_entries ( starttime, endtime, user_id, activity ) VALUES ( ?, ?, ?, ? )"
|
sql = "INSERT INTO time_entries ( starttime, endtime, user_id, activity ) VALUES ( ?, ?, ?, ? )"
|
||||||
try:
|
try:
|
||||||
with self.DBCON as con:
|
with sqlite3.connect(self.DATABASE, detect_types=sqlite3.PARSE_DECLTYPES | sqlite3.PARSE_COLNAMES) as con:
|
||||||
con.execute(sql, [data[1], endtime, self.USERID, action])
|
con.execute(sql, [data[1], endtime, self.USERID, action])
|
||||||
except sqlite3.Error as err:
|
except sqlite3.Error as err:
|
||||||
logging.error('Time entry could not be created')
|
logging.error('Time entry could not be created')
|
||||||
@@ -560,8 +583,28 @@ class TimeTrack:
|
|||||||
if userinput == 1:
|
if userinput == 1:
|
||||||
self.create_user()
|
self.create_user()
|
||||||
elif userinput == 2:
|
elif userinput == 2:
|
||||||
pass
|
users = self.get_users()
|
||||||
|
printtext = ['Wähle deinen User:']
|
||||||
|
count = 1
|
||||||
|
for user in users:
|
||||||
|
printtext.append('[{}] {}'.format(count, user[1]))
|
||||||
|
count += 1
|
||||||
|
|
||||||
|
printtext.append('[0] abbrechen')
|
||||||
|
userid = self.userchoise(printtext, count)
|
||||||
|
self.USERID = users[userid - 1][0]
|
||||||
|
self.set_user()
|
||||||
elif userinput == 3:
|
elif userinput == 3:
|
||||||
|
users = self.get_users()
|
||||||
|
printtext = ['Welcher User soll gelöscht werden?:']
|
||||||
|
count = 1
|
||||||
|
for user in users:
|
||||||
|
printtext.append('[{}] {}'.format(count, user[1]))
|
||||||
|
count += 1
|
||||||
|
|
||||||
|
printtext.append('[0] abbrechen')
|
||||||
|
userid = self.userchoise(printtext, count)
|
||||||
|
self.delete_user(users[userid - 1][0])
|
||||||
pass
|
pass
|
||||||
|
|
||||||
## Reports
|
## Reports
|
||||||
@@ -590,12 +633,13 @@ class TimeTrack:
|
|||||||
ENTRIECOUNT = len(timedata)
|
ENTRIECOUNT = len(timedata)
|
||||||
|
|
||||||
self.clear_screen()
|
self.clear_screen()
|
||||||
print('{:40} {}'.format("Beginn des ersten Zeiteintrags:", FIRSTSTARTTIME.strftime('%d.%m.%Y %H:%M')))
|
text = "{:40} {}"
|
||||||
print('{:40} {}'.format("Ende des letzen Zeiteintrags:", LASTENDTIME.strftime('%d.%m.%Y %H:%M')))
|
print(text.format("Beginn des ersten Zeiteintrags:", FIRSTSTARTTIME.strftime('%d.%m.%Y %H:%M')))
|
||||||
print('{:40} {}'.format("Maximal erfassbare Zeit:", self.timedela_to_string(TRACKEDTIMESPAN)))
|
print(text.format("Ende des letzen Zeiteintrags:", LASTENDTIME.strftime('%d.%m.%Y %H:%M')))
|
||||||
print('{:40} {}'.format("Nicht erfasste Zeit:", self.timedela_to_string(NONTRACKEDTIME)))
|
print(text.format("Maximal erfassbare Zeit:", self.timedela_to_string(TRACKEDTIMESPAN)))
|
||||||
print('{:40} {}'.format("Erfasste Zeit:", self.timedela_to_string(TRACKEDTIME)))
|
print(text.format("Nicht erfasste Zeit:", self.timedela_to_string(NONTRACKEDTIME)))
|
||||||
print('{:40} {}'.format("Zeiteinträge:", ENTRIECOUNT))
|
print(text.format("Erfasste Zeit:", self.timedela_to_string(TRACKEDTIME)))
|
||||||
|
print(text.format("Zeiteinträge:", ENTRIECOUNT))
|
||||||
|
|
||||||
printtext = [
|
printtext = [
|
||||||
'Zeiteinträge anzeigen?',
|
'Zeiteinträge anzeigen?',
|
||||||
@@ -653,5 +697,4 @@ class TimeTrack:
|
|||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
test = TimeTrack()
|
test = TimeTrack()
|
||||||
test.start_interactive_mode()
|
test.start_interactive_mode()
|
||||||
test.DBCON.close()
|
|
||||||
Reference in New Issue
Block a user