-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
92 lines (71 loc) · 2.88 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import datetime
from flask import Flask, render_template, jsonify, request
from flask_cors import CORS
app = Flask(__name__)
app.secret_key = "change"
CORS(app)
vocabulary = [
{"question": "Haus", "answer": "house"},
{"question": "Baum", "answer": "tree"},
{"question": "Computer", "answer": "computer"},
{"question": "Ente", "answer": "duck"},
{"question": "Hund", "answer": "dog"},
{"question": "Katze", "answer": "cat"},
{"question": "Schule", "answer": "school"},
{"question": "Apfel", "answer": "apple"},
{"question": "Banane", "answer": "banana"},
{"question": "Pfirsich", "answer": "peach"}
]
statistics = [
{"name": "Edmund", "points": 80},
{"name": "Silas", "points": 70},
{"name": "Manuel", "points": 50},
]
@app.route("/")
def index():
return render_template("index.html")
@app.route("/learn/")
def learn():
return render_template("learn.html")
@app.route("/teach/")
def teach():
return render_template("teach.html")
@app.route("/api/vocabulary/", methods=["GET"])
def get_vocabulary():
return jsonify(vocabulary)
@app.route("/api/vocabulary/", methods=["POST"])
def post_vocabulary():
global vocabulary
# Check whether all fields have a value
for i in ["answer", "question"]:
for j in range(1, 11):
if not request.form.get(i + str(j)):
return jsonify({"error": "Nicht alles ausgefüllt"})
vocabulary = [
{"question": request.form.get("question1"), "answer": request.form.get("answer1")},
{"question": request.form.get("question2"), "answer": request.form.get("answer2")},
{"question": request.form.get("question3"), "answer": request.form.get("answer3")},
{"question": request.form.get("question4"), "answer": request.form.get("answer4")},
{"question": request.form.get("question5"), "answer": request.form.get("answer5")},
{"question": request.form.get("question6"), "answer": request.form.get("answer6")},
{"question": request.form.get("question7"), "answer": request.form.get("answer7")},
{"question": request.form.get("question8"), "answer": request.form.get("answer8")},
{"question": request.form.get("question9"), "answer": request.form.get("answer9")},
{"question": request.form.get("question10"), "answer": request.form.get("answer10")}
]
return jsonify(vocabulary)
@app.route("/api/statistics/", methods=["POST"])
def post_statistics():
# Append new statistic to list
statistics.append({
"name": request.form.get("name"),
"points": int(request.form.get("points"))
})
# Create copy of statisics
copied_statistics = statistics[:]
# Sort statistics
copied_statistics.sort(key=lambda d: (d['points']))
copied_statistics = copied_statistics[::-1]
return jsonify(copied_statistics)
if __name__ == "__main__":
app.run(debug=True, host="0.0.0.0")