Compare commits
7 Commits
53d0f8d83f
...
dev
| Author | SHA1 | Date | |
|---|---|---|---|
| aa2c08838e | |||
| d06ab9d6f0 | |||
| f553f75462 | |||
| 106d93074d | |||
| e49d0c3ab5 | |||
| c9ac507ab5 | |||
| 33dd249601 |
146
timeTrack.py
146
timeTrack.py
@@ -3,7 +3,7 @@
|
||||
#
|
||||
# timeTrack.py
|
||||
# by 4nima
|
||||
# v.2.1.2
|
||||
# v.2.2.0a
|
||||
#
|
||||
#########################
|
||||
# simple time tracking with database
|
||||
@@ -23,29 +23,32 @@ class TimeTrack:
|
||||
self.USERID = 0
|
||||
self.USERNAME = ''
|
||||
self.OLDEVENT = 2
|
||||
self.CLIENTS = False
|
||||
self.CATEGORIES = False
|
||||
self.REFERENCES = False
|
||||
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,
|
||||
format='%(asctime)s - %(process)d-%(levelname)s: %(message)s',
|
||||
datefmt='%Y-%m-%d %H:%M:%S'
|
||||
)
|
||||
if os.name == 'posix':
|
||||
logging.debug('Unix/Linux system detected')
|
||||
self.LINUX = True
|
||||
else:
|
||||
logging.debug('Winwos System detected')
|
||||
|
||||
self.db_setup()
|
||||
self.load_config()
|
||||
|
||||
def __del__(self):
|
||||
self.DBCON.close()
|
||||
|
||||
## Prepartation
|
||||
#==============
|
||||
### Check OS and clear screen
|
||||
def clear_screen(self):
|
||||
if os.name == 'posix':
|
||||
logging.debug('Unix/Linux system detected')
|
||||
if self.LINUX:
|
||||
_ = os.system('clear')
|
||||
else:
|
||||
logging.debug('Winwos System detected')
|
||||
_ = os.system('cls')
|
||||
|
||||
### Loads or creates a config file
|
||||
@@ -61,17 +64,23 @@ class TimeTrack:
|
||||
quit()
|
||||
else:
|
||||
logging.info('Config file was loaded successfully')
|
||||
self.USERID = data['user']
|
||||
self.OLDEVENT = data['oldevent']
|
||||
logging.debug('UserID {} was used'.format(data['user']))
|
||||
self.USERID = data['userid']
|
||||
self.OLDEVENT = data['oldevent']
|
||||
self.CLIENTS = data['clients']
|
||||
self.CATEGORIES = data['categories']
|
||||
self.REFERENCES = data['references']
|
||||
logging.debug('UserID {} was used'.format(data['userid']))
|
||||
self.set_user()
|
||||
|
||||
else:
|
||||
logging.warning('Config file not found')
|
||||
config = {
|
||||
'default' : 'interactive',
|
||||
'user' : 1,
|
||||
'oldevent' : 2
|
||||
'userid' : 1,
|
||||
'oldevent' : 2,
|
||||
'clients' : False,
|
||||
'categories' : False,
|
||||
'references' : False
|
||||
}
|
||||
with open(self.CONFIG, "w") as outfile:
|
||||
json.dump(config, outfile, indent=4, sort_keys=True)
|
||||
@@ -137,7 +146,7 @@ class TimeTrack:
|
||||
|
||||
logging.debug('Create initial database tables')
|
||||
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:
|
||||
con.execute(SQL)
|
||||
except sqlite3.Error as err:
|
||||
@@ -281,8 +290,9 @@ class TimeTrack:
|
||||
logging.debug('Accepted username: {}'.format(username))
|
||||
|
||||
sql = "INSERT INTO users ( name ) values ( ? )"
|
||||
|
||||
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])
|
||||
except sqlite3.Error as err:
|
||||
logging.error('User could not be saved in database')
|
||||
@@ -307,7 +317,7 @@ class TimeTrack:
|
||||
sql = "SELECT * FROM users"
|
||||
|
||||
try:
|
||||
with self.DBCON as con:
|
||||
with sqlite3.connect(self.DATABASE, detect_types=sqlite3.PARSE_DECLTYPES | sqlite3.PARSE_COLNAMES) as con:
|
||||
cur = con.cursor()
|
||||
if data:
|
||||
cur.execute(sql, [data])
|
||||
@@ -339,6 +349,23 @@ class TimeTrack:
|
||||
else:
|
||||
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
|
||||
#===============
|
||||
### Creates an active event if none exists for the user
|
||||
@@ -348,7 +375,7 @@ class TimeTrack:
|
||||
|
||||
sql = "INSERT INTO active_events ( starttime, user_id ) VALUES ( ?, ? )"
|
||||
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])
|
||||
except sqlite3.Error as err:
|
||||
logging.error('Event could not be created')
|
||||
@@ -381,7 +408,7 @@ class TimeTrack:
|
||||
return 0
|
||||
|
||||
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])
|
||||
except sqlite3.Error as err:
|
||||
logging.error('Event could not be deleted')
|
||||
@@ -484,7 +511,7 @@ class TimeTrack:
|
||||
|
||||
sql = "INSERT INTO time_entries ( starttime, endtime, user_id, activity ) VALUES ( ?, ?, ?, ? )"
|
||||
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])
|
||||
except sqlite3.Error as err:
|
||||
logging.error('Time entry could not be created')
|
||||
@@ -524,7 +551,8 @@ class TimeTrack:
|
||||
'Was willst du tun?',
|
||||
'[1] Zeiterfassung starten',
|
||||
'[2] heutiger Report',
|
||||
'[3] Benutzerverwaltung',
|
||||
'[3] Reports',
|
||||
'[4] Benutzerverwaltung',
|
||||
'[0] Programm verlassen'
|
||||
]
|
||||
userinput = self.userchoise(printtext, 4)
|
||||
@@ -537,14 +565,42 @@ class TimeTrack:
|
||||
self.report_by_day(DETAIL=True)
|
||||
input()
|
||||
elif userinput == 3:
|
||||
self.interactive_usermanagment()
|
||||
self.interactive_reports()
|
||||
elif userinput == 4:
|
||||
print('commig soon ...')
|
||||
input()
|
||||
self.interactive_usermanagment()
|
||||
else:
|
||||
exit()
|
||||
self.start_interactive_mode()
|
||||
|
||||
### Reports menu
|
||||
def interactive_reports(self):
|
||||
self.clear_screen()
|
||||
printtext = [
|
||||
'==> Reports für {}'.format(self.USERNAME),
|
||||
'Welcher Report soll angezeigt werden?',
|
||||
'[1] Tages Report von [Datum]',
|
||||
'[0] abbrechen'
|
||||
]
|
||||
userinput = self.userchoise(printtext, 2)
|
||||
|
||||
if userinput == 1:
|
||||
valid = False
|
||||
while not valid:
|
||||
self.clear_screen()
|
||||
print('Report für welches Datum soll angezeigt werden?')
|
||||
print('[0] für abbruch')
|
||||
userdate = input("Datum: [yyyy-mm-dd]:")
|
||||
if userdate == 0:
|
||||
break
|
||||
else:
|
||||
try:
|
||||
date = datetime.datetime.strptime(userdate, "%Y-%m-%d")
|
||||
valid = True
|
||||
except:
|
||||
pass
|
||||
self.report_by_day(date.date())
|
||||
|
||||
### Usermanagment menu
|
||||
def interactive_usermanagment(self):
|
||||
self.clear_screen()
|
||||
printtext = [
|
||||
@@ -560,9 +616,28 @@ class TimeTrack:
|
||||
if userinput == 1:
|
||||
self.create_user()
|
||||
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:
|
||||
pass
|
||||
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])
|
||||
|
||||
## Reports
|
||||
#=========
|
||||
@@ -571,6 +646,11 @@ class TimeTrack:
|
||||
if not USER:
|
||||
USER = self.USERID
|
||||
timedata = self.get_time_entry(DAY=DATE, USERID=USER)
|
||||
if timedata == 0:
|
||||
print("Es wurden keine Zeiteinträge für den angegeben Tag gefunden")
|
||||
logging.debug('No time entries for date: '.format(DATE))
|
||||
input()
|
||||
return 1
|
||||
|
||||
STARTTIMES = []
|
||||
ENDTIMES = []
|
||||
@@ -590,12 +670,13 @@ class TimeTrack:
|
||||
ENTRIECOUNT = len(timedata)
|
||||
|
||||
self.clear_screen()
|
||||
print('{:40} {}'.format("Beginn des ersten Zeiteintrags:", FIRSTSTARTTIME.strftime('%d.%m.%Y %H:%M')))
|
||||
print('{:40} {}'.format("Ende des letzen Zeiteintrags:", LASTENDTIME.strftime('%d.%m.%Y %H:%M')))
|
||||
print('{:40} {}'.format("Maximal erfassbare Zeit:", self.timedela_to_string(TRACKEDTIMESPAN)))
|
||||
print('{:40} {}'.format("Nicht erfasste Zeit:", self.timedela_to_string(NONTRACKEDTIME)))
|
||||
print('{:40} {}'.format("Erfasste Zeit:", self.timedela_to_string(TRACKEDTIME)))
|
||||
print('{:40} {}'.format("Zeiteinträge:", ENTRIECOUNT))
|
||||
text = "{:40} {}"
|
||||
print(text.format("Beginn des ersten Zeiteintrags:", FIRSTSTARTTIME.strftime('%d.%m.%Y %H:%M')))
|
||||
print(text.format("Ende des letzen Zeiteintrags:", LASTENDTIME.strftime('%d.%m.%Y %H:%M')))
|
||||
print(text.format("Maximal erfassbare Zeit:", self.timedela_to_string(TRACKEDTIMESPAN)))
|
||||
print(text.format("Nicht erfasste Zeit:", self.timedela_to_string(NONTRACKEDTIME)))
|
||||
print(text.format("Erfasste Zeit:", self.timedela_to_string(TRACKEDTIME)))
|
||||
print(text.format("Zeiteinträge:", ENTRIECOUNT))
|
||||
|
||||
printtext = [
|
||||
'Zeiteinträge anzeigen?',
|
||||
@@ -653,5 +734,4 @@ class TimeTrack:
|
||||
|
||||
if __name__ == "__main__":
|
||||
test = TimeTrack()
|
||||
test.start_interactive_mode()
|
||||
test.DBCON.close()
|
||||
test.start_interactive_mode()
|
||||
Reference in New Issue
Block a user