From a57858daf3a118dc2f4c5d1c406418f18aa50487 Mon Sep 17 00:00:00 2001 From: lovely90133 Date: Wed, 29 Apr 2026 15:50:08 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E4=BB=BB=E5=8A=A1?= =?UTF-8?q?=E4=BC=98=E5=85=88=E7=BA=A7=E3=80=81=E6=88=AA=E6=AD=A2=E6=97=A5?= =?UTF-8?q?=E6=9C=9F=E5=92=8C=E7=BC=96=E8=BE=91=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在Todo模型中新增priority和due_date字段 - 实现任务列表按优先级和截止日期排序功能 - 添加任务编辑功能,包括修改标题、优先级和截止日期 - 在前端添加编辑模态框和样式优化 --- app.py | 57 +++++++++++++- templates/base.html | 186 +++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 228 insertions(+), 15 deletions(-) diff --git a/app.py b/app.py index 4b6ccca..1dd23fd 100644 --- a/app.py +++ b/app.py @@ -1,5 +1,6 @@ from flask import Flask, render_template, request, redirect, url_for from flask_sqlalchemy import SQLAlchemy +from datetime import datetime app = Flask(__name__) @@ -13,18 +14,45 @@ class Todo(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(100)) complete = db.Column(db.Boolean) + priority = db.Column(db.Integer, default=3) + due_date = db.Column(db.Date, nullable=True) @app.route("/") def home(): - todo_list = Todo.query.all() - return render_template("base.html", todo_list=todo_list) + sort_by = request.args.get('sort_by', 'id') + order = request.args.get('order', 'asc') + + if sort_by == 'priority': + if order == 'asc': + todo_list = Todo.query.order_by(Todo.priority.asc()).all() + else: + todo_list = Todo.query.order_by(Todo.priority.desc()).all() + elif sort_by == 'due_date': + if order == 'asc': + todo_list = Todo.query.order_by(Todo.due_date.asc().nulls_last()).all() + else: + todo_list = Todo.query.order_by(Todo.due_date.desc().nulls_last()).all() + else: + if order == 'asc': + todo_list = Todo.query.order_by(Todo.id.asc()).all() + else: + todo_list = Todo.query.order_by(Todo.id.desc()).all() + + return render_template("base.html", todo_list=todo_list, sort_by=sort_by, order=order) @app.route("/add", methods=["POST"]) def add(): title = request.form.get("title") - new_todo = Todo(title=title, complete=False) + priority = request.form.get("priority", 3, type=int) + due_date_str = request.form.get("due_date") + + due_date = None + if due_date_str: + due_date = datetime.strptime(due_date_str, "%Y-%m-%d").date() + + new_todo = Todo(title=title, complete=False, priority=priority, due_date=due_date) db.session.add(new_todo) db.session.commit() return redirect(url_for("home")) @@ -38,6 +66,25 @@ def update(todo_id): return redirect(url_for("home")) +@app.route("/edit", methods=["POST"]) +def edit(): + todo_id = request.form.get("id", type=int) + title = request.form.get("title") + priority = request.form.get("priority", 3, type=int) + due_date_str = request.form.get("due_date") + + todo = Todo.query.filter_by(id=todo_id).first() + if todo: + todo.title = title + todo.priority = priority + if due_date_str: + todo.due_date = datetime.strptime(due_date_str, "%Y-%m-%d").date() + else: + todo.due_date = None + db.session.commit() + return redirect(url_for("home")) + + @app.route("/delete/") def delete(todo_id): todo = Todo.query.filter_by(id=todo_id).first() @@ -45,6 +92,8 @@ def delete(todo_id): db.session.commit() return redirect(url_for("home")) -if __name__ == "__main__": +with app.app_context(): db.create_all() + +if __name__ == "__main__": app.run(debug=True) diff --git a/templates/base.html b/templates/base.html index b40815c..f64f5f8 100644 --- a/templates/base.html +++ b/templates/base.html @@ -8,6 +8,46 @@ + @@ -17,28 +57,152 @@

To Do App

-
+
+
+
+
+ + +
+
+ + +

+ + {% for todo in todo_list %} -
+

{{todo.id }} | {{ todo.title }}

+ +
+ {% if todo.complete == False %} + Not Complete + {% else %} + Completed + {% endif %} + + {% if todo.priority >= 5 %} + Priority: {{ todo.priority }} + {% elif todo.priority >= 4 %} + Priority: {{ todo.priority }} + {% elif todo.priority >= 3 %} + Priority: {{ todo.priority }} + {% elif todo.priority >= 2 %} + Priority: {{ todo.priority }} + {% else %} + Priority: {{ todo.priority }} + {% endif %} + + {% if todo.due_date %} + Due: {{ todo.due_date.strftime('%Y-%m-%d') }} + {% endif %} +
- {% if todo.complete == False %} - Not Complete - {% else %} - Completed - {% endif %} - - Update + Toggle + Delete
{% endfor %}
- - \ No newline at end of file + + + + +