base.py 11.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
"""
Microsite configuration backend module.

Contains the base classes for microsite backends.

AbstractBaseMicrositeBackend is Abstract Base Class for the microsite configuration backend.
BaseMicrositeBackend is Base Class for microsite configuration backend.
BaseMicrositeTemplateBackend is Base Class for the microsite template backend.
"""

from __future__ import absolute_import

import abc
import os.path
import threading

from django.conf import settings

from util.url import strip_port_from_host


# pylint: disable=unused-argument
class AbstractBaseMicrositeBackend(object):
    """
    Abstract Base Class for the microsite backends.
    """
    __metaclass__ = abc.ABCMeta

    def __init__(self, **kwargs):
        pass

    @abc.abstractmethod
    def set_config_by_domain(self, domain):
        """
        For a given request domain, find a match in our microsite configuration
        and make it available to the complete django request process
        """
        raise NotImplementedError()

    @abc.abstractmethod
    def get_value(self, val_name, default=None, **kwargs):
        """
        Returns a value associated with the request's microsite, if present
        """
        raise NotImplementedError()

    @abc.abstractmethod
    def get_dict(self, dict_name, default=None, **kwargs):
        """
        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
        """
        raise NotImplementedError()

    @abc.abstractmethod
    def is_request_in_microsite(self):
        """
        This will return True/False if the current request is a request within a microsite
        """
        raise NotImplementedError()

    @abc.abstractmethod
    def has_override_value(self, val_name):
        """
        Returns True/False whether a Microsite has a definition for the
        specified named value
        """
        raise NotImplementedError()

    @abc.abstractmethod
    def get_all_config(self):
        """
        This returns a set of orgs that are considered within all microsites.
        This can be used, for example, to do filtering
        """
        raise NotImplementedError()

    @abc.abstractmethod
    def get_value_for_org(self, org, val_name, default=None):
        """
        This returns a configuration value for a microsite which has an org_filter that matches
        what is passed in
        """
        raise NotImplementedError()

    @abc.abstractmethod
    def get_all_orgs(self):
        """
        This returns a set of orgs that are considered within a microsite. This can be used,
        for example, to do filtering
        """
        raise NotImplementedError()

    @abc.abstractmethod
    def clear(self):
        """
        Clears out any microsite configuration from the current request/thread
        """
        raise NotImplementedError()


class BaseMicrositeBackend(AbstractBaseMicrositeBackend):
    """
    Base class for Microsite backends.
    """

    def __init__(self, **kwargs):
        super(BaseMicrositeBackend, self).__init__(**kwargs)
        self.current_request_configuration = threading.local()
        self.current_request_configuration.data = {}
        self.current_request_configuration.cache = {}

    def has_configuration_set(self):
        """
        Returns whether there is any Microsite configuration settings
        """
        return getattr(settings, "MICROSITE_CONFIGURATION", False)

    def get_configuration(self):
        """
        Returns the current request's microsite configuration.
        if request's microsite configuration is not present returns empty dict.
        """
        if not hasattr(self.current_request_configuration, 'data'):
            return {}

        return self.current_request_configuration.data

    def get_key_from_cache(self, key):
        """
        Retrieves a key from a cache scoped to the thread
        """
        if hasattr(self.current_request_configuration, 'cache'):
            return self.current_request_configuration.cache.get(key)

    def set_key_to_cache(self, key, value):
        """
        Stores a key value pair in a cache scoped to the thread
        """
        if hasattr(self.current_request_configuration, 'cache'):
            self.current_request_configuration.cache[key] = value

    def set_config_by_domain(self, domain):
        """
        For a given request domain, find a match in our microsite configuration
        and then assign it to the thread local in order to make it available
        to the complete Django request processing
        """
        if not self.has_configuration_set() or not domain:
            return

        for key, value in settings.MICROSITE_CONFIGURATION.items():
            subdomain = value.get('domain_prefix')
            if subdomain and domain.startswith(subdomain):
                self._set_microsite_config(key, subdomain, domain)
                return

        # if no match on subdomain then see if there is a 'default' microsite defined
        # if so, then use that
        if 'default' in settings.MICROSITE_CONFIGURATION:
            self._set_microsite_config('default', subdomain, domain)
            return

    def get_value(self, val_name, default=None, **kwargs):
        """
        Returns a value associated with the request's microsite, if present
        """
        configuration = self.get_configuration()
        return configuration.get(val_name, default)

    def get_dict(self, dict_name, default=None, **kwargs):
        """
        Returns a dictionary product of merging the request's microsite and
        the default value.
        Supports storing a cache of the merged value to improve performance
        """
        cached_dict = self.get_key_from_cache(dict_name)
        if cached_dict:
            return cached_dict

        default = default or {}
        output = default.copy()
        output.update(self.get_value(dict_name, {}))

        self.set_key_to_cache(dict_name, output)
        return output

    def is_request_in_microsite(self):
        """
        This will return if current request is a request within a microsite
        """
        return bool(self.get_configuration())

    def has_override_value(self, val_name):
        """
        Will return True/False whether a Microsite has a definition for the
        specified val_name
        """
        configuration = self.get_configuration()
        return val_name in configuration

    def get_all_config(self):
        """
        This returns all configuration for all microsites
        """
        config = {}

        for key, value in settings.MICROSITE_CONFIGURATION.iteritems():
            config[key] = value

        return config

    def get_value_for_org(self, org, val_name, default=None):
        """
        This returns a configuration value for a microsite which has an org_filter that matches
        what is passed in
        """

        if not self.has_configuration_set():
            return default

        # Filter at the setting file
        for value in settings.MICROSITE_CONFIGURATION.itervalues():
            org_filter = value.get('course_org_filter', None)
            if org_filter == org:
                return value.get(val_name, default)
        return default

    def get_all_orgs(self):
        """
        This returns a set of orgs that are considered within a microsite. This can be used,
        for example, to do filtering
        """
        org_filter_set = set()

        if not self.has_configuration_set():
            return org_filter_set

        # Get the orgs in the db
        for microsite in settings.MICROSITE_CONFIGURATION.itervalues():
            org_filter = microsite.get('course_org_filter')
            if org_filter:
                org_filter_set.add(org_filter)

        return org_filter_set

    def _set_microsite_config(self, microsite_config_key, subdomain, domain):
        """
        Helper internal method to actually find the microsite configuration
        """
        config = settings.MICROSITE_CONFIGURATION[microsite_config_key].copy()
        config['subdomain'] = strip_port_from_host(subdomain)
        config['microsite_config_key'] = microsite_config_key
        config['site_domain'] = strip_port_from_host(domain)

        template_dir = settings.MICROSITE_ROOT_DIR / microsite_config_key / 'templates'
        config['template_dir'] = template_dir
        self.current_request_configuration.data = config

    def clear(self):
        """
        Clears out any microsite configuration from the current request/thread
        """
        self.current_request_configuration.data = {}
        self.current_request_configuration.cache = {}

    def enable_microsites(self, log):
        """
        Configure the paths for the microsites feature
        """
        microsites_root = settings.MICROSITE_ROOT_DIR
        if os.path.isdir(microsites_root):
            settings.STATICFILES_DIRS.insert(0, microsites_root)

            log.info('Loading microsite path at %s', microsites_root)
        else:
            log.error(
                'Error loading %s. Directory does not exist',
                microsites_root
            )

    def enable_microsites_pre_startup(self, log):
        """
        The TEMPLATE_ENGINE directory to search for microsite templates
        in non-mako templates must be loaded before the django startup
        """
        microsites_root = settings.MICROSITE_ROOT_DIR

291
        if self.has_configuration_set():
292
            settings.MAKO_TEMPLATES['main'].insert(0, microsites_root)
293 294 295 296 297 298 299 300 301 302
            settings.DEFAULT_TEMPLATE_ENGINE['DIRS'].append(microsites_root)


class BaseMicrositeTemplateBackend(object):
    """
    Interface for microsite template providers. Base implementation is to use the filesystem.
    When this backend is used templates are first searched in location set in `template_dir`
    configuration of microsite on filesystem.
    """

303
    def get_template_path(self, template_path, **kwargs):
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
        """
        Returns a path (string) to a Mako template, which can either be in
        an override or will just return what is passed in which is expected to be a string
        """

        from microsite_configuration.microsite import get_value as microsite_get_value

        microsite_template_path = microsite_get_value('template_dir', None)
        if not microsite_template_path:
            microsite_template_path = '/'.join([
                settings.MICROSITE_ROOT_DIR,
                microsite_get_value('microsite_config_key', 'default'),
                'templates',
            ])

319
        relative_path = template_path[1:] if template_path.startswith('/') else template_path
320 321 322 323 324 325 326 327
        search_path = os.path.join(microsite_template_path, relative_path)
        if os.path.isfile(search_path):
            path = '/{0}/templates/{1}'.format(
                microsite_get_value('microsite_config_key'),
                relative_path
            )
            return path
        else:
328
            return template_path
329 330 331 332 333 334 335 336 337

    def get_template(self, uri):
        """
        Returns the actual template for the microsite with the specified URI,
        default implementation returns None, which means that the caller framework
        should use default behavior
        """

        return