Newer
Older
from __future__ import annotations
__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 Iterable
from typing import TYPE_CHECKING, Any, cast
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit
from django import forms, http
from django.conf import settings
from django.contrib import messages
ObjectDoesNotExist,
PermissionDenied,
SuspiciousOperation,
from django.db import transaction
from django.db.models import query
from django.shortcuts import get_object_or_404, redirect, render
from django.urls import reverse
from django.utils.safestring import mark_safe
from django.utils.translation import gettext, gettext_lazy as _
from django_select2.forms import Select2Widget
FLOW_SESSION_EXPIRATION_MODE_CHOICES,
GRADE_AGGREGATION_STRATEGY_CHOICES,
SESSION_LOCKED_TO_FLOW_PK,
flow_permission,
flow_session_expiration_mode,
flow_session_interaction_kind,
grade_aggregation_strategy,
is_expiration_mode_allowed,
participation_permission as pperm,
)
from course.content import FlowPageDesc
from course.exam import get_login_exam_ticket
Course,
FlowPageData,
FlowPageVisit,
FlowPageVisitGrade,
FlowSession,
GradeChange,
Participation,
get_feedback_for_grade,
update_bulk_feedback,
from course.page import InvalidPageData
FlowContext,
FlowPageContext,
FlowSessionGradingRule,
LanguageOverride,
PageOrdinalOutOfRange,
course_view,
get_session_access_rule,
get_session_grading_rule,
get_session_start_rule,
instantiate_flow_page_with_ctx,
from course.views import get_now_or_fake_time
StyledForm,
as_local_time,
format_datetime_local,
local_now,
remote_address_from_request,
Andreas Klöckner
committed
# {{{ mypy
if TYPE_CHECKING:
from accounts.models import User
from course.content import FlowDesc
from course.models import Course
from course.page.base import AnswerFeedback, PageBase, PageBehavior
from course.utils import CoursePageContext, FlowSessionStartRule
from relate.utils import Repo_ish
Andreas Klöckner
committed
# }}}
# {{{ page data wrangling
@retry_transaction_decorator(serializable=True)
Andreas Klöckner
committed
def _adjust_flow_session_page_data_inner(
repo: Repo_ish, flow_session: FlowSession,
course_identifier: str, flow_desc, commit_sha: bytes) -> int:
"""
:returns: new page count
"""
from course.page.base import PageContext
pctx = PageContext(
course=flow_session.course,
repo=repo,
commit_sha=commit_sha,
flow_session=flow_session,
in_sandbox=False,
page_uri=None)
Andreas Klöckner
committed
desc_group_ids = []
# {{{ helper functions
def remove_page(fpd: FlowPageData) -> None:
if fpd.page_ordinal is not None:
fpd.page_ordinal = None
fpd.save()
Andreas Klöckner
committed
def find_page_desc(page_id: str) -> FlowPageDesc:
new_page_desc = None
for page_desc in grp.pages: # pragma: no branch
if page_desc.id == page_id:
new_page_desc = page_desc
break
assert new_page_desc is not None
return new_page_desc
def instantiate_page(page_desc: FlowPageDesc) -> PageBase:
from course.content import instantiate_flow_page
return instantiate_flow_page(
f"course '{course_identifier}', flow '{flow_session.flow_id}', "
f"page '{grp.id}/{page_desc.id}'",
Andreas Klöckner
committed
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
repo, page_desc, commit_sha)
def create_fpd(new_page_desc: FlowPageDesc) -> FlowPageData:
page = instantiate_page(new_page_desc)
data = page.initialize_page_data(pctx)
return FlowPageData(
flow_session=flow_session,
page_ordinal=None,
page_type=new_page_desc.type,
group_id=grp.id,
page_id=new_page_desc.id,
data=data,
title=page.title(pctx, data))
def add_page(fpd: FlowPageData) -> None:
if fpd.page_ordinal != ordinal[0]:
fpd.page_ordinal = ordinal[0]
fpd.save()
page_desc = find_page_desc(fpd.page_id)
page = instantiate_page(page_desc)
title = page.title(pctx, fpd.data)
if fpd.title != title:
fpd.title = title
fpd.save()
ordinal[0] += 1
available_page_ids.remove(fpd.page_id)
group_pages.append(fpd)
# }}}
ordinal = [0]
for grp in flow_desc.groups:
desc_group_ids.append(grp.id)
shuffle = getattr(grp, "shuffle", False)
max_page_count = getattr(grp, "max_page_count", None)
available_page_ids = [page_desc.id for page_desc in grp.pages]
Loading
Loading full blame...