Ejemplo n.º 1
0
def task_add_edit(id=None):

    form = AddOrEditTask()
    form.category.choices = get_unique_categories("task")

    if id:
        task = Task.query.filter_by(id=int(id)).first()
        title = "Edit task"
        edit = True
    else:
        task = Task()
        title = "Add task"
        edit = False

    if request.method == "POST" and form.validate_on_submit():

        task.status = form.status.data
        task.category = form.category.data
        if task.category and form.add_category.data:
            task.category = form.add_category.data
        task.title = form.title.data
        task.description = form.description.data
        if not task.description:
            task.description = "_[ No description ]_"

        if id:
            task.modified = datetime.utcnow() + timedelta(hours=1)
            db.session.commit()
            flash(f"The changes on '{task.title}' have been saved.")

        else:
            task.created = datetime.utcnow() + timedelta(hours=1)
            db.session.add(task)
            db.session.commit()
            flash(f"The task '{form.title.data}' was successfully added.")

        return redirect(url_for("tasks"))

    elif id and request.method == "GET":

        form.status.data = task.status
        form.category.data = task.category
        form.title.data = task.title
        if task.description == "_[ No description ]_":
            form.description.data = ""
        else:
            form.description.data = task.description

    return render_template("task.html", title=title, form=form, edit=edit)
Ejemplo n.º 2
0
def edit_task(task_id):
    task = Task(ObjectId(task_id))
    task.flask_validate(edit=True)
    form = TaskForm(obj=task.to_struct())
    form.time.data = task.date.time()
    if form.validate_on_submit():
        date = form.date.data
        time = form.time.data
        dt = datetime.combine(date, time
                              or datetime.min.time()) if date else None
        task.name = form.name.data
        task.description = form.description.data
        task.category = form.category.data
        task.date = dt
        return redirect(
            url_for('pages.view_class', class_id=str(task.class_.get_id())))
    return render_template('edit_task.html', form=form, task=task)