forked from overhangio/tutor-indigo
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplugin.py
More file actions
343 lines (307 loc) · 9.64 KB
/
Copy pathplugin.py
File metadata and controls
343 lines (307 loc) · 9.64 KB
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
from __future__ import annotations
import itertools
import json
import os
import typing as t
from glob import glob
import importlib_resources
from tutor import hooks
from tutor.__about__ import __version_suffix__
from tutormfe.hooks import MFE_APPS, MFE_ATTRS_TYPE, PLUGIN_SLOTS
from .__about__ import __version__
# Handle version suffix in main mode, just like tutor core
if __version_suffix__:
__version__ += "-" + __version_suffix__
################# Configuration
config: t.Dict[str, t.Dict[str, t.Any]] = {
# Add here your new settings
"defaults": {
"VERSION": __version__,
"WELCOME_MESSAGE": "The place for all your online learning",
"PRIMARY_COLOR": "#003366", # Indigo
"ENABLE_DARK_TOGGLE": True,
# Footer links are dictionaries with a "title" and "url"
# To remove all links, run:
# tutor config save --set INDIGO_FOOTER_NAV_LINKS=[]
"FOOTER_NAV_LINKS": [
{"title": "About Us", "url": "/about"},
{"title": "Blog", "url": "/blog"},
{"title": "Donate", "url": "/donate"},
{"title": "Terms of Service", "url": "/tos"},
{"title": "Privacy Policy", "url": "/privacy"},
{"title": "Help", "url": "/help"},
{"title": "Contact Us", "url": "/contact"},
],
},
"unique": {},
"overrides": {},
}
# Theme templates
hooks.Filters.ENV_TEMPLATE_ROOTS.add_item(
str(importlib_resources.files("tutorindigo") / "templates")
)
# This is where the theme is rendered in the openedx build directory
hooks.Filters.ENV_TEMPLATE_TARGETS.add_items(
[
("indigo", "build/openedx/themes"),
("indigo/env.config.jsx", "plugins/mfe/build/mfe"),
],
)
# Force the rendering of scss files, even though they are included in a
# "partials" directory
hooks.Filters.ENV_PATTERNS_INCLUDE.add_items(
[
r"indigo/lms/static/sass/partials/lms/theme/",
r"indigo/cms/static/sass/partials/cms/theme/",
]
)
# init script: set theme automatically
with open(
os.path.join(
str(importlib_resources.files("tutorindigo") / "templates"),
"indigo",
"tasks",
"init.sh",
),
encoding="utf-8",
) as task_file:
hooks.Filters.CLI_DO_INIT_TASKS.add_item(("lms", task_file.read()))
# Override openedx & mfe docker image names
@hooks.Filters.CONFIG_DEFAULTS.add(priority=hooks.priorities.LOW)
def _override_openedx_docker_image(
items: list[tuple[str, t.Any]],
) -> list[tuple[str, t.Any]]:
openedx_image = ""
mfe_image = ""
for k, v in items:
if k == "DOCKER_IMAGE_OPENEDX":
openedx_image = v
elif k == "MFE_DOCKER_IMAGE":
mfe_image = v
if openedx_image:
items.append(("DOCKER_IMAGE_OPENEDX", f"{openedx_image}-indigo"))
if mfe_image:
items.append(("MFE_DOCKER_IMAGE", f"{mfe_image}-indigo"))
return items
# Load all configuration entries
hooks.Filters.CONFIG_DEFAULTS.add_items(
[(f"INDIGO_{key}", value) for key, value in config["defaults"].items()]
)
hooks.Filters.CONFIG_UNIQUE.add_items(
[(f"INDIGO_{key}", value) for key, value in config["unique"].items()]
)
hooks.Filters.CONFIG_OVERRIDES.add_items(list(config["overrides"].items()))
# MFEs that are styled using Indigo
indigo_styled_mfes = [
"learning",
"learner-dashboard",
"profile",
"account",
"discussions",
]
for mfe in indigo_styled_mfes:
hooks.Filters.ENV_PATCHES.add_items(
[
(
f"mfe-dockerfile-post-npm-install-{mfe}",
"""
RUN npm install '@edx/brand@github:@edly-io/brand-openedx#indigo-2.5.3'
""", # noqa: E501
),
]
)
hooks.Filters.ENV_PATCHES.add_item(
(
"mfe-dockerfile-post-npm-install-authn",
"RUN npm install '@edx/brand@github:@edly-io/brand-openedx#indigo-2.5.3'",
)
)
# Include js file in lms main.html, main_django.html, and certificate.html
hooks.Filters.ENV_PATCHES.add_items(
[
# for production
(
"openedx-common-assets-settings",
"""
javascript_files = ['base_application', 'application', 'certificates_wv']
dark_theme_filepath = ['indigo/js/dark-theme.js']
for filename in javascript_files:
if filename in PIPELINE['JAVASCRIPT']:
PIPELINE['JAVASCRIPT'][filename]['source_filenames'] += dark_theme_filepath
""",
),
# for development
(
"openedx-lms-development-settings",
"""
javascript_files = ['base_application', 'application', 'certificates_wv']
dark_theme_filepath = ['indigo/js/dark-theme.js']
for filename in javascript_files:
if filename in PIPELINE['JAVASCRIPT']:
PIPELINE['JAVASCRIPT'][filename]['source_filenames'] += dark_theme_filepath
MFE_CONFIG['INDIGO_ENABLE_DARK_TOGGLE'] = {{ INDIGO_ENABLE_DARK_TOGGLE }}
MFE_CONFIG['INDIGO_FOOTER_NAV_LINKS'] = {{ INDIGO_FOOTER_NAV_LINKS }}
# Ensure Mako templates from the theme are found by the comprehensive theming system.
# The env var COMPREHENSIVE_THEME_DIRS=/openedx/themes is set in the Dockerfile but
# may not be picked up by the dev settings chain; set it explicitly here.
COMPREHENSIVE_THEME_DIRS = ['/openedx/themes']
""",
),
(
"openedx-lms-production-settings",
"""
MFE_CONFIG['INDIGO_ENABLE_DARK_TOGGLE'] = {{ INDIGO_ENABLE_DARK_TOGGLE }}
MFE_CONFIG['INDIGO_FOOTER_NAV_LINKS'] = {{ INDIGO_FOOTER_NAV_LINKS }}
COMPREHENSIVE_THEME_DIRS = ['/openedx/themes']
""",
),
]
)
# Add react components and patches from tutor-indigo
for path in itertools.chain(
glob(
os.path.join(str(importlib_resources.files("tutorindigo") / "components"), "*")
),
glob(os.path.join(str(importlib_resources.files("tutorindigo") / "patches"), "*")),
):
with open(path, encoding="utf-8") as patch_file:
hooks.Filters.ENV_PATCHES.add_item((os.path.basename(path), patch_file.read()))
for mfe in indigo_styled_mfes:
PLUGIN_SLOTS.add_item(
(
mfe,
"org.openedx.frontend.layout.footer.v1",
"""
{
op: PLUGIN_OPERATIONS.Insert,
widget: {
id: 'read_theme_cookie',
type: DIRECT_PLUGIN,
priority: 2,
RenderWidget: AddDarkTheme,
},
},
""",
),
)
if mfe != "learning":
PLUGIN_SLOTS.add_item(
(
mfe,
"desktop_secondary_menu_slot",
"""
{
op: PLUGIN_OPERATIONS.Insert,
widget: {
id: 'theme_switch_button',
type: DIRECT_PLUGIN,
RenderWidget: ToggleThemeButton,
},
},
""",
)
)
PLUGIN_SLOTS.add_items(
[
(
# Hide the default mobile header as it only shows logo
mfe,
"mobile_header_slot",
"""
{
op: PLUGIN_OPERATIONS.Hide,
widgetId: 'default_contents',
}
""",
),
(
mfe,
"mobile_header_slot",
"""
{
op: PLUGIN_OPERATIONS.Insert,
widget: {
id: 'theme_switch_button',
type: DIRECT_PLUGIN,
RenderWidget: MobileViewHeader,
},
},
""",
),
]
)
PLUGIN_SLOTS.add_items(
[
(
# Hide the default Help Link added in plugin slot
"learning",
"learning_help_slot",
"""
{
op: PLUGIN_OPERATIONS.Hide,
widgetId: 'default_contents',
}
""",
),
(
"learning",
"learning_help_slot",
"""
{
op: PLUGIN_OPERATIONS.Insert,
widget: {
id: 'theme_switch_button',
type: DIRECT_PLUGIN,
RenderWidget: ToggleThemeButton,
},
},
""",
),
]
)
paragon_theme_urls = {
"variants": {
"light": {
"urls": {
"default": "https://{{ LMS_HOST }}/static/indigo/css/light.min.css",
"brandOverride": "https://{{ LMS_HOST }}/static/indigo/css/light.min.css",
},
},
"dark": {
"urls": {
"default": "https://{{ LMS_HOST }}/static/indigo/css/dark.min.css",
"brandOverride": "https://{{ LMS_HOST }}/static/indigo/css/dark.min.css",
}
},
}
}
fstring = f"""
MFE_CONFIG["PARAGON_THEME_URLS"] = {json.dumps(paragon_theme_urls)}
"""
hooks.Filters.ENV_PATCHES.add_item(("mfe-lms-common-settings", fstring))
@MFE_APPS.add() # type: ignore
def _add_themed_logo(
mfes: dict[str, MFE_ATTRS_TYPE],
) -> dict[str, MFE_ATTRS_TYPE]:
for mfe in mfes:
PLUGIN_SLOTS.add_item(
(
str(mfe),
"logo_slot",
"""
{
op: PLUGIN_OPERATIONS.Hide,
widgetId: 'default_contents',
},
{
op: PLUGIN_OPERATIONS.Insert,
widget: {
id: 'custom_logo',
type: DIRECT_PLUGIN,
RenderWidget: ThemedLogo,
}
}
""",
)
)
return mfes