-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
323 lines (193 loc) · 7.72 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
"""Server for final project"""
from flask import Flask, render_template, request, flash, session, redirect
from model import connect_to_db
import crud
import api
import random
from jinja2 import StrictUndefined
app = Flask(__name__)
app.secret_key = "fsdgsdfgsgf"
app.jinja_env.undefined = StrictUndefined
AFFIRMATIONS = [
"There are people out there who will help me.",
"How I feel matters.",
"I am happy to be me.",
"I matter.",
"I love myself for who I am.",
"I am worthy of love and happiness.",
"I treat myself with love and kindness.",
"My feelings are valid.",
"I deserve all the happiness in the world.",
"Reaching out for support shows strength.",
"I am allowed to have mental health struggles."
]
@app.route('/affirmations')
def fortune():
"""Return a single fortune as a text string (*not* the whole HTML page!)"""
return random.choice(AFFIRMATIONS)
@app.route("/")
def homepage():
"""View homepage."""
return render_template("homepage.html")
@app.route("/create-account")
def create_account():
return render_template("create-account.html")
@app.route("/resources")
def resources():
return render_template("assessmentinfo.html")
@app.route("/users", methods=["POST"])
def register_user():
"""Create a new user."""
email = request.form.get("email")
password = request.form.get("password")
name= request.form.get("name")
lname=request.form.get("lname")
user = crud.get_user_by_email(email)
if user:
flash("Email already associated with account, try logging in")
else:
crud.create_user(email, password, name, lname)
flash("Account created! Please log in.")
return redirect("/")
@app.route("/login", methods=["POST", "GET"])
def process_login():
"""Process user login."""
email = request.form.get("email")
password = request.form.get("password")
name=request.form.get("name")
lname=request.form.get("lname")
user = crud.get_user_by_email(email)
if not user or user.password != password:
flash("The email or password you entered was incorrect.")
else:
# Log in user by storing the user's id in session
session["user_id"] = user.user_id
session["name"] = user.name
return render_template("user-profile.html", user=user)
@app.route("/anxiety")
def anxiety_questionnaire():
if "user_id" not in session:
return redirect("/")
anxiety_questions = crud.get_anxiety_questions()
return render_template("anxiety.html", question=anxiety_questions)
@app.route("/depression")
def depression_questionnaire():
if "user_id" not in session:
return redirect("/")
depression_questions = crud.get_depression_questions()
return render_template("depression.html", question=depression_questions)
@app.route("/answers", methods=["POST"])
def get_user_answers():
user_key=[]
user_values=[]
# session["user_id"] = user.user_id
# answers = request.form.getlist("answer") #gets list of answers
question_info={}
fk_user_id= session["user_id"]
for k,v in request.form.items():
question_info[k]= v
fk_test_question_id=list(question_info.keys())
fk_test_question_id=[int(i) for i in fk_test_question_id]
user_test_question_answer=list(question_info.values())
user_test_question_answer=[int(i) for i in user_test_question_answer]
# print(user.user_id)
print()
print(user_test_question_answer)
print(session["user_id"])
answers=crud.user_total(user_test_question_answer, fk_test_question_id,fk_user_id)
individual_answers=crud.user_nums(user_test_question_answer, fk_test_question_id,fk_user_id)
anxiety_questions = crud.get_anxiety_questions()
baselines=crud.get_rubric()
print(baselines)
# return answers
# print("added")
return render_template("results.html", answers=answers, baselines=baselines, individual_answers=individual_answers, anxiety_questions=anxiety_questions)
@app.route("/depression_answers", methods=["POST"])
def get_user_answers_dep():
if "user_id" not in session:
return redirect("/")
user_key=[]
user_values=[]
# session["user_id"] = user.user_id
# answers = request.form.getlist("answer") #gets list of answers
question_info={}
fk_user_id= session["user_id"]
for k,v in request.form.items():
question_info[k]= v
fk_test_question_id=list(question_info.keys())
fk_test_question_id=[int(i) for i in fk_test_question_id]
user_test_question_answer=list(question_info.values())
user_test_question_answer=[int(i) for i in user_test_question_answer]
# print(user.user_id)
print(fk_test_question_id)
print(user_test_question_answer)
print(session["user_id"])
individual_answers=crud.user_nums(user_test_question_answer, fk_test_question_id,fk_user_id)
depression_questions=crud.get_depression_questions()
answers=crud.user_total(user_test_question_answer, fk_test_question_id,fk_user_id)
baselines=crud.get_rubric_depression()
print(baselines)
# return answers
# print("added")
return render_template("dresults.html", answers=answers, baselines=baselines, depression_questions=depression_questions, individual_answers=individual_answers)
@app.route("/therapist", methods=["POST"])
def find_therapist():
city= request.form.get("city")
provider_list = api.get_therapist_info(city)
return render_template("therapist.html", provider_list=provider_list)
@app.route("/therapist")
def therapist_list():
# provider_list = api.get_therapist_info()
# print(provider_list)
# print(type(provider_list))
return render_template("therapist.html", provider_list=[])
@app.route("/insomnia")
def insomnia_questionnaire():
if "user_id" not in session:
return redirect("/")
insomnia_questions = crud.get_insomnia_questions()
return render_template("insomnia.html", question=insomnia_questions)
@app.route("/insomnia_answers", methods=["POST"])
def get_user_answers_ins():
user_key=[]
user_values=[]
question_info={}
fk_user_id= session["user_id"]
for k,v in request.form.items():
question_info[k]= v
fk_test_question_id=list(question_info.keys())
fk_test_question_id=[int(i) for i in fk_test_question_id]
user_test_question_answer=list(question_info.values())
user_test_question_answer=[int(i) for i in user_test_question_answer]
# print(user.user_id)
print(question_info)
print(fk_test_question_id)
print(user_test_question_answer)
print(session["user_id"])
individual_answers=crud.user_nums(user_test_question_answer, fk_test_question_id,fk_user_id)
insomnia_questions=crud.get_insomnia_questions()
answers=crud.user_total(user_test_question_answer, fk_test_question_id,fk_user_id)
baselines=crud.get_rubric_insomnia()
print(baselines)
# return answers
# print("added")
return render_template("iresults.html", answers=answers, baselines=baselines, insomnia_questions=insomnia_questions, individual_answers=individual_answers)
@app.route('/logout')
def process_logout():
"""Remove user from session"""
if 'user' in session:
session.pop('user', None)
flash('You have successfully logged out.')
else:
flash('You are not logged in.')
return render_template('homepage.html')
@app.route("/map")
def get_map():
return render_template("map.html")
@app.route("/about")
def get_about():
return render_template("about.html")
if __name__ == "__main__":
# DebugToolbarExtension(app)
connect_to_db(app)
app.run(host="0.0.0.0", debug=True)