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.
"""
from django.shortcuts import ( # noqa
render, get_object_or_404, redirect)
from django.contrib import messages # noqa
from django.core.exceptions import (
PermissionDenied, ObjectDoesNotExist, SuspiciousOperation)
import django.forms as forms
import django.views.decorators.http as http_dec
from django import http
from django.utils.safestring import mark_safe
from django.db import transaction
ifaint
committed
from django.utils.translation import (
ugettext_lazy as _,
ifaint
committed
ugettext,
string_concat,
pgettext,
ifaint
committed
)
from django.utils.functional import lazy
mark_safe_lazy = lazy(mark_safe, six.text_type)
from django.views.decorators.cache import cache_control
from crispy_forms.layout import Submit, Layout, Div
from bootstrap3_datetime.widgets import DateTimePicker
from course.auth import get_role_and_participation
from course.constants import (
participation_role,
participation_status,
FLOW_PERMISSION_CHOICES,
)
Course,
InstantFlowRequest,
Participation,
FlowSession,
FlowRuleException)
from course.content import (get_course_repo, get_course_desc)
from course.utils import course_view, render_course_page
NONE_SESSION_TAG = "<<<NONE>>>" # noqa
courses_and_descs_and_invalid_flags = []
for course in Course.objects.filter(listed=True):
desc = get_course_desc(repo, course, course.active_git_commit_sha.encode())
role, participation = get_role_and_participation(request, course)
show = True
if course.hidden:
if role not in [participation_role.teaching_assistant,
participation_role.instructor]:
show = False
if role != participation_role.instructor:
show = False
if show:
courses_and_descs_and_invalid_flags.append(
courses_and_descs_and_invalid_flags.sort(key=course_sort_key)
return render(request, "course/home.html", {
"courses_and_descs_and_invalid_flags": courses_and_descs_and_invalid_flags
def maintenance(request):
return render(request, "maintenance.html")
def check_course_state(course, role):
if course.hidden:
if role not in [participation_role.teaching_assistant,
participation_role.instructor]:
raise PermissionDenied(_("only course staff have access"))
if role != participation_role.instructor:
raise PermissionDenied(_("only the instructor has access"))
@course_view
def course_page(pctx):
from course.content import get_processed_course_chunks
chunks = get_processed_course_chunks(
pctx.course, pctx.repo, pctx.course_commit_sha, pctx.course_desc,
pctx.role, get_now_or_fake_time(pctx.request),
remote_address=pctx.remote_address)
show_enroll_button = (
pctx.course.accepts_enrollment
and pctx.role == participation_role.unenrolled)
if pctx.request.user.is_authenticated() and Participation.objects.filter(
user=pctx.request.user,
course=pctx.course,
status=participation_status.requested).count():
show_enroll_button = False
messages.add_message(pctx.request, messages.INFO,
_("Your enrollment request is pending. You will be "
"notified once it has been acted upon."))
return render_course_page(pctx, "course/course-page.html", {
"show_enroll_button": show_enroll_button,
def media_etag_func(request, course_identifier, commit_sha, media_path):
return ":".join([course_identifier, commit_sha, media_path])
@cache_control(max_age=3600*24*31) # cache for a month
@http_dec.condition(etag_func=media_etag_func)
def get_media(request, course_identifier, commit_sha, media_path):
course = get_object_or_404(Course, identifier=course_identifier)
role, participation = get_role_and_participation(request, course)
repo = get_course_repo(course)
from course.content import get_repo_blob_data_cached
try:
data = get_repo_blob_data_cached(
repo, "media/"+media_path, commit_sha.encode())
except ObjectDoesNotExist:
raise http.Http404()
content_type, _ = guess_type(media_path)
return http.HttpResponse(data, content_type=content_type)
def repo_file_etag_func(request, course_identifier, commit_sha, path):
return ":".join([course_identifier, commit_sha, path])
@cache_control(max_age=3600*24*31) # cache for a month
@http_dec.condition(etag_func=repo_file_etag_func)
def get_repo_file(request, course_identifier, commit_sha, path):
course = get_object_or_404(Course, identifier=course_identifier)
role, participation = get_role_and_participation(request, course)
repo = get_course_repo(course)
from course.content import is_repo_file_public
Loading
Loading full blame...