Newer
Older
__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 collections.abc import Collection, Iterable
from ipaddress import IPv4Address, IPv6Address
from django.core.exceptions import ObjectDoesNotExist
from django.shortcuts import get_object_or_404, render
from django.utils.safestring import SafeString, mark_safe
from django.utils.translation import gettext as _, pgettext_lazy
from course.constants import flow_permission, flow_rule_kind
CourseCommitSHADoesNotExist,
FlowDesc,
FlowPageDesc,
FlowSessionAccessRuleDesc,
FlowSessionGradingRuleDesc,
FlowSessionStartRuleDesc,
get_course_commit_sha,
get_course_repo,
get_flow_desc,
parse_date_spec,
from course.page.base import PageBase, PageContext
from relate.utils import (
RelateHttpRequest,
not_none,
remote_address_from_request,
string_concat,
)
Andreas Klöckner
committed
# {{{ mypy
if TYPE_CHECKING:
from course.content import Repo_ish
from course.models import (
Course,
ExamTicket,
FlowPageData,
FlowSession,
Participation,
Andreas Klöckner
committed
# }}}
import re
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
# {{{ flow permissions
class FlowSessionRuleBase:
Andreas Klöckner
committed
pass
class FlowSessionStartRule(FlowSessionRuleBase):
Andreas Klöckner
committed
def __init__(
self,
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:
Andreas Klöckner
committed
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):
Andreas Klöckner
committed
def __init__(
self,
permissions: frozenset[str],
message: str | None = None,
) -> None:
Andreas Klöckner
committed
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):
Andreas Klöckner
committed
def __init__(
self,
grade_identifier: str | None,
due: datetime.datetime | None,
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,
) -> None:
Andreas Klöckner
committed
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(
participation: Participation | None,
now_datetime: datetime.datetime,
flow_id: str,
login_exam_ticket: ExamTicket | None,
*,
remote_ip_address: IPv4Address | IPv6Address | None = None,
Andreas Klöckner
committed
if hasattr(rule, "if_before"):
ds = parse_date_spec(course, rule.if_before)
return False
if hasattr(rule, "if_after"):
ds = parse_date_spec(course, rule.if_after)
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:
return False
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...