Newer
Older
Andreas Klöckner
committed
from __future__ import division, unicode_literals
__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.
"""
Andreas Klöckner
committed
from django.db import models
from django.urls import reverse
from django.core.exceptions import ValidationError, ObjectDoesNotExist
from django.utils.translation import (
from django.db.models.signals import post_save
from django.dispatch import receiver
ifaint
committed
from relate.utils import string_concat
from course.constants import ( # noqa
user_status, USER_STATUS_CHOICES,
participation_status, PARTICIPATION_STATUS_CHOICES,
flow_permission, FLOW_PERMISSION_CHOICES,
flow_session_expiration_mode, FLOW_SESSION_EXPIRATION_MODE_CHOICES,
grade_aggregation_strategy, GRADE_AGGREGATION_STRATEGY_CHOICES,
grade_state_change_types, GRADE_STATE_CHANGE_CHOICES,
flow_rule_kind, FLOW_RULE_KIND_CHOICES,
exam_ticket_states, EXAM_TICKET_STATE_CHOICES,
participation_permission, PARTICIPATION_PERMISSION_CHOICES,
COURSE_ID_REGEX, GRADING_OPP_ID_REGEX, NAME_VALID_REGEX, EVENT_KIND_REGEX
Andreas Klöckner
committed
# {{{ mypy
from typing import List, Dict, Any, Optional, Text, Iterable, Tuple, FrozenSet, TYPE_CHECKING # noqa
if TYPE_CHECKING:
Andreas Klöckner
committed
from course.content import FlowDesc # noqa
from course.page.base import AnswerFeedback # noqa: F401
Andreas Klöckner
committed
# }}}
from yamlfield.fields import YAMLField
def validate_course_specific_language(value):
# type: (Text) -> None
if not value.strip():
# the default value is ""
return
if value not in (
[lang_code for lang_code, lang_descr in settings.LANGUAGES]
+ [settings.LANGUAGE_CODE]):
raise ValidationError(
_("'%s' is currently not supported as a course specific "
"language at this site.") % value)
identifier = models.CharField(max_length=200, unique=True,
help_text=_("A course identifier. Alphanumeric with dashes, "
"no spaces. This is visible in URLs and determines the location "
"on your file system where the course's git repository lives. "
"This should <em>not</em> be changed after the course has been created "
"without also moving the course's git on the server."),
db_index=True,
validators=[
RegexValidator(
"^"+COURSE_ID_REGEX+"$",
message=_(
"Identifier may only contain letters, "
"numbers, and hypens ('-').")),
]
)
name = models.CharField(
null=True, blank=False,
max_length=200,
verbose_name=_('Course name'),
help_text=_("A human-readable name for the course. "
"(e.g. 'Numerical Methods')"))
number = models.CharField(
null=True, blank=False,
max_length=200,
verbose_name=_('Course number'),
help_text=_("A human-readable course number/ID "
"for the course (e.g. 'CS123')"))
time_period = models.CharField(
null=True, blank=False,
max_length=200,
verbose_name=_('Time Period'),
help_text=_("A human-readable description of the "
"time period for the course (e.g. 'Fall 2014')"))
start_date = models.DateField(
verbose_name=_('Start date'),
null=True, blank=True)
end_date = models.DateField(
verbose_name=_('End date'),
null=True, blank=True)
hidden = models.BooleanField(
default=True,
help_text=_("Is the course only accessible to course staff?"),
verbose_name=_('Only visible to course staff'))
help_text=_("Should the course be listed on the main page?"),
accepts_enrollment = models.BooleanField(
Dong Zhuang
committed
default=True,
verbose_name=_('Accepts enrollment'))
git_source = models.CharField(max_length=200, blank=False,
help_text=_("A Git URL from which to pull course updates. "
"to get some sample content."),
ssh_private_key = models.TextField(blank=True,
help_text=_("An SSH private key to use for Git authentication. "
"Not needed for the sample URL above."
"You may use <a href='/generate-ssh-key'>this tool</a> to generate "
"a key pair."),
course_root_path = models.CharField(max_length=200, blank=True,
help_text=_(
'Subdirectory <em>within</em> the git repository to use as '
'course root directory. Not required, and usually blank. '
'Use only if your course content lives in a subdirectory '
'of your git repository. '
'Should not include trailing slash.'),
verbose_name=_('Course root in repository'))
course_file = models.CharField(max_length=200,
default="course.yml",
help_text=_("Name of a YAML file in the git repository that "
"contains the root course descriptor."),
events_file = models.CharField(max_length=200,
default="events.yml",
help_text=_("Name of a YAML file in the git repository that "
"contains calendar information."),
enrollment_approval_required = models.BooleanField(
help_text=_("If set, each enrolling student must be "
"individually approved."),
verbose_name=_('Enrollment approval required'))
preapproval_require_verified_inst_id = models.BooleanField(
default=True,
help_text=_("If set, students cannot get participation "
"preapproval using institutional ID if "
verbose_name=_('Prevent preapproval by institutional ID if not '
enrollment_required_email_suffix = models.CharField(
max_length=200, blank=True, null=True,
help_text=_("Enrollee's email addresses must end in the "
"specified suffix, such as '@illinois.edu'."),
verbose_name=_('Enrollment required email suffix'))
from_email = models.EmailField(
# Translators: replace "RELATE" with the brand name of your
# website if necessary.
help_text=_("This email address will be used in the 'From' line "
ifaint
committed
"of automated emails sent by RELATE."),
notify_email = models.EmailField(
"notifications about the course."),
force_lang = models.CharField(max_length=200, blank=True, null=True,
default="",
validators=[validate_course_specific_language],
help_text=_(
"Which language is forced to be used for this course."),
verbose_name=_('Course language forcibly used'))
# {{{ XMPP
course_xmpp_id = models.CharField(max_length=200, blank=True, null=True,
help_text=_("(Required only if the instant message feature is "
"desired.) The Jabber/XMPP ID (JID) the course will use to sign "
"in to an XMPP server."),
course_xmpp_password = models.CharField(max_length=200, blank=True, null=True,
help_text=_("(Required only if the instant message feature is "
"desired.) The password to go with the JID above."),
recipient_xmpp_id = models.CharField(max_length=200, blank=True, null=True,
help_text=_("(Required only if the instant message feature is "
"desired.) The JID to which instant messages will be sent."),
active_git_commit_sha = models.CharField(max_length=200, null=False,
participants = models.ManyToManyField(settings.AUTH_USER_MODEL,
class Meta:
verbose_name = _("Course")
def __unicode__(self):
return self.identifier
def clean(self):
if self.force_lang:
self.force_lang = self.force_lang.strip()
return reverse("relate-course_page", args=(self.identifier,))
Dong Zhuang
committed
if settings.RELATE_EMAIL_SMTP_ALLOW_NONAUTHORIZED_SENDER:
return getattr(
settings, "NOTIFICATION_EMAIL_FROM",
settings.ROBOT_EMAIL_FROM)
def get_reply_to_email(self):
# this functionality need more fields in Course model,
# about the preference of the course.
Dong Zhuang
committed
if settings.RELATE_EMAIL_SMTP_ALLOW_NONAUTHORIZED_SENDER:
return self.from_email
else:
return self.notify_email
def save(self, *args, **kwargs):
self.full_clean() # performs regular validation then clean()
super(Course, self).save(*args, **kwargs)
class Event(models.Model):
"""An event is an identifier that can be used to specify dates in
verbose_name=_('Course'), on_delete=models.CASCADE)
kind = models.CharField(max_length=50,
# Translators: format of event kind in Event model
help_text=_("Should be lower_case_with_underscores, no spaces "
"allowed."),
verbose_name=_('Kind of event'),
validators=[
RegexValidator(
"^" + EVENT_KIND_REGEX + "$",
message=_("Should be lower_case_with_underscores, no spaces "
"allowed.")),
]
)
ordinal = models.IntegerField(blank=True, null=True,
# Translators: ordinal of event of the same kind
time = models.DateTimeField(verbose_name=_('Start time'))
end_time = models.DateTimeField(null=True, blank=True,
all_day = models.BooleanField(default=False,
# Translators: for when the due time is "All day", how the webpage
# of a event is displayed.
help_text=_("Only affects the rendering in the class calendar, "
shown_in_calendar = models.BooleanField(default=True,
verbose_name = _("Event")
ordering = ("course", "time")
unique_together = (("course", "kind", "ordinal"))
def __unicode__(self):
if self.ordinal is not None:
return "%s %s" % (self.kind, self.ordinal)
else:
return self.kind
def clean(self, *args, **kwargs):
super(Event, self).clean()
if self.end_time:
if self.end_time < self.time:
raise ValidationError(
{"end_time":
_("End time must not be ahead of start time.")})
if self.ordinal is None:
null_ordinal_qset = Event.objects.filter(
course=self.course,
kind=self.kind,
ordinal__isnull=True)
if self.pk:
null_ordinal_qset = null_ordinal_qset.exclude(id=self.pk)
if null_ordinal_qset.exists():
raise ValidationError(
_("May not create multiple ordinal-less events "
"of kind '{evt_kind}' in course '{course}'")
.format(evt_kind=self.kind, course=self.course))
return super(Event, self).clean(*args, **kwargs)
def save(self, *args, **kwargs):
self.full_clean()
return super(Event, self).save(*args, **kwargs)
# {{{ participation
verbose_name=_('Course'), on_delete=models.CASCADE)
name = models.CharField(max_length=100,
help_text=_("Format is lower-case-with-hyphens. "
shown_to_participant = models.BooleanField(default=False,
verbose_name=_('Shown to pariticpant'))
super(ParticipationTag, self).clean()
raise ValidationError(
{field_name:
_("'%s' contains invalid characters.") % field_name})
def __unicode__(self):
return "%s (%s)" % (self.name, self.course)
verbose_name = _("Participation tag")
unique_together = (("course", "name"),)
ordering = ("course", "name")
class ParticipationRole(models.Model):
course = models.ForeignKey(Course,
verbose_name=_('Course'), on_delete=models.CASCADE)
identifier = models.CharField(
max_length=100, blank=False, null=False,
help_text=_("A symbolic name for this role, used in course code. "
"lower_case_with_underscores, no spaces. May be any string. The "
"name 'unenrolled' is special and refers to anyone not enrolled "
"in the course."),
verbose_name=_('Role identifier'))
name = models.CharField(max_length=200, blank=False, null=False,
help_text=_("A human-readable description of this role."),
verbose_name=_('Role name'))
Andreas Klöckner
committed
is_default_for_new_participants = models.BooleanField(default=False,
verbose_name=_('Is default role for new participants'))
is_default_for_unenrolled = models.BooleanField(default=False,
verbose_name=_('Is default role for unenrolled users'))
def clean(self):
super(ParticipationRole, self).clean()
import re
if identifier_valid_re.match(self.identifier) is None:
field_name = "identifier"
raise ValidationError(
{field_name:
_("'%s' contains invalid characters.") % field_name})
def __unicode__(self):
return _("%(identifier)s in %(course)s") % {
"identifier": self.identifier,
"course": self.course}
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
# {{{ permissions handling
_permissions_cache = None # type: FrozenSet[Tuple[Text, Optional[Text]]]
def permission_tuples(self):
# type: () -> FrozenSet[Tuple[Text, Optional[Text]]]
if self._permissions_cache is not None:
return self._permissions_cache
perm = list(
ParticipationRolePermission.objects.filter(role=self)
.values_list("permission", "argument"))
fset_perm = frozenset(
(permission, argument) if argument else (permission, None)
for permission, argument in perm)
self._permissions_cache = fset_perm
return fset_perm
def has_permission(self, perm, argument=None):
# type: (Text, Optional[Text]) -> bool
return (perm, argument) in self.permission_tuples()
# }}}
class Meta:
verbose_name = _("Participation role")
verbose_name_plural = _("Participation roles")
unique_together = (("course", "identifier"),)
ordering = ("course", "identifier")
class ParticipationPermissionBase(models.Model):
permission = models.CharField(max_length=200, blank=False, null=False,
choices=PARTICIPATION_PERMISSION_CHOICES,
verbose_name=_('Permission'),
db_index=True)
argument = models.CharField(max_length=200, blank=True, null=True,
verbose_name=_('Argument'))
class Meta:
abstract = True
def __unicode__(self):
if self.argument:
return "%s %s" % (self.permission, self.argument)
else:
return self.permission
class ParticipationRolePermission(ParticipationPermissionBase):
role = models.ForeignKey(ParticipationRole,
verbose_name=_('Role'), on_delete=models.CASCADE,
related_name="permissions")
def __unicode__(self):
# Translators: permissions for roles
return _("%(permission)s for %(role)s") % {
"permission": super(ParticipationRolePermission, self).__unicode__(),
"role": self.role}
class Meta:
verbose_name = _("Participation role permission")
verbose_name_plural = _("Participation role permissions")
unique_together = (("role", "permission", "argument"),)
user = models.ForeignKey(settings.AUTH_USER_MODEL,
verbose_name=_('User ID'), on_delete=models.CASCADE,
related_name="participations")
course = models.ForeignKey(Course, related_name="participations",
verbose_name=_('Course'), on_delete=models.CASCADE)
ifaint
committed
enroll_time = models.DateTimeField(default=now,
role = models.CharField(max_length=50,
verbose_name=_("Role (unused)"),)
roles = models.ManyToManyField(ParticipationRole, blank=True,
time_factor = models.DecimalField(
max_digits=10, decimal_places=2,
ifaint
committed
default=1,
help_text=_("Multiplier for time available on time-limited "
preview_git_commit_sha = models.CharField(max_length=200, null=True,
ifaint
committed
tags = models.ManyToManyField(ParticipationTag, blank=True,
notes = models.TextField(blank=True, null=True,
verbose_name=_('Notes'))
# Translators: displayed format of Participation: some user in some
# course as some role
ifaint
committed
return _("%(user)s in %(course)s as %(role)s") % {
"user": self.user, "course": self.course,
Andreas Klöckner
committed
"role": "/".join(
role.identifier
Andreas Klöckner
committed
}
verbose_name = _("Participation")
unique_together = (("user", "course"),)
def get_role_desc(self):
return ", ".join(role.name for role in self.roles.all())
Andreas Klöckner
committed
# {{{ permissions handling
_permissions_cache = None # type: FrozenSet[Tuple[Text, Optional[Text]]]
Andreas Klöckner
committed
def permissions(self):
# type: () -> FrozenSet[Tuple[Text, Optional[Text]]]
if self._permissions_cache is not None:
Andreas Klöckner
committed
return self._permissions_cache
perm = (
list(
ParticipationRolePermission.objects.filter(
role__course=self.course,
role__participation=self)
.values_list("permission", "argument"))
Andreas Klöckner
committed
ParticipationPermission.objects.filter(
participation=self)
.values_list("permission", "argument")))
Andreas Klöckner
committed
(permission, argument) if argument else (permission, None)
for permission, argument in perm)
self._permissions_cache = fset_perm
return fset_perm
Andreas Klöckner
committed
def has_permission(self, perm, argument=None):
# type: (Text, Optional[Text]) -> bool
Andreas Klöckner
committed
return (perm, argument) in self.permissions()
# }}}
class ParticipationPermission(ParticipationPermissionBase):
participation = models.ForeignKey(Participation,
verbose_name=_('Participation'), on_delete=models.CASCADE,
related_name="individual_permissions")
class Meta:
verbose_name = _("Participation permission")
verbose_name_plural = _("Participation permissionss")
unique_together = (("participation", "permission", "argument"),)
class ParticipationPreapproval(models.Model):
email = models.EmailField(max_length=254, null=True, blank=True,
institutional_id = models.CharField(max_length=254, null=True, blank=True,
verbose_name=_('Institutional ID'))
verbose_name=_('Course'), on_delete=models.CASCADE)
role = models.CharField(max_length=50,
verbose_name=_("Role (unused)"),)
Andreas Klöckner
committed
roles = models.ManyToManyField(ParticipationRole, blank=True,
verbose_name=_("Roles"), related_name="+")
creator = models.ForeignKey(settings.AUTH_USER_MODEL, null=True,
Andreas Klöckner
committed
verbose_name=_('Creator'), on_delete=models.SET_NULL)
creation_time = models.DateTimeField(default=now, db_index=True,
# Translators: somebody's email in some course in Participation
# Preapproval
return _("Email %(email)s in %(course)s") % {
"email": self.email, "course": self.course}
elif self.institutional_id:
# Translators: somebody's Institutional ID in some course in
# Participation Preapproval
return _("Institutional ID %(inst_id)s in %(course)s") % {
"inst_id": self.institutional_id, "course": self.course}
else:
return _("Preapproval with pk %(pk)s in %(course)s") % {
"pk": self.pk, "course": self.course}
verbose_name = _("Participation preapproval")
verbose_name_plural = _("Participation preapprovals")
unique_together = (("course", "email"),)
ordering = ("course", "email")
def add_default_roles_and_permissions(course,
role_model=ParticipationRole,
role_permission_model=ParticipationRolePermission):
from course.constants import participation_permission as pp
rpm = role_permission_model
Andreas Klöckner
committed
rpm(role=role, permission=pp.view_calendar).save()
rpm(role=role, permission=pp.access_files_for,
rpm(role=role, permission=pp.access_files_for,
argument="public").save()
def add_student_permissions(role):
Andreas Klöckner
committed
rpm(role=role, permission=pp.send_instant_message).save()
rpm(role=role, permission=pp.access_files_for,
argument="student").save()
add_unenrolled_permissions(role)
Andreas Klöckner
committed
def add_teaching_assistant_permissions(role):
rpm(role=role, permission=pp.impersonate_role,
argument="student").save()
rpm(role=role, permission=pp.set_fake_time).save()
rpm(role=role, permission=pp.set_pretend_facility).save()
Andreas Klöckner
committed
rpm(role=role, permission=pp.view_hidden_course_page).save()
rpm(role=role, permission=pp.access_files_for,
rpm(role=role, permission=pp.access_files_for,
rpm(role=role, permission=pp.issue_exam_ticket).save()
Andreas Klöckner
committed
rpm(role=role, permission=pp.view_flow_sessions_from_role,
Andreas Klöckner
committed
rpm(role=role, permission=pp.view_gradebook).save()
rpm(role=role, permission=pp.assign_grade).save()
Dong Zhuang
committed
rpm(role=role, permission=pp.skip_during_manual_grading).save()
Andreas Klöckner
committed
rpm(role=role, permission=pp.view_grader_stats).save()
Andreas Klöckner
committed
rpm(role=role, permission=pp.batch_download_submission).save()
Andreas Klöckner
committed
rpm(role=role, permission=pp.impose_flow_session_deadline).save()
rpm(role=role, permission=pp.end_flow_session).save()
rpm(role=role, permission=pp.regrade_flow_session).save()
rpm(role=role, permission=pp.recalculate_flow_session_grade).save()
Andreas Klöckner
committed
rpm(role=role, permission=pp.reopen_flow_session).save()
Andreas Klöckner
committed
rpm(role=role, permission=pp.grant_exception).save()
rpm(role=role, permission=pp.view_analytics).save()
rpm(role=role, permission=pp.preview_content).save()
rpm(role=role, permission=pp.use_markup_sandbox).save()
rpm(role=role, permission=pp.use_page_sandbox).save()
rpm(role=role, permission=pp.test_flow).save()
rpm(role=role, permission=pp.query_participation).save()
rpm(role=role, permission=pp.edit_participation).save()
Andreas Klöckner
committed
add_student_permissions(role)
def add_instructor_permissions(role):
Andreas Klöckner
committed
rpm(role=role, permission=pp.use_admin_interface).save()
rpm(role=role, permission=pp.impersonate_role,
Andreas Klöckner
committed
argument="ta").save()
rpm(role=role, permission=pp.edit_course_permissions).save()
rpm(role=role, permission=pp.edit_course).save()
rpm(role=role, permission=pp.manage_authentication_tokens).save()
rpm(role=role, permission=pp.access_files_for,
rpm(role=role, permission=pp.edit_exam).save()
rpm(role=role, permission=pp.batch_issue_exam_ticket).save()
Andreas Klöckner
committed
rpm(role=role, permission=pp.view_flow_sessions_from_role,
argument="ta").save()
rpm(role=role, permission=pp.edit_grading_opportunity).save()
rpm(role=role, permission=pp.batch_import_grade).save()
rpm(role=role, permission=pp.batch_export_grade).save()
Andreas Klöckner
committed
rpm(role=role, permission=pp.batch_impose_flow_session_deadline).save()
rpm(role=role, permission=pp.batch_end_flow_session).save()
rpm(role=role, permission=pp.batch_regrade_flow_session).save()
rpm(role=role, permission=pp.batch_recalculate_flow_session_grade).save()
Andreas Klöckner
committed
rpm(role=role, permission=pp.update_content).save()
rpm(role=role, permission=pp.edit_events).save()
rpm(role=role, permission=pp.manage_instant_flow_requests).save()
rpm(role=role, permission=pp.preapprove_participation).save()
add_teaching_assistant_permissions(role)
instructor = role_model(
course=course, identifier="instructor",
name=_("Instructor"))
instructor.save()
teaching_assistant = role_model(
Andreas Klöckner
committed
course=course, identifier="ta",
name=_("Teaching Assistant"))
teaching_assistant.save()
student = role_model(
course=course, identifier="student",
Andreas Klöckner
committed
name=_("Student"),
is_default_for_new_participants=True)
student.save()
unenrolled = role_model(
course=course, identifier="unenrolled",
Andreas Klöckner
committed
name=_("Unenrolled"),
is_default_for_unenrolled=True)
rpm(role=student, permission=pp.included_in_grade_statistics).save()
rpm(role=unenrolled, permission=pp.included_in_grade_statistics).save()
Andreas Klöckner
committed
add_student_permissions(student)
add_teaching_assistant_permissions(teaching_assistant)
add_instructor_permissions(instructor)
@receiver(post_save, sender=Course, dispatch_uid="add_default_permissions")
def _set_up_course_permissions(sender, instance, created, raw, using, update_fields,
add_default_roles_and_permissions(instance)
# {{{ auth token
class AuthenticationToken(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL,
verbose_name=_('User ID'), on_delete=models.CASCADE)
participation = models.ForeignKey(Participation,
verbose_name=_('Participation'), on_delete=models.CASCADE)
restrict_to_participation_role = models.ForeignKey(ParticipationRole,
verbose_name=_('Restrict to role'), on_delete=models.CASCADE,
blank=True, null=True)
description = models.CharField(max_length=200,
verbose_name=_('Description'))
creation_time = models.DateTimeField(
default=now, verbose_name=_('Creation time'))
last_use_time = models.DateTimeField(
verbose_name=_('Last use time'),
blank=True, null=True)
default=None, verbose_name=_('Valid until'),
blank=True, null=True)
revocation_time = models.DateTimeField(
default=None, verbose_name=_('Revocation time'),
blank=True, null=True)
token_hash = models.CharField(max_length=200,
help_text=_("A hash of the authentication token to be "
"used for direct git access."),
null=True, blank=True, unique=True,
verbose_name=_('Hash of git authentication token'))
def __unicode__(self):
return _("Token %(id)d for %(participation)s: %(description)s") % {
"id": self.id,
"participation": self.participation,
"description": self.description}
class Meta:
verbose_name = _("Authentication token")
verbose_name_plural = _("Authentication tokens")
ordering = ("participation", "creation_time")
# {{{ instant flow request
verbose_name=_('Course'), on_delete=models.CASCADE)
class Meta:
verbose_name = _("Instant flow request")
verbose_name_plural = _("Instant flow requests")
def __unicode__(self):
return _("Instant flow request for "
"%(flow_id)s in %(course)s at %(start_time)s") \
% {
"flow_id": self.flow_id,
"course": self.course,
"start_time": self.start_time,
}
class FlowSession(models.Model):
# This looks like it's redundant with 'participation', below--but it's not.
# 'participation' is nullable.
verbose_name=_('Course'), on_delete=models.CASCADE)
participation = models.ForeignKey(Participation, null=True, blank=True,
db_index=True, related_name="flow_sessions",
verbose_name=_('Participation'), on_delete=models.CASCADE)
Andreas Klöckner
committed
# This looks like it's redundant with participation, above--but it's not.
# Again--'participation' is nullable, and it is useful to be able to
# remember what user a session belongs to, even if they're not enrolled.
user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True,
Andreas Klöckner
committed
verbose_name=_('User'), on_delete=models.SET_NULL)
Andreas Klöckner
committed
active_git_commit_sha = models.CharField(max_length=200,
flow_id = models.CharField(max_length=200, db_index=True,
completion_time = models.DateTimeField(null=True, blank=True,
page_count = models.IntegerField(null=True, blank=True,
# This field allows avoiding redundant checks for whether the
# page data is in line with the course material and the current
# version of RELATE.
# See course.flow.adjust_flow_session_page_data.
page_data_at_revision_key = models.CharField(
max_length=200, null=True, blank=True,
verbose_name=_('Page data at course revision'),
help_text=_(
"Page set-up data is up-to date for this revision of the "
"course material"))
access_rules_tag = models.CharField(max_length=200, null=True,
verbose_name=_('Access rules tag'))
expiration_mode = models.CharField(max_length=20, null=True,
# Non-normal: These fields can be recomputed, albeit at great expense.
#
# Looking up the corresponding GradeChange is also invalid because
# some flow sessions are not for credit and still have results.
points = models.DecimalField(max_digits=10, decimal_places=2,
max_points = models.DecimalField(max_digits=10, decimal_places=2,
result_comment = models.TextField(blank=True, null=True,
verbose_name = _("Flow session")
def __unicode__(self):
if self.participation is None:
ifaint
committed
return _("anonymous session %(session_id)d on '%(flow_id)s'") % {
'session_id': self.id,
'flow_id': self.flow_id}
ifaint
committed
return _("%(user)s's session %(session_id)d on '%(flow_id)s'") % {
'user': self.participation.user,
'session_id': self.id,
'flow_id': self.flow_id}
def append_comment(self, s):
if s is None:
return
if self.result_comment:
self.result_comment += "\n" + s
else:
self.result_comment = s
def points_percentage(self):
if self.points is None:
return None
elif self.max_points:
return 100*self.points/self.max_points
else:
return None
def answer_visits(self):
# only use this from templates
from course.flow import assemble_answer_visits
return assemble_answer_visits(self)
# type: () -> Optional[datetime.datetime]
for visit in (FlowPageVisit.objects
.filter(
flow_session=self,
is_synthetic=False)
.order_by("-visit_time")
[:1]):
return visit.visit_time
return None
def get_expiration_mode_desc(self):
return dict(FLOW_SESSION_EXPIRATION_MODE_CHOICES).get(
self.expiration_mode)
# }}}
# {{{ flow page data
class FlowPageData(models.Model):
flow_session = models.ForeignKey(FlowSession, related_name="page_data",
verbose_name=_('Flow session'), on_delete=models.CASCADE)
page_ordinal = models.IntegerField(null=True, blank=True,
verbose_name=_('Page ordinal'))
# This exists to catch changing page types in course content,
# which will generally lead to an inconsistency disaster.
page_type = models.CharField(max_length=200,
verbose_name=_('Page type as indicated in course content'),
null=True, blank=True)