models.py 3.68 KB
Newer Older
1 2 3
"""
Models for the custom course feature
"""
4
from __future__ import unicode_literals
5

6
import json
7
import logging
8
from datetime import datetime
9

10
from ccx_keys.locator import CCXLocator
11 12
from django.contrib.auth.models import User
from django.db import models
13
from lazy import lazy
14
from pytz import utc
15

16
from openedx.core.djangoapps.xmodule_django.models import CourseKeyField, LocationKeyField
17 18 19 20
from xmodule.error_module import ErrorDescriptor
from xmodule.modulestore.django import modulestore

log = logging.getLogger("edx.ccx")
21 22


cewing committed
23
class CustomCourseForEdX(models.Model):
24
    """
cewing committed
25
    A Custom Course.
26 27 28 29
    """
    course_id = CourseKeyField(max_length=255, db_index=True)
    display_name = models.CharField(max_length=255)
    coach = models.ForeignKey(User, db_index=True)
30 31 32
    # if not empty, this field contains a json serialized list of
    # the master course modules
    structure_json = models.TextField(verbose_name='Structure JSON', blank=True, null=True)
33

34 35 36
    class Meta(object):
        app_label = 'ccx'

37
    @lazy
38
    def course(self):
cewing committed
39
        """Return the CourseDescriptor of the course related to this CCX"""
40 41 42 43 44 45 46 47 48 49
        store = modulestore()
        with store.bulk_operations(self.course_id):
            course = store.get_course(self.course_id)
            if not course or isinstance(course, ErrorDescriptor):
                log.error("CCX {0} from {2} course {1}".format(  # pylint: disable=logging-format-interpolation
                    self.display_name, self.course_id, "broken" if course else "non-existent"
                ))
            return course

    @lazy
50
    def start(self):
cewing committed
51 52
        """Get the value of the override of the 'start' datetime for this CCX
        """
53 54 55
        # avoid circular import problems
        from .overrides import get_override_for_ccx
        return get_override_for_ccx(self, self.course, 'start')
56

57
    @lazy
58
    def due(self):
cewing committed
59 60
        """Get the value of the override of the 'due' datetime for this CCX
        """
61 62 63
        # avoid circular import problems
        from .overrides import get_override_for_ccx
        return get_override_for_ccx(self, self.course, 'due')
64

65 66 67 68 69 70 71 72 73 74
    @lazy
    def max_student_enrollments_allowed(self):
        """
        Get the value of the override of the 'max_student_enrollments_allowed'
        datetime for this CCX
        """
        # avoid circular import problems
        from .overrides import get_override_for_ccx
        return get_override_for_ccx(self, self.course, 'max_student_enrollments_allowed')

75
    def has_started(self):
cewing committed
76
        """Return True if the CCX start date is in the past"""
77
        return datetime.now(utc) > self.start
78 79

    def has_ended(self):
cewing committed
80
        """Return True if the CCX due date is set and is in the past"""
81 82 83
        if self.due is None:
            return False

84
        return datetime.now(utc) > self.due
85

86 87 88 89 90 91 92 93 94
    @property
    def structure(self):
        """
        Deserializes a course structure JSON object
        """
        if self.structure_json:
            return json.loads(self.structure_json)
        return None

95 96 97 98 99 100 101 102 103 104
    @property
    def locator(self):
        """
        Helper property that gets a corresponding CCXLocator for this CCX.

        Returns:
            The CCXLocator corresponding to this CCX.
        """
        return CCXLocator.from_course_locator(self.course_id, unicode(self.id))

105

cewing committed
106
class CcxFieldOverride(models.Model):
107
    """
cewing committed
108
    Field overrides for custom courses.
109
    """
cewing committed
110
    ccx = models.ForeignKey(CustomCourseForEdX, db_index=True)
111 112 113
    location = LocationKeyField(max_length=255, db_index=True)
    field = models.CharField(max_length=255)

114
    class Meta(object):
115
        app_label = 'ccx'
cewing committed
116
        unique_together = (('ccx', 'location', 'field'),)
117 118

    value = models.TextField(default='null')