Skip to content

Commit

Permalink
Allow static files as materials
Browse files Browse the repository at this point in the history
Each session can now have static files as materials.
Expected usage:
- Homework assignments (in PDF)
- Slides for presentations
- Homework feedback

These are the use cases for:
pyvec/naucse.python.cz#432
  • Loading branch information
encukou committed Feb 6, 2019
1 parent dffc8c5 commit 89f4bd2
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 4 deletions.
26 changes: 22 additions & 4 deletions naucse_render/course.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from .load import read_yaml
from .markdown import convert_markdown
from .lesson import get_static_files


def encode_to_json(value):
Expand All @@ -22,10 +23,10 @@ def encode_to_json(value):
return {k: encode_to_json(v) for k, v in value.items()}
elif isinstance(value, (list, tuple)):
return [encode_to_json(v) for v in value]
elif isinstance(value, (str, int, bool, type(None))):
return value
elif isinstance(value, Markup):
return str(value)
elif isinstance(value, (str, int, bool, type(None))):
return value
raise TypeError(value)


Expand Down Expand Up @@ -101,7 +102,8 @@ def get_course(course_slug: str, *, path='.', version=None):

# Update all materials
for material in session.get('materials', []):
update_material(material, vars=info.get('vars'), path=base_path)
update_material(
session, material, vars=info.get('vars'), path=base_path)

# Convert Markdown in "description" to HTML
if 'description' in session:
Expand All @@ -126,7 +128,7 @@ def get_course(course_slug: str, *, path='.', version=None):
}


def update_material(material, vars=None, *, path):
def update_material(session, material, vars=None, *, path):
"""Update material entry: mainly, add computed fields"""
# All materials should have a "type", as used for the icon in lists
lesson_slug = material.pop('lesson', None)
Expand All @@ -136,6 +138,8 @@ def update_material(material, vars=None, *, path):
if material.pop('url', None):
pass
# XXX: raise ValueError(f'Material {material} has URL')
if material.pop('static', None):
raise ValueError(f'Material {material} has a static file')
material.setdefault('type', 'lesson')
if 'title' not in material:
# Set title based on the referenced lesson
Expand All @@ -144,10 +148,24 @@ def update_material(material, vars=None, *, path):
else:
# External link (or link-less entry)
url = material.pop('url', None)
static = material.pop('static', None)
if url:
material['external_url'] = url
# XXX: Probably a bug; this should be just 'link'
material.setdefault('type', 'none-link')
if static:
raise ValueError('`url` and `static_file` are exclusive')
elif static:
session_path = path.joinpath(session['source_file']).parent
filename = session_path / 'static' / static
if filename.exists():
material['static_file'] = {
'path': str(filename.relative_to(path)),
}
else:
raise FileNotFound(filename)
material.setdefault('slug', static)
material.setdefault('type', 'special')
else:
material.setdefault('type', 'special')

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
api_version:
- 0
- 0
course:
sessions:
- materials:
- external_url: https://nauc.se
title: A URL
type: none-link
- title: A note
type: special
slug: apecial
source_file: courses/content-test/info.yml
title: Special materials
- materials:
- slug: report.txt
static_file:
path: courses/content-test/static/report.txt
title: A static text file
type: special
- slug: slides.html
static_file:
path: courses/content-test/static/slides.html
title: A static HTML page
type: special
- slug: smile.png
static_file:
path: courses/content-test/static/smile.png
title: A static image
type: special
slug: static
source_file: courses/content-test/info.yml
title: Static file materials
source_file: courses/content-test/info.yml
title: A course for testing content
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
title: A course for testing content

sessions:
- title: Special materials
slug: apecial
materials:
- title: A URL
url: https://nauc.se
- title: A note
url: null
- title: Static file materials
slug: static
materials:
- title: A static text file
static: report.txt
- title: A static HTML page
static: slides.html
- title: A static image
static: smile.png
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
All OK!
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<html>
<head>
<title>Slides</title>
</head>
<body>
<section>A</section>
<section>B</section>
</body>
</html>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions test_naucse_render/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
'slug',
[
'courses/normal-course',
'courses/content-test',
'2000/run-without-times',
'2000/run-with-times',
'lessons',
Expand Down

0 comments on commit 89f4bd2

Please sign in to comment.