Skip to content
flow.py 101 KiB
Newer Older
from __future__ import annotations
Andreas Klöckner's avatar
Andreas Klöckner committed


__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
Andreas Klöckner's avatar
Andreas Klöckner committed

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
Andreas Klöckner's avatar
Andreas Klöckner committed
from django.contrib.auth.decorators import login_required
from django.core.exceptions import (
    ObjectDoesNotExist,
    PermissionDenied,
    SuspiciousOperation,
Andreas Klöckner's avatar
Andreas Klöckner committed
)
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
Andreas Klöckner's avatar
Andreas Klöckner committed
from django.utils.safestring import mark_safe
from django.utils.translation import gettext, gettext_lazy as _
from django_select2.forms import Select2Widget
from course.constants import (
    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,
Andreas Klöckner's avatar
Andreas Klöckner committed
    participation_permission as pperm,
)
from course.content import FlowPageDesc
from course.exam import get_login_exam_ticket
Andreas Klöckner's avatar
Andreas Klöckner committed
from course.models import (
    Course,
    FlowPageData,
    FlowPageVisit,
    FlowPageVisitGrade,
    FlowSession,
    GradeChange,
    Participation,
    get_feedback_for_grade,
    update_bulk_feedback,
Andreas Klöckner's avatar
Andreas Klöckner committed
)
from course.page import InvalidPageData
Andreas Klöckner's avatar
Andreas Klöckner committed
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,
Andreas Klöckner's avatar
Andreas Klöckner committed
    render_course_page,
)
from course.views import get_now_or_fake_time
Andreas Klöckner's avatar
Andreas Klöckner committed
from relate.utils import (
    StyledForm,
    as_local_time,
    format_datetime_local,
    local_now,
    retry_transaction_decorator,
    string_concat,
Andreas Klöckner's avatar
Andreas Klöckner committed
)

    import datetime
Andreas Klöckner's avatar
Andreas Klöckner committed

    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
# {{{ page data wrangling

@retry_transaction_decorator(serializable=True)
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)

    desc_group_ids = []

    # {{{ helper functions

    def remove_page(fpd: FlowPageData) -> None:
        if fpd.page_ordinal is not None:
            fpd.page_ordinal = None
    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}'",
                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)
Loading
Loading full blame...