-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
163 lines (133 loc) · 4.74 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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import flask
import random
import json
from flask import jsonify
from flask import request
from flask import send_from_directory
import csv
app = flask.Flask(__name__)
# app.config["DEBUG"] = True
@app.route('/', methods=['GET'])
def get():
return '''
<h3>GET /answers/user_id</h3>
<h3>POST /answers/user_id</h3>
<h3>GET /questions/question_id</h3>
<h3>GET /questions/h3>
<h3>GET /config/user_id</h3>
<h3>GET /login/user_id</h3>
<h3>POST /register</h3>
<h3>GET /export/user_id</h3>
'''
@app.route('/export/<user_id>', methods=['GET'])
def export(user_id):
with open(f'answers/{user_id}.json') as json_file:
file = json.load(json_file)
data = file['data']
# now we will open a file for writing
data_file = open(f'csvs/{user_id}.csv', 'w')
# create the csv writer object
csv_writer = csv.writer(data_file)
# Counter variable used for writing
# headers to the CSV file
header = ["ID", "Tekst", "Svar", "Dato", "Spørsmålsgruppe"]
csv_writer.writerow(header)
for session in data:
date = session.get("date")
question_group = session.get("questionGroup")
for answer in session.get("answers"):
row = [answer.get("question_ID"), answer.get("text"), answer.get("value"), date, question_group]
csv_writer.writerow(row)
data_file.close()
return send_from_directory('csvs/', f'{user_id}.csv', as_attachment=True)
@app.route('/answers/<user_id>', methods=['GET', 'POST'])
def answers(user_id):
if request.method == 'GET':
data = open(f'answers/{user_id}.json', 'rb').read()
return json.loads(data)
elif request.method == 'POST':
new_answer = request.get_json()
config_data = json.loads(open(f'answers/{user_id}.json', 'rb').read())
# Add the new answer from POST to current JSON
config_data["data"].append(new_answer)
with open(f'answers/{user_id}.json', 'w', encoding='utf-8') as f:
json.dump(config_data, f, ensure_ascii=False, indent=4)
return f"Data for {user_id} updated!"
@app.route('/questions/<question_id>', methods=['GET'])
def question(question_id):
question_data = open('questions.json', 'rb').read()
question_data = json.loads(question_data)
for question in question_data:
if question["id"] == int(question_id):
return question
return "Question not found!"
@app.route('/questions/', methods=['GET'])
def questions():
question_data = open('questions.json', 'rb').read()
question_data = json.loads(question_data)
print(question_data)
return jsonify(question_data)
@app.route('/config/<user_id>', methods=['GET'])
def config(user_id):
config_data = open('users.json', 'rb').read()
config_data = json.loads(config_data)
for user in config_data:
if user["id"] == int(user_id):
return user
return "User config not found!"
@app.route('/login/<user_id>', methods=['GET'])
def login(user_id):
config_data = open('users.json', 'rb').read()
config_data = json.loads(config_data)
for user in config_data:
if user["id"] == int(user_id):
return {"status": True}
return {"status": False}
def generate_new_id():
config_data = open('users.json', 'rb').read()
config_data = json.loads(config_data)
taken_ids = [user["id"] for user in config_data]
available_ids = [item for item in range(
100000, 999999) if item not in taken_ids]
return random.choice(available_ids)
# Send {"name": "<Name for user>"} in POST
@app.route('/register', methods=['POST'])
def register():
user_id = generate_new_id()
user_name = request.get_json()["name"]
if len(user_name) < 1:
return "Bad username given!"
new_user = {
"id": user_id,
"name": user_name,
"config": {
"answering": {
"yes_no": {
"enabled": True,
"icon": True
},
"slider": {
"enabled": True
},
"voice_recording": {
"enabled": True
},
"digital_clock": True
},
"question": {
"text_to_speech": True
}
}
}
answer_data = {
"answers": [],
"patientID": user_id
}
config_data = open('users.json', 'rb').read()
config_data = json.loads(config_data)
config_data.append(new_user)
with open(f'users.json', 'w', encoding='utf-8') as f:
json.dump(config_data, f, ensure_ascii=False, indent=4)
with open(f'answers/{user_id}.json', 'w', encoding='utf-8') as f:
json.dump(answer_data, f, ensure_ascii=False, indent=4)
return new_user