-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
101 lines (72 loc) · 2.61 KB
/
main.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
from flask import Flask, render_template, request, redirect, url_for
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.orm import DeclarativeBase
from sqlalchemy import Integer, String, Text, Boolean, update
from sqlalchemy.orm import Mapped, mapped_column
class Base(DeclarativeBase):
pass
db = SQLAlchemy(model_class=Base)
# create the app
app = Flask(__name__)
# db = SQLAlchemy(app)
# configure the SQLite database, relative to the app instance folder
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///todo.db"
# initialize the app with the extension
db.init_app(app)
@app.route("/")
def index():
todos = Todo.query.all()
return render_template("index.html", todos=todos)
@app.route("/add", methods=["POST"])
def addTodo():
title = request.form.get("title")
content = request.form.get("content")
newTodo = Todo(title=title, content=content, complete=False)
db.session.add(newTodo)
db.session.commit()
return redirect(url_for("index"))
@app.route("/complete/<string:id>")
def completeTodo(id):
todo = Todo.query.filter_by(id=id).first()
if todo.complete == False:
todo.complete = True
else:
todo.complete = False
db.session.commit()
return redirect(url_for("index"))
@app.route("/delete/<string:id>")
def deleteTodo(id):
todo = Todo.query.filter_by(id=id).first()
db.session.delete(todo)
db.session.commit()
return redirect(url_for("index"))
@app.route("/detail/<string:id>")
def detailTodo(id):
todo = Todo.query.filter_by(id=id).first()
return render_template("detail.html", todo=todo)
# @app.route("/register")
# def register():
# return render_template("register.html")
# @app.route("/registration", methods=["POST"])
# def registration():
# username = request.form.get("username")
# password = request.form.get("password")
# newUser = Users(username=username, password=password)
# db.session.add(newUser)
# db.session.commit()
# return redirect(url_for("index"))
class Todo(db.Model):
# __tablename__ = 'todo'
id: Mapped[int] = mapped_column(Integer, primary_key=True)
title: Mapped[str] = mapped_column(String, nullable=False)
content: Mapped[str] = mapped_column(Text)
complete: Mapped[bool] = mapped_column(Boolean, default=False)
# class Users(db.Model):
# __tablename__ = 'users'
# id: Mapped[int] = mapped_column(Integer, primary_key=True)
# username: Mapped[str] = mapped_column(String, nullable=False)
# password: Mapped[str] = mapped_column(String, nullable=False)
with app.app_context():
db.create_all()
if __name__ == "__main__":
app.run(debug=True)