Skip to content
models.py 75.3 KiB
Newer Older
from __future__ import annotations
Andreas Klöckner's avatar
Andreas Klöckner committed

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

from collections.abc import Iterable
from decimal import Decimal
from typing import (
    TYPE_CHECKING,
    Any,
    cast,
Andreas Klöckner's avatar
Andreas Klöckner committed
)
Andreas Klöckner's avatar
Andreas Klöckner committed
from django.conf import settings
from django.core.exceptions import ObjectDoesNotExist, ValidationError
Andreas Klöckner's avatar
Andreas Klöckner committed
from django.core.validators import RegexValidator
Andreas Klöckner's avatar
Andreas Klöckner committed
from django.db import models
from django.db.models.signals import post_save
from django.dispatch import receiver
Andreas Klöckner's avatar
Andreas Klöckner committed
from django.urls import reverse
from django.utils.timezone import now
from django.utils.translation import gettext_lazy as _, pgettext_lazy
from course.constants import (  # noqa
    COURSE_ID_REGEX,
    EVENT_KIND_REGEX,
    EXAM_TICKET_STATE_CHOICES,
    FLOW_PERMISSION_CHOICES,
    FLOW_RULE_KIND_CHOICES,
    FLOW_SESSION_EXPIRATION_MODE_CHOICES,
    GRADE_AGGREGATION_STRATEGY_CHOICES,
    GRADE_STATE_CHANGE_CHOICES,
    GRADING_OPP_ID_REGEX,
    PARTICIPATION_PERMISSION_CHOICES,
    PARTICIPATION_STATUS_CHOICES,
    USER_STATUS_CHOICES,
    exam_ticket_states,
    flow_permission,
    flow_rule_kind,
    flow_session_expiration_mode,
    grade_aggregation_strategy,
    grade_state_change_types,
    participation_permission,
    participation_status,
Andreas Klöckner's avatar
Andreas Klöckner committed
    user_status,
)
from relate.utils import not_none, string_concat
    import datetime
Andreas Klöckner's avatar
Andreas Klöckner committed

    from course.content import FlowDesc
    from course.page.base import AnswerFeedback
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

def validate_course_specific_language(value: str) -> None:
    if not value.strip():
        # the default value is ""
        return
    if value not in (
                [lang_code for lang_code, lang_descr in settings.LANGUAGES]
                + [settings.LANGUAGE_CODE]):
        raise ValidationError(
            _("'%s' is currently not supported as a course specific "
              "language at this site.") % value)


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."),
            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, "
    name = models.CharField(
            null=True, blank=False,
            max_length=200,
            verbose_name=_("Course name"),
            help_text=_("A human-readable name for the course. "
                "(e.g. 'Numerical Methods')"))
    number = models.CharField(
            null=True, blank=False,
            max_length=200,
            verbose_name=_("Course number"),
            help_text=_("A human-readable course number/ID "
                "for the course (e.g. 'CS123')"))
    time_period = models.CharField(
            null=True, blank=False,
            max_length=200,
            verbose_name=_("Time Period"),
            help_text=_("A human-readable description of the "
                "time period for the course (e.g. 'Fall 2014')"))

    start_date = models.DateField(
            verbose_name=_("Start date"),
            null=True, blank=True)
    end_date = models.DateField(
            verbose_name=_("End date"),

    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?"),
            verbose_name=_("Listed on main page"))
    accepts_enrollment = models.BooleanField(
            verbose_name=_("Accepts enrollment"))
    git_source = models.CharField(max_length=200, blank=False,
ifaint's avatar
ifaint committed
            help_text=_("A Git URL from which to pull course updates. "
            "If you're just starting out, enter "
            "<tt>https://github.com/inducer/relate-sample.git</tt> "
            "to get some sample content."),
            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."),
            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."),
            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,
ifaint's avatar
ifaint committed
            help_text=_("If set, each enrolling student must be "
            "individually approved."),
            verbose_name=_("Enrollment approval required"))
    preapproval_require_verified_inst_id = models.BooleanField(
            default=True,
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 "
            verbose_name=_("Prevent preapproval by institutional ID if not "
                           "verified?"))
    enrollment_required_email_suffix = models.CharField(
            max_length=200, blank=True, null=True,
Loading
Loading full blame...