startup.py 4.1 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

10
settings.INSTALLED_APPS  # pylint: disable=pointless-statement
11

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

20 21 22
import xmodule.x_module
import lms_xblock.runtime

23
from startup_configurations.validate_config import validate_lms_config
24 25 26
from openedx.core.djangoapps.theming.core import enable_theming
from openedx.core.djangoapps.theming.helpers import is_comprehensive_theming_enabled

27 28
from microsite_configuration import microsite

29
log = logging.getLogger(__name__)
30

31

32 33 34 35
def run():
    """
    Executed during django startup
    """
36
    third_party_auth.patch()
37
    django_db_models_options.patch()
38 39 40 41 42

    # 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()

43 44
    # Comprehensive theming needs to be set up before django startup,
    # because modifying django template paths after startup has no effect.
45 46
    if is_comprehensive_theming_enabled():
        enable_theming()
47

48 49 50 51
    # 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)
52

53
    django.setup()
54

55
    autostartup()
56

57 58
    add_mimetypes()

59 60 61
    # Mako requires the directories to be added after the django setup.
    microsite.enable_microsites(log)

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
        # 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
72
        from lms.djangoapps.instructor.services import InstructorService
73
        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
    # validate configurations on startup
    validate_lms_config(settings)

89

90 91 92
def add_mimetypes():
    """
    Add extra mimetypes. Used in xblock_resource.
93 94

    If you add a mimetype here, be sure to also add it in cms/startup.py.
95 96 97 98 99 100 101 102 103
    """
    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')


104 105
def enable_microsites():
    """
106 107
    Calls the enable_microsites function in the microsite backend.
    Here for backwards compatibility
108
    """
109
    microsite.enable_microsites(log)
110 111 112 113 114 115 116 117 118 119


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
120
    auth_settings.apply_settings(settings)