-
Notifications
You must be signed in to change notification settings - Fork 0
/
taskboards.py
352 lines (272 loc) · 10.8 KB
/
taskboards.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
#imports
import webapp2
import jinja2
from google.appengine.api import users
from google.appengine.ext import ndb
import os
from myuser import MyUser
from task import Task
from myuser import TaskBoard
from datetime import datetime
JINJA_ENVIRONMENT = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
extensions=['jinja2.ext.autoescape'],
autoescape=True
)
#TaskBoard class
class TaskBoards(webapp2.RequestHandler):
# get method called by webapp2 when page is instantiated
def get(self):
self.response.headers['Content-Typ'] = 'text/html'
# gets current user
user = users.get_current_user()
# gets id passed in the url
id = self.request.get('id')
#selection if no id is in url
if id == '':
#redirects to view taskboard page
self.redirect('/viewtaskboard')
else:
# gets TaskBoard
taskboard = TaskBoard.get_by_id(int(id))
#task stats
n_tasks = len(taskboard.tasks)
n_ctasks = 0
n_atasks = 0
n_ctaskst = 0
#calculate stats
for i in taskboard.tasks:
if i.completion == True:
n_ctasks += 1
today = datetime.date(datetime.now())
if datetime.date(i.completion_date) == today:
n_ctaskst += 1
else:
n_atasks += 1
# selection statement for user
if user:
#get user key
user_key = ndb.Key('MyUser', user.user_id())
#gets user
myuser = user_key.get()
#initializes permisiion
permission = False
taskboards_created_keys = myuser.taskboards_created
#loops through taskboards created by user to find if user created current taskboard
for i in taskboards_created_keys:
if i == taskboard.key:
permission = True
#querys data store to find all users
q = MyUser.query().fetch()
# values to be rendered to the createtaskboard.html page
template_values = {
'user' : user,
'taskboard' : taskboard,
'permission' : permission,
'q' : q,
'n_tasks' : n_tasks,
'n_ctasks' : n_ctasks,
'n_atasks' : n_atasks,
'n_ctaskst' : n_ctaskst
}
template = JINJA_ENVIRONMENT.get_template('taskboards.html')
self.response.write(template.render(template_values))
else:
#if no user redirect back to home page
self.redirect('/')
# called when you submit a web form
def post(self):
self.response.headers['Content-Type'] = 'text/html'
# gets current user
user = users.get_current_user()
u_key = ndb.Key('MyUser', user.user_id())
u = u_key.get()
# get value of button clicked
action = self.request.get('button')
# selection statement for if add user button is clicked
if action == 'AddUser':
# gets for attributes
id = self.request.get('id')
k_str = self.request.get('user_id')
# gets TaskBoard
taskboard = TaskBoard.get_by_id(int(id))
taskboard_key = ndb.Key('TaskBoard', int(id))
#gets user key
user_key = ndb.Key(urlsafe=k_str)
myuser = user_key.get()
#loops through taskboard users
for i in taskboard.users:
#selection statement for if user already in taskboard
if i == user_key:
self.redirect('/taskboards?id='+id)
return
#add taskboard and user attributes
taskboard.users.append(user_key)
myuser.taskboards.append(taskboard_key)
#saves taskboard and users
taskboard.put()
myuser.put()
#redirects back to taskboard
self.redirect('/taskboards?id='+id)
# selection statement for if addtask button is clicked
if action == 'AddTask':
#create a new task
task = Task()
# gets id passed in the form
id = self.request.get('id')
#add form values to task
task.title = self.request.get('title')
k_str = self.request.get('assigned_user')
task.assigned_user = ndb.Key(urlsafe=k_str)
due_date = self.request.get('due_date')
#formats due date in form
task.due_date = datetime.strptime(due_date, "%Y-%m-%d")
# gets TaskBoard
taskboard = TaskBoard.get_by_id(int(id))
# loops through all tasks in taskboard
for i in taskboard.tasks:
# if task found with same name redirect back to page
if i.title == task.title:
self.redirect('/taskboards?id='+id)
return
# add taskboard attributes
taskboard.tasks.append(task)
taskboard.completion = False
#save taskboard
taskboard.put()
#redirect back to page
self.redirect('/taskboards?id='+id)
# selection statement for if completed button is clicked
if action == 'Completed':
# gets values passed in form
id = self.request.get('id')
index = int(self.request.get('index'))
#gets current time
time = datetime.now()
# gets TaskBoard
taskboard = TaskBoard.get_by_id(int(id))
#adds form values to task
taskboard.tasks[index - 1].completion = True
taskboard.tasks[index - 1].completion_date = time
# saves taskboard
taskboard.put()
# redirects back to page
self.redirect('/taskboards?id='+id)
# selection statement for if delete button is clicked
if action == 'Delete':
# gets values passed in form
id = self.request.get('id')
index = int(self.request.get('index'))
# gets TaskBoard
taskboard = TaskBoard.get_by_id(int(id))
# deletes task
del taskboard.tasks[index - 1]
# saves taskboard
taskboard.put()
# redirects back to page
self.redirect('/taskboards?id='+id)
# selection statement for if edit button is clicked
if action == 'Edit':
# gets values passed in form
id = self.request.get('id')
index = int(self.request.get('index'))
k_str = self.request.get('assigned_user')
due_date = self.request.get('due_date')
title = self.request.get('title')
# gets TaskBoard
taskboard = TaskBoard.get_by_id(int(id))
# validates taskboard title to avoid duplicates
if taskboard.tasks[index - 1].title != title:
# loops through tasks
for i in taskboard.tasks:
if i.title == title:
#redirects back to page
self.redirect('/taskboards?id='+id)
return
#adds form values to task
taskboard.tasks[index - 1].title = title
taskboard.tasks[index - 1].due_date = datetime.strptime(due_date, "%Y-%m-%d")
taskboard.tasks[index - 1].assigned_user = ndb.Key(urlsafe=k_str)
# saves taskboard
taskboard.put()
# redirects back to page
self.redirect('/taskboards?id='+id)
# selection statement for if change button is clicked
if action == 'Change':
# gets id passed in the form
id = self.request.get('id')
# gets TaskBoard
taskboard = TaskBoard.get_by_id(int(id))
# gets values passed in form
name = self.request.get('name')
#validates form value
if name == '':
self.redirect('/taskboards?id='+id)
#validates taskboard name to avoid duplicate names
for i in u.taskboards:
if i.get().name == name:
self.redirect('/taskboards?id='+id)
return
#updates taskboard name
taskboard.name = name
#saves taskboard
taskboard.put()
# redirects back to page
self.redirect('/taskboards?id='+id)
# selection statement for if drop button is clicked
if action == 'Drop':
# gets values passed in form
id = self.request.get('id')
k_str = self.request.get('assigned_user')
key = ndb.Key(urlsafe=k_str)
# gets TaskBoard
taskboard = TaskBoard.get_by_id(int(id))
#removes user from taskboard
index = 0
for i in taskboard.users:
if i == key:
del taskboard.users[index]
index += 1
#unassgins tasks assigned to user
for i in taskboard.tasks:
if i.assigned_user == key:
i.assigned_user = None
#removes users access to taskboard
usr = key.get()
index = 0
for i in usr.taskboards:
if i == taskboard.key:
del usr.taskboards[index]
index += 1
#update taskboard and user
taskboard.put()
usr.put()
# redirects back to page
self.redirect('/taskboards?id='+id)
# selection statement for if deletetaskboard button is clicked
if action == 'DeleteTaskboard':
# gets values passed in form
id = self.request.get('id')
# gets TaskBoard
taskboard = TaskBoard.get_by_id(int(id))
#confirms if users or tasks still exist
if (taskboard.users != [] ) or (taskboard.tasks != []):
self.redirect('/taskboards?id='+id)
return
#removes taskboard from user
index = 0
for i in u.taskboards:
if i == taskboard.key:
del u.taskboards[index]
index += 1
index = 0
for i in u.taskboards_created:
if i == taskboard.key:
del u.taskboards_created[index]
index += 1
# updates user
u.put()
#delete taskboard
taskboard.key.delete()
# redirects back to homepage
self.redirect('/')