Skip to content
content.py 41.5 KiB
Newer Older
# -*- coding: utf-8 -*-

from __future__ import division

__copyright__ = "Copyright (C) 2014 Andreas Kloeckner"

__license__ = """
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""

Andreas Klöckner's avatar
Andreas Klöckner committed
from django.conf import settings
from django.utils.translation import ugettext as _
Andreas Klöckner's avatar
Andreas Klöckner committed

import re
import datetime
import six
Andreas Klöckner's avatar
Andreas Klöckner committed

from django.utils.timezone import now
from django.core.exceptions import ObjectDoesNotExist, ImproperlyConfigured
from django.urls import NoReverseMatch
Andreas Klöckner's avatar
Andreas Klöckner committed

from markdown.extensions import Extension
from markdown.treeprocessors import Treeprocessor

from six.moves import html_parser
from jinja2 import (
        BaseLoader as BaseTemplateLoader, TemplateNotFound, FileSystemLoader)
from relate.utils import dict_to_struct, SubdirRepoWrapper
if sys.version_info >= (3,):
    CACHE_KEY_ROOT = "py3"
else:
    CACHE_KEY_ROOT = "py2"

from typing import (  # noqa
        cast, Union, Any, List, Tuple, Optional, Callable, Text)
if False:
    # for mypy
    from course.models import Course, Participation  # noqa
    import dulwich  # noqa
    from course.validation import ValidationContext  # noqa
    from course.page.base import PageBase  # noqa
    from relate.utils import Struct, Repo_ish  # noqa

    Date_ish = Union[datetime.datetime, datetime.date]
    Datespec = Union[datetime.datetime, datetime.date, Text]


class ChunkRulesDesc(Struct):
    if_has_role = None  # type: List[Text]
    if_before = None  # type: Datespec
    if_after = None  # type: Datespec
    if_in_facility = None  # type: Text
    roles = None  # type: List[Text]
    start = None  # type: Datespec
    end = None  # type: Datespec
    shown = None  # type: bool
    weight = None  # type: float


class ChunkDesc(Struct):
    weight = None  # type: float
    shown = None  # type: bool
    title = None  # type: Optional[Text]
    content = None  # type: Text
    rules = None  # type: List[ChunkRulesDesc]
class StaticPageDesc(Struct):
    chunks = None  # type: List[ChunkDesc]
    content = None  # type: Text


class CourseDesc(StaticPageDesc):
    pass


class StartRuleDesc(Struct):
    pass

class AccessRuleDesc(Struct):
    pass


class GradingRuleDesc(Struct):
    grade_identifier = None  # type: Optional[Text]
    grade_aggregation_strategy = None  # type: Optional[Text]


class FlowRulesDesc(Struct):
    start = None  # type: List[StartRuleDesc]
    access = None  # type: List[AccessRuleDesc]
    grading = None  # type: List[GradingRuleDesc]
    grade_identifier = None  # type: Optional[Text]
    grade_aggregation_strategy = None  # type: Optional[Text]


class FlowPageDesc(Struct):
    id = None  # type: Text
    type = None  # type: Text


class FlowPageGroupDesc(Struct):
    id = None  # type: Text
    pages = None  # type: List[FlowPageDesc]


class FlowDesc(Struct):
    title = None  # type: Text
    rules = None  # type: FlowRulesDesc
    pages = None  # type: List[FlowPageDesc]
    groups = None  # type: List[FlowPageGroupDesc]
    notify_on_submit = None  # type: Optional[List[Text]]


# }}}


# {{{ repo blob getting
def get_true_repo_and_path(repo, path):
    # type: (Repo_ish, Text) -> Tuple[dulwich.Repo, Text]

    if isinstance(repo, SubdirRepoWrapper):
        if path:
            path = repo.subdir + "/" + path
        else:
            path = repo.subdir

        return repo.repo, path

    else:
        return repo, path


def get_course_repo_path(course):
    from os.path import join
    return join(settings.GIT_ROOT, course.identifier)


def get_course_repo(course):
    repo = Repo(get_course_repo_path(course))

    if course.course_root_path:
        return SubdirRepoWrapper(repo, course.course_root_path)
    else:
        return repo
Andreas Klöckner's avatar
Andreas Klöckner committed
def get_repo_blob(repo, full_name, commit_sha, allow_tree=True):
    # type: (Repo_ish, Text, bytes, bool) -> dulwich.Blob

    :arg full_name: A Unicode string indicating the file name.
    :arg commit_sha: A byte string containing the commit hash
Andreas Klöckner's avatar
Andreas Klöckner committed
    :arg allow_tree: Allow the resulting object to be a directory
    dul_repo, full_name = get_true_repo_and_path(repo, full_name)
    names = full_name.split("/")

    # Allow non-ASCII file name
    full_name_bytes = full_name.encode('utf-8')
    tree_sha = dul_repo[commit_sha].tree
    tree = dul_repo[tree_sha]
Loading
Loading full blame...