Skip to content
utils.py 44.1 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.
"""

Andreas Klöckner's avatar
Andreas Klöckner committed
import datetime
from collections.abc import Collection, Iterable
Andreas Klöckner's avatar
Andreas Klöckner committed
from contextlib import ContextDecorator
from dataclasses import dataclass
from ipaddress import IPv4Address, IPv6Address
from typing import (
    TYPE_CHECKING,
    Any,
    cast,
Andreas Klöckner's avatar
Andreas Klöckner committed
)
from django import forms, http
from django.core.exceptions import ObjectDoesNotExist
from django.shortcuts import get_object_or_404, render
from django.utils import translation
from django.utils.safestring import SafeString, mark_safe
Andreas Klöckner's avatar
Andreas Klöckner committed
from django.utils.translation import gettext as _, pgettext_lazy
Andreas Klöckner's avatar
Andreas Klöckner committed
from course.constants import flow_permission, flow_rule_kind
from course.content import (
    CourseCommitSHADoesNotExist,
    FlowDesc,
    FlowPageDesc,
    FlowSessionAccessRuleDesc,
    FlowSessionGradingRuleDesc,
    FlowSessionStartRuleDesc,
    get_course_commit_sha,
    get_course_repo,
    get_flow_desc,
    parse_date_spec,
Andreas Klöckner's avatar
Andreas Klöckner committed
)
from course.page.base import PageBase, PageContext
from relate.utils import (
    RelateHttpRequest,
    not_none,
    remote_address_from_request,
    string_concat,
)
Andreas Klöckner's avatar
Andreas Klöckner committed


    from course.content import Repo_ish
    from course.models import (
        Course,
        ExamTicket,
        FlowPageData,
        FlowSession,
        Participation,
Andreas Klöckner's avatar
Andreas Klöckner committed
    )
    from relate.utils import Repo_ish  # noqa
Andreas Klöckner's avatar
Andreas Klöckner committed


CODE_CELL_DIV_ATTRS_RE = re.compile(r'(<div class="[^>]*code_cell[^>"]*")(>)')
def getattr_with_fallback(
        aggregates: Iterable[Any], attr_name: str, default: Any = None) -> Any:
    for agg in aggregates:
        result = getattr(agg, attr_name, None)
        if result is not None:
            return result

    return default


class FlowSessionStartRule(FlowSessionRuleBase):
            tag_session: str | None = None,
            may_start_new_session: bool | None = None,
            may_list_existing_sessions: bool | None = None,
            default_expiration_mode: str | None = None,
            ) -> None:
        self.tag_session = tag_session
        self.may_start_new_session = may_start_new_session
        self.may_list_existing_sessions = may_list_existing_sessions
        self.default_expiration_mode = default_expiration_mode


class FlowSessionAccessRule(FlowSessionRuleBase):
            permissions: frozenset[str],
            message: str | None = None,
            ) -> None:
        self.permissions = permissions
        self.message = message
    def human_readable_permissions(self):
        from course.models import FLOW_PERMISSION_CHOICES
        permission_dict = dict(FLOW_PERMISSION_CHOICES)
        return [permission_dict[p] for p in self.permissions]
class FlowSessionGradingRule(FlowSessionRuleBase):
            grade_aggregation_strategy: str,
            generates_grade: bool,
            description: str | None = None,
            credit_percent: float | None = None,
            use_last_activity_as_completion_time: bool | None = None,
            max_points: float | None = None,
            max_points_enforced_cap: float | None = None,
            bonus_points: float = 0,

        self.grade_identifier = grade_identifier
        self.grade_aggregation_strategy = grade_aggregation_strategy
        self.due = due
        self.generates_grade = generates_grade
        self.description = description
        self.credit_percent = credit_percent
        self.use_last_activity_as_completion_time = \
                use_last_activity_as_completion_time
        self.max_points = max_points
        self.max_points_enforced_cap = max_points_enforced_cap
        self.bonus_points = bonus_points


def _eval_generic_conditions(
        rule: Any,
        course: Course,
        participation: Participation | None,
        now_datetime: datetime.datetime,
        flow_id: str,
        login_exam_ticket: ExamTicket | None,
        *,
        remote_ip_address: IPv4Address | IPv6Address | None = None,
        ) -> bool:
    if hasattr(rule, "if_before"):
        ds = parse_date_spec(course, rule.if_before)
        if not (now_datetime <= ds):
            return False

    if hasattr(rule, "if_after"):
        ds = parse_date_spec(course, rule.if_after)
        if not (now_datetime >= ds):
            return False

    if hasattr(rule, "if_has_role"):
        from course.enrollment import get_participation_role_identifiers
        roles = get_participation_role_identifiers(course, participation)
        if all(role not in rule.if_has_role for role in roles):
    if (hasattr(rule, "if_signed_in_with_matching_exam_ticket")
            and rule.if_signed_in_with_matching_exam_ticket):
        if login_exam_ticket is None:
            return False
        if login_exam_ticket.exam.flow_id != flow_id:
            return False
        if login_exam_ticket.participation != participation:
    if hasattr(rule, "if_has_prairietest_exam_access"):
        if remote_ip_address is None:
            return False
        if participation is None:
            return False
Loading
Loading full blame...