-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
55 lines (43 loc) · 1.26 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from flask import Flask, request, redirect, render_template
from flask_sqlalchemy import SQLAlchemy
# app and database setup
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
# Database Model
class Todo(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(60))
status = db.Column(db.Boolean, default=False)
# CRUD Operations: Create, Read, Update Delete
# Create
@app.route('/add', methods=['POST'])
def add():
title = request.form.get('title')
todo = Todo(title=title)
db.session.add(todo)
db.session.commit()
return redirect('/')
# Read
@app.route('/')
def index():
todo_list = Todo.query.all()
count = Todo.query.filter_by(status=False).count()
print(count)
return render_template('index.html', todo_list=todo_list, count=count)
# Update
@app.route('/update/<int:todo_id>')
def update(todo_id):
todo = Todo.query.filter_by(id=todo_id).first()
todo.status = not todo.status
db.session.add(todo)
db.session.commit()
return redirect('/')
# Delete
@app.route('/delete/<int:todo_id>')
def delete(todo_id):
todo = Todo.query.filter_by(id=todo_id).first()
db.session.delete(todo)
db.session.commit()
return redirect('/')