class Root(Model): """The base of the model""" def __init__(self, path): super().__init__(self, path) collections = DirProperty(Collection, 'lessons') courses = DirProperty(Course, 'courses') run_years = DirProperty(RunYear, 'runs', keyfunc=int) licenses = DirProperty(License, 'licenses') courses_edit_path = Path("courses") runs_edit_path = Path("runs") @reify def runs(self): return {(year, slug): run for year, run_year in self.run_years.items() for slug, run in run_year.runs.items()} def get_lesson(self, name, base_collection=None): if isinstance(name, Lesson): return name if '/' in name: base_collection, name = name.split('/', 2) collection = self.collections[base_collection] return collection.lessons[name] @reify def lesson_jinja_env(self): env = jinja2.Environment( loader=jinja2.FileSystemLoader([str(self.path / 'lessons')]), autoescape=jinja2.select_autoescape(['html', 'xml']), ) setup_jinja_env(env) return env
class RunYear(Model): """A year of runs""" def __str__(self): return self.path.parts[-1] runs = DirProperty(Course)
class Collection(Model): """A collection of lessons""" def __str__(self): return self.path.parts[-1] lessons = DirProperty(Lesson)