def handle(self, task_id): # create new task (with parent) parent_task_key = ndb.Key(urlsafe=task_id) task = Task(parent=parent_task_key) # check if user is authed to add task if not authed_for_task(parent_task_key.get(), self.user_entity): return self.abort(401) # init form object with POST data form = TaskForm(self.request.POST) # if form was posted and it validates if self.request.method == 'POST' and form.validate(): # build task from form and save form.populate_obj(task) task.put() # add history record for this task history_text = 'Task added' add_to_history(task, self.user_entity, history_text) self.session.add_flash(history_text) # build url to redirect to and issue 302 to browser redirect_url = self.uri_for('task-view', task_id=task.key.urlsafe()) return self.redirect(redirect_url) # render form and return (rendering errors if necessary) context = {'form': form, 'task_or_project': 'task'} return self.render_response('task_form.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 = reassign_task_form(task, self.request.POST, with_default=False) # check if form validates if form.validate(): # coerce form data into valid datastore data if form.assigned_to.data == 'None': data = None else: data = ndb.Key(urlsafe=form.assigned_to.data) # construct history record if task.assigned_to is None: from_user = '******' else: from_user = task.assigned_to.get().given_name if data is None: to_user = '******' else: to_user = data.get().given_name history_text = 'Task reassigned from %s to %s' % (from_user, to_user) # reassign task task.assigned_to = data # store task task.put() # add history record and add flash msg add_to_history(task, self.user_entity, history_text) 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 url of task and redirect to it redirect_url = self.uri_for('task-view', task_id=task.key.urlsafe()) return self.redirect(redirect_url)
def post(self, task_id): # create new task (with parent) task_key = ndb.Key(urlsafe=task_id) # check if user is authed to reassign task if not authed_for_task(task_key.get(), self.user_entity): return self.abort(401) # get user key user_key = self.user_entity.key # create new comment object comment = Comment(parent=task_key) # populate form with POST data form = CommentForm(self.request.POST) if form.validate(): # populate comment object with validated form data form.populate_obj(comment) # set reference properties on comment object comment.task = task_key comment.user = user_key # store comment comment.put() # record history item history_text = 'Comment added' add_to_history(task_key.get(), 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) # redirect to task view page redirect_url = self.uri_for('task-view', task_id=task_key.urlsafe()) return self.redirect(redirect_url)
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)