Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
81 lines (67 loc) · 1.98 KB
/
Copy pathapp.py
File metadata and controls
81 lines (67 loc) · 1.98 KB
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
#Imports
from flask import Flask, render_template, redirect, request
from flask_scss import Scss
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
# My App
# create the app
app = Flask(__name__)
Scss(app) #add Css to the app
# configure the SQLite database, relative to the app instance folder
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///database.db"
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"]=False
db = SQLAlchemy(app)
# Data Class ~ row of data
class MyTask(db.Model):
id = db.Column(db.Integer, primary_key=True)
content = db.Column(db.String(100), nullable = False)
complete = db.Column(db.Integer)
created = db.Column(db.DateTime, default = datetime.utcnow )
def __repr__(self) -> str:
return f"Task {self.id}"
with app.app_context():
db.create_all()
#Routes to the webpage
# Home page
@app.route("/", methods = ["POST","GET"])
def index():
#Add a task
if request.method == "POST":
current_task = request.form['content']
new_task= MyTask(content = current_task )
try:
db.session.add(new_task)
db.session.commit()
return redirect("/")
except Exception as e:
print(f"Error: {e}")
return f"Error: {e}"
#see the current task
else:
tasks= MyTask.query.order_by(MyTask.created).all()
return render_template('index.html', tasks=tasks)
# Edit an Item
@app.route("/edit/<int:id>", methods = ["POST","GET"])
def edit(id:int):
task = MyTask.query.get_or_404(id)
if request.method == "POST":
task.content = request.form['content']
try:
db.session.commit()
return redirect("/")
except Exception as e:
return f"Error: {e}"
else:
return render_template('edit.html', task=task)
# Delete an Item
@app.route("/delete/<int:id>")
def delete(id:int):
delete_task = MyTask.query.get_or_404(id)
try:
db.session.delete(delete_task)
db.session.commit()
return redirect("/")
except Exception as e:
return f"ERROR:{e}"
if __name__ == "__main__":
app.run(debug=True)