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.
"""
import django.forms as forms
from course.validation import validate_struct, ValidationError
from course.constants import MAX_EXTRA_CREDIT_FACTOR
from relate.utils import StyledForm, Struct, string_concat
from django.forms import ValidationError as FormValidationError
from django.utils.safestring import mark_safe
from django.utils.functional import lazy
ifaint
committed
from django.utils.translation import (
gettext_lazy as _,
gettext_noop,
ifaint
committed
)
from django.conf import settings
Andreas Klöckner
committed
# {{{ mypy
from typing import (Any, Callable, TYPE_CHECKING)
if TYPE_CHECKING:
# FIXME There seem to be some cyclic imports that prevent importing these
# outright.
from course.models import ( # noqa
Course,
FlowSession
)
from relate.utils import Repo_ish
Andreas Klöckner
committed
# }}}
Stub Docs of Internals
======================
.. class:: Repo_ish
See ``relate.utils.Repo_ish``.
.. class:: Course
See ``course.models.Course``.
.. class:: FlowSession
See ``course.models.FlowSession``.
Page Interface
==============
.. autoclass:: PageContext
.. autoclass:: PageBehavior
.. autoclass:: AnswerFeedback
.. exception:: InvalidPageData
Base Classes For Pages
======================
.. autoclass:: PageBaseWithTitle
.. autoclass:: PageBaseWithHumanTextFeedback
.. autoclass:: PageBaseWithCorrectAnswer
Automatic Feedback
==================
.. autofunction:: get_auto_feedback
mark_safe_lazy = lazy(mark_safe, str)
class PageContext:
"""
.. attribute:: course
.. attribute:: repo
.. attribute:: commit_sha
.. attribute:: flow_session
May be None.
.. attribute:: page_uri
Note that this is different from :class:`course.utils.FlowPageContext`,
which is used internally by the flow views.
"""
Andreas Klöckner
committed
def __init__(
self,
course: Course,
repo: Repo_ish,
flow_session: FlowSession,
page_uri: str | None = None,
) -> None:
Andreas Klöckner
committed
self.course = course
self.repo = repo
self.commit_sha = commit_sha
self.flow_session = flow_session
self.in_sandbox = in_sandbox
class PageBehavior:
"""
.. attribute:: show_correctness
.. attribute:: show_answer
.. attribute:: may_change_answer
"""
Andreas Klöckner
committed
def __init__(
self,
show_correctness: bool,
show_answer: bool,
may_change_answer: bool,
) -> None:
Andreas Klöckner
committed
self.show_correctness = show_correctness
self.show_answer = show_answer
self.may_change_answer = may_change_answer
def __bool__(self):
# This is for compatiblity: page_behavior used to be a bool argument
# 'answer_is_final'.
return not self.may_change_answer
__nonzero__ = __bool__
page_context: PageContext,
text: str,
use_jinja: bool = True,
reverse_func: Callable = None,
) -> str:
from course.content import markup_to_html as mth
return mth(
page_context.course,
page_context.repo,
page_context.commit_sha,
use_jinja=use_jinja,
reverse_func=reverse_func)
# {{{ answer feedback type
class InvalidFeedbackPointsError(ValueError):
pass
def round_point_count_to_quarters(
value: float, atol: float = 1e-5) -> float | int:
"""
If 'value' is close to an int, a half or quarter, return the close value,
otherwise return the original value.
"""
if abs(value - int(value)) < atol:
return int(value)
import math
v = value * 4
if abs(v - math.floor(v)) < _atol:
v = math.floor(v)
elif abs(v - math.ceil(v)) < _atol:
v = math.ceil(v)
else:
return value
return round(v / 4, 2)
def validate_point_count(
correctness: float | None, atol: float = 1e-5
) -> (float | int | None):
if correctness is None:
return None
if correctness < -atol or correctness > MAX_EXTRA_CREDIT_FACTOR + atol:
raise InvalidFeedbackPointsError(
_("'correctness' is invalid: expecting "
"a value within [0, %(max_extra_credit_factor)s] or None, "
"got %(invalid_value)s.")
% {"max_extra_credit_factor": MAX_EXTRA_CREDIT_FACTOR,
"invalid_value": correctness}
)
return round_point_count_to_quarters(correctness, atol)
def get_auto_feedback(correctness: float | None) -> str:
correctness = validate_point_count(correctness)
gettext_noop("No information on correctness of answer."))
return str(gettext_noop("Your answer is not correct."))
elif correctness == 1:
return str(gettext_noop("Your answer is correct."))
" (%.1f %%)")
% (100*correctness))
gettext_noop("Your answer is mostly correct."),
gettext_noop("Your answer is somewhat correct. "),
class AnswerFeedback:
"""
.. attribute:: correctness
A :class:`float` between 0 and 1 (inclusive),
indicating the degree of correctness of the
answer. May be *None*.
.. attribute:: feedback
Text (at least as a full sentence, or even multi-paragraph HTML)
providing feedback to the student about the provided answer. Should not
reveal the correct answer.
May be None, in which case generic feedback
is generated from :attr:`correctness`.
.. attribute:: bulk_feedback
def __init__(self,
correctness: float | None,
feedback: str | None = None,
bulk_feedback: str | None = None) -> None:
correctness = validate_point_count(correctness)
if feedback is None:
feedback = get_auto_feedback(correctness)
self.correctness = correctness
self.feedback = feedback
self.bulk_feedback = bulk_feedback
def as_json(self) -> tuple[dict[str, Any], dict[str, Any]]:
result = {
"correctness": self.correctness,
"feedback": self.feedback,
}
bulk_result = {
"bulk_feedback": self.bulk_feedback,
}
return result, bulk_result
def from_json(json: Any, bulk_json: Any) -> AnswerFeedback | None:
Andreas Klöckner
committed
if json is None:
return json
if bulk_json is not None:
bulk_feedback = bulk_json.get("bulk_feedback")
else:
bulk_feedback = None
return AnswerFeedback(
correctness=json["correctness"],
feedback=json["feedback"],
def percentage(self) -> float | None:
Andreas Klöckner
committed
if self.correctness is not None:
return 100*self.correctness
else:
return None
# }}}
# {{{ abstract page base class
class InvalidPageData(RuntimeError):
pass
class PageBase:
"""The abstract interface of a flow page.
.. attribute:: location
A string 'location' for reporting errors.
.. attribute:: id
The page identifier.
.. automethod:: required_attrs
.. automethod:: allowed_attrs
.. automethod:: get_modified_permissions_for_page
Dong Zhuang
committed
.. automethod:: initialize_page_data
.. automethod:: title
.. automethod:: body
.. automethod:: expects_answer
.. automethod:: max_points
.. rubric:: Student Input
.. automethod:: answer_data
.. automethod:: make_form
.. automethod:: process_form_post
.. automethod:: form_to_html
.. rubric:: Grader Input
.. automethod:: make_grading_form
.. automethod:: post_grading_form
.. automethod:: update_grade_data_from_grading_form_v2
.. automethod:: grading_form_to_html
.. rubric:: Grading/Feedback
.. automethod:: grade
.. automethod:: correct_answer
.. automethod:: analytic_view_body
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
"""
def __init__(self, vctx, location, page_desc):
"""
:arg vctx: a :class:`course.validation.ValidationContext`, or None
if no validation is desired
"""
self.location = location
if isinstance(page_desc, Struct):
if vctx is not None:
validate_struct(
vctx,
location,
page_desc,
required_attrs=self.required_attrs(),
allowed_attrs=self.allowed_attrs())
# {{{ validate access_rules
if hasattr(page_desc, "access_rules"):
ar_loc = "%s: access rules" % location
validate_struct(
vctx,
ar_loc,
page_desc.access_rules,
required_attrs=(),
allowed_attrs=(
("add_permissions", list),
("remove_permissions", list),
))
from course.validation import validate_flow_permission
for attr in ["add_permissions", "remove_permissions"]:
if hasattr(page_desc.access_rules, attr):
for perm in getattr(page_desc.access_rules, attr):
validate_flow_permission(
vctx,
f"{ar_loc}: {attr}",
perm)
# }}}
self.page_desc = page_desc
self.is_optional_page = getattr(page_desc, "is_optional_page", False)
else:
from warnings import warn
Dong Zhuang
committed
warn(_("Not passing page_desc to PageBase.__init__ is deprecated"),
DeprecationWarning)
id = page_desc
del page_desc
self.id = id
def required_attrs(self):
"""Required attributes, as accepted by
:func:`course.validation.validate_struct`.
Subclasses should only add to, not remove entries from this.
"""
return (
("id", str),
("type", str),
)
def allowed_attrs(self):
"""Allowed attributes, as accepted by
:func:`course.validation.validate_struct`.
Subclasses should only add to, not remove entries from this.
"""
return (
("access_rules", Struct),
("is_optional_page", bool),
def get_modified_permissions_for_page(
self, permissions: frozenset[str]) -> frozenset[str]:
Andreas Klöckner
committed
rw_permissions = set(permissions)
if hasattr(self.page_desc, "access_rules"):
if hasattr(self.page_desc.access_rules, "add_permissions"):
for perm in self.page_desc.access_rules.add_permissions:
Andreas Klöckner
committed
rw_permissions.add(perm)
if hasattr(self.page_desc.access_rules, "remove_permissions"):
for perm in self.page_desc.access_rules.remove_permissions:
Andreas Klöckner
committed
if perm in rw_permissions:
rw_permissions.remove(perm)
def make_page_data(self) -> dict:
Dong Zhuang
committed
return {}
def initialize_page_data(self, page_context: PageContext) -> dict:
"""Return (possibly randomly generated) data that is used to generate
the content on this page. This is passed to methods below as the *page_data*
argument. One possible use for this argument would be a random permutation
of choices that is generated once (at flow setup) and then used whenever
this page is shown.
"""
Dong Zhuang
committed
data = self.make_page_data()
if data:
from warnings import warn
warn(_("%s is using the make_page_data compatiblity hook, which "
"is deprecated.") % type(self).__name__,
DeprecationWarning)
return data
def title(self, page_context: PageContext, page_data: dict) -> str:
Andreas Klöckner
committed
"""Return the (non-HTML) title of this page."""
raise NotImplementedError()
def analytic_view_body(self, page_context: PageContext, page_data: dict) -> str:
"""
Return the (HTML) body of the page, which is shown in page analytic
view."""
return self.body(page_context, page_data)
def body(self, page_context: PageContext, page_data: dict) -> str:
Andreas Klöckner
committed
"""Return the (HTML) body of the page."""
raise NotImplementedError()
def expects_answer(self) -> bool:
Andreas Klöckner
committed
"""
:return: a :class:`bool` indicating whether this page lets the
user provide an answer of some type.
"""
raise NotImplementedError()
def is_answer_gradable(self) -> bool:
"""
:return: a :class:`bool` indicating whether answers on this can
have :meth:`grade` called on them.
True by default.
"""
return True
def max_points(self, page_data: Any) -> float:
"""
:return: a :class:`int` or :class:`float` indicating how many points
are achievable on this page.
"""
raise NotImplementedError()
# {{{ student input
page_context: PageContext,
page_data: Any,
form: forms.Form,
files_data: Any,
) -> Any:
"""Return a JSON-persistable object reflecting the user's answer on the
form. This will be passed to methods below as *answer_data*.
"""
raise NotImplementedError()
Andreas Klöckner
committed
def make_form(
self,
page_context: PageContext,
page_data: Any,
answer_data: Any,
page_behavior: Any,
Andreas Klöckner
committed
):
"""
:arg answer_data: value returned by :meth:`answer_data`.
May be *None*.
:arg page_behavior: an instance of :class:`PageBehavior`
:return:
a :class:`django.forms.Form` instance with *answer_data* prepopulated.
If ``page_behavior.may_change_answer`` is *False*, the form should
be read-only.
"""
raise NotImplementedError()
page_context: PageContext,
page_data: Any,
post_data: Any,
files_data: Any,
) -> forms.Form:
raise NotImplementedError()
page_context: PageContext,
page_data: Any,
post_data: Any,
files_data: Any,
page_behavior: PageBehavior,
"""Return a form with the POST response from *post_data* and *files_data*
filled in.
:arg page_behavior: an instance of :class:`PageBehavior`
:return: a
:class:`django.forms.Form` instance with *answer_data* prepopulated.
If ``page_behavior.may_change_answer`` is *False*, the form should
be read-only.
from warnings import warn
Dong Zhuang
committed
warn(_("%s is using the post_form compatiblity hook, which "
"is deprecated.") % type(self).__name__,
DeprecationWarning)
return self.post_form(page_context, page_data, post_data, files_data)
Andreas Klöckner
committed
def form_to_html(
self,
request: http.HttpRequest,
page_context: PageContext,
form: StyledForm,
answer_data: Any,
Andreas Klöckner
committed
):
"""Returns an HTML rendering of *form*."""
from django.template import loader
return loader.render_to_string(
"course/crispy-form.html",
context={"form": form},
request=request)
# }}}
# {{{ grader input
page_context: PageContext,
page_data: Any,
grade_data: Any,
) -> forms.Form:
"""
:arg grade_data: value returned by
:meth:`update_grade_data_from_grading_form_v2`. May be *None*.
:return:
a :class:`django.forms.Form` instance with *grade_data* prepopulated.
"""
return None
grade_data: Any,
post_data: Any,
"""Return a form with the POST response from *post_data* and *files_data*
filled in.
:return: a
:class:`django.forms.Form` instance with *grade_data* prepopulated.
"""
raise NotImplementedError()
def update_grade_data_from_grading_form_v2(
request: http.HttpRequest,
page_context: PageContext,
page_data: Any,
grade_data: Any,
grading_form: Any,
files_data: Any
"""Return an updated version of *grade_data*, which is a
JSON-persistable object reflecting data on grading of this response.
This will be passed to other methods as *grade_data*.
"""
from warnings import warn
warn(_("%s is using the update_grade_data_from_grading_form "
"compatiblity hook, which "
"is deprecated.") % type(self).__name__,
DeprecationWarning)
return self.update_grade_data_from_grading_form(
page_context, page_data, grade_data, grading_form, files_data)
def update_grade_data_from_grading_form(
self,
page_context: PageContext,
page_data: Any,
grade_data: Any,
grading_form: Any,
files_data: Any
):
request: http.HttpRequest,
page_context: PageContext,
grading_form: Any,
grade_data: Any
) -> str:
"""Returns an HTML rendering of *grading_form*."""
# http://bit.ly/2GxzWr1
from crispy_forms.utils import render_crispy_form
from django.template.context_processors import csrf
ctx: dict = {}
ctx.update(csrf(request))
return render_crispy_form(grading_form, context=ctx)
# }}}
# {{{ grading/feedback
Andreas Klöckner
committed
def grade(
self,
page_context: PageContext,
page_data: Any,
answer_data: Any,
grade_data: Any,
) -> AnswerFeedback | None:
"""Grade the answer contained in *answer_data*.
:arg answer_data: value returned by :meth:`answer_data`,
or *None*, which means that no answer was supplied.
:arg grade_data: value updated by
:meth:`update_grade_data_from_grading_form_v2`
:return: a :class:`AnswerFeedback` instanstance, or *None* if the
grade is not yet available.
"""
raise NotImplementedError()
Andreas Klöckner
committed
def correct_answer(
self,
page_context: PageContext,
page_data: Any,
answer_data: Any,
grade_data: Any,
) -> str | None:
"""The correct answer to this page's interaction, formatted as HTML,
or *None*.
"""
return None
page_context: PageContext,
page_data: Any,
answer_data: Any
) -> str | None:
"""An HTML-formatted answer to be used for summarization and
display in analytics.
"""
return None
def normalized_bytes_answer(
self,
page_context: PageContext,
page_data: Any,
answer_data: Any,
) -> tuple[str, bytes] | None:
"""An answer to be used for batch download, given as a batch of bytes
to be stuffed in a zip file.
:returns: a tuple of ``(file_ext, data)`` where *file_ext* is a suggested
file extension (inlcuding the leading period, if applicable).
May also return *None*.
One use case of this function is to work as input for a plagiarism
checker.
"""
return None
# }}}
# }}}
# {{{ utility base classes
class PageBaseWithTitle(PageBase):
def __init__(self, vctx, location, page_desc):
super().__init__(vctx, location, page_desc)
title = None
try:
title = self.page_desc.title
except AttributeError:
pass
if title is None:
try:
md_body = self.markup_body_for_title()
except NotImplementedError:
from warnings import warn
% type(self).__name__)
else:
from course.content import extract_title_from_markup
title = extract_title_from_markup(md_body)
if title is None:
raise ValidationError(
string_concat(
"%s: ",
_("no title found in body or title attribute"))
from markdown import markdown
from django.utils.html import strip_tags
title = strip_tags(markdown(title))
if not title and vctx is not None:
vctx.add_warning(location, _("the rendered title is an empty string"))
self._title = title
def allowed_attrs(self):
return super().allowed_attrs() + (
("title", str),
)
def markup_body_for_title(self):
raise NotImplementedError()
def title(self, page_context, page_data):
return self._title
class PageBaseWithValue(PageBase):
def __init__(self, vctx, location, page_desc):
super().__init__(vctx, location, page_desc)
if vctx is not None:
if hasattr(page_desc, "value") and self.is_optional_page:
Dong Zhuang
committed
raise ValidationError(
string_concat(
location,
_("Attribute 'value' should be removed when "
"'is_optional_page' is True.")))
if hasattr(page_desc, "value") and page_desc.value < 0:
raise ValidationError(
string_concat(
location,
_("Attribute 'value' expects a non-negative value, "
"got %s instead") % str(page_desc.value)))
return super().allowed_attrs() + (
("value", (int, float)),
)
def expects_answer(self):
return True
def max_points(self, page_data):
if self.is_optional_page:
return 0
return getattr(self.page_desc, "value", 1)
# }}}
# {{{ human text feedback page base
Matt Wala
committed
def create_default_point_scale(total_points):
"""
Return a scale that has sensible intervals for assigning points.
"""
if total_points <= 5:
incr = 0.25
elif total_points <= 10:
incr = 0.5
elif total_points <= 20:
incr = 1
else:
incr = 5
def as_int(x):
return int(x) if int(x) == x else x
Matt Wala
committed
points = [as_int(idx*incr) for idx in range(int(total_points/incr))]
points.append(as_int(total_points))
return points
class TextInputWithButtons(forms.TextInput):
def __init__(self, button_values, *args, **kwargs):
self.button_values = button_values
super().__init__(*args, **kwargs)
Matt Wala
committed
def render(self, name, value, attrs=None, renderer=None):
html = super().render(name, value, attrs,
Matt Wala
committed
from django.utils.html import format_html, mark_safe, escapejs
id = attrs["id"]
def make_feedback_func(feedback):
return "'$(\"#{id}\").val(\"{feedback}\")'".format(
Matt Wala
committed
id=id, feedback=escapejs(feedback))
Matt Wala
committed
buttons = []
# Add buttons.
for button_value in self.button_values:
buttons.append(format_html(
"<button class='btn btn-sm btn-outline-secondary me-1' "
Matt Wala
committed
func=mark_safe(make_feedback_func(button_value)),
val=button_value))
# Add a clear button.
buttons.append(format_html(
"<button class='btn btn-sm btn-outline-danger' "
Matt Wala
committed
func=mark_safe(make_feedback_func(""))))
return format_html("{html}<div class='lh-lg'>{button_row}</div>",
Matt Wala
committed
html=html, button_row=mark_safe("".join(buttons)))
class HumanTextFeedbackForm(StyledForm):
def __init__(self, point_value, *args, **kwargs):
super().__init__(*args, **kwargs)
self.point_value = point_value
self.fields["grade_percent"] = forms.FloatField(
min_value=0,
max_value=100 * MAX_EXTRA_CREDIT_FACTOR,
required=False,
# avoid unfortunate scroll wheel accidents reported by graders
widget=TextInputWithButtons(
[0, 10, 20, 25, 30, 40, 50, 60, 70, 75, 80, 90, 100]),
if point_value is not None and point_value != 0:
self.fields["grade_points"] = forms.FloatField(
min_value=0,
max_value=MAX_EXTRA_CREDIT_FACTOR*point_value,
help_text=_("Grade assigned, as points out of %.1f. "
"Fill out either this or 'grade percent'.")
% point_value,
required=False,
# avoid unfortunate scroll wheel accidents reported by graders
widget=TextInputWithButtons(
create_default_point_scale(point_value)),
self.fields["feedback_text"] = forms.CharField(
widget=forms.Textarea(),
required=False,
help_text=mark_safe_lazy(
_("Feedback to be shown to student, using "
"<a href='http://documen.tician.de/"
"RELATE-flavored Markdown</a>")),
label=_("Feedback text"))
self.fields["notify"] = forms.BooleanField(
initial=False, required=False,
help_text=_("Checking this box and submitting the form "
"with a generic message containing the feedback text"),
label=_("Notify"))
self.fields["may_reply"] = forms.BooleanField(
initial=False, required=False,
help_text=_("Allow recipient to reply to this email?"),
label=_("May reply email to me"))
self.fields["released"] = forms.BooleanField(
initial=True, required=False,
help_text=_("Whether the grade and feedback are to "
"be shown to student. (If you would like to release "
"all grades at once, do not use this. Instead, use "
"the 'shown to students' checkbox for this 'grading "
"opportunity' in the grade book admin.)"),
self.fields["notes"] = forms.CharField(
widget=forms.Textarea(),
help_text=_("Internal notes, not shown to student"),
self.fields["notify_instructor"] = forms.BooleanField(
initial=False, required=False,
help_text=_("Checking this box and submitting the form "
"will notify the instructor "
"with a generic message containing the notes"),
label=_("Notify instructor"))
grade_percent = self.cleaned_data.get("grade_percent")
grade_points = self.cleaned_data.get("grade_points")
and grade_percent is not None
and grade_points is not None):
points_percent = 100*grade_points/self.point_value
direct_percent = grade_percent
if abs(points_percent - direct_percent) > 0.1:
raise FormValidationError(
_("Grade (percent) and Grade (points) "