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.
"""
from django.utils.translation import (
from django.contrib.auth.decorators import login_required
from django.utils.functional import lazy
from django.shortcuts import ( # noqa
render, get_object_or_404, redirect)
from django.contrib import messages
from django.core.exceptions import (
PermissionDenied, SuspiciousOperation,
ObjectDoesNotExist)
from django.db import transaction
Andreas Klöckner
committed
from django.db.models import query # noqa
from django.utils.safestring import mark_safe
mark_safe_lazy = lazy(mark_safe, str)
from django.urls import reverse
from crispy_forms.helper import FormHelper
from relate.utils import (
StyledForm, local_now, as_local_time,
format_datetime_local, string_concat)
from django_select2.forms import Select2Widget
from course.constants import (
flow_permission,
Andreas Klöckner
committed
participation_permission as pperm,
FLOW_SESSION_EXPIRATION_MODE_CHOICES,
is_expiration_mode_allowed,
grade_aggregation_strategy,
GRADE_AGGREGATION_STRATEGY_CHOICES,
flow_session_interaction_kind
FlowSession, FlowPageData, FlowPageVisit,
FlowPageVisitGrade,
get_feedback_for_grade,
GradeChange, update_bulk_feedback)
from course.utils import (
Andreas Klöckner
committed
FlowContext,
FlowPageContext,
PageOrdinalOutOfRange,
instantiate_flow_page_with_ctx,
course_view, render_course_page,
get_session_start_rule,
get_session_access_rule,
get_session_grading_rule,
FlowSessionGradingRule,
LanguageOverride,
from course.exam import get_login_exam_ticket
from course.page import InvalidPageData
from course.views import get_now_or_fake_time
from relate.utils import retry_transaction_decorator
Andreas Klöckner
committed
# {{{ mypy
from typing import Any, Optional, Iterable, Sequence, Tuple, Text, List, FrozenSet, TYPE_CHECKING # noqa
if TYPE_CHECKING:
from course.utils import ( # noqa
CoursePageContext,
FlowSessionStartRule,
)
from course.content import ( # noqa
FlowDesc,
)
from course.page.base import ( # noqa
PageBase,
PageBehavior,
AnswerFeedback
)
from relate.utils import Repo_ish # noqa
Andreas Klöckner
committed
# }}}
# {{{ page data wrangling
@retry_transaction_decorator(serializable=True)
def _adjust_flow_session_page_data_inner(repo, flow_session,
course_identifier, flow_desc, commit_sha):
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)
def remove_page(fpd):
if fpd.page_ordinal is not None:
fpd.page_ordinal = None
fpd.save()
desc_group_ids = []
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)
group_pages = []
# {{{ helper functions
def find_page_desc(page_id):
new_page_desc = None
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):
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):
page = instantiate_page(new_page_desc)
Dong Zhuang
committed
data = page.initialize_page_data(pctx)
return FlowPageData(
flow_session=flow_session,
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):
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)
Loading
Loading full blame...