-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.py
74 lines (54 loc) · 2.26 KB
/
base.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
74
from flask import Flask, redirect, url_for, render_template, request, flash
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
class Todo_list(db.Model):
srno = db.Column(db.Integer , primary_key=True)
title = db.Column(db.String(200) , nullable=False)
description = db.Column(db.String(500) , nullable=True)
complete = db.Column(db.Boolean)
date = db.Column(db.DateTime , default=datetime.now)
# @app.route("/", methods = ['GET', 'POST'])
# def home():
# if (request.method=='POST'):
# '''Add entry to the database'''
# title = request.form.get('title')
# description = request.form.get('description')
# entry = Todo_list(title=title, description = description , date= datetime.now())
# db.session.add(entry)
# db.session.commit()
# return render_template("index.html", todo_items = entry)
@app.route('/')
def index():
# show all list of TODOs
todo_list = Todo_list.query.all()
return render_template("index.html", todo_list = todo_list)
@app.route('/add' , methods = ['POST'])
def add():
title = request.form.get('title')
description = request.form.get('description')
entry = Todo_list(title=title, description = description , date= datetime.now() , complete = False)
db.session.add(entry)
db.session.commit()
return redirect(url_for('index'))
@app.route('/update/<int:todo_srno>')
def update(todo_srno):
todo = Todo_list.query.filter_by(srno = todo_srno).first()
todo.complete = not todo.complete
db.session.commit()
return redirect(url_for('index'))
@app.route('/delete/<int:todo_srno>')
def delete(todo_srno):
todo = Todo_list.query.filter_by(srno = todo_srno).first()
db.session.delete(todo)
db.session.commit()
return redirect(url_for('index'))
if __name__ == "__main__":
db.create_all()
# new_todo = Todo_list(title= 'Todo 1' , description = 'First Todo Item', complete = False , date = datetime.now())
# db.session.add(new_todo)
# db.session.commit()
app.run(debug=True)