forked from hedyorg/hedy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
for_teachers.py
827 lines (713 loc) · 39.9 KB
/
for_teachers.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
import collections
import json
import os
import uuid
from flask import g, jsonify, request, session
from jinja_partials import render_partial
from flask_babel import gettext
import hedy
import hedy_content
import hedyweb
import utils
from safe_format import safe_format
from website.server_types import SortedAdventure
from website.flask_helpers import render_template
from website.auth import (
current_user,
is_admin,
is_teacher,
requires_login,
requires_teacher,
store_new_student_account,
validate_student_signup_data,
)
from .achievements import Achievements
from .database import Database
from .website_module import WebsiteModule, route
SLIDES = collections.defaultdict(hedy_content.NoSuchSlides)
for lang in hedy_content.ALL_LANGUAGES.keys():
SLIDES[lang] = hedy_content.Slides(lang)
class ForTeachersModule(WebsiteModule):
def __init__(self, db: Database, achievements: Achievements):
super().__init__("teachers", __name__, url_prefix="/for-teachers")
self.db = db
self.achievements = achievements
@route("/", methods=["GET"])
@requires_teacher
def for_teachers_page(self, user):
welcome_teacher = session.get("welcome-teacher") or False
session.pop("welcome-teacher", None)
teacher_classes = self.db.get_teacher_classes(current_user()["username"], True)
adventures = []
for adventure in self.db.get_teacher_adventures(current_user()["username"]):
adventures.append(
{
"id": adventure.get("id"),
"name": adventure.get("name"),
"date": utils.localized_date_format(adventure.get("date")),
"level": adventure.get("level"),
}
)
keyword_language = request.args.get('keyword_language', default=g.keyword_lang, type=str)
slides = []
for level in range(hedy.HEDY_MAX_LEVEL + 1):
if SLIDES[g.lang].get_slides_for_level(level, keyword_language):
slides.append(level)
return render_template(
"for-teachers.html",
current_page="for-teachers",
page_title=gettext("title_for-teacher"),
teacher_classes=teacher_classes,
teacher_adventures=adventures,
welcome_teacher=welcome_teacher,
slides=slides,
javascript_page_options=dict(
page='for-teachers',
welcome_teacher=welcome_teacher,
))
@route("/manual", methods=["GET"], defaults={'section_key': 'intro'})
@route("/manual/<section_key>", methods=["GET"])
def get_teacher_manual(self, section_key):
content = hedyweb.PageTranslations("for-teachers").get_page_translations(g.lang)
# Code very defensively around types here -- Weblate has a tendency to mess up the YAML,
# so the structure cannot be trusted.
page_title = content.get('title', '')
sections = {section['key']: section for section in content['sections']}
section_titles = [(section['key'], section.get('title', '')) for section in content['sections']]
current_section = sections.get(section_key)
if not current_section:
return utils.error_page(error=404, ui_message=gettext("page_not_found"))
intro = current_section.get('intro')
# Some pages have 'subsections', others have 'levels'. We're going to treat them ~the same.
# Give levels a 'title' field as well (doesn't have it in the YAML).
subsections = current_section.get('subsections', [])
for subsection in subsections:
subsection.setdefault('title', '')
levels = current_section.get('levels', [])
for level in levels:
level['title'] = gettext('level') + ' ' + str(level['level'])
subsection_titles = [x.get('title', '') for x in subsections + levels]
return render_template("teacher-manual.html",
current_page="teacher-manual",
page_title=page_title,
section_titles=section_titles,
section_key=section_key,
section_title=current_section['title'],
intro=intro,
subsection_titles=subsection_titles,
subsections=subsections,
levels=levels)
@route("/class/<class_id>", methods=["GET"])
@requires_login
def get_class(self, user, class_id):
if not is_teacher(user) and not is_admin(user):
return utils.error_page(error=403, ui_message=gettext("retrieve_class_error"))
Class = self.db.get_class(class_id)
if not Class or (Class["teacher"] != user["username"] and not is_admin(user)):
return utils.error_page(error=404, ui_message=gettext("no_such_class"))
students = []
for student_username in Class.get("students", []):
student = self.db.user_by_username(student_username)
programs = self.db.programs_for_user(student_username)
# Fixme: The get_quiz_stats function requires a list of ids -> doesn't work on single string
quiz_scores = self.db.get_quiz_stats([student_username])
# Verify if the user did finish any quiz before getting the max() of the finished levels
finished_quizzes = any("finished" in x for x in quiz_scores)
highest_quiz = max([x.get("level") for x in quiz_scores if x.get("finished")]) if finished_quizzes else "-"
students.append(
{
"username": student_username,
"last_login": student["last_login"],
"programs": len(programs),
"highest_level": highest_quiz,
}
)
# Sort the students by their last login
students = sorted(students, key=lambda d: d.get("last_login", 0), reverse=True)
# After sorting: replace the number value by a string format date
for student in students:
student["last_login"] = utils.localized_date_format(student.get("last_login", 0))
if utils.is_testing_request(request):
return jsonify({"students": students, "link": Class["link"], "name": Class["name"], "id": Class["id"]})
achievement = None
if len(students) > 20:
achievement = self.achievements.add_single_achievement(user["username"], "full_house")
if achievement:
achievement = json.dumps(achievement)
invites = []
for invite in self.db.get_class_invites(Class["id"]):
invites.append(
{
"username": invite["username"],
"timestamp": utils.localized_date_format(invite["timestamp"], short_format=True),
"expire_timestamp": utils.localized_date_format(invite["ttl"], short_format=True),
}
)
return render_template(
"class-overview.html",
current_page="for-teachers",
page_title=gettext("title_class-overview"),
achievement=achievement,
invites=invites,
class_info={
"students": students,
"link": os.getenv("BASE_URL", "") + "/hedy/l/" + Class["link"],
"teacher": Class["teacher"],
"name": Class["name"],
"id": Class["id"],
},
javascript_page_options=dict(page="class-overview"),
)
@route("/customize-class/<class_id>", methods=["GET"])
@requires_login
def get_class_customization_page(self, user, class_id):
if not is_teacher(user) and not is_admin(user):
return utils.error_page(error=403, ui_message=gettext("retrieve_class_error"))
Class = self.db.get_class(class_id)
if not Class or (Class["teacher"] != user["username"] and not is_admin(user)):
return utils.error_page(error=404, ui_message=gettext("no_such_class"))
session['class_id'] = class_id
customizations, adventures, adventure_names, available_adventures, min_level = self.get_class_info(
user, class_id)
return render_template(
"customize-class.html",
page_title=gettext("title_customize-class"),
class_info={"name": Class["name"], "id": Class["id"], "teacher": Class["teacher"]},
max_level=hedy.HEDY_MAX_LEVEL,
customizations=customizations,
adventures=adventures,
adventure_names=adventure_names,
available_adventures=available_adventures,
adventures_default_order=hedy_content.ADVENTURE_ORDER_PER_LEVEL,
current_page="for-teachers",
min_level=min_level,
class_id=class_id,
javascript_page_options=dict(
page='customize-class',
class_id=class_id
))
@route("/get-customization-level", methods=["GET"])
@requires_login
def change_dropdown_level(self, user):
if not is_teacher(user) and not is_admin(user):
return utils.error_page(error=403, ui_message=gettext("retrieve_class_error"))
Class = self.db.get_class(session['class_id'])
if not Class or (Class["teacher"] != user["username"] and not is_admin(user)):
return utils.error_page(error=404, ui_message=gettext("no_such_class"))
level = request.args.get('level')
customizations, adventures, adventure_names, available_adventures, _ = self.get_class_info(
user, session['class_id'])
return render_partial('customize-class/partial-sortable-adventures.html',
level=level,
customizations=customizations,
adventures=adventures,
max_level=hedy.HEDY_MAX_LEVEL,
adventure_names=adventure_names,
adventures_default_order=hedy_content.ADVENTURE_ORDER_PER_LEVEL,
available_adventures=available_adventures,
class_id=session['class_id'])
@route("/add-adventure/level/<level>", methods=["POST"])
@requires_login
def add_adventure(self, user, level):
if not is_teacher(user) and not is_admin(user):
return utils.error_page(error=403, ui_message=gettext("retrieve_class_error"))
Class = self.db.get_class(session['class_id'])
if not Class or (Class["teacher"] != user["username"] and not is_admin(user)):
return utils.error_page(error=404, ui_message=gettext("no_such_class"))
adventure_id = request.form.get('adventure_id')
customizations, adventures, adventure_names, available_adventures, _ = self.get_class_info(
user, session['class_id'])
teacher_adventures = self.db.get_teacher_adventures(user["username"])
is_teacher_adventure = self.is_adventure_from_teacher(adventure_id, teacher_adventures)
customizations['sorted_adventures'][level].append({'name': adventure_id, 'from_teacher': is_teacher_adventure})
sorted_adventure = SortedAdventure(short_name=adventure_id,
long_name=adventure_names[adventure_id],
is_teacher_adventure=is_teacher_adventure,
is_command_adventure=adventure_id in hedy_content.KEYWORDS_ADVENTURES)
adventures[int(level)].append(sorted_adventure)
self.db.update_class_customizations(customizations)
available_adventures = self.get_unused_adventures(adventures, teacher_adventures, adventure_names)
return render_partial('customize-class/partial-sortable-adventures.html',
level=level,
customizations=customizations,
adventures=adventures,
max_level=hedy.HEDY_MAX_LEVEL,
adventure_names=adventure_names,
adventures_default_order=hedy_content.ADVENTURE_ORDER_PER_LEVEL,
available_adventures=available_adventures,
class_id=session['class_id'])
@route("/remove-adventure", methods=["POST"])
@requires_login
def remove_adventure_from_class(self, user):
if not is_teacher(user) and not is_admin(user):
return utils.error_page(error=403, ui_message=gettext("retrieve_class_error"))
Class = self.db.get_class(session['class_id'])
if not Class or (Class["teacher"] != user["username"] and not is_admin(user)):
return utils.error_page(error=404, ui_message=gettext("no_such_class"))
adventure_id = request.args.get('adventure_id')
level = request.args.get('level')
teacher_adventures = self.db.get_teacher_adventures(user["username"])
is_teacher_adventure = self.is_adventure_from_teacher(adventure_id, teacher_adventures)
customizations, adventures, adventure_names, available_adventures, _ = self.get_class_info(
user, session['class_id'])
customizations['sorted_adventures'][level].remove({'name': adventure_id, 'from_teacher': is_teacher_adventure})
sorted_adventure = SortedAdventure(short_name=adventure_id,
long_name=adventure_names[adventure_id],
is_teacher_adventure=is_teacher_adventure,
is_command_adventure=adventure_id in hedy_content.KEYWORDS_ADVENTURES)
adventures[int(level)].remove(sorted_adventure)
self.db.update_class_customizations(customizations)
available_adventures = self.get_unused_adventures(adventures, teacher_adventures, adventure_names)
return render_partial('customize-class/partial-sortable-adventures.html',
level=level,
customizations=customizations,
adventures=adventures,
max_level=hedy.HEDY_MAX_LEVEL,
adventure_names=adventure_names,
adventures_default_order=hedy_content.ADVENTURE_ORDER_PER_LEVEL,
available_adventures=available_adventures,
class_id=session['class_id'])
@route("/sort-adventures", methods=["POST"])
@requires_login
def sort_adventures_in_class(self, user):
if not is_teacher(user) and not is_admin(user):
return utils.error_page(error=403, ui_message=gettext("retrieve_class_error"))
Class = self.db.get_class(session['class_id'])
if not Class or (Class["teacher"] != user["username"] and not is_admin(user)):
return utils.error_page(error=404, ui_message=gettext("no_such_class"))
customizations, adventures, adventure_names, available_adventures, _ = self.get_class_info(
user, session['class_id'])
level = request.args.get('level')
adventures_from_request = request.form.getlist('adventure')
teacher_adventures = self.db.get_teacher_adventures(user["username"])
customizations['sorted_adventures'][level] = []
adventures[int(level)] = []
for adventure in adventures_from_request:
is_teacher_adventure = self.is_adventure_from_teacher(adventure, teacher_adventures)
customizations['sorted_adventures'][level].append({'name': adventure, 'from_teacher': is_teacher_adventure})
sorted_adventure = SortedAdventure(short_name=adventure,
long_name=adventure_names[adventure],
is_teacher_adventure=is_teacher_adventure,
is_command_adventure=adventure in hedy_content.KEYWORDS_ADVENTURES)
adventures[int(level)].append(sorted_adventure)
self.db.update_class_customizations(customizations)
return render_partial('customize-class/partial-sortable-adventures.html',
level=level,
customizations=customizations,
adventures=adventures,
max_level=hedy.HEDY_MAX_LEVEL,
adventure_names=adventure_names,
adventures_default_order=hedy_content.ADVENTURE_ORDER_PER_LEVEL,
available_adventures=available_adventures,
class_id=session['class_id'])
def get_class_info(self, user, class_id):
if hedy_content.Adventures(g.lang).has_adventures():
default_adventures = hedy_content.Adventures(g.lang).get_adventure_keyname_name_levels()
else:
default_adventures = hedy_content.Adventures("en").get_adventure_keyname_name_levels()
teacher_adventures = self.db.get_teacher_adventures(user["username"])
customizations = self.db.get_class_customizations(class_id)
# Initialize the data structures that will hold the adventures for each level
adventures = {i: [] for i in range(1, hedy.HEDY_MAX_LEVEL+1)}
min_level = 1
adventure_names = {}
for adv_key, adv_dic in default_adventures.items():
for name, _ in adv_dic.items():
adventure_names[adv_key] = name
for adventure in teacher_adventures:
adventure_names[adventure['id']] = adventure['name']
if customizations:
# in case this class has thew new way to select adventures
if 'sorted_adventures' in customizations:
# remove from customizations adventures that we have removed
self.purge_customizations(customizations['sorted_adventures'], default_adventures)
# it uses the old way so convert it to the new one
elif 'adventures' in customizations:
customizations['sorted_adventures'] = {str(i): [] for i in range(1, hedy.HEDY_MAX_LEVEL + 1)}
for adventure, levels in customizations['adventures'].items():
for level in levels:
customizations['sorted_adventures'][str(level)].append(
{"name": adventure, "from_teacher": False})
self.db.update_class_customizations(customizations)
min_level = min(customizations['levels'])
else:
# Since it doesn't have customizations loaded, we create a default customization object.
# This makes further updating with HTMX easier
adventures_to_db = {}
for level, default_adventures in hedy_content.ADVENTURE_ORDER_PER_LEVEL.items():
adventures_to_db[str(level)] = [{'name': adventure, 'from_teacher': False}
for adventure in default_adventures]
customizations = {
"id": class_id,
"levels": [i for i in range(1, hedy.HEDY_MAX_LEVEL + 1)],
"opening_dates": {},
"other_settings": [],
"level_thresholds": {},
"sorted_adventures": adventures_to_db
}
self.db.update_class_customizations(customizations)
for level, sorted_adventures in customizations['sorted_adventures'].items():
for adventure in sorted_adventures:
sorted_adventure = SortedAdventure(short_name=adventure['name'],
long_name=adventure_names[adventure['name']],
is_command_adventure=adventure['name']
in hedy_content.KEYWORDS_ADVENTURES,
is_teacher_adventure=adventure['from_teacher'])
adventures[int(level)].append(sorted_adventure)
available_adventures = self.get_unused_adventures(adventures, teacher_adventures, adventure_names)
return customizations, adventures, adventure_names, available_adventures, min_level
# This function is used to remove from the customizations default adventures that we have removed
# Otherwise they might cause an error when the students try to access a level
def purge_customizations(self, sorted_adventures, adventures):
for _, adventure_list in sorted_adventures.items():
for adventure in list(adventure_list):
if not adventure['from_teacher'] and adventure['name'] not in adventures:
adventure_list.remove(adventure)
def get_unused_adventures(self, adventures, teacher_adventures_db, adventure_names):
available_adventures = {i: [] for i in range(1, hedy.HEDY_MAX_LEVEL+1)}
default_adventures = {i: set() for i in range(1, hedy.HEDY_MAX_LEVEL+1)}
teacher_adventures = {i: set() for i in range(1, hedy.HEDY_MAX_LEVEL+1)}
for level, level_default_adventures in hedy_content.ADVENTURE_ORDER_PER_LEVEL.items():
for short_name in level_default_adventures:
adventure = SortedAdventure(short_name=short_name,
long_name=adventure_names[short_name],
is_teacher_adventure=False,
is_command_adventure=short_name in hedy_content.KEYWORDS_ADVENTURES)
default_adventures[level].add(adventure)
for teacher_adventure in teacher_adventures_db:
adventure = SortedAdventure(short_name=teacher_adventure['id'],
long_name=teacher_adventure['name'],
is_teacher_adventure=True,
is_command_adventure=False)
teacher_adventures[int(teacher_adventure['level'])].add(adventure)
for level in range(1, hedy.HEDY_MAX_LEVEL + 1):
adventures_set = set(adventures[level])
available_teacher_adventures = teacher_adventures[level].difference(adventures_set)
available_default_adventures = default_adventures[level].difference(adventures_set)
available_adventures[level] = list(available_teacher_adventures.union(available_default_adventures))
available_adventures[level].sort(key=lambda item: item.long_name.upper())
return available_adventures
def is_adventure_from_teacher(self, adventure_id, teacher_adventures):
is_teacher_adventure = False
for adventure in teacher_adventures:
if adventure_id == adventure['id']:
is_teacher_adventure = True
return is_teacher_adventure
@route("/restore-customizations", methods=["POST"])
@requires_teacher
def restore_customizations_to_default(self, user):
class_id = session['class_id']
level = request.args.get('level')
Class = self.db.get_class(class_id)
teacher_adventures = self.db.get_teacher_adventures(user["username"])
if not Class or Class["teacher"] != user["username"]:
return utils.error_page(error=404, ui_message=gettext("no_such_class"))
customizations, adventures, adventure_names, available_adventures, _ = self.get_class_info(
user, session['class_id'])
db_adventures = {str(i): [] for i in range(1, hedy.HEDY_MAX_LEVEL + 1)}
adventures = {i: [] for i in range(1, hedy.HEDY_MAX_LEVEL + 1)}
for lvl, default_adventures in hedy_content.ADVENTURE_ORDER_PER_LEVEL.items():
for adventure in default_adventures:
db_adventures[str(lvl)].append({'name': adventure, 'from_teacher': False})
sorted_adventure = SortedAdventure(short_name=adventure,
long_name=adventure_names[adventure],
is_command_adventure=adventure in hedy_content.KEYWORDS_ADVENTURES,
is_teacher_adventure=False)
adventures[lvl].append(sorted_adventure)
for adventure in teacher_adventures:
available_adventures[int(adventure['level'])].append(
{"name": adventure['id'], "from_teacher": True})
customizations = {
"id": class_id,
"levels": [i for i in range(1, hedy.HEDY_MAX_LEVEL + 1)],
"opening_dates": {},
"other_settings": [],
"level_thresholds": {},
"sorted_adventures": db_adventures
}
self.db.update_class_customizations(customizations)
available_adventures = self.get_unused_adventures(adventures, teacher_adventures, adventure_names)
return render_partial('customize-class/partial-sortable-adventures.html',
level=level,
customizations=customizations,
adventures=adventures,
max_level=hedy.HEDY_MAX_LEVEL,
adventure_names=adventure_names,
adventures_default_order=hedy_content.ADVENTURE_ORDER_PER_LEVEL,
available_adventures=available_adventures,
class_id=session['class_id'])
@route("/restore-adventures/level/<level>", methods=["POST"])
@requires_teacher
def restore_adventures_to_default(self, user, level):
class_id = session['class_id']
Class = self.db.get_class(class_id)
if not Class or Class["teacher"] != user["username"]:
return utils.error_page(error=404, ui_message=gettext("no_such_class"))
customizations, adventures, adventure_names, available_adventures, _ = self.get_class_info(
user, session['class_id'])
teacher_adventures = self.db.get_teacher_adventures(user["username"])
db_adventures = {str(i): [] for i in range(1, hedy.HEDY_MAX_LEVEL + 1)}
adventures = {i: [] for i in range(1, hedy.HEDY_MAX_LEVEL + 1)}
for lvl, default_adventures in hedy_content.ADVENTURE_ORDER_PER_LEVEL.items():
for adventure in default_adventures:
db_adventures[str(lvl)].append({'name': adventure, 'from_teacher': False})
sorted_adventure = SortedAdventure(short_name=adventure,
long_name=adventure_names[adventure],
is_command_adventure=adventure in hedy_content.KEYWORDS_ADVENTURES,
is_teacher_adventure=False)
adventures[lvl].append(sorted_adventure)
customizations['sorted_adventures'] = db_adventures
available_adventures = self.get_unused_adventures(adventures, teacher_adventures, adventure_names)
self.db.update_class_customizations(customizations)
return render_partial('customize-class/partial-sortable-adventures.html',
level=level,
customizations=customizations,
adventures=adventures,
max_level=hedy.HEDY_MAX_LEVEL,
adventure_names=adventure_names,
adventures_default_order=hedy_content.ADVENTURE_ORDER_PER_LEVEL,
available_adventures=available_adventures,
class_id=session['class_id'])
@route("/restore-adventures-modal/level/<level>", methods=["GET"])
@requires_teacher
def get_restore_adventures_modal(self, user, level):
Class = self.db.get_class(session['class_id'])
if not Class or Class["teacher"] != user["username"]:
return utils.error_page(error=404, ui_message=gettext("no_such_class"))
modal_text = gettext('reset_adventure_prompt')
htmx_endpoint = f'/for-teachers/restore-adventures/level/{level}'
htmx_target = "#adventure-dragger"
htmx_indicator = "#indicator"
return render_partial('modal/hx-modal-confirm.html',
modal_text=modal_text,
htmx_endpoint=htmx_endpoint,
htmx_target=htmx_target,
htmx_indicator=htmx_indicator)
@route("/customize-class/<class_id>", methods=["POST"])
@requires_teacher
def update_customizations(self, user, class_id):
Class = self.db.get_class(class_id)
if not Class or Class["teacher"] != user["username"]:
return utils.error_page(error=404, ui_message=gettext("no_such_class"))
body = request.json
# Validations
if not isinstance(body, dict):
return gettext("ajax_error"), 400
if not isinstance(body.get("levels"), list):
return "Levels must be a list", 400
if not isinstance(body.get("other_settings"), list):
return "Other settings must be a list", 400
if not isinstance(body.get("opening_dates"), dict):
return "Opening dates must be a dict", 400
if not isinstance(body.get("level_thresholds"), dict):
return "Level thresholds must be a dict", 400
# Values are always strings from the front-end -> convert to numbers
levels = [int(i) for i in body["levels"]]
opening_dates = body["opening_dates"].copy()
for level, timestamp in body.get("opening_dates").items():
if len(timestamp) < 1:
opening_dates.pop(level)
else:
try:
opening_dates[level] = utils.datetotimeordate(timestamp)
except BaseException:
return "One or more of your opening dates is invalid", 400
level_thresholds = {}
for name, value in body.get("level_thresholds").items():
# We only manually check for the quiz threshold, if we add more -> generalize this code
if name == "quiz":
try:
value = int(value)
except BaseException:
return "Quiz threshold value is invalid", 400
if value < 0 or value > 100:
return "Quiz threshold value is invalid", 400
level_thresholds[name] = value
customizations = self.db.get_class_customizations(class_id)
customizations = {
"id": class_id,
"levels": levels,
"opening_dates": opening_dates,
"other_settings": body["other_settings"],
"level_thresholds": level_thresholds,
"sorted_adventures": customizations["sorted_adventures"]
}
self.db.update_class_customizations(customizations)
achievement = self.achievements.add_single_achievement(user["username"], "my_class_my_rules")
if achievement:
return {"achievement": achievement, "success": gettext("class_customize_success")}, 200
return {"success": gettext("class_customize_success")}, 200
@route("/create-accounts/<class_id>", methods=["GET"])
@requires_teacher
def create_accounts(self, user, class_id):
current_class = self.db.get_class(class_id)
if not current_class or current_class.get("teacher") != user.get("username"):
return utils.error_page(error=403, ui_message=gettext("no_such_class"))
return render_template("create-accounts.html", current_class=current_class)
@route("/create-accounts", methods=["POST"])
@requires_teacher
def store_accounts(self, user):
body = request.json
# Validations
if not isinstance(body, dict):
return gettext("ajax_error"), 400
if not isinstance(body.get("accounts"), list):
return "accounts should be a list!", 400
if len(body.get("accounts", [])) < 1:
return gettext("no_accounts"), 400
usernames = []
# Validation for correct types and duplicates
for account in body.get("accounts", []):
validation = validate_student_signup_data(account)
if validation:
return validation, 400
if account.get("username").strip().lower() in usernames:
return {"error": gettext("unique_usernames"), "value": account.get("username")}, 200
usernames.append(account.get("username").strip().lower())
# Validation for duplicates in the db
classes = self.db.get_teacher_classes(user["username"], False)
for account in body.get("accounts", []):
if account.get("class") and account["class"] not in [i.get("name") for i in classes]:
return "not your class", 404
if self.db.user_by_username(account.get("username").strip().lower()):
return {"error": gettext("usernames_exist"), "value": account.get("username").strip().lower()}, 200
# Now -> actually store the users in the db
for account in body.get("accounts", []):
# Set the current teacher language and keyword language as new account language
account["language"] = g.lang
account["keyword_language"] = g.keyword_lang
store_new_student_account(self.db, account, user["username"])
if account.get("class"):
class_id = [i.get("id") for i in classes if i.get("name") == account.get("class")][0]
self.db.add_student_to_class(class_id, account.get("username").strip().lower())
return {"success": gettext("accounts_created")}, 200
@route("/customize-adventure/view/<adventure_id>", methods=["GET"])
@requires_login
def view_adventure(self, user, adventure_id):
if not is_teacher(user) and not is_admin(user):
return utils.error_page(error=403, ui_message=gettext("retrieve_adventure_error"))
adventure = self.db.get_adventure(adventure_id)
if not adventure:
return utils.error_page(error=404, ui_message=gettext("no_such_adventure"))
if adventure["creator"] != user["username"] and not is_admin(user):
return utils.error_page(error=403, ui_message=gettext("retrieve_adventure_error"))
# Add level to the <pre> tag to let syntax highlighting know which highlighting we need!
adventure["content"] = adventure["content"].replace(
"<pre>", "<pre class='no-copy-button' level='" + str(adventure["level"]) + "'>"
)
adventure["content"] = safe_format(adventure["content"], **hedy_content.KEYWORDS.get(g.keyword_lang))
return render_template(
"view-adventure.html",
adventure=adventure,
page_title=gettext("title_view-adventure"),
current_page="for-teachers",
)
@route("/customize-adventure/<adventure_id>", methods=["GET"])
@requires_teacher
def get_adventure_info(self, user, adventure_id):
adventure = self.db.get_adventure(adventure_id)
if not adventure or adventure["creator"] != user["username"]:
return utils.error_page(error=404, ui_message=gettext("no_such_adventure"))
# Now it gets a bit complex, we want to get the teacher classes as well as the customizations
# This is a quite expensive retrieval, but we should be fine as this page is not called often
# We only need the name, id and if it already has the adventure set as data to the front-end
Classes = self.db.get_teacher_classes(user["username"])
class_data = []
for Class in Classes:
temp = {"name": Class.get("name"), "id": Class.get("id"), "checked": False}
customizations = self.db.get_class_customizations(Class.get("id"))
if customizations and adventure_id in customizations.get("teacher_adventures", []):
temp["checked"] = True
class_data.append(temp)
return render_template(
"customize-adventure.html",
page_title=gettext("title_customize-adventure"),
adventure=adventure,
class_data=class_data,
max_level=hedy.HEDY_MAX_LEVEL,
current_page="for-teachers",
)
@route("/customize-adventure", methods=["POST"])
@requires_teacher
def update_adventure(self, user):
body = request.json
# Validations
if not isinstance(body, dict):
return gettext("ajax_error"), 400
if not isinstance(body.get("id"), str):
return gettext("adventure_id_invalid"), 400
if not isinstance(body.get("name"), str):
return gettext("adventure_name_invalid"), 400
if not isinstance(body.get("level"), str):
return gettext("level_invalid"), 400
if not isinstance(body.get("content"), str):
return gettext("content_invalid"), 400
if len(body.get("content")) < 20:
return gettext("adventure_length"), 400
if not isinstance(body.get("public"), bool):
return gettext("public_invalid"), 400
current_adventure = self.db.get_adventure(body["id"])
if not current_adventure or current_adventure["creator"] != user["username"]:
return utils.error_page(error=404, ui_message=gettext("no_such_adventure"))
adventures = self.db.get_teacher_adventures(user["username"])
for adventure in adventures:
if adventure["name"] == body["name"] and adventure["id"] != body["id"]:
return gettext("adventure_duplicate"), 400
# We want to make sure the adventure is valid and only contains correct placeholders
# Try to parse with our current language, if it fails -> return an error to the user
# NOTE: format() instead of safe_format() on purpose!
try:
body["content"].format(**hedy_content.KEYWORDS.get(g.keyword_lang))
except BaseException:
return gettext("something_went_wrong_keyword_parsing"), 400
adventure = {
"date": utils.timems(),
"creator": user["username"],
"name": body["name"],
"level": body["level"],
"content": body["content"],
"public": body["public"],
}
self.db.update_adventure(body["id"], adventure)
return {"success": gettext("adventure_updated")}, 200
@route("/customize-adventure/<adventure_id>", methods=["DELETE"])
@requires_teacher
def delete_adventure(self, user, adventure_id):
adventure = self.db.get_adventure(adventure_id)
if not adventure or adventure["creator"] != user["username"]:
return utils.error_page(error=404, ui_message=gettext("no_such_adventure"))
self.db.delete_adventure(adventure_id)
return {}, 200
@route("/preview-adventure", methods=["POST"])
def parse_preview_adventure(self):
body = request.json
try:
code = safe_format(body.get("code"), **hedy_content.KEYWORDS.get(g.keyword_lang))
except BaseException:
return gettext("something_went_wrong_keyword_parsing"), 400
return {"code": code}, 200
@route("/create_adventure", methods=["POST"])
@requires_teacher
def create_adventure(self, user):
body = request.json
# Validations
if not isinstance(body, dict):
return gettext("ajax_error"), 400
if not isinstance(body.get("name"), str):
return gettext("adventure_name_invalid"), 400
if len(body.get("name")) < 1:
return gettext("adventure_empty"), 400
adventures = self.db.get_teacher_adventures(user["username"])
for adventure in adventures:
if adventure["name"] == body["name"]:
return gettext("adventure_duplicate"), 400
adventure = {
"id": uuid.uuid4().hex,
"date": utils.timems(),
"creator": user["username"],
"name": body["name"],
"level": 1,
"content": "",
}
self.db.store_adventure(adventure)
return {"id": adventure["id"]}, 200