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

from __future__ import division

__copyright__ = "Copyright (C) 2014 Andreas Kloeckner"

__license__ = """
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""

Andreas Klöckner's avatar
Andreas Klöckner committed
from django.db import models
from django.contrib.auth.models import User
from django.utils.timezone import now
from django.core.urlresolvers import reverse
from django.core.exceptions import ValidationError, ObjectDoesNotExist
from django.utils.translation import (
        ugettext_lazy as _, pgettext_lazy, string_concat)
Andreas Klöckner's avatar
Andreas Klöckner committed
from django.core.validators import RegexValidator
from course.constants import (  # noqa
        user_status, USER_STATUS_CHOICES,
        participation_role, PARTICIPATION_ROLE_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,
Andreas Klöckner's avatar
Andreas Klöckner committed

        COURSE_ID_REGEX
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

# {{{ facility

class Facility(models.Model):
    """Data about a facility from where content may be accessed."""

    identifier = models.CharField(max_length=50, unique=True,
            help_text=_("Format is lower-case-with-hyphens. "
            "Do not use spaces."),
ifaint's avatar
ifaint committed
            verbose_name=_("Facility ID"))
ifaint's avatar
ifaint committed
    description = models.CharField(max_length=100,
ifaint's avatar
ifaint committed
            verbose_name=_("Facility description"))
ifaint's avatar
ifaint committed
        verbose_name = _("Facility")
ifaint's avatar
ifaint committed
        verbose_name_plural = _("Facilities")

    def __unicode__(self):
        return self.identifier


class FacilityIPRange(models.Model):
    """Network data about a facility from where content may be accessed."""

    facility = models.ForeignKey(Facility, related_name="ip_ranges")

    ip_range = models.CharField(
            max_length=200,
ifaint's avatar
ifaint committed
            verbose_name=_("IP range"))
    description = models.CharField(max_length=100,
ifaint's avatar
ifaint committed
            verbose_name=_('IP range description'))
        verbose_name = _("Facility IP range")

    def clean(self):
        import ipaddr
        try:
            ipaddr.IPNetwork(self.ip_range)
        except Exception as e:
            raise ValidationError({"ip_range": str(e)})

# }}}


# {{{ user status
def get_user_status(user):
    try:
        return user.user_status
    except AttributeError:
        ustatus = UserStatus()
        ustatus.user = user
        ustatus.status = user_status.unconfirmed
        ustatus.save()

        return ustatus


Andreas Klöckner's avatar
Andreas Klöckner committed
class UserStatus(models.Model):
    user = models.OneToOneField(User, db_index=True,
            related_name="user_status",
ifaint's avatar
ifaint committed
            verbose_name=_('User ID'))
Andreas Klöckner's avatar
Andreas Klöckner committed
    status = models.CharField(max_length=50,
ifaint's avatar
ifaint committed
            verbose_name=_('User status'))
    sign_in_key = models.CharField(max_length=50,
            help_text=_("The sign in token sent out in email."),
            null=True, unique=True, db_index=True, blank=True,
ifaint's avatar
ifaint committed
            # Translators: the sign in token of the user.
            verbose_name=_('Sign in key'))
    key_time = models.DateTimeField(default=now,
            help_text=_("The time stamp of the sign in token."),
ifaint's avatar
ifaint committed
            # Translators: the time when the token is sent out.
            verbose_name=_('Key time'))
    editor_mode = models.CharField(max_length=20,
            choices=(
ifaint's avatar
ifaint committed
                ("default", _("Default")),
                ("sublime", "Sublime text"),
                ("emacs", "Emacs"),
                ("vim", "Vim"),
                ),
            # Translators: the text editor used by participants
ifaint's avatar
ifaint committed
            verbose_name=_("Editor mode"))
ifaint's avatar
ifaint committed
        verbose_name = _("User status")
        verbose_name_plural = _("User statuses")
        ordering = ("key_time",)

    def __unicode__(self):
        return _("User status for %(user)s") % {'user': self.user}
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 "
ifaint's avatar
ifaint committed
            "on your file system where the course's git repository lives."),
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 ('-').")),
                    ]
            )

    hidden = models.BooleanField(
            default=True,
            help_text=_("Is the course only accessible to course staff?"),
ifaint's avatar
ifaint committed
            verbose_name=_('Hidden to student'))
    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'))
    valid = models.BooleanField(
            default=True,
ifaint's avatar
ifaint committed
            help_text=_("Whether the course content has passed validation."),
ifaint's avatar
ifaint committed
            verbose_name=_('Valid'))
    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."),
ifaint's avatar
ifaint committed
            verbose_name=_('SSH private key'))
    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'))
    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(User,
            through='Participation')

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

    def get_absolute_url(self):
        return reverse("relate-course_page", args=(self.identifier,))
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,
ifaint's avatar
ifaint committed
            verbose_name=_('Course identifier'))
    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
class ParticipationTag(models.Model):
ifaint's avatar
ifaint committed
    course = models.ForeignKey(Course,
ifaint's avatar
ifaint committed
            verbose_name=_('Course identifier'))
    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'))

    def clean(self):
        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)

    class Meta:
        verbose_name = _("Participation tag")
ifaint's avatar
ifaint committed
        verbose_name_plural = _("Participation tags")
        unique_together = (("course", "name"),)
        ordering = ("course", "name")


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

    enroll_time = models.DateTimeField(default=now,
ifaint's avatar
ifaint committed
            verbose_name=_('Enroll time'))
Andreas Klöckner's avatar
Andreas Klöckner committed
    role = models.CharField(max_length=50,
Andreas Klöckner's avatar
Andreas Klöckner committed
            choices=PARTICIPATION_ROLE_CHOICES,
ifaint's avatar
ifaint committed
            help_text=_("Instructors may update course content. "
Andreas Klöckner's avatar
Andreas Klöckner committed
            "Teaching assistants may access and change grade data. "
            "Observers may access analytics. "
ifaint's avatar
ifaint committed
            "Each role includes privileges from subsequent roles."),
ifaint's avatar
ifaint committed
            verbose_name=_('Participation role'))
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 "
            "flows (time-limited flows are currently unimplemented)."),
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'))
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,
                "role": dict(PARTICIPATION_ROLE_CHOICES).get(self.role).lower()}
        verbose_name = _("Participation")
ifaint's avatar
ifaint committed
        verbose_name_plural = _("Participations")
        unique_together = (("user", "course"),)
        ordering = ("course", "user")

class ParticipationPreapproval(models.Model):
ifaint's avatar
ifaint committed
    email = models.EmailField(max_length=254,
ifaint's avatar
ifaint committed
            verbose_name=_('Email'))
ifaint's avatar
ifaint committed
    course = models.ForeignKey(Course,
ifaint's avatar
ifaint committed
            verbose_name=_('Course identifier'))
    role = models.CharField(max_length=50,
ifaint's avatar
ifaint committed
            choices=PARTICIPATION_ROLE_CHOICES,
ifaint's avatar
ifaint committed
            verbose_name=_('Role'))
ifaint's avatar
ifaint committed
    creator = models.ForeignKey(User, null=True,
ifaint's avatar
ifaint committed
            verbose_name=_('Creator'))
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):
        # Translators: somebody's email in some course in Particiaption
        # Preapproval
        return _("%(email)s in %(course)s") % {
                "email": self.email, "course": self.course}
        verbose_name = _("Participation preapproval")
ifaint's avatar
ifaint committed
        verbose_name_plural = _("Participation preapprovals")
        unique_together = (("course", "email"),)
        ordering = ("course", "email")

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

class InstantFlowRequest(models.Model):
ifaint's avatar
ifaint committed
    course = models.ForeignKey(Course,
ifaint's avatar
ifaint committed
            verbose_name=_('Course identifier'))
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

# {{{ 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,
ifaint's avatar
ifaint committed
            verbose_name=_('Course identifier'))
Andreas Klöckner's avatar
Andreas Klöckner committed
    participation = models.ForeignKey(Participation, null=True, blank=True,
ifaint's avatar
ifaint committed
            db_index=True,
ifaint's avatar
ifaint committed
            verbose_name=_('Participation'))
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,
ifaint's avatar
ifaint committed
            verbose_name=_('Completition time'))
ifaint's avatar
ifaint committed
    page_count = models.IntegerField(null=True, blank=True,
ifaint's avatar
ifaint committed
            verbose_name=_('Page count'))
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}
    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)

    def last_activity(self):
        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

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",
ifaint's avatar
ifaint committed
            verbose_name=_('Flow session'))
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

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

    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})
    # 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


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,
ifaint's avatar
ifaint committed
            verbose_name=_('Flow session'))
ifaint's avatar
ifaint committed
    page_data = models.ForeignKey(FlowPageData, db_index=True,
ifaint's avatar
ifaint committed
            verbose_name=_('Page data'))
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'))
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.
ifaint's avatar
ifaint committed
            result += unicode(_(" (with answer)"))
    class Meta:
ifaint's avatar
ifaint committed
        verbose_name = _("Flow page visit")
ifaint's avatar
ifaint committed
        verbose_name_plural = _("Flow page visits")
        # These must be distinguishable, to figure out what came later.
        unique_together = (("page_data", "visit_time"),)

    def get_most_recent_grade(self):
        grades = self.grades.order_by("-grade_time")[:1]

        for grade in grades:
            return grade

        return None

    def get_most_recent_feedback(self):
        grade = self.get_most_recent_grade()

        if grade is None:
            return None
        else:
            return get_feedback_for_grade(grade)

# }}}


#  {{{ flow page visit grade

class FlowPageVisitGrade(models.Model):
ifaint's avatar
ifaint committed
    visit = models.ForeignKey(FlowPageVisit, related_name="grades",
ifaint's avatar
ifaint committed
            verbose_name=_('Visit'))
ifaint's avatar
ifaint committed
    grader = models.ForeignKey(User, null=True, blank=True,
ifaint's avatar
ifaint committed
            verbose_name=_('Grader'))
ifaint's avatar
ifaint committed
    grade_time = models.DateTimeField(db_index=True, default=now,
ifaint's avatar
ifaint committed
            verbose_name=_('Grade time'))
    graded_at_git_commit_sha = models.CharField(
ifaint's avatar
ifaint committed
            max_length=200, null=True, blank=True,
ifaint's avatar
ifaint committed
            verbose_name=_('Graded at git commit SHA'))
ifaint's avatar
ifaint committed
    grade_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=_('Grade data'))
    # This data should be recomputable, but we'll cache it here,
    # because it might be very expensive (container-launch expensive
    # for code questions, for example) to recompute.

    max_points = models.FloatField(null=True, blank=True,
ifaint's avatar
ifaint committed
            help_text=_("Point value of this question when receiving "
ifaint's avatar
ifaint committed
            "full credit."),
ifaint's avatar
ifaint committed
            verbose_name=_('Max points'))
    correctness = models.FloatField(null=True, blank=True,
ifaint's avatar
ifaint committed
            help_text=_("Real number between zero and one (inclusively) "
ifaint's avatar
ifaint committed
            "indicating the degree of correctness of the answer."),
ifaint's avatar
ifaint committed
            verbose_name=_('Correctness'))

    # This JSON object has fields corresponding to
    # :class:`course.page.AnswerFeedback`, except for
    # :attr:`course.page.AnswerFeedback.correctness`, which is stored
    # separately for efficiency.

ifaint's avatar
ifaint committed
    feedback = JSONField(null=True, blank=True,
            # Show correct characters in admin for non ascii languages.
ifaint's avatar
ifaint committed
            dump_kwargs={'ensure_ascii': False},
            # Translators: "Feedback" stands for the feedback of answers.
ifaint's avatar
ifaint committed
            verbose_name=_('Feedback'))
    def percentage(self):
        if self.correctness is not None:
            return 100*self.correctness
        else:
            return None

    def value(self):
        if self.correctness is not None and self.max_points is not None:
            return self.correctness * self.max_points
        else:
            return None

ifaint's avatar
ifaint committed
        verbose_name = _("Flow page visit grade")
ifaint's avatar
ifaint committed
        verbose_name_plural = _("Flow page visit grades")
        # These must be distinguishable, to figure out what came later.
        unique_together = (("visit", "grade_time"),)

        ordering = ("visit", "grade_time")

    def __unicode__(self):
        # information on FlowPageVisitGrade class
        # Translators: return the information of the grade of a user
        # by percentage.
        return _("grade of %(visit)s: %(percentage)s") % {
                "visit": self.visit, "percentage": self.percentage()}

class FlowPageBulkFeedback(models.Model):
    # We're only storing one of these per page, because
    # they're 'bulk' (i.e. big, like plots or program output)
ifaint's avatar
ifaint committed
    page_data = models.OneToOneField(FlowPageData,
ifaint's avatar
ifaint committed
            verbose_name=_('Page data'))
ifaint's avatar
ifaint committed
    grade = models.ForeignKey(FlowPageVisitGrade,
ifaint's avatar
ifaint committed
            verbose_name=_('Grade'))
ifaint's avatar
ifaint committed
    bulk_feedback = 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=_('Bulk feedback'))
def update_bulk_feedback(page_data, grade, bulk_feedback_json):
    FlowPageBulkFeedback.objects.update_or_create(
            page_data=page_data,
            defaults=dict(
                grade=grade,
                bulk_feedback=bulk_feedback_json))


def get_feedback_for_grade(grade):
    try:
        bulk_feedback_json = FlowPageBulkFeedback.objects.get(
                page_data=grade.visit.page_data,
                grade=grade).bulk_feedback
    except ObjectDoesNotExist:
        bulk_feedback_json = None

    from course.page import AnswerFeedback
    if grade is not None:
        return AnswerFeedback.from_json(
                grade.feedback, bulk_feedback_json)
    else:
        return None

# {{{ flow access
def validate_stipulations(stip):
    if not isinstance(stip, dict):
        raise ValidationError(_("stipulations must be a dictionary"))
    allowed_keys = set(["credit_percent", "allowed_session_count"])
    if not set(stip.keys()) <= allowed_keys:
        raise ValidationError(
                string_concat(
                    _("unrecognized keys in stipulations"),
                    ": %s")
                % ", ".join(set(stip.keys()) - allowed_keys))

    if "credit_percent" in stip and not isinstance(
            stip["credit_percent"], (int, float)):
        raise ValidationError(_("credit_percent must be a float"))
    if ("allowed_session_count" in stip
            and (
                not isinstance(stip["allowed_session_count"], int)
                or stip["allowed_session_count"] < 0)):
        raise ValidationError(
                _("'allowed_session_count' must be a non-negative integer"))
# {{{ deprecated exception stuff

class FlowAccessException(models.Model):
ifaint's avatar
ifaint committed
    participation = models.ForeignKey(Participation, db_index=True,
ifaint's avatar
ifaint committed
            verbose_name=_('Participation'))
ifaint's avatar
ifaint committed
    flow_id = models.CharField(max_length=200, blank=False, null=False,
ifaint's avatar
ifaint committed
            verbose_name=_('Flow ID'))
ifaint's avatar
ifaint committed
    expiration = models.DateTimeField(blank=True, null=True,
ifaint's avatar
ifaint committed
            verbose_name=_('Expiration'))
    stipulations = JSONField(blank=True, null=True,
            # Translators: help text for stipulations in FlowAccessException
            # (deprecated)
ifaint's avatar
ifaint committed
            help_text=_("A dictionary of the same things that can be added "
            "to a flow access rule, such as allowed_session_count or "
            "credit_percent. If not specified here, values will default "
ifaint's avatar
ifaint committed
            "to the stipulations in the course content."),
ifaint's avatar
ifaint committed
            validators=[validate_stipulations],
            dump_kwargs={'ensure_ascii': False},
ifaint's avatar
ifaint committed
            verbose_name=_('Stipulations'))
ifaint's avatar
ifaint committed
    creator = models.ForeignKey(User, null=True,
ifaint's avatar
ifaint committed
            verbose_name=_('Creator'))
ifaint's avatar
ifaint committed
    creation_time = models.DateTimeField(default=now, db_index=True,
ifaint's avatar
ifaint committed
            verbose_name=_('Creation time'))
    is_sticky = models.BooleanField(
            default=False,
ifaint's avatar
ifaint committed
            # Translators: deprecated
ifaint's avatar
ifaint committed
            help_text=_("Check if a flow started under this "
            "exception rule set should stay "
ifaint's avatar
ifaint committed
            "under this rule set until it is expired."),
            # Translators: deprecated
ifaint's avatar
ifaint committed
            verbose_name=_('Is sticky'))
ifaint's avatar
ifaint committed
    comment = models.TextField(blank=True, null=True,
ifaint's avatar
ifaint committed
            verbose_name=_('Comment'))
    def __unicode__(self):
Dong Zhuang's avatar
Dong Zhuang committed
                # Translators: flow access exception in admin (deprecated)
                _("Access exception for '%(user)s' to '%(flow_id)s' "
                "in '%(course)s'") %
                    "user": self.participation.user,
                    "flow_id": self.flow_id,
                    "course": self.participation.course
                    })

class FlowAccessExceptionEntry(models.Model):
    exception = models.ForeignKey(FlowAccessException,
ifaint's avatar
ifaint committed
            related_name="entries",
ifaint's avatar
ifaint committed
            verbose_name=_('Exception'))
    permission = models.CharField(max_length=50,
ifaint's avatar
ifaint committed
            choices=FLOW_PERMISSION_CHOICES,
ifaint's avatar
ifaint committed
            verbose_name=_('Permission'))
        # Translators: FlowAccessExceptionEntry (deprecated)
ifaint's avatar
ifaint committed
        verbose_name_plural = _("Flow access exception entries")
    def __unicode__(self):
        return self.permission

# }}}


class FlowRuleException(models.Model):
ifaint's avatar
ifaint committed
    flow_id = models.CharField(max_length=200, blank=False, null=False,
ifaint's avatar
ifaint committed
            verbose_name=_('Flow ID'))
ifaint's avatar
ifaint committed
    participation = models.ForeignKey(Participation, db_index=True,
ifaint's avatar
ifaint committed
            verbose_name=_('Participation'))
ifaint's avatar
ifaint committed
    expiration = models.DateTimeField(blank=True, null=True,
ifaint's avatar
ifaint committed
            verbose_name=_('Expiration'))
ifaint's avatar
ifaint committed
    creator = models.ForeignKey(User, null=True,
ifaint's avatar
ifaint committed
            verbose_name=_('Creator'))
ifaint's avatar
ifaint committed
    creation_time = models.DateTimeField(default=now, db_index=True,
ifaint's avatar
ifaint committed
            verbose_name=_('Creation time'))
ifaint's avatar
ifaint committed
    comment = models.TextField(blank=True, null=True,
ifaint's avatar
ifaint committed
            verbose_name=_('Comment'))

    kind = models.CharField(max_length=50, blank=False, null=False,
ifaint's avatar
ifaint committed
            choices=FLOW_RULE_KIND_CHOICES,
ifaint's avatar
ifaint committed
            verbose_name=_('Kind'))
ifaint's avatar
ifaint committed
    rule = YAMLField(blank=False, null=False,
ifaint's avatar
ifaint committed
            verbose_name=_('Rule'))
ifaint's avatar
ifaint committed
    active = models.BooleanField(default=True,
            verbose_name=pgettext_lazy(
                "Is the flow rule exception activated?", "Active"))
                # Translators: For FlowRuleException
                _("%(kind)s exception for '%(user)s' to '%(flow_id)s'"
                "in '%(course)s'")
                % {
                    "kind": self.kind,
                    "user": self.participation.user,
                    "flow_id": self.flow_id,
                    "course": self.participation.course})

    def clean(self):
        if (self.kind == flow_rule_kind.grading
                and self.expiration is not None):
ifaint's avatar
ifaint committed
            raise ValidationError(_("grading rules may not expire"))

        from course.validation import (
                ValidationError as ContentValidationError,
                validate_session_access_rule,
                validate_session_grading_rule,
                ValidationContext)
        from course.content import (get_course_repo,
                get_course_commit_sha,
                get_flow_desc)

Andreas Klöckner's avatar
Andreas Klöckner committed
        from relate.utils import dict_to_struct
        rule = dict_to_struct(self.rule)

        repo = get_course_repo(self.participation.course)
        commit_sha = get_course_commit_sha(
                self.participation.course, self.participation)
        ctx = ValidationContext(
                repo=repo,
                commit_sha=commit_sha)

        flow_desc = get_flow_desc(repo,
                self.participation.course,
                self.flow_id, commit_sha)

        tags = None
        if hasattr(flow_desc, "rules"):
            tags = getattr(flow_desc.rules, "tags", None)

        try:
            if self.kind == flow_rule_kind.start:
                validate_session_start_rule(ctx, unicode(self), rule, tags)
            elif self.kind == flow_rule_kind.access:
                validate_session_access_rule(ctx, unicode(self), rule, tags)
            elif self.kind == flow_rule_kind.grading:
                validate_session_grading_rule(ctx, unicode(self), rule, tags)
            else:
                # the rule refers to FlowRuleException rule
ifaint's avatar
ifaint committed
                raise ValidationError(_("invalid rule kind: ")+self.kind)

        except ContentValidationError as e:
            # the rule refers to FlowRuleException rule
ifaint's avatar
ifaint committed
            raise ValidationError(_("invalid existing_session_rules: ")+str(e))
ifaint's avatar
ifaint committed
        verbose_name = _("Flow rule exception")
ifaint's avatar
ifaint committed
        verbose_name_plural = _("Flow rule exceptions")
# }}}


# {{{ grading

class GradingOpportunity(models.Model):
ifaint's avatar
ifaint committed
    course = models.ForeignKey(Course,
ifaint's avatar
ifaint committed
            verbose_name=_('Course identifier'))

    identifier = models.CharField(max_length=200, blank=False, null=False,
ifaint's avatar
ifaint committed
            # Translators: format of identifier for GradingOpportunity
ifaint's avatar
ifaint committed
            help_text=_("A symbolic name for this grade. "
ifaint's avatar
ifaint committed
            "lower_case_with_underscores, no spaces."),
ifaint's avatar
ifaint committed
            verbose_name=_('Grading opportunity ID'))
    name = models.CharField(max_length=200, blank=False, null=False,
ifaint's avatar
ifaint committed
            help_text=_("A human-readable identifier for the grade."),
ifaint's avatar
ifaint committed
            verbose_name=_('Grading opportunity name'))
    flow_id = models.CharField(max_length=200, blank=True, null=True,
ifaint's avatar
ifaint committed
            help_text=_("Flow identifier that this grading opportunity "
ifaint's avatar
ifaint committed
            "is linked to, if any"),
ifaint's avatar
ifaint committed
            verbose_name=_('Flow ID'))
    aggregation_strategy = models.CharField(max_length=20,
ifaint's avatar
ifaint committed
            choices=GRADE_AGGREGATION_STRATEGY_CHOICES,
            # Translators: strategy on how the grading of mutiple sessioins
ifaint's avatar
ifaint committed
            verbose_name=_('Aggregation strategy'))
ifaint's avatar
ifaint committed
    due_time = models.DateTimeField(default=None, blank=True, null=True,
ifaint's avatar
ifaint committed
            verbose_name=_('Due time'))
ifaint's avatar
ifaint committed
    creation_time = models.DateTimeField(default=now,
ifaint's avatar
ifaint committed
            verbose_name=_('Creation time'))
ifaint's avatar
ifaint committed
    shown_in_grade_book = models.BooleanField(default=True,