microsite.py 4.78 KB
Newer Older
1 2 3 4 5 6 7 8
"""
This file implements the Microsite support for the Open edX platform.
A microsite enables the following features:

1) Mapping of sub-domain name to a 'brand', e.g. foo-university.edx.org
2) Present a landing page with a listing of courses that are specific to the 'brand'
3) Ability to swap out some branding elements in the website
"""
9
import inspect
10

11
from importlib import import_module
12
from django.conf import settings
13
from microsite_configuration.backends.base import BaseMicrositeBackend, BaseMicrositeTemplateBackend
14 15


16 17 18 19 20 21
__all__ = [
    'is_request_in_microsite', 'get_value', 'has_override_value',
    'get_template_path', 'get_value_for_org', 'get_all_orgs',
    'clear', 'set_by_domain', 'enable_microsites', 'get_all_config',
    'is_feature_enabled', 'enable_microsites_pre_startup',
]
22

23 24
BACKEND = None
TEMPLATES_BACKEND = None
25 26


27
def is_feature_enabled():
28
    """
29
    Returns whether the feature flag to enable microsite has been set
30
    """
31
    return settings.FEATURES.get('USE_MICROSITES', False)
32 33 34 35 36 37


def is_request_in_microsite():
    """
    This will return if current request is a request within a microsite
    """
38
    return BACKEND.is_request_in_microsite()
39 40


41
def get_value(val_name, default=None, **kwargs):
42 43 44
    """
    Returns a value associated with the request's microsite, if present
    """
45
    return BACKEND.get_value(val_name, default, **kwargs)
46 47


48
def get_dict(dict_name, default=None, **kwargs):
49
    """
50 51 52 53
    Returns a dictionary product of merging the request's microsite and
    the default value.
    This can be used, for example, to return a merged dictonary from the
    settings.FEATURES dict, including values defined at the microsite
54
    """
55
    return BACKEND.get_dict(dict_name, default, **kwargs)
56 57


58
def has_override_value(val_name):
59
    """
60 61
    Returns True/False whether a Microsite has a definition for the
    specified named value
62
    """
63
    return BACKEND.has_override_value(val_name)
64 65 66 67 68 69 70


def get_value_for_org(org, val_name, default=None):
    """
    This returns a configuration value for a microsite which has an org_filter that matches
    what is passed in
    """
71
    return BACKEND.get_value_for_org(org, val_name, default)
72 73 74 75 76 77 78


def get_all_orgs():
    """
    This returns a set of orgs that are considered within a microsite. This can be used,
    for example, to do filtering
    """
79
    return BACKEND.get_all_orgs()
80 81


82 83 84 85 86 87
def get_all_config():
    """
    This returns a dict have all microsite configs. Each key in the dict represent a
    microsite config.
    """
    return BACKEND.get_all_config()
88 89 90 91 92 93


def clear():
    """
    Clears out any microsite configuration from the current request/thread
    """
94
    BACKEND.clear()
95 96


97
def set_by_domain(domain):
98
    """
99 100
    For a given request domain, find a match in our microsite configuration
    and make it available to the complete django request process
101
    """
102
    BACKEND.set_config_by_domain(domain)
103 104


105
def enable_microsites_pre_startup(log):
106
    """
107 108
    Prepare the feature settings that must be enabled before django.setup() or
    autostartup() during the startup script
109
    """
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
    if is_feature_enabled():
        BACKEND.enable_microsites_pre_startup(log)


def enable_microsites(log):
    """
    Enable the use of microsites during the startup script
    """
    if is_feature_enabled():
        BACKEND.enable_microsites(log)


def get_template(uri):
    """
    Returns a template for the specified URI, None if none exists or if caller should
    use default templates/search paths
    """
    if not is_request_in_microsite():
128 129
        return

130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
    return TEMPLATES_BACKEND.get_template(uri)


def get_template_path(relative_path, **kwargs):
    """
    Returns a path (string) to a template
    """
    if not is_request_in_microsite():
        return relative_path

    return TEMPLATES_BACKEND.get_template_path(relative_path, **kwargs)


def get_backend(name, expected_base_class, **kwds):
    """
    Load a microsites backend and return an instance of it.
    If backend is None (default) settings.MICROSITE_BACKEND is used.
    Any additional args(kwds) will be used in the constructor of the backend.
    """
    if not name:
        return None

    try:
        parts = name.split('.')
        module_name = '.'.join(parts[:-1])
        class_name = parts[-1]
    except IndexError:
        raise ValueError('Invalid microsites backend %s' % name)

    try:
        module = import_module(module_name)
        cls = getattr(module, class_name)
        if not inspect.isclass(cls) or not issubclass(cls, expected_base_class):
            raise TypeError
    except (AttributeError, ValueError):
        raise ValueError('Cannot find microsites backend %s' % module_name)

    return cls(**kwds)

169

170 171
BACKEND = get_backend(settings.MICROSITE_BACKEND, BaseMicrositeBackend)
TEMPLATES_BACKEND = get_backend(settings.MICROSITE_TEMPLATE_BACKEND, BaseMicrositeTemplateBackend)