-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
188 lines (136 loc) · 5.8 KB
/
main.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
import os
import urllib
from google.appengine.api import users
from google.appengine.ext import ndb
import jinja2
import webapp2
import datetime
jinja_environment = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__) + '/templates'),
extensions=['jinja2.ext.autoescape'],
autoescape=True)
class Person(ndb.Model):
next_agenda = ndb.IntegerProperty()
class Task(ndb.Model):
task_id = ndb.IntegerProperty()
title = ndb.StringProperty()
description = ndb.TextProperty()
create_date = ndb.DateTimeProperty(auto_now_add=True)
priority = ndb.IntegerProperty(default=2) # 1 = high, 2 = medium, 3 = low
due_date = ndb.DateTimeProperty()
class MainPage(webapp2.RequestHandler):
def get(self):
template = jinja_environment.get_template('index.html')
self.response.write(template.render())
class Home(webapp2.RequestHandler):
# Front page for those logged in
def get(self):
user = users.get_current_user()
if user: # signed in already
parent_key = ndb.Key('Person', users.get_current_user().email())
person = parent_key.get()
query = ndb.gql("SELECT * FROM Task WHERE ANCESTOR IS :1 AND priority <= 3 ORDER BY priority ASC, create_date ASC", parent_key)
done_query = ndb.gql("SELECT * FROM Task WHERE ANCESTOR IS :1 AND priority > 3 ORDER BY priority ASC, create_date ASC", parent_key)
template_values = {
'user_mail': users.get_current_user().email(),
'agenda': query,
'done_query': done_query,
'person': person,
'logout': users.create_logout_url(self.request.host_url),
}
template = jinja_environment.get_template('home.html')
self.response.out.write(template.render(template_values))
else:
self.redirect(self.request.host_url)
class AddTask(webapp2.RequestHandler):
def get(self):
user = users.get_current_user()
if user: # signed in already
template_values = {
'user_mail': users.get_current_user().email(),
'logout': users.create_logout_url(self.request.host_url),
}
template = jinja_environment.get_template('add_task.html')
self.response.out.write(template.render(template_values))
else:
self.redirect(self.request.host_url)
def post(self):
parent_key = ndb.Key('Person', users.get_current_user().email())
person = parent_key.get()
if person == None:
person = Person(id=users.get_current_user().email())
person.next_agenda = 1
person.put()
newtask = Task(parent=parent_key, id=str(person.next_agenda))
newtask.task_id = person.next_agenda
newtask.title = self.request.get('title')
newtask.description = self.request.get('desc')
newtask.priority = int(self.request.get('priority'))
person.next_agenda = person.next_agenda + 1
newtask.put()
person.put()
self.redirect('/home')
class DeleteTask(webapp2.RequestHandler):
def post(self):
parent_key = ndb.Key('Person', users.get_current_user().email())
person = parent_key.get()
#del_task_key = ndb.Key('Task', str(self.request.get('id_to_delete')), parent=parent_key)
#dt = del_task_key.get()
dt = Task.get_by_id(id=str(self.request.get('id_to_delete')), parent=parent_key)
#self.response.write(dt.key)
dt.key.delete()
person.put()
self.redirect('/home')
class EditTask(webapp2.RequestHandler):
def get(self):
parent_key = ndb.Key('Person', users.get_current_user().email())
person = parent_key.get()
task_to_edit = Task.get_by_id(id=str(self.request.get('id_to_edit')), parent=parent_key)
#self.response.write(task_to_edit)
template_values = {
'user_mail': users.get_current_user().email(),
'logout': users.create_logout_url(self.request.host_url),
'task_to_edit': task_to_edit,
}
template = jinja_environment.get_template('edit_task.html')
self.response.out.write(template.render(template_values))
def post(self):
parent_key = ndb.Key('Person', users.get_current_user().email())
person = parent_key.get()
task_to_edit = Task.get_by_id(id=str(self.request.get('id_to_edit')), parent=parent_key)
#self.response.write(task_to_edit)
task_to_edit.description = self.request.get('desc')
task_to_edit.title = self.request.get('title')
task_to_edit.priority = int(self.request.get('priority'))
task_to_edit.put()
person.put()
self.redirect('/home')
class Done(webapp2.RequestHandler):
def post(self):
parent_key = ndb.Key('Person', users.get_current_user().email())
person = parent_key.get()
task_done = Task.get_by_id(id=str(self.request.get('id_done')), parent=parent_key)
#self.response.write(task_done)
task_done.priority = task_done.priority + 3
task_done.put()
person.put()
self.redirect('/home')
class NotDone(webapp2.RequestHandler):
def post(self):
parent_key = ndb.Key('Person', users.get_current_user().email())
person = parent_key.get()
task_notdone = Task.get_by_id(id=str(self.request.get('id_notdone')), parent=parent_key)
#self.response.write(parent_key)
task_notdone.priority = task_notdone.priority - 3
task_notdone.put()
person.put()
self.redirect('/home')
application = webapp2.WSGIApplication([
('/', MainPage),
('/home', Home),
('/addtask', AddTask),
('/delete', DeleteTask),
('/edit', EditTask),
('/done', Done),
('/notdone', NotDone),
], debug=True)