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
committed
from django.utils.translation import ugettext as _
from django.utils.timezone import now
from django.core.exceptions import ObjectDoesNotExist, ImproperlyConfigured
from django.urls import NoReverseMatch
from markdown.extensions import Extension
from markdown.treeprocessors import Treeprocessor
from jinja2 import (
BaseLoader as BaseTemplateLoader, TemplateNotFound, FileSystemLoader)
from relate.utils import dict_to_struct, Struct, SubdirRepoWrapper
Andreas Klöckner
committed
from yaml import load as load_yaml
if sys.version_info >= (3,):
CACHE_KEY_ROOT = "py3"
else:
CACHE_KEY_ROOT = "py2"
Andreas Klöckner
committed
# {{{ mypy
Andreas Klöckner
committed
from typing import ( # noqa
cast, Union, Any, List, Tuple, Optional, Callable, Text)
Andreas Klöckner
committed
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
Andreas Klöckner
committed
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]
Andreas Klöckner
committed
html_content = None # type: Text
Andreas Klöckner
committed
class StaticPageDesc(Struct):
chunks = None # type: List[ChunkDesc]
content = None # type: Text
class CourseDesc(StaticPageDesc):
pass
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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
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
committed
Andreas Klöckner
committed
grade_identifier = None # type: Optional[Text]
grade_aggregation_strategy = None # type: Optional[Text]
class FlowRulesDesc(Struct):
start = None # type: List[FlowSessionStartRuleDesc]
access = None # type: List[FlowSessionAccessRuleDesc]
grading = None # type: List[FlowSessionGradingRuleDesc]
Andreas Klöckner
committed
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
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):
Andreas Klöckner
committed
# 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):
Andreas Klöckner
committed
# type: (Course) -> Text
from os.path import join
return join(settings.GIT_ROOT, course.identifier)
def get_course_repo(course):
Andreas Klöckner
committed
# type: (Course) -> Repo_ish
Andreas Klöckner
committed
from dulwich.repo import Repo
repo = Repo(get_course_repo_path(course))
Loading
Loading full blame...