forked from mik-st/TimeTrackingProject
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.py
More file actions
662 lines (480 loc) · 22.5 KB
/
functions.py
File metadata and controls
662 lines (480 loc) · 22.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
import json
import time
from datetime import date, timedelta
from dateutil import parser
import pandas as pd
from IPython.display import display
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.message import EmailMessage
import ssl
import smtplib
msg='error'
def write (whole_data , file_name) : #re-write our updated dictionary
with open(file_name, "w") as file:
json.dump(whole_data, file)
def read ( file_name) : #return a python dict
with open(file_name , 'r') as file:
if file.read() !='' :
file.seek(0)
return json.loads( file.read())
else :
return {}
json_file='users.json'
dicti=read(json_file)
break_time=5
study_time=25
long=30
def visualize(name) :
#this would take a number from 1 to 9( depending on the sessions completed )
#and return a progress bar that represent percentage of how much is done in the currenct session : depending on that number
# ['study1', , 'break1' , 'study2', 'break2', study3 , 'break3','study4' , long_break']
#total time (100% of the promodoro) should be : 25+5+25+5+25+5+25+30 = 145 minutes
#Example : visualize (3) >> should return the sketch : meaning : he has completed s1 and b1 and now he is standing on s3
#example : visualize (1) >> means you have not started the promodoro > you are on s1
#example : visualize (9) >> you have done all >> the man should be standing on lastpoint (at 9) █
def printProgressBar (iteration, total, prefix = '', suffix = '', decimals = 1, length = 100, fill = '>', printEnd = "\r"):
percent = ("{0:." + str(decimals) + "f}").format(100 * (iteration / float(total)))
filledLength = int(length * iteration // total)
bar = fill * filledLength + '>>웃' +'-' * (length - filledLength)
return f'\r{prefix} |{bar}| {percent}% {suffix}'
# Print New Line on Complete
if iteration == total:
print()
global msg
s=['start', 'study1', 'break1' , 'study2', 'break2', 'study3' , 'break3','study4' , 'long_break', 'finish' ]
p=[0,25,5,25,5,25,5,25,30]
index_done = s.index(name)
sum_done=sum (p[: index_done] )
items = list(range(0,145))
l = len(items)
if name=='finish' :
name='study1'
msg= printProgressBar( sum_done , l, prefix = 'Progress:', suffix = f'Completed. Next >> {name}', length = 50)
def send_emails(data, recipients ):
global msg
email_sender = 'test.email.group3@gmail.com'
password = 'ojnertaqnwmklank'
# recipients = ['mikdad.kanbar1@gmail.com' ]
emaillist = [elem.strip().split(',') for elem in recipients]
email_msg = MIMEMultipart()
email_msg['Subject'] = "Time Tracking Report"
email_msg['From'] = 'test.email.group3@gmail.com'
df = pd.DataFrame(data = data[1:], columns = data[0])
html = f"""\
<html>
<head></head>
<body>
This is your history report requested on {current_date(), current_time()}
{ df . to_html()}
</body>
</html>
"""
part1 = MIMEText(html, 'html')
email_msg.attach(part1)
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
smtp.login(email_sender, password)
smtp.sendmail(recipients, recipients , email_msg.as_string())
msg='Email Sent Successfully'
# def send_email (data, recipient ):
# global msg
# if type ( data ) == list and len(data) > 2 :
# #converting it to DataFrame , first list would be the header
# data = pd.DataFrame(data[1:], columns = data[0])
# email_sender = 'test.email.group3@gmail.com'
# password = 'ojnertaqnwmklank'
# subject = 'TimeTrackingApp Report'
# body = f'''
# This is a report requested on {current_date()}
# {data}'''
# em = EmailMessage()
# em['From'] = email_sender
# em['To'] = recipient
# em['Subject'] = subject
# em.set_content(body)
# context = ssl.create_default_context()
# with smtplib.SMTP_SSL('smtp.gmail.com', 465, context=context) as smtp:
# smtp.login(email_sender, password)
# smtp.sendmail(email_sender, recipient , em.as_string())
# msg='Email Sent Successfully'
# global msg
# if len(recipients) > 0 :
# for recipient in recipients :
# send_email(data,recipient)
# msg='Emails Sent Successfully'
# else:
# msg='No Emails found!'
def email_spelling_control (email) :
if email !='' and '@' in email and '.' in email and email.count( '@') ==1 and email[email.index('@'):].count('.')==1 and email[-1]!= '.' and email.count(' ')==0 :
return True
else :
return False
def name_spelling_control (name) :
if name!='' and name[0] !=' ' and name.count(' ') <2 and name[-1] != ' ' :
return True
else:
return False
def signup (email, name) :
global msg
email=email.lower()
if email_spelling_control(email) and name_spelling_control(name) :
if email not in dicti.keys() :
new_user_data ={ email : {"name": name,
"projects": {} ,
'recipients' : [email] } }
dicti.update (new_user_data)
write(dicti,json_file)
msg=f'{name} is successfully registered!'
return True
else:
msg= 'Email already registered!'
return False
else:
msg='Email or name format is incorrect!'
def login(email):
global msg
email=email.lower()
if email_spelling_control(email) :
if email in dicti.keys() :
global user1
user1=User(email)
msg=f'{user1.name} is logged in'
return True
else :
user1='Not found'
msg='Email not registered!'
return False
else:
msg='Email format is incorrect'
def time_difference(a,b) :
return str(parser.parse(b) - parser.parse(a ))[:-3]
def current_time() :
t = time.localtime()
return time.strftime("%H:%M", t)
def current_date():
today = date.today()
return today.strftime("%d %B, %Y")
def timer (t) :
global msg
t*=60
print (f't is {t}')
while t:
mins, secs = divmod(t, 60)
timer = '{:02d}:{:02d}'.format(mins, secs)
# msg=f'timer, end="\r"'
print (timer, end="\r")
time.sleep(1)
t -= 1
# print (msg)
msg='Finishedhhhh!'
class User :
def __init__ ( self, email):
self.data = dicti.get (email)
self.email=email
self.name=self.data.get('name')
self.projects= self.data.get('projects')
self.recipients=self.data.get('recipients')
self.history=[]
#
def add_recipient (self , email) :
global msg
if email_spelling_control (email):
if email not in self.recipients :
self.recipients.append ( email)
self.update()
msg='Recipient Added Successfully!'
else:
msg='Recipient Already Existed!'
else:
msg='Email format is incorrect!'
def delete_recipient(self, email) :
global msg
if email_spelling_control (email):
if email in self.recipients :
self.recipients.remove ( email)
self.update()
msg='Recipient Deleted Successfully!'
else:
msg='Recipient Not Found!!'
else:
msg='Email format is incorrect!'
def project_existed (self, project) :
if project in self.data.get('projects').keys() :
return True
else:
return False
def subject_existed (self, project, subject) :
if self.project_existed(project) and subject in self.data.get('projects') .get(project).keys():
return True
else:
return False
def add_project (self,project) :
global msg
if name_spelling_control(project) :
if not self.project_existed (project) :
self.data.get('projects')[project]={}
self.update()
msg=f'{project} is successfully added !'
else :
msg= f'{project} is already added before!'
# print (msg)
else :
msg='Project name format is incorrect!'
def delete_project (self,project) :
global msg
if self.project_existed (project) :
del self.projects [ project ]
self.update()
msg= f' {project} Deleted Successfully!'
else:
msg='Project Not found!'
print (msg)
def add_subject(self, project, subject) :
global msg
if name_spelling_control (subject) :
if self.project_existed ( project ) :
if not self.subject_existed(project, subject) :
self.data.get('projects') .get(project)[subject] = {}
self.add_promodoro ( project, subject)
self.update()
msg = f'{subject} added to Project {project}'
else :
msg='Subject already existed !'
else :
msg = 'Project Not existed !'
else:
msg='''Format of subject's name is incorrect '''
# print (msg)
def delete_subject(self, project, subject) :
global msg
if self.project_existed ( project ) :
if self.subject_existed(project, subject) :
del user1.data.get('projects') .get(project)[subject]
self.update()
msg = f'{subject} Deleted Successfully'
else :
msg='Subject Not Found! !'
else :
msg = 'Project Not existed !'
print (msg)
def add_promodoro(self, project, subject) :
global msg
if self.project_existed ( project ) :
if self.subject_existed ( project, subject ) :
task='task'
Break='break'
# #determine last promodoro number :
l=sorted ( list( self.data.get('projects') .get(project).get (subject).keys()) )
print (l)
if len(l) == 0 :
next_promodoro='1'
else :
last_promodoro= int (l[-1])
print (last_promodoro)
next_promodoro = str( last_promodoro +1 )
# [0, Break, 0 , break_time, 'start', 'finish','time consumed' , 'date' ]
self.data.get('projects') .get(project).get (subject)[ next_promodoro ] = {
'study1' : [ 0, task, 0 , study_time, '00:00', '00:00','00:00' , 'date' ],
'break1' : [0, Break, 0 , break_time,'00:00', '00:00','00:00' , 'date' ],
'study2' : [ 0,task, 0 , study_time, '00:00', '00:00','00:00' , 'date' ],
'break2' : [ 0,Break, 0 , break_time, '00:00', '00:00','00:00' , 'date' ],
'study3' : [ 0,task,0 , study_time, '00:00', '00:00','00:00' , 'date' ],
'break3' : [0, Break, 0 , break_time, '00:00', '00:00','00:00' , 'date' ],
'study4' : [0, task, 0 , study_time, '00:00', '00:00','00:00' , 'date' ],
'long_break' : [0, Break, 0 , long, '00:00', '00:00','00:00' , 'date' ],
}
self.update()
# msg=f'{next_promodoro} successfully added'
# print (msg)
else:
msg='Subject Not Found'
else:
msg='Project Not found !'
print (msg)
def delete_promodoro(self, project, subject , promodoro ):
global msg
if self.project_existed ( project ) :
if self.subject_existed ( project, subject ) :
if promodoro in self.data.get('projects').get(project).get(subject) :
del self.data.get('projects').get(project).get(subject)[promodoro]
self.update()
msg='Promodoro Deleted Successfully !'
else:
msg='Promodoro Not Found!'
else:
msg='Subject Not Found'
else:
msg='Project Not found !'
print (msg)
def get_promodoros (self, project, subject ) :
global msg
if self.project_existed ( project ) :
if self.subject_existed ( project, subject ) :
list_p= list(self.data.get('projects').get(project).get (subject).keys())
# msg='done'
return list_p
else:
msg='Subject Not Found'
else:
msg='Project Not found !'
# print (msg)
def get_sessions (self, project, subject, promodoro) :
global msg
if self.project_existed ( project ) :
if self.subject_existed ( project, subject ) :
if promodoro in self.data.get('projects').get(project).get (subject).keys() :
list_sessions = list(self.data.get('projects').get(project).get (subject).get(promodoro).keys())
# msg='list of sessions returned successfully'
# print (msg)
return list_sessions
else:
msg='Promodoro Not existed!'
else:
msg='Subject Not Found'
else:
msg='Project Not found !'
def get_subjects (self, project):
if self.project_existed ( project ) :
subjects_list = list(self.data.get('projects').get(project).keys())
if len( subjects_list ) > 0 :
return subjects_list
else:
return ['...' ]
else :
msg = 'Project Not existed !'
return ['...']
def get_projects(self) :
projects_list = list(self.data.get('projects').keys())
if len( projects_list ) > 0 :
return projects_list
else :
return ['...' ]
def get_history (self, project, subject,before_days) :
# this should return all sessions for a subject , as lists , each for a session (rows)
#all sessions must satisfy the date condition (should be after the date passed)
#first : list all sessions , then we will return only those which satisfy the date
all_sessions =[]
current_list=[]
date_point=0
if before_days!='All' :
before_days=int(before_days)
#creating a new date point : by subtracting this number of days from current date
date_point = parser.parse (current_date()) - timedelta(days=before_days)
if self. project_existed(project) and self. subject_existed (project ,subject):
#1- get promodoros : 2-get sessions for each promodoro
for p in self.get_promodoros(project, subject) :
# print (p)
for session2 in self.get_sessions(project, subject,p):
current_list = (self.data.get('projects') .get(project).get (subject).get(p).get(session2))
current_list = [session2] + current_list
# current_list.insert(0,session2)
# print (f'{session2} : {current_list}')
#now, before adding : let's filter it :
if (date_point ==0 ) or (current_list[-1] !='date' and parser.parse (current_list[-1]) > date_point ) :
if current_list[1]!=0 :
#now it from here until we append, it is all optional ,
# # changing numbers into readable text :
if current_list[1] ==1 :
current_list[1]='Yes'
else:
current_list[1]='Skipped'
if current_list[3]==0 :
current_list[3]='...'
elif current_list[3]==1 :
current_list[3]='Yes'
else:
current_list[3]='No'
all_sessions.append(current_list)
#filtering all sessions :
self.history=all_sessions.copy()
header = [ 'Name of session', 'Session Done', 'Task','Task Done' , 'Normal Time', 'Start Time', 'Finish Time','Time Consumed' , 'Date' ]
self.history.insert(0, header)
return all_sessions
def get_next_session (self , project, subject ) :
#list all promodoros :
global msg
next_s='all sessions done > create new promodoro'
if self.project_existed(project) and self.subject_existed (project, subject) :
last_p=self.get_promodoros ( project, subject) [-1]
# get sesisons inside it :
# msg=f'last P is : {last_p} '
lastP_sessions= self.data.get('projects').get(project).get (subject).get(last_p)
for _session in lastP_sessions.keys() :
if lastP_sessions.get(_session)[0]==0 :
next_s=_session
break
print (next_s)
if next_s=='all sessions done > create new promodoro' :
self.add_promodoro(project, subject )
print ('New Promodoro Created!')
self.get_next_session(project, subject )
# creating a current object + passing all found info to it :
if next_s!='all sessions done > create new promodoro' :
global current
current = Current( project, subject , last_p , next_s )
print (current.__dict__)
return True
return False
# [ 0, task, 0 , study_time, 'start', 'finish','finish-start' , 'date' ]
def start_timing_session ( self,current ) :
global skipped
skipped= False
if isinstance (current, Current) :
current.session[4]=current_time() #start at
if not skipped :
current.session[0]=1 #done
current.session[5]=current_time() #finish
current.session[6]= time_difference( current.session[4] ,current.session[5])
current.session[-1]= current_date() #date
current.update_in_user()
def skip (self):
global msg
skipped=True
#first let's update the current session : make it -1 instead of 0 :
current.session[0]=-1 #skipped
current.session[5]=current_time() #finish
#if start date is not alreay there : add it :
if current.session[4] =='00:00' :
current.session[4] =current_time()
current.session[6]= time_difference( current.session[4] ,current.session[5])
current.session[-1]= current_date() #date
#if start is not yet registered > he skipped immediately
current.session[4]=current_time()
current.update_in_user()
msg='Session Skipped!'
def label_not_finished (self) :
global msg
if current.session_name not in ['break1', 'break2', 'break3','long_break'] and current.session[1] !='task' and current.session[0] !=0:
current.session[2]= -1 #task not done
current.update_in_user()
msg='Task labeled unfinished'
else:
msg='you need to either to start the session , or to add a task!'
def label_finished (self) :
global msg
if current.session_name not in ['break1', 'break2', 'break3','long_break'] and current.session[1] !='task' and current.session[0] !=0:
current.session[2]=1 #task done!
current.session[5]=current_time() #finish time
current.session[6]= time_difference( current.session[4] ,current.session[5]) #time consumed
current.session[-1]= current_date()
current.update_in_user()
msg='Task labeled finished!'
else:
msg='you need to add a task!'
def update(self) :
dicti.update ({self.email : { 'name' : self.name , 'projects' : self.projects , 'recipients': self.recipients }})
self.data = dicti.get (self.email)
write(dicti,json_file) #Re write
class Current():
def __init__(self, project, subject , promodoro , session ) :
self.project = project
self.subject = subject
self.promodoro = promodoro
self.session_name = session
self.session = user1.data.get('projects').get(project).get (subject).get(promodoro).get(session)
def update_in_user (self) :
user1.data.get('projects').get(self.project).get (self.subject).get(self.promodoro)[self.session_name]= self.session
user1.update()
print ('updated')
# print( visualize ('break1'))