-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews.py
920 lines (825 loc) · 33.2 KB
/
views.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
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
"""
This file defines functions to handle requests at URLs defined in urls.py.
"""
from django.core.exceptions import ObjectDoesNotExist
from django.db.models import Q
from django.http import HttpResponse, JsonResponse
from django.template import loader
from django.views.decorators.csrf import csrf_exempt
from .models import *
from .filesystem import *
from . import functions
import json
import pathlib
import re
import tarfile
def json_response(d={}, status='SUCCESS'):
d.update({'status': status})
resp = JsonResponse(d)
return resp
def session_id_required(f):
@functions.wraps(f)
def g(request, *args, **kwargs):
session_id = request.COOKIES['session_id']
try:
study_session = StudySession.objects.get(session_id=session_id)
return f(request, *args, study_session=study_session, **kwargs)
except ObjectDoesNotExist:
return json_response(status='STUDY_SESSION_DOES_NOT_EXIST')
return g
def task_session_id_required(f):
@functions.wraps(f)
def g(request, *args, **kwargs):
session_id = request.COOKIES['session_id']
task_session_id = request.COOKIES['task_session_id']
# ensure that the task session id matches the study session id
if not task_session_id.startswith(session_id):
return json_response(
status='STUDY_SESSION_AND_TASK_SESSION_MISMATCH')
try:
task_session = TaskSession.objects.get(session_id=task_session_id)
return f(request, *args, task_session=task_session, **kwargs)
except ObjectDoesNotExist:
return json_response(status='TASK_SESSION_DOES_NOT_EXIST')
return g
# --- Task Management --- #
@task_session_id_required
def task_session_pause(request, task_session):
task_session.pause()
return json_response()
@task_session_id_required
def update_task_timing(request, task_session):
study_session = task_session.study_session
current_time = timezone.now()
if task_session.start_time is None:
task_session.set_start_time(current_time)
return json_response({
'time_left': task_session.time_left.seconds,
'half_session_time_left':
study_session.half_session_time_left.seconds
})
else:
# User has visited the task URL before, check time left in the task
# session
# TODO: frontend should prevent a user from visiting a task URL more
# than once
# task_session.resume()
return json_response({
'time_left': task_session.time_left.seconds,
'half_session_time_left': study_session.half_session_time_left.seconds
})
@task_session_id_required
def get_current_task(request, task_session):
"""
Args:
task_session
Returns the information of the task which the user is currently working on
in the study session.
"""
task = task_session.task
study_session = task_session.study_session
task_part = study_session.stage
order_number = study_session.num_tasks_completed + 1
is_training = task_session.is_training
if study_session.treatment == 'A':
assistant_tool = 'Tellina'
constraint_tool = ''
else:
assistant_tool = 'Explainshell'
if study_session.stage == 'II':
constraint_tool = '(excluding Tellina) '
else:
constraint_tool = ''
tool_reminder = 'Assistance: {}, the Internet {}and man pages'\
.format(assistant_tool, constraint_tool)
if is_training:
context = {
"is_training": is_training,
"task_part": 'Part {} Training'.format(task_part),
"task_assistant_tool_reminder": tool_reminder,
"task_description": task.description,
"task_order_number": 1,
"total_num_tasks": 1,
"first_name": study_session.user.first_name,
"last_name": study_session.user.last_name,
}
else:
context = {
"is_training": is_training,
"task_part": 'Part {}'.format(task_part),
"task_assistant_tool_reminder": tool_reminder,
"task_description": task.description,
"task_order_number": order_number,
"total_num_tasks": study_session.total_num_tasks,
"first_name": study_session.user.first_name,
"last_name": study_session.user.last_name,
}
template = loader.get_template('task.html')
return HttpResponse(template.render(context, request))
@task_session_id_required
def get_additional_task_info(request, task_session):
"""
Args:
task_session:
Returns additional the maximum time length the user is allowed to spend on
the task.
"""
study_session = task_session.study_session
task = task_session.task
container = task_session.container
container_port = container.port
research_tool_url = Software.objects.get(name='Tellina').url
# with open('fs-7-8.json', 'w') as o_f:
# json.dump(disk_2_dict(
# pathlib.Path('/{}/home/website'.format(container.filesystem_name)),
# [filesystem._MTIME]), o_f)
fs_diff = compute_filesystem_diff(container, task, [],
save_initial_filesystem=True)
if fs_diff:
filesystem_status = "FILE_SYSTEM_WRITTEN_TO_DISK"
else:
filesystem_status = "FILE_SYSTEM_ERROR"
if study_session.stage_change():
if study_session.stage == 'I':
status = 'ENTERING_STAGE_I'
elif study_session.stage == 'II':
status = 'ENTERING_STAGE_II'
else:
status = ''
if task.type == "stdout":
stdout_diff = compute_stdout_diff('', task)
annotate_stdout_errors(fs_diff, stdout_diff)
resp = {
'task_duration': task.duration.seconds,
'task_solution': task.solution,
'treatment_order': task_session.study_session.treatment_order,
'research_tool_url': research_tool_url,
'filesystem_status': filesystem_status,
'filesystem_diff': fs_diff,
'stdout_diff': stdout_diff,
'container_port': container_port
}
else:
resp = {
'task_duration': task.duration.seconds,
'task_solution': task.solution,
'treatment_order': task_session.study_session.treatment_order,
'research_tool_url': research_tool_url,
'filesystem_status': filesystem_status,
'filesystem_diff': fs_diff,
'container_port': container_port
}
return json_response(resp, status=status)
@session_id_required
def go_to_next_task(request, study_session):
"""
Args:
task_session:
Close the existing task session (if there is any) and create a new task
session.
"""
status = ''
# close the currently running task session if there is any
if study_session.current_task_session_id:
task_session = TaskSession.objects.get(
session_id=study_session.current_task_session_id)
if task_session.status == 'running':
# close current_task_session
task_session.close(request.GET['reason_for_close'])
# update relevant study session attributes
if task_session.is_training:
study_session.inc_num_training_tasks_completed()
if study_session.stage == 'I':
status = 'FIRST_TRAINING_TASK_COMPLETE'
elif study_session.stage == 'II':
status = 'SECOND_TRAINING_TASK_COMPLETE'
else:
raise AttributeError('Wrong study session stage: {} while '
'closing training task session'.format(study_session.stage))
else:
# update time left in the current half of the study session
# needs to be done before
print('task_session_time_spent: {}'.format(
task_session.time_spent))
study_session.update_half_session_time_left(
task_session.time_spent)
study_session.inc_num_tasks_completed()
# check for study session stage change or completion
if study_session.stage == 'III':
# study session completed
study_session.close('finished')
resp = json_response(
{
"num_passed": TaskSession.objects.filter(
study_session=study_session, status='passed').count(),
"num_given_up": TaskSession.objects.filter(
study_session=study_session, status='quit').count(),
"num_total": study_session.total_num_tasks
},
status='STUDY_SESSION_COMPLETE')
resp.set_cookie('session_id', '')
resp.set_cookie('task_session_id', '')
else:
# create new task session
next_task_session_id = study_session.update_current_task_session_id()
try:
create_task_session(study_session)
resp = json_response({
"task_session_id": next_task_session_id,
"treatment_order": study_session.treatment_order
}, status=status)
resp.set_cookie('study_session', study_session.session_id)
resp.set_cookie('task_session_id', next_task_session_id)
except ObjectDoesNotExist:
study_session.close('closed_with_error')
resp = json_response(status='TASK_SESSION_CREATION_FAILED')
return resp
def create_task_session(study_session):
"""
Pick a task from the database and initialize a new task session
for the user.
"""
user = study_session.user
study_session_stage = study_session.stage
task_session_id = study_session.current_task_session_id
if not TaskSession.objects.filter(session_id=task_session_id).exists():
# select a task from the task database
if study_session.stage_change() and study_session.stage in ['I', 'II']:
study_session.start_half_session_timer()
is_training = True
task_id = TASK_TRAINING[study_session.num_training_tasks_completed]
else:
is_training = False
if user.group in ['group1', 'group4']:
part1_tasks = TASK_BLOCK_I
part2_tasks = TASK_BLOCK_II
else:
part1_tasks = TASK_BLOCK_II
part2_tasks = TASK_BLOCK_I
if study_session_stage == 'I':
task_id = part1_tasks[study_session.num_tasks_completed]
else:
task_id = part2_tasks[study_session.num_tasks_completed
- len(part1_tasks)]
# create the container of the task session
task = Task.objects.get(task_id=task_id)
container = create_container(task_session_id, task)
start_time = timezone.now() if is_training else None
time_left = task.duration if study_session.half_session_time_left\
> task.duration else study_session.half_session_time_left
TaskSession.objects.create(
study_session = study_session,
study_session_stage = study_session.stage,
session_id = task_session_id,
container = container,
is_training = is_training,
task = task,
start_time = start_time,
time_left = time_left,
status = 'running'
)
print('Task session {} created'.format(task_session_id))
@task_session_id_required
@csrf_exempt
def on_command_execution(request, task_session):
"""
Args:
task_session:
Record user's terminal standard output upon command execution. Check for
task completion.
"""
study_session = task_session.study_session
task = task_session.task
stdout = request.POST['stdout']
# check if there are file path in the stdout
stdout_lines = stdout.split('\n')
current_dir = pathlib.Path(stdout_lines[-1][16:-2])
command = stdout_lines[0]
tokens = command.split()
is_ls_command = False
if tokens and tokens[0] == 'ls':
is_ls_command = True
partial_path = extract_path_from_ls_command(command, tokens)
if partial_path is not None:
current_dir = current_dir / partial_path
stdout_paths = []
for stdout_line in stdout_lines[1:-1]:
path = extract_path(stdout_line, current_dir, is_ls_command)
if path:
stdout_paths.append(path)
stdout = '\n'.join(stdout_lines[1:-1]) if len(stdout_lines) > 2 else ''
ActionHistory.objects.create(
task_session=task_session,
action = command,
stdout = stdout,
action_time = timezone.now()
)
# compute distance between current file system and the goal file system
container = task_session.container
fs_diff = compute_filesystem_diff(container, task, stdout_paths)
if fs_diff is None:
return json_response(status='FILE_SYSTEM_ERROR')
task_completed = False
if task.type == 'stdout':
stdout_diff = compute_stdout_diff(
'\n'.join(stdout_lines[1:-1]), task, current_dir, is_ls_command)
# check if stdout signals task completion
# the files/directories being checked must be presented in full paths
# the file/directory names cannot contain spaces
if stdout_diff['tag'] == 'correct':
task_completed = True
else:
annotate_stdout_errors(fs_diff, stdout_diff)
resp = {
'filesystem_diff': fs_diff,
'stdout_diff': stdout_diff,
'treatment': study_session.treatment
}
elif task.type == 'file_search' or task.type == 'filesystem_change':
# check if the current file system is the same as the goal file system
if not fs_diff['tag']:
task_completed = True
resp = {
'filesystem_diff': fs_diff,
'treatment': study_session.treatment
}
else:
raise AttributeError('Unrecognized task type "{}": must be "stdout",'
'"file_search" or "filesystem_change"'.format(task.type))
if task_completed:
return json_response(resp, status= 'TASK_COMPLETED')
else:
return json_response(resp)
# --- File System Management --- #
@task_session_id_required
def reset_file_system(request, task_session):
"""
Args:
task_session:
Reset the file system of the current task session.
"""
task = task_session.task
# destroy the current container and create a new one
task_session.create_new_container()
container = task_session.container
container_id = container.container_id
ActionHistory.objects.create(
task_session=task_session,
action = '__reset__',
action_time = timezone.now()
)
fs_diff = compute_filesystem_diff(container, task, [])
if fs_diff is None:
filesystem_status = 'FILE_SYSTEM_ERROR'
else:
filesystem_status = 'FILE_SYSTEM_WRITTEN_TO_DISK'
if task.type == 'stdout':
stdout_diff = compute_stdout_diff('', task)
annotate_stdout_errors(fs_diff, stdout_diff)
resp = {
'container_id': container_id,
'container_port': container.port,
'filesystem_diff': fs_diff,
'filesystem_status': filesystem_status,
'stdout_diff': stdout_diff
}
else:
resp = {
'container_id': container_id,
'container_port': container.port,
'filesystem_diff': fs_diff,
'filesystem_status': filesystem_status
}
return json_response(resp)
# --- Task Result Verification --- #
def compute_filesystem_diff(container, task, stdout_paths,
save_initial_filesystem=False):
"""
Compute the difference between the current file system on disk and the goal
file system. Return None if the current file system does not exist.
Args:
container: the container object on which the file system is mounted
task: the task object which contains the definition of the file system
stdout_paths: the paths detected from the user's terminal standard
output which shall be annotated on the diff object
save_initial_filesystem: set to True if the current file system on disk
should be saved to the Task object
"""
filesystem_vfs_path = '/{}/home/website'.format(container.filesystem_name)
current_filesystem = disk_2_dict(pathlib.Path(filesystem_vfs_path),
json.loads(task.file_attributes))
if save_initial_filesystem:
task.initial_filesystem = json.dumps(current_filesystem)
task.save()
if current_filesystem is None:
return None
goal_filesystem = task.initial_filesystem if task.type == 'stdout' \
else task.goal_filesystem
fs_diff = filesystem_diff(current_filesystem, json.loads(goal_filesystem))
# annotate the fs_diff with the stdout_paths
annotate_path_selection(fs_diff, task.type, stdout_paths)
if not contains_error_in_child(fs_diff) and task.task_id == 2:
files_in_tar = set()
try:
tar = tarfile.open(os.path.join(filesystem_vfs_path, 'html.tar'))
for member in tar.getmembers():
files_in_tar.add(os.path.basename(member.name))
except tarfile.ReadError:
# valid tar file does not exist on the target path
pass
if files_in_tar != {'index.html', 'home.html', 'labs.html',
'lesson.html', 'menu.html', 'navigation.html'}:
annotate_node(fs_diff, pathlib.Path('html.tar'),
'incorrect')
return fs_diff
def compute_stdout_diff(stdout, task, current_dir=None, is_ls_command=False):
"""
Compute the difference between the user's current terminal output and the
goal output.
Args:
stdout: the user's current terminal output
task: the task object which contains the definition of the file system
current_dir: the user's current directory
is_ls_command: the user issued an "ls" command
Return:
e.g.
[
{
"line": XXX
"tag": 'correct'
},
{
"line": XXX
"tag": 'extra'
},
{
"line": XXX
"tag": 'missing'
}
]
"""
def __equal__(l1, l2, task_id):
if task_id == 16:
# loose comparison is enough for tasks that requires date/time
# to be outputed in a specific format
path1 = extract_path(l1, current_dir, is_ls_command)
path2 = extract_path(l2, '~/website', is_ls_command)
if path1 == path2:
time_long_iso_re = re.compile(
r'\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}(:\d{2}(\.\d+)?)?')
if re.search(time_long_iso_re, l1):
return True
elif task_id == 19:
# loose comparison is enough for tasks that requires the number of
# lines in a file
num_of_lines, _ = l2.split()
num_of_lines_pattern = re.compile(r'{}\s'.format(num_of_lines))
path1 = extract_path(l1, current_dir, is_ls_command)
path2 = extract_path(l2, '~/website', is_ls_command)
if path1 == path2 and (re.search(num_of_lines_pattern, l1)):
return True
else:
return l1 == l2
return False
stdout1 = [line.strip() for line in stdout.split('\n') if line]
stdout2 = [line.strip() for line in task.stdout.split('\n')]
stdout_diff = []
tag = 'correct'
if task.task_id != 10:
# boolean variable which is used decide if the "total line" should be
# shown as correct or as an error
matched_stdout2 = []
for l1 in stdout1:
if not l1:
continue
matched = False
for i in range(len(stdout2)):
if not i in matched_stdout2 and \
__equal__(l1, stdout2[i], task.task_id):
matched = True
matched_stdout2.append(i)
break
if matched:
line_tag = 'correct'
else:
total_pattern = re.compile(r'(total\s|\stotal)')
current_parent_dir_pattern = re.compile(r'\s(\.|\.\.)$')
if (re.search(total_pattern, l1) and len(l1) < 20):
line_tag = 'correct'
elif re.search(current_parent_dir_pattern, l1):
line_tag = 'correct'
else:
line_tag = 'extra'
stdout_diff.append({
'line': l1,
'tag': line_tag
})
for i in range(len(stdout2)):
if not i in matched_stdout2:
l2 = stdout2[i]
stdout_diff.append({
'line': l2,
'tag': 'missing'
})
tag = 'incorrect'
else:
i = 0
j = 0
while i < len(stdout1) and j < len(stdout2):
l1 = stdout1[i]
l2 = stdout2[j]
path1 = extract_path(l1, current_dir)
path2 = extract_path(l2, current_dir)
if path1 and path1 == path2:
stdout_diff.append({
'line': l1,
'tag': 'correct'
})
i += 1
j += 1
else:
if path1 is None or path1.name < path2.name:
stdout_diff.append({
'line': l1,
'tag': 'extra'
})
tag = 'incorrect'
i += 1
else:
stdout_diff.append({
'line': l2,
'tag': 'missing'
})
tag = 'incorrect'
j += 1
if i < len(stdout1):
for l1 in stdout1[i:]:
stdout_diff.append({
'line': l1,
'tag': 'extra'
})
tag = 'incorrect'
if j < len(stdout2):
for l2 in stdout2[j:]:
stdout_diff.append({
'line': l2,
'tag': 'missing'
})
tag = 'incorrect'
return { 'lines': stdout_diff, 'tag': tag }
# --- User Login --- #
def register_user(request):
"""
Args:
first_name
last_name
Returns "USER_EXISTS" message if the user is already registered in the
database, otherwise, returns the system-wise unique access code for the
user.
"""
first_name = request.GET['first_name']
last_name = request.GET['last_name']
if User.objects.filter(first_name=first_name, last_name=last_name).exists():
return json_response({
'access_code': 'USER_EXISTS'
})
else:
# assign the new user to a group
num_registered_users = User.objects.all().count()
groups = ['group4', 'group1', 'group2', 'group3']
group = groups[num_registered_users % 4]
# make access code for user
access_code = first_name.lower() + '-' + last_name.lower()
User.objects.create(
first_name = first_name,
last_name = last_name,
access_code = access_code,
group = group
)
return json_response({
'access_code': access_code,
'group': group
})
def user_login(request):
"""
Args:
access_code
check_existing_session
Create a container for the new study session.
"""
access_code = request.GET['access_code']
check_existing_session = request.GET['check_existing_session']
try:
user = User.objects.get(access_code=access_code)
# check if an incomplete study session of the user exists
if check_existing_session == "true":
existing_sessions = []
for session in StudySession.objects\
.filter(user=user).filter(Q(status='reading_consent') |
Q(status='reading_instructions')):
# if the user is still at the pre-training stage for that
# session, ignore it
session.close('closed_with_error')
healthy_sessions = []
for session in StudySession.objects\
.filter(user=user).filter(Q(status='running') |
Q(status='training'))\
.order_by('creation_time'):
try:
task_session = TaskSession.objects.get(
session_id=session.current_task_session_id)
if task_session.status in ['running', 'paused']:
healthy_sessions.append(session)
else:
session.close('closed_with_error')
except ObjectDoesNotExist:
# close the study session due to errors in the task session
session.close('closed_with_error')
if healthy_sessions:
# close previous running sessions except for the most recent one
existing_sessions.extend(healthy_sessions)
for session in existing_sessions[:-1]:
session.close('closed_with_error')
session = existing_sessions[-1]
# cache the study session id and the task session id in cookies
resp = JsonResponse({
"status": "RUNNING_STUDY_SESSION_FOUND",
"task_session_id": session.current_task_session_id
})
if check_existing_session == "false" or not healthy_sessions:
# register a new study session for the user
session_id = '-'.join([access_code, "study_session",
str(StudySession.objects.filter(user=user).count() + 1)])
StudySession.objects.create(
user = user,
session_id = session_id,
creation_time = timezone.now(),
)
# remember the study session id with cookies
resp = json_response(status="SESSION_CREATED")
resp.set_cookie('session_id', session_id)
except ObjectDoesNotExist:
resp = json_response(status='USER_DOES_NOT_EXIST')
return resp
def resume_task_session(request):
# recreate a new container in case the original session is off
# due to container issue
task_session_id = request.GET['task_session_id']
task_session = TaskSession.objects.get(session_id=task_session_id)
task_session.create_new_container()
resp = json_response({"task_session_id": task_session_id},
status="SESSION_CREATED")
resp.set_cookie('session_id', task_session.study_session.session_id)
resp.set_cookie('task_session_id', task_session_id)
return resp
def retrieve_access_code(request):
first_name = request.GET['first_name']
last_name = request.GET['last_name']
try:
access_code = User.objects.get(first_name=first_name,
last_name=last_name).access_code
return JsonResponse({
"access_code": access_code
})
except ObjectDoesNotExist:
return JsonResponse({
"access_code": 'USER_DOES_NOT_EXIST'
})
@session_id_required
def instruction(request, study_session):
template = loader.get_template('instruction.html')
context = {
'treatment_order': study_session.treatment_order,
}
return HttpResponse(template.render(context, request))
@session_id_required
def instruction_read(request, study_session):
study_session.status = 'running'
study_session.save()
return json_response({
'task_session_id': study_session.current_task_session_id
})
@session_id_required
def consent(request, study_session):
template = loader.get_template('consent.html')
context = {
'researcher_list': Researcher.objects.all()
}
return HttpResponse(template.render(context, request))
@session_id_required
def consent_signed(request, study_session):
# log user's IP address for the study session
ip_address = request.GET['ip_address']
if ip_address is None:
# "http://ipinfo.io failed
ip_address = ''
study_session.set_ip_address(ip_address)
study_session.status = 'reading_instructions'
study_session.save()
return json_response()
# --- Study Session Report --- #
def study_session_report(request):
template = loader.get_template('report.html')
context = {}
first_name = request.GET['first_name']
last_name = request.GET['last_name']
user = User.objects.get(first_name=first_name, last_name=last_name)
for study_session in StudySession.objects.filter(
user=user, status='finished'):
if study_session.treatment_order == '0':
part_i_assistant_tool = 'Tellina'
part_ii_assistant_tool = 'Explainshell'
elif study_session.treatment_order == '1':
part_i_assistant_tool = 'Explainshell'
part_ii_assistant_tool = 'Tellina'
else:
raise ValueError('Unrecognized treatment_order "{}"'.format(
study_session.treatment_order
))
part_i_task_sessions = []
part_ii_task_sessions = []
for task_session in TaskSession.objects.filter(
study_session=study_session, is_training=False)\
.order_by('start_time'):
if task_session.study_session_stage == 'I':
part_i_task_sessions.append(task_session)
elif task_session.study_session_stage == 'II':
part_ii_task_sessions.append(task_session)
context = {
'first_name': first_name,
'last_name': last_name,
'part_i_task_sessions': part_i_task_sessions,
'part_ii_task_sessions': part_ii_task_sessions,
'part_i_average_time_spent': study_session.
stage_average_time_spent('I'),
'part_ii_average_time_spent': study_session.
stage_average_time_spent('II'),
'part_i_assistant_tool': part_i_assistant_tool,
'part_ii_assistant_tool': part_ii_assistant_tool,
'session_id': study_session.session_id
}
return HttpResponse(template.render(context, request))
return HttpResponse(template.render(context, request))
def overview(request):
template = loader.get_template('overview.html')
user_groups = [[], [], [], []]
for user in User.objects.all():
if len(user.access_code) > 3:
# find the user's most recently finished study session
finished_study_session = None
for study_session in StudySession.objects.filter(user=user,
status='finished').order_by('creation_time'):
finished_study_session = study_session
treatment_effect = None
if finished_study_session:
print(finished_study_session.session_id)
if finished_study_session.treatment_order == '0':
treatment_effect = \
finished_study_session.stage_average_time_spent('I') -\
finished_study_session.stage_average_time_spent('II')
elif finished_study_session.treatment_order == '1':
treatment_effect = \
finished_study_session.stage_average_time_spent('II') -\
finished_study_session.stage_average_time_spent('I')
if treatment_effect is not None and treatment_effect < \
timezone.timedelta(seconds=0):
treatment_effect = '-{}'.format(timezone.timedelta(seconds=0)-
treatment_effect)
else:
treatment_effect = '{}'.format(treatment_effect)
# not a sudo user
if user.group == 'group1':
user_groups[0].append((user, treatment_effect))
elif user.group == 'group2':
user_groups[1].append((user, treatment_effect))
elif user.group == 'group3':
user_groups[2].append((user, treatment_effect))
elif user.group == 'group4':
user_groups[3].append((user, treatment_effect))
context = { 'user_groups': user_groups }
return HttpResponse(template.render(context, request))
def action_history(request):
template = loader.get_template('action_history.html')
session_id = request.GET['study_session_id']
stage = request.GET['stage']
task_order_number = int(request.GET['task_order_number'])
print(task_order_number)
study_session = StudySession.objects.get(session_id=session_id)
i = 0
task_session = None
for task_session in TaskSession.objects.filter(
study_session=study_session, study_session_stage=stage,
is_training=False).order_by('start_time'):
i += 1
if i == task_order_number:
break
action_history = []
for action in ActionHistory.objects.filter(task_session=task_session)\
.order_by('action_time'):
action_history.append(action)
context = {
'task_order_number': task_order_number,
'task_session': task_session,
'action_history': action_history
}
return HttpResponse(template.render(context, request))