startup.py 5.3 KB
Newer Older
1 2 3
"""
Module for code that should run during LMS startup
"""
Calen Pennington committed
4

5
import django
6 7 8
from django.conf import settings

# Force settings to run so that the python path is modified
9
settings.INSTALLED_APPS  # pylint: disable=pointless-statement
10

11
from openedx.core.lib.django_startup import autostartup
12
import edxmako
13
import logging
14
import analytics
15 16
from monkey_patch import (
    third_party_auth,
17
    django_db_models_options
18
)
19

20 21 22
import xmodule.x_module
import lms_xblock.runtime

23
from openedx.core.djangoapps.theming.core import enable_comprehensive_theme
24 25
from microsite_configuration import microsite

26
log = logging.getLogger(__name__)
27

28

29 30 31 32
def run():
    """
    Executed during django startup
    """
33
    third_party_auth.patch()
34
    django_db_models_options.patch()
35 36 37 38 39

    # To override the settings before executing the autostartup() for python-social-auth
    if settings.FEATURES.get('ENABLE_THIRD_PARTY_AUTH', False):
        enable_third_party_auth()

40 41 42 43 44
    # Comprehensive theming needs to be set up before django startup,
    # because modifying django template paths after startup has no effect.
    if settings.COMPREHENSIVE_THEME_DIR:
        enable_comprehensive_theme(settings.COMPREHENSIVE_THEME_DIR)

45 46 47 48
    # We currently use 2 template rendering engines, mako and django_templates,
    # and one of them (django templates), requires the directories be added
    # before the django.setup().
    microsite.enable_microsites_pre_startup(log)
49

50
    django.setup()
51

52
    autostartup()
53

54 55
    add_mimetypes()

56 57 58
    # Mako requires the directories to be added after the django setup.
    microsite.enable_microsites(log)

59
    if settings.FEATURES.get('USE_CUSTOM_THEME', False):
David Baumgold committed
60
        enable_stanford_theme()
61

62
    # Initialize Segment analytics module by setting the write_key.
63 64
    if settings.LMS_SEGMENT_KEY:
        analytics.write_key = settings.LMS_SEGMENT_KEY
65

66 67
    # register any dependency injections that we need to support in edx_proctoring
    # right now edx_proctoring is dependent on the openedx.core.djangoapps.credit
68
    if settings.FEATURES.get('ENABLE_SPECIAL_EXAMS'):
69 70 71 72 73
        # Import these here to avoid circular dependencies of the form:
        # edx-platform app --> DRF --> django translation --> edx-platform app
        from edx_proctoring.runtime import set_runtime_service
        from instructor.services import InstructorService
        from openedx.core.djangoapps.credit.services import CreditService
74
        set_runtime_service('credit', CreditService())
75 76

        # register InstructorService (for deleting student attempts and user staff access roles)
77 78
        set_runtime_service('instructor', InstructorService())

79 80 81 82 83 84 85
    # In order to allow modules to use a handler url, we need to
    # monkey-patch the x_module library.
    # TODO: Remove this code when Runtimes are no longer created by modulestores
    # https://openedx.atlassian.net/wiki/display/PLAT/Convert+from+Storage-centric+runtimes+to+Application-centric+runtimes
    xmodule.x_module.descriptor_global_handler_url = lms_xblock.runtime.handler_url
    xmodule.x_module.descriptor_global_local_resource_url = lms_xblock.runtime.local_resource_url

86

87 88 89
def add_mimetypes():
    """
    Add extra mimetypes. Used in xblock_resource.
90 91

    If you add a mimetype here, be sure to also add it in cms/startup.py.
92 93 94 95 96 97 98 99 100
    """
    import mimetypes

    mimetypes.add_type('application/vnd.ms-fontobject', '.eot')
    mimetypes.add_type('application/x-font-opentype', '.otf')
    mimetypes.add_type('application/x-font-ttf', '.ttf')
    mimetypes.add_type('application/font-woff', '.woff')


David Baumgold committed
101
def enable_stanford_theme():
102 103 104 105 106 107 108
    """
    Enable the settings for a custom theme, whose files should be stored
    in ENV_ROOT/themes/THEME_NAME (e.g., edx_all/themes/stanford).
    """
    # Workaround for setting THEME_NAME to an empty
    # string which is the default due to this ansible
    # bug: https://github.com/ansible/ansible/issues/4812
David Baumgold committed
109
    if getattr(settings, "THEME_NAME", "") == "":
110 111 112 113 114 115 116 117 118 119 120 121
        settings.THEME_NAME = None
        return

    assert settings.FEATURES['USE_CUSTOM_THEME']
    settings.FAVICON_PATH = 'themes/{name}/images/favicon.ico'.format(
        name=settings.THEME_NAME
    )

    # Calculate the location of the theme's files
    theme_root = settings.ENV_ROOT / "themes" / settings.THEME_NAME

    # Include the theme's templates in the template search paths
122
    settings.DEFAULT_TEMPLATE_ENGINE['DIRS'].insert(0, theme_root / 'templates')
123
    edxmako.paths.add_lookup('main', theme_root / 'templates', prepend=True)
124 125 126 127 128 129

    # Namespace the theme's static files to 'themes/<theme_name>' to
    # avoid collisions with default edX static files
    settings.STATICFILES_DIRS.append(
        (u'themes/{}'.format(settings.THEME_NAME), theme_root / 'static')
    )
130

131 132 133
    # Include theme locale path for django translations lookup
    settings.LOCALE_PATHS = (theme_root / 'conf/locale',) + settings.LOCALE_PATHS

134 135 136

def enable_microsites():
    """
137 138
    Calls the enable_microsites function in the microsite backend.
    Here for backwards compatibility
139
    """
140
    microsite.enable_microsites(log)
141 142 143 144 145 146 147 148 149 150


def enable_third_party_auth():
    """
    Enable the use of third_party_auth, which allows users to sign in to edX
    using other identity providers. For configuration details, see
    common/djangoapps/third_party_auth/settings.py.
    """

    from third_party_auth import settings as auth_settings
151
    auth_settings.apply_settings(settings)