Beispiel #1
0
class RunYear(Model):
    """A year of runs"""
    def __str__(self):
        return self.path.parts[-1]

    runs = MultipleModelDirProperty([Course, CourseLink])
Beispiel #2
0
class Root(Model):
    """The base of the model"""
    def __init__(self, path):
        super().__init__(self, path)

    collections = DirProperty(Collection, 'lessons')
    courses = MultipleModelDirProperty([Course, CourseLink], '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()}

    @reify
    def safe_runs(self):
        return {(year, run.slug): run
                for year, run_year in self.safe_run_years.items()
                for run in run_year}

    @reify
    def meta(self):
        return MetaInfo()

    @reify
    def safe_run_years(self):
        # since even the basic info about the forked runs can be broken, we need to make sure the required info
        # is provided. If ``RAISE_FORK_ERRORS`` is set, exceptions are raised here, otherwise the run is
        # ignored completely.
        safe_years = {}
        for year, run_years in self.run_years.items():
            safe_run_years = []

            for run in run_years.runs.values():
                if not run.is_link():
                    safe_run_years.append(run)
                elif (naucse.utils.routes.forks_enabled()
                      and naucse.utils.routes.does_course_return_info(
                          run, extra_required=["start_date", "end_date"])):
                    safe_run_years.append(run)

            safe_years[year] = safe_run_years

        return safe_years

    @reify
    def ongoing_and_recent_runs(self):
        """Get runs that are either ongoing or ended in the last 3 months."""
        ongoing = [run for run in self.safe_runs.values() if run.is_ongoing]
        cutoff = _TODAY - datetime.timedelta(days=3 * 31)
        recent = [
            run for run in self.safe_runs.values()
            if not run.is_ongoing and run.end_date > cutoff
        ]
        return {"ongoing": ongoing, "recent": recent}

    def runs_from_year(self, year):
        """Get all runs that either started or ended in a given year."""
        return [
            run for run in self.safe_runs.values()
            if run.start_date.year <= year and run.end_date.year >= year
        ]

    def get_lesson(self, name):
        if isinstance(name, Lesson):
            return name
        if name[-1] == "/":
            name = name[:-1]
        collection_name, name = name.split('/', 2)
        collection = self.collections[collection_name]
        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
Beispiel #3
0
class Root(Model):
    """The base of the model"""
    def __init__(self, path):
        super().__init__(self, path)

    collections = DirProperty(Collection, 'lessons')
    courses = MultipleModelDirProperty([Course, CourseLink], '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()}

    @reify
    def safe_runs(self):
        return {(year, run.slug): run
                for year, run_year in self.safe_run_years.items()
                for run in run_year}

    @reify
    def meta(self):
        return MetaInfo()

    @reify
    def safe_run_years(self):
        # since even the basic info about the forked runs can be broken,
        # we need to make sure the required info is provided.
        # If ``RAISE_FORK_ERRORS`` is set, exceptions are raised here.
        # Otherwise the run is ignored completely.
        safe_years = {}
        for year, run_years in self.run_years.items():
            safe_run_years = []

            for run in run_years.runs.values():
                if not run.is_link():
                    safe_run_years.append(run)
                elif (naucse.utils.views.forks_enabled()
                      and naucse.utils.views.does_course_return_info(
                          run, extra_required=["start_date", "end_date"])):
                    safe_run_years.append(run)

            safe_years[year] = safe_run_years

        return safe_years

    def runs_from_year(self, year):
        """Get all runs started in a given year."""
        run_year = self.safe_run_years.get(year)
        if run_year:
            return sorted(run_year, key=attrgetter('start_date'))
        return []

    def get_lesson(self, name):
        if isinstance(name, Lesson):
            return name
        if name[-1] == "/":
            name = name[:-1]
        collection_name, name = name.split('/', 2)
        collection = self.collections[collection_name]
        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