Skip to content
models.py 68.9 KiB
Newer Older
# -*- coding: utf-8 -*-

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.
"""

Dong Zhuang's avatar
Dong Zhuang committed
from typing import cast
import six

Andreas Klöckner's avatar
Andreas Klöckner committed
from django.db import models
from django.utils.timezone import now
from django.urls import reverse
from django.core.exceptions import ValidationError, ObjectDoesNotExist
from django.utils.translation import (
        ugettext_lazy as _, pgettext_lazy)
Andreas Klöckner's avatar
Andreas Klöckner committed
from django.core.validators import RegexValidator
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.conf import settings

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,
Andreas Klöckner's avatar
Andreas Klöckner committed
        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
from course.page.base import AnswerFeedback


# {{{ mypy

if False:
    from typing import List, Dict, Any, Optional, Text, Iterable, Tuple, FrozenSet  # noqa
    from course.content import FlowDesc  # noqa
    import datetime # noqa
from jsonfield import JSONField
from yamlfield.fields import YAMLField
Andreas Klöckner's avatar
Andreas Klöckner committed

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

Andreas Klöckner's avatar
Andreas Klöckner committed
class Course(models.Model):
    identifier = models.CharField(max_length=200, unique=True,
ifaint's avatar
ifaint committed
            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."),
ifaint's avatar
ifaint committed
            verbose_name=_('Course identifier'),
Andreas Klöckner's avatar
Andreas Klöckner committed
            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,
            help_text=_("A human-readable name for the course. "
                "(e.g. 'Numerical Methods')"))
    number = models.CharField(
            null=True, blank=False,
            max_length=200,
            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,
            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'))
    listed = models.BooleanField(
            default=True,
            help_text=_("Should the course be listed on the main page?"),
ifaint's avatar
ifaint committed
            verbose_name=_('Listed on main page'))
    accepts_enrollment = models.BooleanField(
            verbose_name=_('Accepts enrollment'))
    git_source = models.CharField(max_length=200, blank=True,
ifaint's avatar
ifaint committed
            help_text=_("A Git URL from which to pull course updates. "
            "If you're just starting out, enter "
Andreas Klöckner's avatar
Andreas Klöckner committed
            "<tt>git://github.com/inducer/relate-sample</tt> "
            "to get some sample content."),
ifaint's avatar
ifaint committed
            verbose_name=_('git source'))
    ssh_private_key = models.TextField(blank=True,
ifaint's avatar
ifaint committed
            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."),
ifaint's avatar
ifaint committed
            verbose_name=_('SSH private key'))
    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."),
ifaint's avatar
ifaint committed
            verbose_name=_('Course file'))
Andreas Klöckner's avatar
Andreas Klöckner committed
    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."),
ifaint's avatar
ifaint committed
            verbose_name=_('Events file'))
    enrollment_approval_required = models.BooleanField(
            default=False,
ifaint's avatar
ifaint committed
            help_text=_("If set, each enrolling student must be "
            "individually approved."),
ifaint's avatar
ifaint committed
            verbose_name=_('Enrollment approval required'))
    preapproval_require_verified_inst_id = models.BooleanField(
            default=True,
Andreas Klöckner's avatar
Andreas Klöckner committed
            help_text=_("If set, students cannot get participation "
                        "preapproval using institutional ID if "
Andreas Klöckner's avatar
Andreas Klöckner committed
                        "the institutional ID they provided is not "
Andreas Klöckner's avatar
Andreas Klöckner committed
            verbose_name=_('Prevent preapproval by institutional ID if not '
    enrollment_required_email_suffix = models.CharField(
            max_length=200, blank=True, null=True,
ifaint's avatar
ifaint committed
            help_text=_("Enrollee's email addresses must end in the "
            "specified suffix, such as '@illinois.edu'."),
ifaint's avatar
ifaint committed
            verbose_name=_('Enrollment required email suffix'))
    from_email = models.EmailField(
            # Translators: replace "RELATE" with the brand name of your
            # website if necessary.
ifaint's avatar
ifaint committed
            help_text=_("This email address will be used in the 'From' line "
            "of automated emails sent by RELATE."),
ifaint's avatar
ifaint committed
            verbose_name=_('From email'))

    notify_email = models.EmailField(
ifaint's avatar
ifaint committed
            help_text=_("This email address will receive "
            "notifications about the course."),
ifaint's avatar
ifaint committed
            verbose_name=_('Notify email'))

    # {{{ 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."),
ifaint's avatar
ifaint committed
            verbose_name=_('Course xmpp ID'))
    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."),
ifaint's avatar
ifaint committed
            verbose_name=_('Course xmpp password'))

    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."),
ifaint's avatar
ifaint committed
            verbose_name=_('Recipient xmpp ID'))
    active_git_commit_sha = models.CharField(max_length=200, null=False,
ifaint's avatar
ifaint committed
            blank=False,
ifaint's avatar
ifaint committed
            verbose_name=_('Active git commit SHA'))
Andreas Klöckner's avatar
Andreas Klöckner committed

    participants = models.ManyToManyField(settings.AUTH_USER_MODEL,
Andreas Klöckner's avatar
Andreas Klöckner committed
            through='Participation')

    class Meta:
        verbose_name = _("Course")
ifaint's avatar
ifaint committed
        verbose_name_plural = _("Courses")
    def __unicode__(self):
        return self.identifier

    if six.PY3:
        __str__ = __unicode__

    def get_absolute_url(self):
        return reverse("relate-course_page", args=(self.identifier,))
    def get_from_email(self):
        if settings.RELATE_EMAIL_SMTP_ALLOW_NONAUTHORIZED_SENDER:
            return self.from_email
        else:
            return settings.DEFAULT_FROM_EMAIL

    def get_reply_to_email(self):
        # this functionality need more fields in Course model,
        # about the preference of the course.
        if settings.RELATE_EMAIL_SMTP_ALLOW_NONAUTHORIZED_SENDER:
            return self.from_email
        else:
            return self.notify_email

Andreas Klöckner's avatar
Andreas Klöckner committed
class Event(models.Model):
    """An event is an identifier that can be used to specify dates in
    course content.
ifaint's avatar
ifaint committed
    course = models.ForeignKey(Course,
            verbose_name=_('Course'), on_delete=models.CASCADE)
    kind = models.CharField(max_length=50,
ifaint's avatar
ifaint committed
            # Translators: format of event kind in Event model
            help_text=_("Should be lower_case_with_underscores, no spaces "
            "allowed."),
ifaint's avatar
ifaint committed
            verbose_name=_('Kind of event'))
ifaint's avatar
ifaint committed
    ordinal = models.IntegerField(blank=True, null=True,
            # Translators: ordinal of event of the same kind
ifaint's avatar
ifaint committed
            verbose_name=_('Ordinal of event'))
ifaint's avatar
ifaint committed
    time = models.DateTimeField(verbose_name=_('Start time'))
ifaint's avatar
ifaint committed
    end_time = models.DateTimeField(null=True, blank=True,
ifaint's avatar
ifaint committed
            verbose_name=_('End time'))
    all_day = models.BooleanField(default=False,
            # Translators: for when the due time is "All day", how the webpage
            # of a event is displayed.
ifaint's avatar
ifaint committed
            help_text=_("Only affects the rendering in the class calendar, "
ifaint's avatar
ifaint committed
            "in that a start time is not shown"),
ifaint's avatar
ifaint committed
            verbose_name=_('All day'))
ifaint's avatar
ifaint committed
    shown_in_calendar = models.BooleanField(default=True,
ifaint's avatar
ifaint committed
            verbose_name=_('Shown in calendar'))
        verbose_name = _("Event")
ifaint's avatar
ifaint committed
        verbose_name_plural = _("Events")
        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
    if six.PY3:
        __str__ = __unicode__

class ParticipationTag(models.Model):
ifaint's avatar
ifaint committed
    course = models.ForeignKey(Course,
            verbose_name=_('Course'), on_delete=models.CASCADE)
    name = models.CharField(max_length=100, unique=True,
ifaint's avatar
ifaint committed
            # Translators: name format of ParticipationTag
ifaint's avatar
ifaint committed
            help_text=_("Format is lower-case-with-hyphens. "
ifaint's avatar
ifaint committed
            "Do not use spaces."),
ifaint's avatar
ifaint committed
            verbose_name=_('Name of participation tag'))
    shown_to_participant = models.BooleanField(default=False,
            verbose_name=_('Shown to pariticpant'))

    def clean(self):
        super(ParticipationTag, self).clean()

        import re
Andreas Klöckner's avatar
Andreas Klöckner committed
        name_valid_re = re.compile(r"^\w+$")
Andreas Klöckner's avatar
Andreas Klöckner committed
        if name_valid_re.match(self.name) is None:
ifaint's avatar
ifaint committed
            # Translators: "Name" is the name of a ParticipationTag
            raise ValidationError(
                    {"name": _("Name contains invalid characters.")})

    def __unicode__(self):
        return "%s (%s)" % (self.name, self.course)

    if six.PY3:
        __str__ = __unicode__

    class Meta:
        verbose_name = _("Participation tag")
ifaint's avatar
ifaint committed
        verbose_name_plural = _("Participation tags")
        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. "
Andreas Klöckner's avatar
Andreas Klöckner committed
            "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'))

    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
        name_valid_re = re.compile(r"^\w+$")

        if name_valid_re.match(self.identifier) is None:
            # Translators: "Name" is the name of a ParticipationTag
            raise ValidationError(
                    {"name": _("Name contains invalid characters.")})

    def __unicode__(self):
        return _("%(identifier)s in %(course)s") % {
            "identifier": self.identifier,
            "course": self.course}

    if six.PY3:
        __str__ = __unicode__

    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

    if six.PY3:
        __str__ = __unicode__


class ParticipationRolePermission(ParticipationPermissionBase):
    role = models.ForeignKey(ParticipationRole,
            verbose_name=_('Role'), on_delete=models.CASCADE,
            related_name="permissions")

    class Meta:
        verbose_name = _("Participation role permission")
        verbose_name_plural = _("Participation role permissions")
        unique_together = (("role", "permission", "argument"),)


Andreas Klöckner's avatar
Andreas Klöckner committed
class Participation(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL,
            verbose_name=_('User ID'), on_delete=models.CASCADE,
            related_name="participations")
ifaint's avatar
ifaint committed
    course = models.ForeignKey(Course, related_name="participations",
            verbose_name=_('Course'), on_delete=models.CASCADE)
Andreas Klöckner's avatar
Andreas Klöckner committed

    enroll_time = models.DateTimeField(default=now,
ifaint's avatar
ifaint committed
            verbose_name=_('Enroll time'))
    role = models.CharField(max_length=50,
            verbose_name=_("Role (unused)"),)
    roles = models.ManyToManyField(ParticipationRole, blank=True,
Andreas Klöckner's avatar
Andreas Klöckner committed
            verbose_name=_("Roles"), related_name="participation")
Andreas Klöckner's avatar
Andreas Klöckner committed
    status = models.CharField(max_length=50,
ifaint's avatar
ifaint committed
            choices=PARTICIPATION_STATUS_CHOICES,
ifaint's avatar
ifaint committed
            verbose_name=_('Participation status'))
Andreas Klöckner's avatar
Andreas Klöckner committed

Andreas Klöckner's avatar
Andreas Klöckner committed
    time_factor = models.DecimalField(
            max_digits=10, decimal_places=2,
ifaint's avatar
ifaint committed
            help_text=_("Multiplier for time available on time-limited "
ifaint's avatar
ifaint committed
            verbose_name=_('Time factor'))
Andreas Klöckner's avatar
Andreas Klöckner committed

    preview_git_commit_sha = models.CharField(max_length=200, null=True,
ifaint's avatar
ifaint committed
            blank=True,
ifaint's avatar
ifaint committed
            verbose_name=_('Preview git commit SHA'))
    tags = models.ManyToManyField(ParticipationTag, blank=True,
ifaint's avatar
ifaint committed
            verbose_name=_('Tags'))
    notes = models.TextField(blank=True, null=True,
            verbose_name=_('Notes'))
Andreas Klöckner's avatar
Andreas Klöckner committed
    def __unicode__(self):
        # Translators: displayed format of Participation: some user in some
        # course as some role
        return _("%(user)s in %(course)s as %(role)s") % {
                "user": self.user, "course": self.course,
                    for role in self.roles.all())
    if six.PY3:
        __str__ = __unicode__

        verbose_name = _("Participation")
ifaint's avatar
ifaint committed
        verbose_name_plural = _("Participations")
        unique_together = (("user", "course"),)
        ordering = ("course", "user")
        return ", ".join(role.name for role in self.roles.all())
    _permissions_cache = None  # type: FrozenSet[Tuple[Text, Optional[Text]]]

        # type: () -> FrozenSet[Tuple[Text, Optional[Text]]]

        if self._permissions_cache is not None:
            return self._permissions_cache

        perm = (
                list(
                    ParticipationRolePermission.objects.filter(
                        role__course=self.course,
                        role__participation=self)
                    .values_list("permission", "argument"))
                +
                list(
                    ParticipationPermission.objects.filter(
                        participation=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.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,
ifaint's avatar
ifaint committed
            verbose_name=_('Email'))
    institutional_id = models.CharField(max_length=254, null=True, blank=True,
            verbose_name=_('Institutional ID'))
ifaint's avatar
ifaint committed
    course = models.ForeignKey(Course,
            verbose_name=_('Course'), on_delete=models.CASCADE)
    role = models.CharField(max_length=50,
            verbose_name=_("Role (unused)"),)
    roles = models.ManyToManyField(ParticipationRole, blank=True,
            verbose_name=_("Roles"), related_name="+")
    creator = models.ForeignKey(settings.AUTH_USER_MODEL, null=True,
            verbose_name=_('Creator'), on_delete=models.SET_NULL)
ifaint's avatar
ifaint committed
    creation_time = models.DateTimeField(default=now, db_index=True,
ifaint's avatar
ifaint committed
            verbose_name=_('Creation time'))

    def __unicode__(self):
Andreas Klöckner's avatar
Andreas Klöckner committed
            # 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:
Andreas Klöckner's avatar
Andreas Klöckner committed
            # 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}
    if six.PY3:
        __str__ = __unicode__

        verbose_name = _("Participation preapproval")
ifaint's avatar
ifaint committed
        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's avatar
Andreas Klöckner committed
    def add_unenrolled_permissions(role):
        rpm(role=role, permission=pp.view_calendar).save()
        rpm(role=role, permission=pp.access_files_for,
Andreas Klöckner's avatar
Andreas Klöckner committed
                argument="unenrolled").save()
        rpm(role=role, permission=pp.access_files_for,
Andreas Klöckner's avatar
Andreas Klöckner committed
                argument="public").save()

    def add_student_permissions(role):
        rpm(role=role, permission=pp.send_instant_message).save()
        rpm(role=role, permission=pp.access_files_for,
Andreas Klöckner's avatar
Andreas Klöckner committed
                argument="student").save()

        add_unenrolled_permissions(role)
    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()
        rpm(role=role, permission=pp.view_hidden_course_page).save()
        rpm(role=role, permission=pp.access_files_for,
Andreas Klöckner's avatar
Andreas Klöckner committed
                argument="ta").save()
        rpm(role=role, permission=pp.access_files_for,
Andreas Klöckner's avatar
Andreas Klöckner committed
                argument="in_exam").save()

        rpm(role=role, permission=pp.issue_exam_ticket).save()
Andreas Klöckner's avatar
Andreas Klöckner committed

        rpm(role=role, permission=pp.view_flow_sessions_from_role,
                argument="student").save()
        rpm(role=role, permission=pp.view_gradebook).save()
        rpm(role=role, permission=pp.assign_grade).save()
        rpm(role=role, permission=pp.skip_during_manual_grading).save()
        rpm(role=role, permission=pp.view_grader_stats).save()
        rpm(role=role, permission=pp.batch_download_submission).save()
        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()
        rpm(role=role, permission=pp.reopen_flow_session).save()
        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()
    def add_instructor_permissions(role):
        rpm(role=role, permission=pp.use_admin_interface).save()
        rpm(role=role, permission=pp.impersonate_role,
        rpm(role=role, permission=pp.edit_course_permissions).save()
        rpm(role=role, permission=pp.edit_course).save()
        rpm(role=role, permission=pp.access_files_for,
Andreas Klöckner's avatar
Andreas Klöckner committed
                argument="instructor").save()

        rpm(role=role, permission=pp.edit_exam).save()
        rpm(role=role, permission=pp.batch_issue_exam_ticket).save()
Andreas Klöckner's avatar
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()
        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()
        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(
            name=_("Teaching Assistant"))
    teaching_assistant.save()
    student = role_model(
            course=course, identifier="student",
            name=_("Student"),
            is_default_for_new_participants=True)
    student.save()
    unenrolled = role_model(
            course=course, identifier="unenrolled",
            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()
    add_unenrolled_permissions(unenrolled)
    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)
Andreas Klöckner's avatar
Andreas Klöckner committed

# {{{ auth token

class AuthenticationToken(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL,
            verbose_name=_('User ID'), on_delete=models.CASCADE,
            related_name="participations")

    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(
            default=now, verbose_name=_('Last use time'))
    valid_until = models.DateTimeField(
            default=None, verbose_name=_('Valid until'))
    revocation_time = models.DateTimeField(
            default=None, verbose_name=_('Revocation time'))

    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'))

# }}}


Andreas Klöckner's avatar
Andreas Klöckner committed
class InstantFlowRequest(models.Model):
ifaint's avatar
ifaint committed
    course = models.ForeignKey(Course,
            verbose_name=_('Course'), on_delete=models.CASCADE)
ifaint's avatar
ifaint committed
    flow_id = models.CharField(max_length=200,
ifaint's avatar
ifaint committed
            verbose_name=_('Flow ID'))
ifaint's avatar
ifaint committed
    start_time = models.DateTimeField(default=now,
ifaint's avatar
ifaint committed
            verbose_name=_('Start time'))
ifaint's avatar
ifaint committed
    end_time = models.DateTimeField(
ifaint's avatar
ifaint committed
            verbose_name=_('End time'))
ifaint's avatar
ifaint committed
    cancelled = models.BooleanField(default=False,
ifaint's avatar
ifaint committed
            verbose_name=_('Cancelled'))
    class Meta:
        verbose_name = _("Instant flow request")
ifaint's avatar
ifaint committed
        verbose_name_plural = _("Instant flow requests")
Andreas Klöckner's avatar
Andreas Klöckner committed

    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,
                        }

    if six.PY3:
        __str__ = __unicode__

# {{{ flow session
class FlowSession(models.Model):
    # This looks like it's redundant with 'participation', below--but it's not.
    # 'participation' is nullable.
ifaint's avatar
ifaint committed
    course = models.ForeignKey(Course,
            verbose_name=_('Course'), on_delete=models.CASCADE)
Andreas Klöckner's avatar
Andreas Klöckner committed
    participation = models.ForeignKey(Participation, null=True, blank=True,
            db_index=True, related_name="flow_sessions",
            verbose_name=_('Participation'), on_delete=models.CASCADE)

    # 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,
            verbose_name=_('User'), on_delete=models.SET_NULL)
ifaint's avatar
ifaint committed
    active_git_commit_sha = models.CharField(max_length=200,
ifaint's avatar
ifaint committed
            verbose_name=_('Active git commit SHA'))
ifaint's avatar
ifaint committed
    flow_id = models.CharField(max_length=200, db_index=True,
ifaint's avatar
ifaint committed
            verbose_name=_('Flow ID'))
ifaint's avatar
ifaint committed
    start_time = models.DateTimeField(default=now,
ifaint's avatar
ifaint committed
            verbose_name=_('Start time'))
ifaint's avatar
ifaint committed
    completion_time = models.DateTimeField(null=True, blank=True,
            verbose_name=_('Completion time'))
ifaint's avatar
ifaint committed
    page_count = models.IntegerField(null=True, blank=True,
ifaint's avatar
ifaint committed
            verbose_name=_('Page count'))
    # 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"))

ifaint's avatar
ifaint committed
    in_progress = models.BooleanField(default=None,
ifaint's avatar
ifaint committed
            verbose_name=_('In progress'))
    access_rules_tag = models.CharField(max_length=200, null=True,
ifaint's avatar
ifaint committed
            blank=True,
            verbose_name=_('Access rules tag'))
    expiration_mode = models.CharField(max_length=20, null=True,
Andreas Klöckner's avatar
Andreas Klöckner committed
            default=flow_session_expiration_mode.end,
ifaint's avatar
ifaint committed
            choices=FLOW_SESSION_EXPIRATION_MODE_CHOICES,
ifaint's avatar
ifaint committed
            verbose_name=_('Expiration mode'))
    # 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.
Andreas Klöckner's avatar
Andreas Klöckner committed

    points = models.DecimalField(max_digits=10, decimal_places=2,
ifaint's avatar
ifaint committed
            blank=True, null=True,
ifaint's avatar
ifaint committed
            verbose_name=_('Points'))
    max_points = models.DecimalField(max_digits=10, decimal_places=2,
ifaint's avatar
ifaint committed
            blank=True, null=True,
ifaint's avatar
ifaint committed
            verbose_name=_('Max point'))
ifaint's avatar
ifaint committed
    result_comment = models.TextField(blank=True, null=True,
ifaint's avatar
ifaint committed
            verbose_name=_('Result comment'))
        verbose_name = _("Flow session")
ifaint's avatar
ifaint committed
        verbose_name_plural = _("Flow sessions")
Andreas Klöckner's avatar
Andreas Klöckner committed
        ordering = ("course", "-start_time")
        if self.participation is None:
            return _("anonymous session %(session_id)d on '%(flow_id)s'") % {
                    'session_id': self.id,
                    'flow_id': self.flow_id}
            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}
    if six.PY3:
        __str__ = __unicode__

        # type: (Optional[Text]) -> None
        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)

    def last_activity(self):
        # type: () -> Optional[datetime.datetime]
        for visit in (FlowPageVisit.objects
                .filter(
                    flow_session=self,
                    answer__isnull=False,
                    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)

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

class FlowPageData(models.Model):
ifaint's avatar
ifaint committed
    flow_session = models.ForeignKey(FlowSession, related_name="page_data",
            verbose_name=_('Flow session'), on_delete=models.CASCADE)
ifaint's avatar
ifaint committed
    ordinal = models.IntegerField(null=True, blank=True,
ifaint's avatar
ifaint committed
            verbose_name=_('Ordinal'))
Andreas Klöckner's avatar
Andreas Klöckner committed

    # 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)
ifaint's avatar
ifaint committed
    group_id = models.CharField(max_length=200,
ifaint's avatar
ifaint committed
            verbose_name=_('Group ID'))
ifaint's avatar
ifaint committed
    page_id = models.CharField(max_length=200,
ifaint's avatar
ifaint committed
            verbose_name=_('Page ID'))
ifaint's avatar
ifaint committed
    data = JSONField(null=True, blank=True,
            # Show correct characters in admin for non ascii languages.
            dump_kwargs={'ensure_ascii': False},
ifaint's avatar
ifaint committed
            verbose_name=_('Data'))
    title = models.CharField(max_length=1000,
            verbose_name=_('Page Title'), null=True, blank=True)
    bookmarked = models.BooleanField(default=False,
            help_text=_("A user-facing 'marking' feature to allow participants to "
                "easily return to pages that still need their attention."),
            verbose_name=_('Bookmarked'))

    class Meta:
ifaint's avatar
ifaint committed
        verbose_name = _("Flow page data")
        verbose_name_plural = _("Flow page data")

    def __unicode__(self):
        # flow page data
        return (_("Data for page '%(group_id)s/%(page_id)s' "
                "(ordinal %(ordinal)s) in %(flow_session)s") % {
                    'group_id': self.group_id,
                    'page_id': self.page_id,
                    'ordinal': self.ordinal,
                    'flow_session': self.flow_session})
    if six.PY3:
        __str__ = __unicode__

    # Django's templates are a little daft. No arithmetic--really?
    def previous_ordinal(self):
        return self.ordinal - 1

    def next_ordinal(self):
        return self.ordinal + 1

    def human_readable_ordinal(self):
        return self.ordinal + 1


class FlowPageVisit(models.Model):
    # This is redundant (because the FlowSession is available through
    # page_data), but it helps the admin site understand the link
    # and provide editing.
ifaint's avatar
ifaint committed
    flow_session = models.ForeignKey(FlowSession, db_index=True,
            verbose_name=_('Flow session'), on_delete=models.CASCADE)
ifaint's avatar
ifaint committed
    page_data = models.ForeignKey(FlowPageData, db_index=True,
            verbose_name=_('Page data'), on_delete=models.CASCADE)
ifaint's avatar
ifaint committed
    visit_time = models.DateTimeField(default=now, db_index=True,
ifaint's avatar
ifaint committed
            verbose_name=_('Visit time'))
ifaint's avatar
ifaint committed
    remote_address = models.GenericIPAddressField(null=True, blank=True,
ifaint's avatar
ifaint committed
            verbose_name=_('Remote address'))
    user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True,
            blank=True, related_name="visitor",
            verbose_name=_('User'), on_delete=models.SET_NULL)
    impersonated_by = models.ForeignKey(settings.AUTH_USER_MODEL,
            null=True, blank=True, related_name="impersonator",
            verbose_name=_('Impersonated by'), on_delete=models.SET_NULL)
ifaint's avatar
ifaint committed
    is_synthetic = models.BooleanField(default=False,
ifaint's avatar
ifaint committed
            help_text=_("Synthetic flow page visits are generated for "
            "unvisited pages once a flow is finished. This is needed "
            "since grade information is attached to a visit, and it "
            "needs a place to go."),
            verbose_name=_('Is synthetic'))
ifaint's avatar
ifaint committed
    answer = JSONField(null=True, blank=True,
            # Show correct characters in admin for non ascii languages.
            dump_kwargs={'ensure_ascii': False},
ifaint's avatar
ifaint committed
            # Translators: "Answer" is a Noun.
ifaint's avatar
ifaint committed
            verbose_name=_('Answer'))
    # is_submitted_answer may seem redundant with answers being
    # non-NULL, but it isn't. This supports saved (but as
    # yet ungraded) answers.

    # NULL means it's not an answer at all.
    #   (Should coincide with 'answer is None')
    # True means it's a final, submitted answer fit for grading.
    # False means it's just a saved answer.
ifaint's avatar
ifaint committed
    is_submitted_answer = models.NullBooleanField(
            # Translators: determine whether the answer is a final,
            # submitted answer fit for grading.
ifaint's avatar
ifaint committed
            verbose_name=_('Is submitted answer'))
    def __unicode__(self):
Dong Zhuang's avatar
Dong Zhuang committed
                # Translators: flow page visit
                _("'%(group_id)s/%(page_id)s' in '%(session)s' "
                "on %(time)s")
                % {"group_id": self.page_data.group_id,
                    "page_id": self.page_data.page_id,
                    "session": self.flow_session,
                    "time": self.visit_time})
        if self.answer is not None:
Andreas Klöckner's avatar
Andreas Klöckner committed
            # Translators: flow page visit: if an answer is
Dong Zhuang's avatar
Dong Zhuang committed
            # provided by user then append the string.
            result += six.text_type(_(" (with answer)"))