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.
"""
Andreas Klöckner
committed
import datetime # noqa
from django.shortcuts import ( # noqa
render, get_object_or_404)
from django.core.exceptions import ObjectDoesNotExist
from django.utils.translation import (
ugettext as _, string_concat, pgettext_lazy)
from course.content import (
get_course_repo, get_flow_desc,
parse_date_spec, get_course_commit_sha)
from course.constants import (
flow_permission, flow_rule_kind)
import dulwich.repo
from course.content import ( # noqa
FlowDesc,
FlowPageDesc,
FlowSessionAccessRuleDesc
)
from course.page.base import ( # noqa
PageBase,
PageContext,
)
Andreas Klöckner
committed
# {{{ mypy
if False:
from typing import Tuple, List, Text, Iterable, Any, Optional, Union # noqa
Andreas Klöckner
committed
from relate.utils import Repo_ish # noqa
from course.models import ( # noqa
Course,
Participation,
Andreas Klöckner
committed
FlowSession,
FlowPageData,
)
# }}}
def getattr_with_fallback(aggregates, attr_name, default=None):
Andreas Klöckner
committed
# type: (Iterable[Any], Text, Any) -> Any
for agg in aggregates:
result = getattr(agg, attr_name, None)
if result is not None:
return result
return default
# {{{ flow permissions
class FlowSessionRuleBase(object):
Andreas Klöckner
committed
pass
class FlowSessionStartRule(FlowSessionRuleBase):
Andreas Klöckner
committed
def __init__(
self,
tag_session=None, # type: Optional[Text]
Andreas Klöckner
committed
may_start_new_session=None, # type: Optional[bool]
may_list_existing_sessions=None, # type: Optional[bool]
default_expiration_mode=None, # type: Optional[Text]
Andreas Klöckner
committed
):
# type: (...) -> 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):
Andreas Klöckner
committed
def __init__(
self,
permissions, # type: frozenset[Text]
message=None, # type: Optional[Text]
):
# type: (...) -> 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):
Andreas Klöckner
committed
def __init__(
self,
grade_identifier, # type: Optional[Text]
grade_aggregation_strategy, # type: Text
due, # type: Optional[datetime.datetime]
generates_grade, # type: bool
Andreas Klöckner
committed
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
description=None, # type: Optional[Text]
credit_percent=None, # type: Optional[float]
use_last_activity_as_completion_time=None, # type: Optional[bool]
max_points=None, # type: Optional[float]
max_points_enforced_cap=None, # type: Optional[float]
bonus_points=None, # type: Optional[float]
):
# type: (...) -> None
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, # type: Any
course, # type: Course
participation, # type: Optional[Participation]
now_datetime, # type: datetime.datetime
flow_id, # type: Text
login_exam_ticket, # type: Optional[ExamTicket]
):
# type: (...) -> bool
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 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, # type: Any
session, # type: FlowSession
now_datetime, # type: datetime.datetime
):
# type: (...) -> bool
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
Loading
Loading full blame...