-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
56 lines (39 loc) · 1.31 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
from flask import Flask, render_template, request, make_response
app = Flask(__name__)
items = [
{"id": 1, "name": "Item 1"},
{"id": 2, "name": "Item 2"},
{"id": 3, "name": "Item 3"},
]
@app.route("/")
def index():
return render_template("index.html", items=items)
@app.route("/edit/<int:item_id>", methods=["GET", "POST"])
def edit_item(item_id):
if request.method == "POST":
template = "item.html"
item = update_item(item_id, request)
else:
template = "edit_item.html"
item = get_item(item_id)
return render_template(template, item=item)
@app.route("/new", methods=["POST"])
def new_item():
item = {"id": len(items) + 1, "name": "New Item"}
items.insert(0, item)
return render_template("item.html", item=item)
@app.route("/done/<int:item_id>", methods=["POST"])
def done_item(item_id):
item = get_item(item_id)
item["done"] = True
return render_template("item_done.html", item=item)
def get_item(item_id):
return next((item for item in items if item["id"] == item_id), None)
def update_item(item_id, request):
new_name = request.form.get("name")
item_to_edit = get_item(item_id)
if new_name and item_to_edit:
item_to_edit["name"] = new_name
return item_to_edit
if __name__ == "__main__":
app.run(debug=True)