-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
executable file
·52 lines (40 loc) · 1.27 KB
/
app.py
File metadata and controls
executable file
·52 lines (40 loc) · 1.27 KB
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
import json
from flask import Flask, request
from pymongo import MongoClient
app = Flask(__name__)
db = MongoClient('db')
@app.route('/')
def root():
# ugly static file serving :-)
with open('index.html', 'r') as f:
return f.read()
@app.route('/frontend.js')
def js():
# ugly static file serving :-)
with open('frontend.js', 'r') as f:
return f.read()
@app.route('/api/students/', methods=['POST', 'GET'])
def student():
if request.method == 'POST':
# get the json
student = request.get_json(force=True)
# check data
if not 'name' in student.keys():
return json.dumps({
'status': 'error: student must have a name'
}), 400
elif not 'grades' in student.keys():
return json.dumps({
'status': 'error: student must have grades'
}), 400
# insert in db
students = db.app.students
students.insert_one(student)
return json.dumps({'status': 'student inserted'})
else:
# return the students in db
students = []
for s in db.app.students.find():
students.append({'name': s['name'], 'grades': s['grades']})
return json.dumps(students)
app.run(host='0.0.0.0', port=8000)