-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
460 lines (397 loc) · 16.1 KB
/
Copy pathapp.py
File metadata and controls
460 lines (397 loc) · 16.1 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
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
# cd /Users/karolisjanusonis/Documents/aws
# ssh -i "quizApp.pem" ec2-user@16.171.13.150
# sudo pkill gunicorn
# git pull
# sudo gunicorn -w 4 -b 0.0.0.0:80 app:app
from flask import Flask, render_template, request, session, redirect, url_for
import gspread
from google.oauth2.service_account import Credentials
import uuid
from datetime import datetime
app = Flask(__name__)
app.secret_key = 'your_secret_key'
SERVICE_ACCOUNT_FILE = 'quiz-436010-6b96c91ffaa6.json'
SCOPES = ['https://www.googleapis.com/auth/spreadsheets']
credentials = Credentials.from_service_account_file(
SERVICE_ACCOUNT_FILE, scopes=SCOPES)
gc = gspread.authorize(credentials)
SPREADSHEET_ID = '1MQmCQIdUVQgbMJYyVK8x9tW8_ztKmNytjEV4kgJ7SY4'
sh = gc.open_by_key(SPREADSHEET_ID)
worksheet = sh.sheet1
SERVICE_ACCOUNT_FILE = 'quiz-input-acd45b255b71.json'
SCOPES = ['https://www.googleapis.com/auth/spreadsheets']
credentials = Credentials.from_service_account_file(
SERVICE_ACCOUNT_FILE, scopes=SCOPES)
gc = gspread.authorize(credentials)
SPREADSHEET_ID = '1kM7sFQfGjVMgi__bP1WqmIGjKbqQTavkQayoC8aHqlc'
sh = gc.open_by_key(SPREADSHEET_ID)
worksheet_input = sh.sheet1
@app.route('/')
def index():
return render_template('index.html', title="Sveiki atvykę")
@app.route('/start', methods=['POST'])
def start():
session['current_question'] = 0
session['answers'] = []
session['user_id'] = str(uuid.uuid4()) # Unique user ID
return redirect(url_for('questions_route'))
# Define the questions
questions = [
{
'question': 'Ar dalyvaujate Mokytojų palaikymo ratuose (MPR)?',
'options': [
'Dalyvauju MPR 1 metai',
'Dalyvauju MPR 2 ir daugiau metų',
'Nedalyvauju MPR'
]
},
{
'question': 'Mokykloje dirbate:',
'options': [
'Iki vienerių metų',
'Nuo vienerių iki 5 metų',
'Nuo 5 iki 15 metų',
'Virš 15 metų'
]
},
{
'question': 'Šiais metais turite:',
'options': [
'0,5 etato arba mažiau',
'0,5 - 0,8 etato',
'0,9 - 1,1 etato',
'1,2 - 1,5 etato',
'Daugiau kaip 1,5 etato'
]
},
{
'question': 'Miestas: (įrašyti)'
},
{
'question': 'Jūsų amžius:',
'options': [
'Iki 29',
'30-39',
'40-49',
'50-59',
'60 ir daugiau metų'
]
}
]
@app.route('/questions', methods=['GET', 'POST'])
def questions_route():
if 'current_question' not in session:
return redirect(url_for('index'))
current_question = session['current_question']
# Define the 1.1 question and the options that trigger it
q1_1 = {
'question': 'MPR dalyvaujate/ -ote:',
'options': [
'Nuotoliniu būdu',
'„Gyvi“ susitikimai'
]
}
trigger_options = [
'Dalyvauju MPR 1 metai',
'Dalyvauju MPR 2 ir daugiau metų'
]
if current_question >= len(questions):
return redirect(url_for('psychological_wellbeing'))
question_data = questions[current_question]
# Special handling for question 1 and 1.1
if current_question == 0:
if request.method == 'POST':
selected_option = request.form.get('option')
# If 1.1 is being answered
if 'option_1_1' in request.form:
selected_option_1_1 = request.form.get('option_1_1')
# Store both answers
session['answers'].append({
'question': question_data['question'],
'selected_option': session['q1_answer']
})
session['answers'].append({
'question': q1_1['question'],
'selected_option': selected_option_1_1
})
session['current_question'] += 1
session.pop('q1_answer', None)
return redirect(url_for('questions_route'))
# If only Q1 is being answered
if selected_option in trigger_options:
session['q1_answer'] = selected_option
# Render Q1 and Q1.1 together
return render_template('questions.html', question=question_data, question_number=1, total_questions=len(questions), show_q1_1=True, q1_1=q1_1, selected_option=selected_option, title="Klausimas 1")
else:
# Store only Q1 answer
session['answers'].append({
'question': question_data['question'],
'selected_option': selected_option
})
session['answers'].append({
'question': q1_1['question'],
'selected_option': '-'
})
session['current_question'] += 1
return redirect(url_for('questions_route'))
# GET request
return render_template('questions.html', question=question_data, question_number=1, total_questions=len(questions), show_q1_1=False, title="Klausimas 1")
# All other questions
if request.method == 'POST':
selected_option = request.form.get('option')
question_text = question_data['question']
session['answers'].append({
'question': question_text,
'selected_option': selected_option
})
session['current_question'] += 1
return redirect(url_for('questions_route'))
return render_template('questions.html', question=question_data, question_number=current_question + 1, total_questions=len(questions), show_q1_1=False, title=f"Klausimas {current_question + 1}")
@app.route('/psychological_wellbeing', methods=['GET', 'POST'])
def psychological_wellbeing():
questions = [
'Vienaip ar kitaip dažniausiai man pavyksta susitvarkyti',
'Didžiuojuosi tuo, ką pasiekiau gyvenime',
'Dažniausiai gyvenimo įvykius priimu ramiai',
'Gerai sutariu su savimi',
'Jaučiu, kad galiu tvarkytis su daug dalykų vienu metu',
'Esu ryžtingas (-a)',
'Galiu susitvarkyti sunkiais momentais, nes esu patyręs (-usi) sunkumų ir anksčiau',
'Esu disciplinuotas (-a)',
'Aš nuolat ko nors domiuosi',
'Paprastai sugebu įžvelgti ką nors juokinga',
'Tikėjimas savimi man padeda susitvarkyti sunkiais momentais',
'Ištikus nelaimei esu tas (-a), kuriuo (-ia) žmonės gali pasikliauti',
'Mano gyvenimas yra prasmingas',
'Atsidūręs (-usi) sudėtingoje situacijoje dažniausiai galiu rasti išeitį',
]
options = [
'Visiškai nesutinku',
'Nesutinku',
'Nei sutinku, nei nesutinku',
'Sutinku',
'Visiškai sutinku'
]
# Define the scoring for each option
option_scores = {
'Visiškai nesutinku': 1,
'Nesutinku': 2,
'Nei sutinku, nei nesutinku': 4,
'Sutinku': 6,
'Visiškai sutinku': 7
}
total_score = 0
if request.method == 'POST':
answers = []
for i in range(1, len(questions) + 1):
answer = request.form.get(f'question_{i}')
answers.append({
'question': questions[i - 1],
'selected_option': answer
})
for answer in answers:
selected_option = answer['selected_option']
if selected_option in option_scores:
total_score += option_scores[selected_option]
session['psychological_wellbeing_answers'] = answers # Store in session
session['Wellbeing_score'] = total_score
# Redirect to a thank you or results page after submission
return redirect(url_for('work_feelings'))
return render_template('psychological_wellbeing.html', questions=questions, options=options)
@app.route('/work_feelings', methods=['GET', 'POST'])
def work_feelings():
questions = [
'Jaučiu, kad savo darbe per daug dirbu',
'Jaučiuosi nusivylęs savo darbu',
'Jaučiuosi išvargęs (-usi), kai ryta atsikeliu ir turiu eiti į darbą',
'Man lengva suprasti, mokinių poreikius bei jausmus',
'Jaučiu, kad kai kuriuos mokinius vertinu, tarsi jie būtų bereikšmiai objektai',
'Jaučiuosi išsekęs (-usi) darbo pabaigoje',
'Labai efektyviai susitvarkau su mokinių problemomis',
'Jaučiuosi „sudegęs (-usi)“ dėl savo darbo',
'Jaučiu, kad teigiamai veikiu kitus žmones, nes jiems rūpi mano darbas',
'Aš jaudinuosi, kad mokytojo darbas emociškai mane suvaržo',
'Aš ėmiau daug šiurkščiau elgtis su žmonėmis, kai pasirinkau mokytojo darbą',
'Jaučiuosi darbe labai energingas (-a)',
'Jaučiuosi emociškai išsekęs (-usi) nuo darbo',
'Nuolatinis darbas mokykloje sukelia man per didelę įtampą',
'Man nesvarbu, kas nutinka kai kuriems mokiniams',
'Darbas su žmonėmis visą dieną man tikras stresas ir įtampa',
'Aš galiu lengvai sukurti lengvą atmosferą su mokiniais',
'Jaučiuosi kupinas jėgų po darbo dienos su mokiniais',
'Darbe įgyvendinu daug vertingų, prasmingų dalykų',
'Jaučiuosi tarsi darbe būčiau pasiekęs (-usi) savo jėgų ir kantrybės ribas',
'Emocines problemas darbe sprendžiu labai ramiai',
'Jaučiu, kad mokiniai kaltina mane dėl savo pačių problemų'
]
if 'categories' not in session:
session['categories'] = {
'Wellbeing': [questions[i] for i in []],
'Emocinis': [questions[i] for i in [0, 1, 2, 5, 7, 12, 13, 15, 19]],
'Depersonalizacija': [questions[i] for i in [4, 9, 10, 14, 21]],
'Asmeniniu': [questions[i] for i in [3, 6, 8, 11, 16, 17, 18, 20]],
}
options = [
'Niekada',
'Keletą kartų per metus ar rečiau',
'Keletą kartų per mėnesį',
'Kartą per savaitę',
'Kasdien'
]
option_scores = {
'Niekada': 0,
'Keletą kartų per metus ar rečiau': 1,
'Keletą kartų per mėnesį': 2,
'Kartą per savaitę': 3,
'Kasdien': 4
}
if 'category_scores' not in session:
session['category_scores'] = {
'Wellbeing': 0,
'Emocinis': 0,
'Depersonalizacija': 0,
'Asmeniniu': 0,
}
answers = []
if request.method == 'POST':
session['category_scores'] = {
'Wellbeing': 0,
'Emocinis': 0,
'Depersonalizacija': 0,
'Asmeniniu': 0,
}
for i in range(1, 23): # There are 22 questions
answer = request.form.get(f'question_{i}')
answers.append({
'question': questions[i - 1],
'selected_option': answer
})
for category, category_questions in session['categories'].items():
if questions[i - 1] in category_questions:
session['category_scores'][category] += option_scores[answer]
break
session['category_scores']['Wellbeing'] = session['Wellbeing_score']
session['work_feelings_answers'] = answers # Store in session
# Redirect to a thank you or results page after submission
return redirect(url_for('email'))
return render_template('work_feelings.html', questions=questions, options=options)
@app.route('/email', methods=['GET', 'POST'])
def email():
if request.method == 'POST':
email = request.form.get('email')
session['email'] = email
rezult = request.form.get('rezult')
stebeti = request.form.get('stebeti')
info = request.form.get('info')
session['rezult'] = rezult
session['stebeti'] = stebeti
session['info'] = info
return redirect(url_for('thank_you'))
return render_template('email.html', title="Įveskite savo el. paštą")
@app.route('/thank_you', methods=['GET', 'POST'])
def thank_you():
answers = session.get('answers', [])
wellbeing_answers = session.get('psychological_wellbeing_answers', [])
work_feelings_answers = session.get('work_feelings_answers', [])
category_scores = session.get('category_scores', {
'Wellbeing': 0,
'Emocinis': 0,
'Depersonalizacija': 0,
'Asmeniniu': 0,
})
categories = session.get('categories', {
'Wellbeing': [],
'Emocinis': [],
'Depersonalizacija': [],
'Asmeniniu': [],
})
# Save answers to Google Spreadsheet
save_answers_to_sheet(answers, wellbeing_answers, work_feelings_answers)
# Get data from the input sheet
input_data = worksheet_input.get_all_records()
session['pa'] = 0
session['ei'] = 0
session['de'] = 0
session['apv'] = 0
for row in input_data:
if row.get('email') == session.get('email'):
session['pa'] = row.get('pa', 0)
session['ei'] = row.get('ei', 0)
session['de'] = row.get('de', 0)
session['apv'] = row.get('apv', 0)
break
previous_results = {
'pa': session['pa'],
'ei': session['ei'],
'de': session['de'],
'apv': session['apv'],
}
custom_order = ['Wellbeing', 'Emocinis', 'Depersonalizacija', 'Asmeniniu']
sorted_category_scores = {key: category_scores[key] for key in custom_order if key in category_scores}
# If no email, show second chance page
if not session.get('email'):
if request.method == 'POST':
email = request.form.get('email', '').strip()
if email:
session['email'] = email
return redirect(url_for('thank_you'))
else:
# Show simple thank you if still no email
return render_template('thank_you_no_email.html', show_form=False)
return render_template('thank_you_no_email.html', show_form=True)
# If email is present, show full results
return render_template('thank_you.html', title="Ačiū", scores=sorted_category_scores, previous_results=previous_results)
def save_answers_to_sheet(answers, wellbeing_answers, work_feelings_answers):
# Check if the sheet is empty
if len(worksheet.get_all_values()) == 0:
# Add headers
headers = ['Timestamp', 'User ID', 'Question', 'Selected Option']
worksheet.append_row(headers)
timestamp = datetime.utcnow().isoformat()
user_id = session.get('user_id')
row = [timestamp, user_id]
# Append answers
for answer in answers:
row.extend([answer['selected_option']])
# Append psychological wellbeing answers
for wellbeing_answer in wellbeing_answers:
row.extend([wellbeing_answer['selected_option']])
# Append work feelings answers
for work_feeling_answer in work_feelings_answers:
row.extend([work_feeling_answer['selected_option']])
# Append the optional email
email = session.get('email')
row.append(email if email else '-')
rezult = session.get('rezult')
stebeti = session.get('stebeti')
info = session.get('info')
row.append(rezult if rezult else '-')
row.append(stebeti if stebeti else '-')
row.append(info if info else '-')
# Append the category scores
category_scores = session.get('category_scores', {
'Wellbeing': 0,
'Emocinis': 0,
'Depersonalizacija': 0,
'Asmeniniu': 0,
})
for category, score in category_scores.items():
row.append(score)
# Find if user_id already exists in the sheet
all_rows = worksheet.get_all_values()
user_id_col = 1 # 0-based index for 'User ID' column
found_row = None
for idx, r in enumerate(all_rows):
if len(r) > user_id_col and r[user_id_col] == user_id:
found_row = idx + 1 # gspread is 1-based
break
if found_row:
# Update the row (especially if email was missing before)
# Only update the email and extra info columns, not answers
# Email is at col: len(row) - (number of appended fields after answers)
# Let's update the whole row for simplicity
worksheet.update([row], f'A{found_row}:AZ{found_row}')
else:
worksheet.append_row(row)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8000)