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)
Andreas Klöckner
committed
from relate.utils import dict_to_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
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]
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
class StartRuleDesc(Struct):
pass
Andreas Klöckner
committed
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
136
137
138
139
140
141
142
143
144
145
146
147
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):
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))
if course.course_root_path:
return SubdirRepoWrapper(repo, course.course_root_path)
else:
return repo
def get_repo_blob(repo, full_name, commit_sha, allow_tree=True):
Andreas Klöckner
committed
# 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
:arg allow_tree: Allow the resulting object to be a directory
Andreas Klöckner
committed
dul_repo, full_name = get_true_repo_and_path(repo, full_name)
names = full_name.split("/")
Andreas Klöckner
committed
full_name_bytes = full_name.encode('utf-8')
Andreas Klöckner
committed
tree_sha = dul_repo[commit_sha].tree
tree = dul_repo[tree_sha]
Loading
Loading full blame...