Skip to content
models.py 44.3 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, 
        ugettext_noop,
        string_concat, 
        ugettext,
        )
Andreas Klöckner's avatar
Andreas Klöckner committed

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,
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.",
            verbose_name = "Facility ID")
    description = models.CharField(max_length=100,
            verbose_name = "Facility description")
        verbose_name = "Facility"
        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,
    description = models.CharField(max_length=100,
            verbose_name = 'Ip range description')

    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",
            verbose_name = 'User ID')
Andreas Klöckner's avatar
Andreas Klöckner committed
    status = models.CharField(max_length=50,
            choices=USER_STATUS_CHOICES,
            verbose_name = 'User status')
    sign_in_key = models.CharField(max_length=50,
            help_text="NEED HELP TEXT",
            null=True, unique=True, db_index=True, blank=True,
            verbose_name = 'Sign in key')
    key_time = models.DateTimeField(default=now,
            help_text="NEED HELP TEXT",
            verbose_name = 'Key time')
    editor_mode = models.CharField(max_length=20,
            choices=(
                ("default", "Default"),
                ("sublime", "Sublime text"),
                ("emacs", "Emacs"),
                ("vim", "Vim"),
                ),
            default="default",
            # Translators: the text editor used by participants
            verbose_name = "Editor mode")
        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,
            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.",
            db_index=True)

    hidden = models.BooleanField(
            default=True,
            help_text="Is the course only accessible to course staff?",
            verbose_name = 'Hidden to student')
    listed = models.BooleanField(
            default=True,
            help_text="Should the course be listed on the main page?",
            verbose_name = 'Listed on main page')
    accepts_enrollment = models.BooleanField(
            default=True,
            verbose_name = 'Accepts enrollment')
    valid = models.BooleanField(
            default=True,
            help_text="Whether the course content has passed validation.",
            verbose_name = 'Valid')
    git_source = models.CharField(max_length=200, blank=True,
            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.",
            verbose_name = 'git source')
    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.",
            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.",
            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.",
            verbose_name = 'Events file')
    enrollment_approval_required = models.BooleanField(
            default=False,
Loading
Loading full blame...