forked from hedyorg/hedy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hedyweb.py
103 lines (84 loc) · 4.23 KB
/
hedyweb.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
import collections
import attr
import glob
from os import path
from flask import jsonify, render_template, abort, redirect
import courses
from auth import current_user
class Translations:
def __init__(self):
self.data = {}
translations = glob.glob('coursedata/texts/*.yaml')
for trans_file in translations:
lang = path.splitext(path.basename(trans_file))[0]
self.data[lang] = courses.load_yaml(trans_file)
def get_translations(self, language, section):
# Merge with English when lacking translations
# Start from a defaultdict
d = collections.defaultdict(lambda: '???')
d.update(**self.data.get('en', {}).get(section, {}))
d.update(**self.data.get(language, {}).get(section, {}))
return d
def render_assignment_editor(request, course, level_number, assignment_number, menu, translations, version, loaded_program, adventure_assignments):
assignment = course.get_assignment(level_number, assignment_number)
if not assignment:
abort(404)
arguments_dict = {}
# Meta stuff
arguments_dict['course'] = course
arguments_dict['level_nr'] = str(level_number)
arguments_dict['assignment_nr'] = assignment.step # Give this a chance to be 'None'
arguments_dict['lang'] = course.language
arguments_dict['level'] = assignment.level
arguments_dict['prev_level'] = int(level_number) - 1 if int(level_number) > 1 else None
arguments_dict['next_level'] = int(level_number) + 1 if int(level_number) < course.max_level() else None
arguments_dict['next_assignment'] = int(assignment_number) + 1 if int(assignment_number) < course.max_step(level_number) else None
arguments_dict['menu'] = menu
arguments_dict['latest'] = version
arguments_dict['selected_page'] = 'code'
arguments_dict['page_title'] = f'Level {level_number} – Hedy'
arguments_dict['docs'] = [attr.asdict(d) for d in assignment.docs]
arguments_dict['auth'] = translations.data [course.language] ['Auth']
arguments_dict['username'] = current_user(request) ['username']
arguments_dict['loaded_program'] = loaded_program
arguments_dict['adventure_assignments'] = adventure_assignments
# Translations
arguments_dict.update(**translations.get_translations(course.language, 'ui'))
# Actual assignment
arguments_dict.update(**attr.asdict(assignment))
# Add markdowns to docs
for doc in arguments_dict ['docs']:
doc ['markdown'] = (course.docs.get(int(level_number), doc ['slug']) or {'markdown': ''}).markdown
return render_template("code-page.html", **arguments_dict)
def render_adventure(adventure_name, adventure, course, request, lang, level_number, menu, translations, version, loaded_program):
arguments_dict = {}
arguments_dict['lang'] = lang
arguments_dict['level_nr'] = str(level_number)
arguments_dict['level'] = level_number
arguments_dict['prev_level'] = level_number - 1 if level_number - 1 in adventure else None
arguments_dict['next_level'] = level_number + 1 if level_number + 1 in adventure else None
arguments_dict['menu'] = menu
arguments_dict['latest'] = version
arguments_dict['selected_page'] = 'code'
arguments_dict['page_title'] = f'Adventure mode: {adventure ["name"]} {level_number} – Hedy'
arguments_dict['auth'] = (translations.data.get (lang) or translations.data ['en']) ['Auth']
arguments_dict['username'] = current_user(request) ['username']
arguments_dict['loaded_program'] = loaded_program
arguments_dict['adventure_name'] = adventure_name
arguments_dict['full_adventure_name'] = adventure ['name']
# Translations
arguments_dict.update(**translations.get_translations(lang, 'ui'))
# Actual assignment
for key, value in adventure [level_number].items ():
arguments_dict [key] = value
if not 'story_text' in arguments_dict:
arguments_dict ['story_text'] = ''
if not 'story_commands' in arguments_dict:
arguments_dict ['story_commands'] = []
# We use the intro_text and commands from the corresponding Hedy level, if they are not present in the adventure
hedy_course = course.get_assignment(level_number, None)
if not 'intro_text' in arguments_dict:
arguments_dict ['intro_text'] = hedy_course.intro_text
if not 'commands' in arguments_dict:
arguments_dict ['commands'] = hedy_course.commands
return render_template("code-page.html", **arguments_dict)