-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
75 lines (55 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
from flask import Flask, redirect, render_template, request, flash
from model import Contact
Contact.load_db()
app = Flask(__name__)
app.secret_key = b"`7C3}4C'ze!i"
@app.route("/")
def index():
return redirect("/contacts")
@app.route("/contacts")
def contacts():
q = request.args.get("q")
if q is None:
contacts = Contact.get_all()
else:
contacts = Contact.search(q)
return render_template("index.html", contacts=contacts)
@app.route("/contacts/htmx")
def contacts_htmx():
return """
<ul>
<li><a href="mailto:joe@example.com">Joe</a></li>
<li><a href="mailto:sarah@example.com">Sarah</a></li>
<li><a href="mailto:fred@example.com">Fred</a></li>
</ul>
"""
@app.route("/contacts/new", methods=["GET"])
def contacts_new_get():
return render_template("new.html", contact=Contact())
@app.route("/contacts/new", methods=["POST"])
def contacts_new_post():
c = Contact(None, request.form["firstName"], request.form["lastName"], request.form["email"], request.form["phone"])
if c.save():
return redirect("/contacts")
else:
return render_template("new.html", contact=c)
@app.route("/contacts/<int:contact_id>")
def contacts_view(contact_id):
return render_template("view.html", contact=Contact.get_by_id(contact_id))
@app.route("/contacts/<int:contact_id>/edit", methods=["GET"])
def contacts_edit_get(contact_id):
return render_template("edit.html", contact=Contact.get_by_id(contact_id))
@app.route("/contacts/<int:contact_id>/edit", methods=["POST"])
def contacts_edit_post(contact_id):
c = Contact.get_by_id(contact_id)
c.update(request.form["firstName"], request.form["lastName"], request.form["email"], request.form["phone"])
if c.save():
flash("Updated Contact")
return redirect(f"/contacts/{c.id}")
else:
return render_template("edit.html", contact=c)
@app.route("/contacts/<int:contact_id>", methods=["DELETE"])
def contacts_delete(contact_id):
Contact.delete_by_id(contact_id)
flash("Deleted Contact")
return redirect("/contacts", 303)