forked from AbrahamKeleta/flask-start-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
73 lines (56 loc) · 1.78 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# This is your library
from flask import Flask, render_template, request, redirect
from flask_sqlalchemy import SQLAlchemy
# This initiates the flask app
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db'
app.config['SECRET_KEY'] = 'thisisasecretkey'
db = SQLAlchemy(app)
# Database Class
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(30))
email = db.Column(db.String)
# Todo class
class Todo(db.Model):
id = db.Column(db.Integer, primary_key=True)
item = db.Column(db.String)
complete = db.Column(db.Boolean)
# We are telling flask where to go for home page
@app.route('/', methods=['GET'])
def index():
todos = Todo.query.all()
return render_template('index.html', todos=todos)
@app.route('/new_todo', methods=['POST'])
def new_todo():
if (request.method == 'POST'):
todo_text = request.form['todo']
new_todo = Todo(item=todo_text, complete=False)
try:
db.session.add(new_todo)
db.session.commit()
return redirect('/')
except:
return "Something went wrong adding your todo item"
return render_template('index.html')
@app.route('/update/<id>', methods=['POST', 'GET'])
def update(id):
item = Todo.query.get_or_404(id)
item.complete = True
db.session.commit()
return redirect('/')
@app.route('/delete/<id>')
def delete(id):
item = Todo.query.get_or_404(id)
db.session.delete(item)
db.session.commit()
return redirect('/')
@app.route('/services')
def services():
return render_template('services.html')
@app.route('/about')
def about():
return render_template('about.html')
# Runs your flask app
if (__name__ == "__main__"):
app.run(debug=True)