This repository has been archived by the owner on May 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
todoapp.py
465 lines (386 loc) · 20.3 KB
/
todoapp.py
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
import datetime
import os
import pytz
from pytz import timezone
import gviz_api
from google.appengine.ext import db
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template
# register custom filters
template.register_template_library('utils.customfilters')
class TimezoneInfo(db.Model):
timezoneinfo = db.StringProperty(default='')
user = db.UserProperty(required=True)
class TodoList(db.Model):
daykey = db.StringProperty(required=True)
user = db.UserProperty(required=True)
dayscore = db.FloatProperty(default=0.0)
future_tasks = db.BooleanProperty(default=False)
class TodoItem(db.Model):
user = db.UserProperty(required=True)
date = db.DateTimeProperty(auto_now_add=True)
belongs_to = db.ReferenceProperty(TodoList)
description = db.StringProperty(multiline=True)
start_time = db.TimeProperty(required=False)
end_time = db.TimeProperty(required=False)
rating = db.IntegerProperty(required=True, default=0)
score = db.IntegerProperty(default=0)
class SetTimeZone(webapp.RequestHandler):
def get(self):
username = users.get_current_user()
if username:
path = os.path.join(os.path.dirname(__file__), 'timezone.html')
self.response.out.write(template.render(path, {}))
else:
self.redirect(users.create_login_url(self.request.uri))
def post(self):
username = users.get_current_user()
timezoneinfo = self.request.get('DropDownTimezone',default_value='UTC')
if username:
try:
timezone(timezoneinfo)
except pytz.UnknownTimeZoneError, e:
self.response.out.write("<h2>Unknown Timezone</h2>")
self.response.out.write("</br>")
self.response.out.write("<h2>Please <a href='/settimezone'>set timezone</a>.</h2>")
else:
timezoneinfo_obj = db.Query(TimezoneInfo)
filtered_timezoneinfo = timezoneinfo_obj.filter('user =', username)
# Override the previously set timezone.
# Create a checkbox to verify override.
if filtered_timezoneinfo.fetch(limit=1):
settimezone = TimezoneInfo(user=username, timezoneinfo=timezoneinfo)
settimezone.put()
else:
settimezone = filtered_timezoneinfo.get()
settimezone = TimezoneInfo(user=username)
settimezone.timezoneinfo = timezoneinfo
settimezone.put()
self.redirect('/')
else:
self.redirect(users.create_login_url(self.request.uri))
class UpdateTodo(webapp.RequestHandler):
def get(self):
# I think, we do not need get here as it is not done.
pass
def post(self):
username = users.get_current_user()
if username:
key = self.request.get('key',default_value=None)
if not key:
self.response.out.write("You do not have such a todo item")
else:
item = db.get(key)
item.description = self.request.get('description',
default_value='default')
item.rating = int(self.request.get('rating', default_value=10))
item.score = int(self.request.get('score',default_value=0))
# modularize
username = users.get_current_user()
timezoneinfo_obj = db.Query(TimezoneInfo)
filtered_timezoneinfo = timezoneinfo_obj.filter('user =', username)
if not filtered_timezoneinfo.fetch(limit=1):
self.redirect('/settimezone')
usertimezone = filtered_timezoneinfo.get()
user_tzinfo = timezone(usertimezone.timezoneinfo)
starttime = self.request.get('starttime',default_value='00:00')
endtime = self.request.get('endtime',default_value='00:00')
starttime_hour, starttime_min = map(int, starttime.split(':'))
endtime_hour, endtime_min = map(int, endtime.split(':'))
item.start_time = datetime.time(starttime_hour, starttime_min, tzinfo=user_tzinfo)
item.end_time = datetime.time(endtime_hour, endtime_min, tzinfo=user_tzinfo)
item.put()
self.redirect('/')
else:
self.redirect(users.create_login_url(self.request.uri))
class MainPage(webapp.RequestHandler):
def get(self):
username = users.get_current_user()
path = os.path.join(os.path.dirname(__file__), 'index.html')
template_values = {}
if username:
timezoneinfo_obj = db.Query(TimezoneInfo)
filtered_timezoneinfo = timezoneinfo_obj.filter('user =', username)
if not filtered_timezoneinfo.fetch(limit=1):
self.redirect('/settimezone')
else:
usertimezone = filtered_timezoneinfo.get()
user_tzinfo = timezone(usertimezone.timezoneinfo)
# Make it timezone aware
today = self.request.get('day',default_value='')
if not today:
today = datetime.datetime.now(user_tzinfo).strftime('%d%m%Y')
displaydate = datetime.datetime.now(user_tzinfo).strftime("%d-%m-%Y")
else:
displaydate = today[:2] + '-' + today[2:4] + '-' + today[4:]
datetime_obj = datetime.datetime.strptime(displaydate,"%d-%m-%Y")
displaydate = datetime_obj.strftime("%a %d %b %Y")
template_values.update({'displaydate': displaydate})
todolist_queryobj = db.Query(TodoList)
filtered_todolist = todolist_queryobj.filter('daykey =', today).filter('user =', username)
if not filtered_todolist.fetch(limit=1):
# There is no todolist for the day yet.
today_todolist = TodoList(daykey=today, user=username, future_tasks= False)
today_todolist.put()
# At this point pull unfinished from yesterday's
yesterday = datetime.datetime.now(user_tzinfo) - datetime.timedelta(1)
yesterday = yesterday.strftime('%d%m%Y')
todolist_queryobj = db.Query(TodoList)
yesterday_todolist = todolist_queryobj.filter('daykey =', yesterday).filter('user =', username)
yesterday_todolist = yesterday_todolist.get()
todoitem_queryobj = db.Query(TodoItem)
filtered_todoitem = todoitem_queryobj.filter('belongs_to =',
yesterday_todolist).filter('user =', username)
# if the filtered_todoitem is empty
# That is if there has been gap of more than one day
# traverse for 30 days maximum
look_behind = 2
while not filtered_todoitem and look_behind <= 30:
yesterday = datetime.datetime.now(user_tzinfo) - datetime.timedelta(look_behind)
yesterday = yesterday.strftime('%d%m%Y')
todolist_queryobj = db.Query(TodoList)
yesterday_todolist = todolist_queryobj.filter('daykey =', yesterday).filter('user =', username)
yesterday_todolist = yesterday_todolist.get()
todoitem_queryobj = db.Query(TodoItem)
filtered_todoitem = todoitem_queryobj.filter('belongs_to =',
yesterday_todolist).filter('user =', username)
for todo in filtered_todoitem:
# incomplete todos from yesterday.
if todo.score < todo.rating:
todoitem = TodoItem(user=username,
belongs_to=today_todolist,
description=todo.description,
rating=todo.rating,
score=todo.score)
todoitem.put()
self.response.out.write("Yesterday's unfinished tasks added.")
self.redirect('/')
else:
# there should be only one list for a user for a day.
todolist = filtered_todolist.get()
if hasattr(todolist, 'future_tasks') and todolist.future_tasks == True:
# add yesterdays item and reset the future_tasks flag.
# At this point pull unfinished from yesterday's
yesterday = datetime.datetime.now(user_tzinfo) - datetime.timedelta(1)
yesterday = yesterday.strftime('%d%m%Y')
todolist_queryobj = db.Query(TodoList)
yesterday_todolist = todolist_queryobj.filter('daykey =', yesterday).filter('user =', username)
yesterday_todolist = yesterday_todolist.get()
todoitem_queryobj = db.Query(TodoItem)
filtered_todoitem = todoitem_queryobj.filter('belongs_to =',
yesterday_todolist).filter('user =', username)
for todo in filtered_todoitem:
# incomplete todos from yesterday.
if todo.score < todo.rating:
todoitem = TodoItem(user=username,
belongs_to=todolist,
description=todo.description,
rating=todo.rating,
score=todo.score)
todoitem.put()
todolist.future_tasks = False
# refactor
todolist.put()
# self.redirect('/')
todoitem_queryobj = db.Query(TodoItem)
filtered_todoitem = todoitem_queryobj.filter('belongs_to =',
todolist).filter('user =', username)
if filtered_todoitem:
total_rating_for_day = 0
total_score_for_day = 0
for todo in filtered_todoitem:
total_rating_for_day += todo.rating
total_score_for_day += todo.score
if total_rating_for_day:
score = (100.0 * total_score_for_day) / total_rating_for_day
else:
score = 0.0
todolist.dayscore = score
todolist.put()
todolist_queryobj = db.Query(TodoList)
filtered_todolist = todolist_queryobj.filter('user =',username)
discipline_score = 0.0
number_of_entrys = 0
for todolist_entry in filtered_todolist:
discipline_score += todolist_entry.dayscore
number_of_entrys += 1
score = "%0.2f" % score
total_score = "%0.2f" % (discipline_score/number_of_entrys)
template_values.update({'score':score, 'total_score':total_score})
template_values.update({'filtered_todoitem':filtered_todoitem})
logout_url = users.create_logout_url('/')
template_values.update({'logout':logout_url})
self.response.out.write(template.render(path, template_values))
else:
self.redirect(users.create_login_url(self.request.uri))
class EditEntry(webapp.RequestHandler):
def get(self):
item_key = self.request.get('key')
item = db.get(item_key)
form_contents = {'description':item.description,
'rating':item.rating,
'score':item.score,
'start_time': item.start_time,
'end_time': item.end_time,
'key': item_key}
path = os.path.join(os.path.dirname(__file__), 'edit.html')
self.response.out.write(template.render(path, form_contents))
class NewEntry(webapp.RequestHandler):
def get(self):
username = users.get_current_user()
if username:
path = os.path.join(os.path.dirname(__file__), 'new.html')
self.response.out.write(template.render(path, {}))
else:
self.redirect(users.create_login_url(self.request.uri))
def post(self):
username = users.get_current_user()
if username:
description = self.request.get('description',default_value='')
starttime = self.request.get('starttime',default_value='00:00')
endtime = self.request.get('endtime',default_value='00:00')
rating = int(self.request.get('rating',default_value='10'))
score = int(self.request.get('score',default_value='0'))
timezoneinfo_obj = db.Query(TimezoneInfo)
filtered_timezoneinfo = timezoneinfo_obj.filter('user =', username)
if not filtered_timezoneinfo.fetch(limit=1):
self.redirect('/settimezone')
usertimezone = filtered_timezoneinfo.get()
user_tzinfo = timezone(usertimezone.timezoneinfo)
today = datetime.datetime.now(user_tzinfo).strftime('%d%m%Y')
starttime_hour, starttime_min = map(int, starttime.split(':'))
endtime_hour, endtime_min = map(int, endtime.split(':'))
starttime = datetime.time(starttime_hour, starttime_min, tzinfo=user_tzinfo)
endtime = datetime.time(endtime_hour, endtime_min, tzinfo=user_tzinfo)
todolist_queryobj = db.Query(TodoList)
filtered_todolist = todolist_queryobj.filter('daykey =', today).filter('user =', username)
if not filtered_todolist.fetch(limit=1):
todolist = TodoList(daykey=today, user=username, future_tasks=False)
todolist.put()
else:
todolist = filtered_todolist.get()
todoitem = TodoItem(user=username, belongs_to=todolist, description=description, start_time = starttime, end_time = endtime, rating=rating, score=score)
todoitem.put()
self.redirect('/')
else:
self.redirect(users.create_login_url(self.request.uri))
class FutureEntry(webapp.RequestHandler):
def get(self):
username = users.get_current_user()
if username:
path = os.path.join(os.path.dirname(__file__), 'future.html')
self.response.out.write(template.render(path, {}))
else:
self.redirect(users.create_login_url(self.request.uri))
def post(self):
username = users.get_current_user()
if username:
future_date_value = self.request.get('future', default_value='')
description = self.request.get('description',default_value='')
starttime = self.request.get('starttime',default_value='00:00')
endtime = self.request.get('endtime',default_value='00:00')
rating = int(self.request.get('rating',default_value='10'))
score = int(self.request.get('score',default_value='0'))
timezoneinfo_obj = db.Query(TimezoneInfo)
filtered_timezoneinfo = timezoneinfo_obj.filter('user =', username)
if not filtered_timezoneinfo.fetch(limit=1):
self.redirect('/settimezone')
usertimezone = filtered_timezoneinfo.get()
user_tzinfo = timezone(usertimezone.timezoneinfo)
today = datetime.datetime.now(user_tzinfo).strftime('%d%m%Y')
if future_date_value:
future_date = datetime.datetime.strptime(future_date_value,'%m/%d/%Y')
future_date = future_date.strftime('%d%m%Y')
starttime_hour, starttime_min = map(int, starttime.split(':'))
endtime_hour, endtime_min = map(int, endtime.split(':'))
starttime = datetime.time(starttime_hour, starttime_min, tzinfo=user_tzinfo)
endtime = datetime.time(endtime_hour, endtime_min, tzinfo=user_tzinfo)
todolist_queryobj = db.Query(TodoList)
filtered_todolist = todolist_queryobj.filter('daykey =', future_date).filter('user =', username)
if not filtered_todolist.fetch(limit=1):
todolist = TodoList(daykey=future_date, future_tasks = True, user=username)
todolist.put()
else:
todolist = filtered_todolist.get()
todoitem = TodoItem(user=username, belongs_to=todolist, description=description, start_time=starttime, end_time=endtime, rating=rating, score=score)
todoitem.put()
self.redirect('/')
else:
self.redirect(users.create_login_url(self.request.uri))
class Archives(webapp.RequestHandler):
def get(self):
username = users.get_current_user()
path = os.path.join(os.path.dirname(__file__), 'archives.html')
if username:
todolist_queryobj = db.Query(TodoList)
filtered_todolist = todolist_queryobj.filter('user =', username)
if not filtered_todolist.fetch(limit=1):
no_todos = True
filtered_todolist = None
else:
no_todos = False
filtered_todolist = filtered_todolist
logout = users.create_logout_url("/")
archive_contents = { 'no_todos': no_todos,
'filtered_todolist' :filtered_todolist,
'logout':logout}
self.response.out.write(template.render(path, archive_contents))
else:
self.redirect(users.create_login_url(self.request.uri))
class Graph(webapp.RequestHandler):
def get(self):
username = users.get_current_user()
path = os.path.join(os.path.dirname(__file__), 'graph.html')
if username:
todolist_queryobj = db.Query(TodoList)
filtered_todolist = todolist_queryobj.filter('user =', username)
if not filtered_todolist.fetch(limit=1):
no_todos = True
filtered_todolist = None
else:
no_todos = False
filtered_todolist = filtered_todolist
logout = users.create_logout_url("/")
description = {"day": ("string","Day"),
"score": ("number", "Day Scores"),
"disciplinescore": ("number", "Discipline Score")
}
data = []
data_table = gviz_api.DataTable(description)
num_days = 1.0
score_so_far = 0.0
for todolist in filtered_todolist:
score_so_far += todolist.dayscore
discipline_score = round(score_so_far/num_days, 2)
num_days += 1
item = {"day": todolist.daykey,"score":todolist.dayscore, "disciplinescore": discipline_score}
data.append(item)
points = len(data)
data.sort(key=lambda x:datetime.datetime.strptime(x["day"],"%d%m%Y"))
data_table.LoadData(data)
json_data = data_table.ToJSon(columns_order=("day","score","disciplinescore"))
archive_contents = { 'no_todos': no_todos,
'filtered_todolist' :filtered_todolist,
'json_data' :json_data,
'username': username,
'points':points,
'disciplinescore': discipline_score,
'logout':logout}
self.response.out.write(template.render(path, archive_contents))
else:
self.redirect(users.create_login_url(self.request.uri))
application = webapp.WSGIApplication([
('/', MainPage),
('/edit',EditEntry),
('/settimezone',SetTimeZone),
('/new',NewEntry),
('/future',FutureEntry),
('/archives', Archives),
('/graph', Graph),
('/update',UpdateTodo)],debug=True)
def main():
run_wsgi_app(application)
if __name__ == '__main__':
main()