forked from hedyorg/hedy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hedyweb.py
69 lines (55 loc) · 2.55 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
import collections
from website.yaml_file import YamlFile
import attr
import glob
from os import path
from flask import g
from flask_helpers import render_template
import hedy_content
from website.auth import current_user, is_teacher
import re
import utils
from config import config
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] = YamlFile.for_file(trans_file)
def get_translations(self, language, section):
# Merge with English when lacking translations
# Start from a defaultdict
d = collections.defaultdict(lambda: 'Unknown Exception')
d.update(**self.data.get('en', {}).get(section, {}))
d.update(**self.data.get(language, {}).get(section, {}))
return d
def render_code_editor_with_tabs(level_defaults, max_level, level_number, menu, translations, version, loaded_program, adventures, restrictions, adventure_name):
user = current_user()
if not level_defaults:
return utils.page_404 (translations, menu, user['username'], g.lang, translations.get_translations (g.lang, 'ui').get ('no_such_level'))
arguments_dict = {}
# Meta stuff
arguments_dict['level_nr'] = str(level_number)
arguments_dict['level'] = level_number
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) < max_level else None
arguments_dict['amount_next_level'] = restrictions['amount_next_level']
arguments_dict['example_programs'] = restrictions['example_programs']
arguments_dict['hide_prev_level'] = restrictions['hide_prev_level']
arguments_dict['hide_next_level'] = restrictions['hide_next_level']
arguments_dict['menu'] = menu
arguments_dict['latest'] = version
arguments_dict['selected_page'] = 'code'
arguments_dict['page_title'] = f'Level {level_number} – Hedy'
arguments_dict['auth'] = translations.get_translations (g.lang, 'Auth')
arguments_dict['username'] = user['username']
arguments_dict['is_teacher'] = is_teacher(user)
arguments_dict['loaded_program'] = loaded_program
arguments_dict['adventures'] = adventures
arguments_dict['adventure_name'] = adventure_name
# Translations
arguments_dict.update(**translations.get_translations(g.lang, 'ui'))
# Merge level defaults into adventures so it is rendered as the first tab
arguments_dict.update(**attr.asdict(level_defaults))
return render_template("code-page.html", **arguments_dict)