Skip to content
content.py 42.3 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, Struct, SubdirRepoWrapper
from course.constants import ATTRIBUTES_FILENAME
if sys.version_info >= (3,):
    CACHE_KEY_ROOT = "py3"
else:
    CACHE_KEY_ROOT = "py2"

        cast, Union, Any, List, Tuple, Optional, Callable, Text, Dict)
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 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


Andreas Klöckner's avatar
Andreas Klöckner committed
class FlowSessionStartRuleDesc(Struct):
    if_after = None  # type: Date_ish
    if_before = None  # type: Date_ish
    if_has_role = None  # type: list
    if_in_facility = None  # type: Text
    if_has_in_progress_session = None  # type: bool
    if_has_session_tagged = None  # type: Optional[Text]
    if_has_fewer_sessions_than = None  # type: int
    if_has_fewer_tagged_sessions_than = None  # type: int
    if_signed_in_with_matching_exam_ticket = None  # type: bool
    tag_session = None  # type: Optional[Text]
    may_start_new_session = None  # type: bool
    may_list_existing_sessions = None  # type: bool
    lock_down_as_exam_session = None  # type: bool
    default_expiration_mode = None  # type: Text
Andreas Klöckner's avatar
Andreas Klöckner committed


class FlowSessionAccessRuleDesc(Struct):
    permissions = None  # type: list
    if_after = None  # type: Date_ish
    if_before = None  # type: Date_ish
    if_started_before = None  # type: Date_ish
    if_has_role = None  # type: List[Text]
    if_in_facility = None  # type: Text
    if_has_tag = None  # type: Optional[Text]
    if_in_progress = None  # type: bool
    if_completed_before = None  # type: Date_ish
    if_expiration_mode = None  # type: Text
    if_session_duration_shorter_than_minutes = None  # type: float
    if_signed_in_with_matching_exam_ticket = None  # type: bool
    message = None  # type: Text
Andreas Klöckner's avatar
Andreas Klöckner committed
class FlowSessionGradingRuleDesc(Struct):
    grade_identifier = None  # type: Optional[Text]
    grade_aggregation_strategy = None  # type: Optional[Text]


class FlowRulesDesc(Struct):
Andreas Klöckner's avatar
Andreas Klöckner committed
    start = None  # type: List[FlowSessionStartRuleDesc]
    access = None  # type: List[FlowSessionAccessRuleDesc]
    grading = None  # type: List[FlowSessionGradingRuleDesc]
    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):
Loading
Loading full blame...