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.
"""
Andreas Klöckner
committed
import datetime # noqa
from contextlib import ContextDecorator
from typing import ( # noqa
TYPE_CHECKING, Any, Dict, FrozenSet, Iterable, List, Optional, Text, Tuple,
Union, cast,
)
from django.core.exceptions import ObjectDoesNotExist
from django.shortcuts import get_object_or_404, render # noqa
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 # noqa
from relate.utils import string_concat
Andreas Klöckner
committed
# {{{ mypy
if TYPE_CHECKING:
from codemirror import CodeMirrorJavascript, CodeMirrorTextarea # noqa
from course.content import Repo_ish # noqa
from course.models import ( # noqa
Course, ExamTicket, FlowPageData, FlowSession, Participation,
)
from relate.utils import Repo_ish # noqa
Andreas Klöckner
committed
# }}}
import re
CODE_CELL_DIV_ATTRS_RE = re.compile('(<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,
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
Andreas Klöckner
committed
def _eval_generic_session_conditions(
rule: Any,
session: FlowSession,
now_datetime: datetime.datetime,
) -> bool:
Andreas Klöckner
committed
if hasattr(rule, "if_has_tag"):
if session.access_rules_tag != rule.if_has_tag:
return False
if hasattr(rule, "if_started_before"):
ds = parse_date_spec(session.course, rule.if_started_before)
if not session.start_time < ds:
return False
return True
def _eval_participation_tags_conditions(
participation: Participation | None,
participation_tags_any_set = (
set(getattr(rule, "if_has_participation_tags_any", [])))
Loading
Loading full blame...