-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
83 lines (63 loc) · 2.08 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
80
81
82
83
# Imports
from flask import Flask, render_template, redirect, request
from flask_scss import Scss
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
# Flask App
app = Flask(__name__)
Scss(app)
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///database.db"
app.config["SQLALCHEMY_TRACK_MODIFICATION"] = False
database = SQLAlchemy(app)
# Class for Data
class MyTask(database.Model):
id = database.Column(database.Integer, primary_key=True)
content = database.Column(database.String(100), nullable=False)
completed = database.Column(database.Integer, default=0)
created = database.Column(database.DateTime, default=datetime.utcnow)
def __repr__(self) -> str:
return f"Task {self.id}"
with app.app_context():
database.create_all()
# 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:
database.session.add(new_task)
database.session.commit()
return redirect("/")
except Exception as e:
print(f"ERROR: {e}")
return f"ERROR: {e}"
else:
tasks = MyTask.query.order_by(MyTask.created).all()
return render_template("index.html", tasks=tasks)
# Delete an item
@app.route("/delete/<int:id>")
def delete(id:int):
delete_task = MyTask.query.get_or_404(id)
try:
database.session.delete(delete_task)
database.session.commit()
return redirect("/")
except Exception as e:
return f"Error: {e}"
# Edit an item
@app.route("/edit/<int:id>", methods = ['GET', 'POST'])
def edit(id:int):
task = MyTask.query.get_or_404(id)
if request.method == "POST":
task.content = request.form['content']
try:
database.session.commit()
return redirect("/")
except Exception as e:
return f"Error as {e}"
else:
return render_template("edit.html" , task=task)
if __name__ == "__main__":
app.run(debug=True)