def get(self, task_id, comment_cursor=None): # get task from datastore using ID passed in URL task_key = ndb.Key(urlsafe=task_id) task = task_key.get() # throw 404 if task not found if task_key is None or task is None: return self.abort(404, detail='Task not found: %s' % task_id) # check if user is authed to view task if not authed_for_task(task, self.user_entity): return self.abort(401, detail="Unauthorised for task") # get subtasks from datastore subtasks = ndb.get_multi_async(task.subtasks) # get list of users from datastore task_users = ndb.get_multi_async(task.users) # get page of comments if comment_cursor is not None: comment_cursor = Cursor(urlsafe=comment_cursor) comments = task.comments().fetch_page_async(5, start_cursor=comment_cursor) # form to allow altering of completion status completion_form = completion_task_form(task, self.request.POST) # form to allow adding comments comment_form = CommentForm() # for to allow reassigning of task reassign_form = reassign_task_form(task, self.request.POST) # form to allow adding users to projects add_user_form = add_user_to_task_form(task, self.request.POST) # add all required objects to context dict context = { 'task': task, 'subtasks': subtasks, 'comment_form': comment_form, 'reassign_form': reassign_form, 'task_users': task_users, 'completion_form': completion_form, 'comments': comments, 'comment_cursor': comment_cursor, 'add_user_form': add_user_form, } # render template and return return self.render_response('task.html', context)
def post(self, task_id): # get task from datastore task_key = ndb.Key(urlsafe=task_id) task = task_key.get() # check if user is authed to reassign task if not authed_for_task(task, self.user_entity): return self.abort(401) # build form with POST data form = completion_task_form(task, self.request.POST, with_default=False) # check if form validates if form.validate(): # get original status before changing from_status = task.task_completion_status # populate task from form and save form.populate_obj(task) task.put() # add history record to_status = form.task_completion_status.data history_text = 'Task completion status changed from %d to %d' % (from_status, to_status) add_to_history(task, self.user_entity, history_text) # add a flash message to session self.session.add_flash(history_text) else: # if form doesn't validate then add a flash message before redirecting flash_msg = form.errors self.session.add_flash(flash_msg) # build redirect url and return redirect_url = self.uri_for('task-view', task_id=task.key.urlsafe()) return self.redirect(redirect_url)