-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
52 lines (34 loc) · 1.14 KB
/
server.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
from flask import Flask, render_template, jsonify, request
from model import db, connect_to_db, Card
app = Flask(__name__)
@app.route("/")
def show_homepage():
"""Show the application's homepage."""
return render_template("homepage.html")
@app.route("/cards")
def show_cards():
"""Show all trading cards."""
return render_template("cards.html")
@app.route("/api/cards.json")
def get_cards_json():
"""Return a JSON response with all cards in DB."""
cards = Card.query.all()
cards_list = []
for c in cards:
cards_list.append({"skill": c.skill, "name": c.name, "imgUrl": c.image_url})
return jsonify({"cards": cards_list})
@app.route("/add-card", methods=["POST"])
def add_card():
"""Add a new card to the DB."""
name = request.form.get('name')
skill = request.form.get('skill')
new_card = Card(name=name, skill=skill)
db.session.add(new_card)
db.session.commit()
return jsonify({"success": True})
@app.route("/cards-jquery")
def show_cards_jquery():
return render_template("cards-jquery.html")
if __name__ == "__main__":
connect_to_db(app)
app.run(debug=True, host='0.0.0.0')