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 typing import TYPE_CHECKING, Any, FrozenSet, Iterable, List, Optional, Tuple
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
Andreas Klöckner
committed
from django.db.models import query # noqa
from django.shortcuts import get_object_or_404, redirect, render # noqa
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,
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
from course.models import (
Course, FlowPageData, FlowPageVisit, FlowPageVisitGrade, FlowSession,
GradeChange, Participation, get_feedback_for_grade, update_bulk_feedback,
)
from course.page import InvalidPageData
from course.utils import (
FlowContext, FlowPageContext, FlowSessionGradingRule, LanguageOverride,
PageOrdinalOutOfRange, course_view, get_session_access_rule,
get_session_grading_rule, get_session_start_rule, instantiate_flow_page_with_ctx,
render_course_page,
)
from course.views import get_now_or_fake_time
from relate.utils import (
StyledForm, as_local_time, format_datetime_local, local_now,
retry_transaction_decorator, string_concat,
)
Andreas Klöckner
committed
# {{{ mypy
if TYPE_CHECKING:
from course.content import FlowDesc # noqa
from course.models import Course # noqa
from course.page.base import AnswerFeedback, PageBase, PageBehavior # noqa
from course.utils import CoursePageContext, FlowSessionStartRule # noqa
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
113
114
115
116
117
118
119
120
121
122
123
124
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
157
158
159
160
161
162
163
164
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(
"course '%s', flow '%s', page '%s/%s'"
% (course_identifier, flow_session.flow_id,
grp.id, page_desc.id),
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]
if max_page_count is None:
max_page_count = len(available_page_ids)
Andreas Klöckner
committed
group_pages: List[FlowPageData] = []
if shuffle:
Andreas Klöckner
committed
# {{{ maintain order of existing pages as much as possible
for fpd in (FlowPageData.objects
.filter(
flow_session=flow_session,
group_id=grp.id,
page_ordinal__isnull=False)
.order_by("page_ordinal")):
if (fpd.page_id in available_page_ids
and len(group_pages) < max_page_count):
add_page(fpd)
else:
remove_page(fpd)
assert len(group_pages) <= max_page_count
Andreas Klöckner
committed
# }}}
# {{{ then add randomly chosen new pages
Loading
Loading full blame...