def handle(self): # check if user has admin permissions if not is_admin(self.user_entity): return self.abort(401, detail="Admin permissions required") # populate form with POST data (if available) form = TaskForm(self.request.POST) # check if form was POSTed and that user input validates if self.request.method == 'POST' and form.validate(): # create new project task = Task() # populate task from form form.populate_obj(task) # add user to project's user list and assign to self task.users.append(self.user_entity.key) task.assigned_to = self.user_entity.key # store task in datastore task.put() # record history item history_text = 'Project added' add_to_history(task, self.user_entity, history_text) self.session.add_flash(history_text) # redirect to task view on succesful save redirect_url = self.uri_for('task-view', task_id=task.key.urlsafe()) return self.redirect(redirect_url) # render form and display context = {'form': form, 'task_or_project': 'project', 'add_or_edit': 'Add new'} return self.render_response('task_form.html', context)
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 handle(self, task_id): # pull project from datastore or issue 404 task = ndb.Key(urlsafe=task_id).get() if task is None: return self.abort(404, 'Project not found') # check if user has admin permissions if task.is_top_level: if not is_admin(self.user_entity): return self.abort(401, detail="Admin permissions required") task_or_project = 'project' else: task_or_project = 'task' # populate form with POST data (if available) form = TaskForm(self.request.POST, task) # check if form was POSTed and that user input validates if self.request.method == 'POST' and form.validate(): # populate task from form form.populate_obj(task) # store task in datastore task.put() # record history item history_text = '%s edited' % task_or_project.capitalize() add_to_history(task, self.user_entity, history_text) self.session.add_flash(history_text) # redirect to task view on succesful save redirect_url = self.uri_for('task-view', task_id=task.key.urlsafe()) return self.redirect(redirect_url) # render form and display context = {'form': form, 'task_or_project': task_or_project, 'add_or_edit': 'Editing'} return self.render_response('task_form.html', context)