-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapp.py
81 lines (60 loc) · 2.37 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
74
75
76
77
78
79
# flask Minimal app code
from flask import Flask, render_template, request, redirect, url_for
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///todo.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
# this is the model for the database
class TodoModel(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(100), nullable=False)
desc = db.Column(db.String(200), nullable=False)
date_created = db.Column(db.DateTime,default=datetime.utcnow)
def __repr__(self) -> str:
return f"{self.id} - {self.title}"
@app.route('/', methods=['GET', 'POST'])
def Home():
if request.method == 'POST':
title = request.form['title']
desc = request.form['desc']
todo = TodoModel(title=title, desc=desc)
db.session.add(todo)
db.session.commit()
alltodo = TodoModel.query.all()
return render_template('index.html', alltodo=alltodo)
# @route is a decorator which tells the app which URL should call the associated function
@app.route('/update/<int:id>', methods=['GET', 'POST'])
def update(id):
if request.method == 'POST':
title = request.form['title']
desc = request.form['desc']
todo = TodoModel.query.filter_by(id=id).first()
todo.title = title
todo.desc = desc
db.session.add(todo)
db.session.commit()
return redirect(url_for('Home'))
todo = TodoModel.query.filter_by(id=id).first()
return render_template('update.html', todo=todo)
@app.route('/delete/<int:id>')
def delete(id):
todo = TodoModel.query.filter_by(id=id).first()
db.session.delete(todo)
db.session.commit()
return redirect(url_for('Home'))
@app.route('/search', methods=['GET'])
def search():
if request.method == 'GET':
search_query = request.args.get('search')
search_query = f"%{search_query}%" # Adding wildcard '%' for partial matching
todos = TodoModel.query.filter(TodoModel.title.like(search_query)).all()
return render_template('search.html', todos=todos)
return redirect(url_for('Home'))
@app.route('/about')
def about():
return render_template('about.html')
# this is the main app which will run the app
if __name__ == '__main__':
app.run(debug=True)