models.py 3.91 KB
Newer Older
1 2
"""Models providing Programs support for the LMS and Studio."""
from collections import namedtuple
3 4 5
from urlparse import urljoin

from django.utils.translation import ugettext_lazy as _
6
from django.db import models
7 8 9 10

from config_models.models import ConfigurationModel


11 12 13
AuthoringAppConfig = namedtuple('AuthoringAppConfig', ['js_url', 'css_url'])


14 15 16 17 18
class ProgramsApiConfig(ConfigurationModel):
    """
    Manages configuration for connecting to the Programs service and using its
    API.
    """
19 20
    OAUTH2_CLIENT_NAME = 'programs'
    CACHE_KEY = 'programs.api.data'
21
    API_NAME = 'programs'
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43

    api_version_number = models.IntegerField(verbose_name=_("API Version"))

    internal_service_url = models.URLField(verbose_name=_("Internal Service URL"))
    public_service_url = models.URLField(verbose_name=_("Public Service URL"))

    authoring_app_js_path = models.CharField(
        verbose_name=_("Path to authoring app's JS"),
        max_length=255,
        blank=True,
        help_text=_(
            "This value is required in order to enable the Studio authoring interface."
        )
    )
    authoring_app_css_path = models.CharField(
        verbose_name=_("Path to authoring app's CSS"),
        max_length=255,
        blank=True,
        help_text=_(
            "This value is required in order to enable the Studio authoring interface."
        )
    )
44

45 46 47 48 49 50 51 52
    cache_ttl = models.PositiveIntegerField(
        verbose_name=_("Cache Time To Live"),
        default=0,
        help_text=_(
            "Specified in seconds. Enable caching by setting this to a value greater than 0."
        )
    )

53 54 55 56
    enable_student_dashboard = models.BooleanField(
        verbose_name=_("Enable Student Dashboard Displays"),
        default=False
    )
57

58 59 60 61
    enable_studio_tab = models.BooleanField(
        verbose_name=_("Enable Studio Authoring Interface"),
        default=False
    )
62

63 64 65 66 67
    enable_certification = models.BooleanField(
        verbose_name=_("Enable Program Certificate Generation"),
        default=False
    )

68 69 70
    @property
    def internal_api_url(self):
        """
71
        Generate a URL based on internal service URL and API version number.
72
        """
73
        return urljoin(self.internal_service_url, '/api/v{}/'.format(self.api_version_number))
74 75 76 77

    @property
    def public_api_url(self):
        """
78
        Generate a URL based on public service URL and API version number.
79
        """
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
        return urljoin(self.public_service_url, '/api/v{}/'.format(self.api_version_number))

    @property
    def authoring_app_config(self):
        """
        Returns a named tuple containing information required for working with the Programs
        authoring app, a Backbone app hosted by the Programs service.
        """
        js_url = urljoin(self.public_service_url, self.authoring_app_js_path)
        css_url = urljoin(self.public_service_url, self.authoring_app_css_path)

        return AuthoringAppConfig(js_url=js_url, css_url=css_url)

    @property
    def is_cache_enabled(self):
        """Whether responses from the Programs API will be cached."""
        return self.cache_ttl > 0
97 98 99 100

    @property
    def is_student_dashboard_enabled(self):
        """
101
        Indicates whether LMS dashboard functionality related to Programs should
102 103 104
        be enabled or not.
        """
        return self.enabled and self.enable_student_dashboard
105 106

    @property
107 108 109 110 111 112 113 114 115 116 117
    def is_studio_tab_enabled(self):
        """
        Indicates whether Studio functionality related to Programs should
        be enabled or not.
        """
        return (
            self.enabled and
            self.enable_studio_tab and
            bool(self.authoring_app_js_path) and
            bool(self.authoring_app_css_path)
        )
118 119 120 121 122 123 124 125

    @property
    def is_certification_enabled(self):
        """
        Indicates whether background tasks should be initiated to grant
        certificates for Program completion.
        """
        return self.enabled and self.enable_certification