def edit_task(task_id=None): model = get_object_or_404(Task, Task.id == task_id) form = TaskForm(obj=model) if form.validate_on_submit(): form.populate_obj(model) db.session.add(model) db.session.commit() flash('Task updated', category='success') return redirect(url_for('admin')) return render_template('edit_task.html', task=model, form=form)
def edit_task(task_id): task = Task.query.get(task_id) if g.user.user_id != task.author_id: flash('You do not have proper authorization to do this!', 'danger') return redirect('/task/' + str(task_id)) form = TaskForm(obj=task) if request.method == 'POST' and form.validate(): form = TaskForm(request.form) form.populate_obj(task) task.save() return redirect(url_for('task', task_id=task.task_id)) return render_template('task_form.html', form=form, edit=True)
def update_task_submission(task_id): # updates the task having id = task_id and returns the updated task # to the gui Use route /tasks/task_id method='patch'to return the # task to the api form = TaskForm() form.volunteer_id.choices = get_volunteer_choices() if not form.validate_on_submit(): # if the form has errors, display error messages and resend the # current page flash('Task ' + request.form['title'] + ' could not be updated be' 'cause one or more fields' ' were invalid:') for field, message in form.errors.items(): flash(field + ' ' + message[0]) task = form.data task['id'] = task_id return render_template('task_form.html', form=form, task=task, title="Update Task") # form is valid # form.volunteer_id.data will = 0 when no volunteer is selected but # since 0 is not an id in the volunteer database, it throws a # ForeignKeyViolation error. The only way I found around this error # is to set a local variable that is not in the form to 'None' and then # overlay the form value with the local variable after the # form.populate_obj statement. Changing volunteer_id.data to 'None' # did not have any effect. volunteer_id = form.volunteer_id.data if volunteer_id == 0: volunteer_id = None try: task = Task.query.get(task_id) form = TaskForm(obj=task) form.populate_obj(task) task.volunteer_id = volunteer_id task.update() except Exception as e: flash('Task ' + request.form['title'] + ' could not be updated') print('422 error', e) abort(422) flash('Task ' + task.title + ' was successfully updated') return redirect('/tasks')
def task(id_project, id_task=None): project = Project.query.get(id_project) from forms import TaskForm if id_task != None: task = task.query.get(id_task) action = "Save" else: task = Task() action = "Add" form = TaskForm(obj=task) if form.validate_on_submit(): form.populate_obj(task) db.session.add(task) db.session.commit() return redirect('/tasks') return render_template('project/task/form.html', form=form, action=action, project=project)