-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
61 lines (50 loc) · 2.16 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
from flask import Flask, render_template, url_for, request, redirect
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import desc
from datetime import datetime
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
db = SQLAlchemy(app) # intialise the database
class Todo(db.Model):
id = db.Column(db.Integer, primary_key = True)
content = db.Column(db.String(200), nullable = False) # we dont want it to be left blank so nullabe is False
date_created = db.Column(db.DateTime, default = datetime.now)
def __repr__(self):
return '<Task %r>' % self.id # return the ask and the id of the task created
@app.route('/', methods=['POST', 'GET'])
def index():
if request.method =="POST":
task_content = request.form['content'] #task_content is equal to the form's content
new_task = Todo(content = task_content) # creating a todo object for the todo model
try:
db.session.add(new_task)
db.session.commit()
return redirect('/')
except:
return "Problem occured while adding this task"
else:
tasks = Todo.query.order_by(Todo.date_created).all()
return render_template('index.html',tasks=tasks)
@app.route('/delete/<int:id>')
def delete(id):
task_to_delete = Todo.query.get_or_404(id) # gets the task by the id and if not found gives a 404
try:
db.session.delete(task_to_delete)
db.session.commit()
return redirect('/')
except:
return "Problem occured while deleting this task"
@app.route('/update/<int:id>', methods=['POST','GET'])
def update(id):
task = Todo.query.get_or_404(id)
if request.method =='POST':
task.content = request.form['content'] # settinf current task content to content in the input box
try:
db.session.commit()
return redirect('/')
except:
return "Problem occured while updating this task"
else:
return render_template('update.html', task=task)
if __name__ == "__main__":
app.run(debug= True)