Vinay Jose commited on
Commit
5596d0f
·
unverified ·
1 Parent(s): 1327a54

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -0
app.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, redirect, render_template, flash, request
2
+ from models import Contact
3
+
4
+ Contact.load_db()
5
+ app = Flask(__name__)
6
+
7
+ app.secret_key = b'just keep swimming'
8
+
9
+ @app.get("/")
10
+ def index():
11
+ return redirect("/contacts")
12
+
13
+ @app.get("/contacts")
14
+ def contacts():
15
+ search = request.args.get("q")
16
+ if search:
17
+ contacts_set = Contact.search(search)
18
+ else:
19
+ contacts_set = Contact.all()
20
+ return render_template("index.html", contacts=contacts_set)
21
+
22
+ @app.get("/contacts/<contact_id>")
23
+ def contacts_view(contact_id=0):
24
+ contact = Contact.find(contact_id)
25
+ return render_template("show.html", contact=contact)
26
+
27
+ @app.get("/contacts/new")
28
+ def contacts_new_get():
29
+ return render_template("new.html", contact=Contact())
30
+
31
+ @app.post("/contacts/new")
32
+ def contacts_new_post():
33
+ c = Contact(
34
+ None,
35
+ request.form['first'],
36
+ request.form['last'],
37
+ request.form['phone'],
38
+ request.form['email']
39
+ )
40
+ if c.save():
41
+ flash("Created New Contact!")
42
+ return redirect("/contacts")
43
+ else:
44
+ return render_template("new.html", contact=c)
45
+
46
+ @app.get("/contacts/<contact_id>/edit")
47
+ def contacts_edit_get(contact_id=0):
48
+ contact = Contact.find(contact_id)
49
+ return render_template("edit.html", contact=contact)
50
+
51
+ @app.post("/contacts/<contact_id>/edit")
52
+ def contacts_edit_post(contact_id=0):
53
+ c = Contact.find(contact_id)
54
+ c.update(
55
+ request.form['first'],
56
+ request.form['last'],
57
+ request.form['phone'],
58
+ request.form['email']
59
+ )
60
+ if c.save():
61
+ flash("Updated Contact!")
62
+ return redirect(f"/contacts/{contact_id}")
63
+ else:
64
+ render_template("edit.html", contact=c)
65
+
66
+ @app.post("/contacts/<contact_id>/delete")
67
+ def contacts_delete(contact_id=0):
68
+ contact = Contact.find(contact_id)
69
+ contact.delete()
70
+ flash("Deleted Contact!")
71
+ return redirect("/contacts")
72
+
73
+ if __name__ == "__main__":
74
+ app.run(host="0.0.0.0", port="7860", debug=True)
75
+