-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
50 lines (37 loc) · 1.23 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
from contacts_model import Contact
from flask import Flask, flash, redirect, render_template, request
from typing_extensions import LiteralString
from werkzeug.wrappers import response
Contact.load_db()
app: Flask = Flask(__name__)
app.secret_key = b"it is over"
@app.route("/")
def index() -> response.Response:
return redirect("/contacts")
@app.route("/contacts")
def contacts() -> str:
search: str | None = request.args.get("q")
if search is not None:
contacts_set: list = Contact.search(search)
else:
contacts_set = Contact.all()
return render_template("index.html", contacts=contacts_set)
@app.route("/contacts/new", methods=["GET"])
def contacts_new_get() -> str:
return render_template("new.html", contact=Contact())
@app.route("/contacts/new", methods=["POST"])
def contacts_new() -> response.Response | str:
c: Contact = Contact(
None,
request.form["first_name"],
request.form["last_name"],
request.form["phone"],
request.form["email"],
)
if c.save():
flash("Created New Contact!")
return redirect("/contacts")
else:
return render_template("new.html", contact=c)
if __name__ == "__main__":
app.run(port=5004)