-
Notifications
You must be signed in to change notification settings - Fork 81
/
flashcards.py
65 lines (51 loc) · 1.53 KB
/
flashcards.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
from flask import (Flask, render_template, abort, jsonify, request,
redirect, url_for)
from model import db, save_db
app = Flask(__name__)
@app.route("/")
def welcome():
return render_template(
"welcome.html",
cards=db
)
@app.route('/card/<int:index>')
def card_view(index):
try:
card = db[index]
return render_template("card.html",
card=card,
index=index,
max_index=len(db)-1)
except IndexError:
abort(404)
@app.route('/add_card', methods=["GET", "POST"])
def add_card():
if request.method == "POST":
# form has been submitted, process data
card = {"question": request.form['question'],
"answer": request.form['answer']}
db.append(card)
save_db()
return redirect(url_for('card_view', index=len(db)-1))
else:
return render_template("add_card.html")
@app.route('/remove_card/<int:index>', methods=["GET", "POST"])
def remove_card(index):
try:
if request.method == "POST":
del db[index]
save_db()
return redirect(url_for('welcome'))
else:
return render_template("remove_card.html", card=db[index])
except IndexError:
abort(404)
@app.route("/api/card/")
def api_card_list():
return jsonify(db)
@app.route("/api/card/<int:index>")
def api_card_detail(index):
try:
return db[index]
except IndexError:
abort(404)