Get It Done! Part 4: Changing a Model Class

Notes

In this lesson we'll improve the delete functionality of our app so that it uses a boolean flag to determine whether a task is completed or not, thus leaving the task in our database. This will allow us to also add a display for completed tasks.

Improve Delete Functionality

Add a boolean property to the Task class and set it to False in the constructor:

    completed = db.Column(db.Boolean)

    def __init__(self, name):
        self.name = name
        self.completed = False

Now fire up the Python shell in your terminal. You're going to drop and then re-create the table. Note: you will lose the data in the table:

>>> db.drop_all()
>>> db.create_all()

Back in main.py remove the code that deleted a task, and instead set its boolean value to True:

@app.route('/delete-task', methods=['POST'])
def delete_task():

    task_id = int(request.form['task-id'])
    task = Task.query.get(task_id)
    task.completed = True
    db.session.add(task)
    db.session.commit()

    return redirect('/')

Amend the index function so that it only gets tasks where the property completed has a value of False:

    tasks = Task.query.filter_by(completed=False).all()

Display Completed Tasks

Add a variable to hold the completed tasks and pass this into the template:

    completed_tasks = Task.query.filter_by(completed=True).all()
    return render_template('todos.html', title="Get It Done!", 
        tasks=tasks, completed_tasks=completed_tasks)

Create the HTML in the todos.html template to display the list of completed tasks:

    <hr />
    <h2>Completed</h2>
    <ul>
        {% for task in completed_tasks %}
        <li>{{task.name}}</li>
        {% endfor %}
    </ul>

Code

View the final code for this lesson.