forked from hedyorg/hedy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hedyweb.py
86 lines (68 loc) · 3.11 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
import collections
import attr
import glob
from os import path
from flask import jsonify, render_template, abort, redirect
import courses
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(course, level_number, assignment_number, menu, translations, version):
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['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]
# Translations
arguments_dict.update(**translations.get_translations(course.language, 'ui'))
# Actual assignment
arguments_dict.update(**attr.asdict(assignment))
return render_template("code-page.html", **arguments_dict)
def render_assignment_docs(doc_type, course, level_number, assignment_number, menu, translations):
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['pagetitle'] = f'Level {level_number} - Hedy'
arguments_dict['lang'] = course.language
arguments_dict['selected_page'] = doc_type
arguments_dict['docs'] = [attr.asdict(d) for d in assignment.docs]
# Translations
arguments_dict.update(**translations.get_translations(course.language, 'ui'))
doc = course.docs.get(int(level_number), doc_type)
if not doc:
# Redirect to code page. Nasty 'import' here to work around
# cyclic imports.
import app
return redirect(app.hedy_link(level_number, assignment_number))
arguments_dict['mkd'] = doc.markdown
arguments_dict['menu'] = menu
arguments_dict['page_title'] = f'Level {level_number}.{assignment_number} – ' + doc.front_matter.get('title', '') + ' – Hedy'
return render_template("per-level-text.html", **arguments_dict)