Skip to content
views.py 50 KiB
Newer Older
from __future__ import annotations
Andreas Klöckner's avatar
Andreas Klöckner committed

__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 typing import (
    TYPE_CHECKING,
    Any,
    cast,
Andreas Klöckner's avatar
Andreas Klöckner committed
)
import django.forms as forms
import django.views.decorators.http as http_dec
Andreas Klöckner's avatar
Andreas Klöckner committed
from crispy_forms.layout import Div, Layout, Submit
from django import http
from django.contrib import messages
Andreas Klöckner's avatar
Andreas Klöckner committed
from django.contrib.auth.decorators import login_required
from django.core.exceptions import (
    ObjectDoesNotExist,
    PermissionDenied,
    SuspiciousOperation,
Andreas Klöckner's avatar
Andreas Klöckner committed
)
from django.db import transaction
from django.shortcuts import get_object_or_404, redirect, render
Andreas Klöckner's avatar
Andreas Klöckner committed
from django.utils.safestring import mark_safe
from django.utils.translation import (
    gettext as _,
    pgettext,
    pgettext_lazy,
Andreas Klöckner's avatar
Andreas Klöckner committed
)
from django.views.decorators.cache import cache_control
Andreas Klöckner's avatar
Andreas Klöckner committed
from django_select2.forms import Select2Widget
from course.auth import get_pre_impersonation_user
from course.constants import (
    FLOW_PERMISSION_CHOICES,
    FLOW_RULE_KIND_CHOICES,
    flow_rule_kind,
    participation_permission as pperm,
    participation_status,
Andreas Klöckner's avatar
Andreas Klöckner committed
)
from course.content import get_course_repo
Andreas Klöckner's avatar
Andreas Klöckner committed
from course.enrollment import (
    get_participation_for_request,
    get_participation_permissions,
Andreas Klöckner's avatar
Andreas Klöckner committed
)
from course.models import (
    Course,
    FlowRuleException,
    FlowSession,
    InstantFlowRequest,
    Participation,
Andreas Klöckner's avatar
Andreas Klöckner committed
)
from course.utils import (
    CoursePageContext,
    course_view,
    get_course_specific_language_choices,
Andreas Klöckner's avatar
Andreas Klöckner committed
    render_course_page,
)
from relate.utils import (
    HTML5DateInput,
    HTML5DateTimeInput,
    RelateHttpRequest,
    StyledForm,
    StyledModelForm,
    string_concat,
Andreas Klöckner's avatar
Andreas Klöckner committed
)

    from accounts.models import User
    from course.content import FlowDesc
NONE_SESSION_TAG = string_concat("<<<", _("NONE"), ">>>")
# {{{ home
def home(request: http.HttpRequest) -> http.HttpResponse:
    now_datetime = get_now_or_fake_time(request)
    current_courses = []
    past_courses = []
    for course in Course.objects.filter(listed=True):
        participation = get_participation_for_request(request, course)

        show = True
        if course.hidden:
            perms = get_participation_permissions(course, participation)
            if (pperm.view_hidden_course_page, None) not in perms:
                show = False

        if show:
            if (course.end_date is None
                    or now_datetime.date() <= course.end_date):
                current_courses.append(course)
            else:
                past_courses.append(course)
    def course_sort_key_minor(course):
        return course.number if course.number is not None else ""
    def course_sort_key_major(course):
        return (course.start_date
                if course.start_date is not None else now_datetime.date())

    current_courses.sort(key=course_sort_key_minor)
    past_courses.sort(key=course_sort_key_minor)
    current_courses.sort(key=course_sort_key_major, reverse=True)
    past_courses.sort(key=course_sort_key_major, reverse=True)

    return render(request, "course/home.html", {
        "current_courses": current_courses,
        "past_courses": past_courses,
def check_course_state(
        course: Course, participation: Participation | None) -> None:
    """
    Check to see if the course is hidden.

    If hidden, only allow access to 'ta' and 'instructor' roles
    """
    if course.hidden:
        if participation is None:
            raise PermissionDenied(_("course page is currently hidden"))
        if not participation.has_permission(pperm.view_hidden_course_page):
            raise PermissionDenied(_("course page is currently hidden"))
def course_page(pctx: CoursePageContext) -> http.HttpResponse:
Andreas Klöckner's avatar
Andreas Klöckner committed
    from course.content import get_course_desc, get_processed_page_chunks
    page_desc = get_course_desc(pctx.repo, pctx.course, pctx.course_commit_sha)

    chunks = get_processed_page_chunks(
            pctx.course, pctx.repo, pctx.course_commit_sha, page_desc,
            pctx.role_identifiers(), get_now_or_fake_time(pctx.request),
            facilities=pctx.request.relate_facilities)
    show_enroll_button = (
            pctx.course.accepts_enrollment
    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."))
        from course.models import ParticipationPreapproval

        if ParticipationPreapproval.objects.filter(
                course=pctx.course).exclude(institutional_id=None).count():
            if not pctx.request.user.institutional_id:
                from django.urls import reverse
                messages.add_message(pctx.request, messages.WARNING,
                        _("This course uses institutional ID for "
                        "enrollment preapproval, please <a href='%s' "
                        "role='button' class='btn btn-md btn-primary'>"
                        "fill in your institutional ID &nbsp;&raquo;"
Andreas Klöckner's avatar
Andreas Klöckner committed
                        "</a> in your profile.")
                        % (
                            reverse("relate-user_profile")
                            + "?referer="
Loading
Loading full blame...