Skip to content

Commit c4401fd

Browse files
author
chrisndodge
committed
Merge pull request #2553 from edx/cdodge/microsite-cleanup
Address Microsite PR feedback - separate out middleware from microsite logic, shor...
2 parents 52b8d0c + aa43689 commit c4401fd

File tree

39 files changed

+391
-342
lines changed

39 files changed

+391
-342
lines changed

cms/djangoapps/contentstore/views/course.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
from student.roles import CourseInstructorRole, CourseStaffRole, CourseCreatorRole, GlobalStaff
5656
from student import auth
5757

58-
from microsite_configuration.middleware import MicrositeConfiguration
58+
from microsite_configuration import microsite
5959

6060
__all__ = ['course_info_handler', 'course_handler', 'course_info_update_handler',
6161
'settings_handler',
@@ -535,7 +535,7 @@ def settings_handler(request, tag=None, package_id=None, branch=None, version_gu
535535

536536
# see if the ORG of this course can be attributed to a 'Microsite'. In that case, the
537537
# course about page should be editable in Studio
538-
about_page_editable = not MicrositeConfiguration.get_microsite_configuration_value_for_org(
538+
about_page_editable = not microsite.get_value_for_org(
539539
course_module.location.org,
540540
'ENABLE_MKTG_SITE',
541541
settings.FEATURES.get('ENABLE_MKTG_SITE', False)

cms/djangoapps/contentstore/views/public.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from edxmako.shortcuts import render_to_response
1111

1212
from external_auth.views import ssl_login_shortcut, ssl_get_cert_from_request
13-
from microsite_configuration.middleware import MicrositeConfiguration
13+
from microsite_configuration import microsite
1414

1515
__all__ = ['signup', 'login_page', 'howitworks']
1616

@@ -49,7 +49,7 @@ def login_page(request):
4949
{
5050
'csrf': csrf_token,
5151
'forgot_password_link': "//{base}/login#forgot-password-modal".format(base=settings.LMS_BASE),
52-
'platform_name': MicrositeConfiguration.get_microsite_configuration_value('platform_name', settings.PLATFORM_NAME),
52+
'platform_name': microsite.get_value('platform_name', settings.PLATFORM_NAME),
5353
}
5454
)
5555

cms/envs/aws.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -239,16 +239,8 @@
239239
MAX_FAILED_LOGIN_ATTEMPTS_ALLOWED = ENV_TOKENS.get("MAX_FAILED_LOGIN_ATTEMPTS_ALLOWED", 5)
240240
MAX_FAILED_LOGIN_ATTEMPTS_LOCKOUT_PERIOD_SECS = ENV_TOKENS.get("MAX_FAILED_LOGIN_ATTEMPTS_LOCKOUT_PERIOD_SECS", 15 * 60)
241241

242-
243242
MICROSITE_CONFIGURATION = ENV_TOKENS.get('MICROSITE_CONFIGURATION', {})
244-
MICROSITE_ROOT_DIR = ENV_TOKENS.get('MICROSITE_ROOT_DIR')
245-
if len(MICROSITE_CONFIGURATION.keys()) > 0:
246-
enable_microsites(
247-
MICROSITE_CONFIGURATION,
248-
SUBDOMAIN_BRANDING,
249-
VIRTUAL_UNIVERSITIES,
250-
microsites_root=path(MICROSITE_ROOT_DIR)
251-
)
243+
MICROSITE_ROOT_DIR = path(ENV_TOKENS.get('MICROSITE_ROOT_DIR', ''))
252244

253245
#### PASSWORD POLICY SETTINGS #####
254246
PASSWORD_MIN_LENGTH = ENV_TOKENS.get("PASSWORD_MIN_LENGTH")

cms/envs/common.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import sys
2828
import lms.envs.common
2929
from lms.envs.common import (
30-
USE_TZ, TECH_SUPPORT_EMAIL, PLATFORM_NAME, BUGS_EMAIL, DOC_STORE_CONFIG, enable_microsites, ALL_LANGUAGES
30+
USE_TZ, TECH_SUPPORT_EMAIL, PLATFORM_NAME, BUGS_EMAIL, DOC_STORE_CONFIG, ALL_LANGUAGES
3131
)
3232
from path import path
3333

@@ -84,6 +84,9 @@
8484

8585
# Toggles embargo functionality
8686
'EMBARGO': False,
87+
88+
# Turn on/off Microsites feature
89+
'USE_MICROSITES': False,
8790
}
8891
ENABLE_JASMINE = False
8992

cms/envs/microsite_test.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66
# pylint: disable=W0401, W0614
77

88
from .dev import *
9-
from .dev import SUBDOMAIN_BRANDING, VIRTUAL_UNIVERSITIES
109

1110
MICROSITE_NAMES = ['openedx']
1211
MICROSITE_CONFIGURATION = {}
13-
14-
if MICROSITE_NAMES and len(MICROSITE_NAMES) > 0:
15-
enable_microsites(MICROSITE_NAMES, MICROSITE_CONFIGURATION, SUBDOMAIN_BRANDING, VIRTUAL_UNIVERSITIES)
12+
FEATURES['USE_MICROSITES'] = True

cms/envs/test.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,31 @@
199199

200200
# Toggles embargo on for testing
201201
FEATURES['EMBARGO'] = True
202+
203+
# set up some testing for microsites
204+
MICROSITE_CONFIGURATION = {
205+
"test_microsite": {
206+
"domain_prefix": "testmicrosite",
207+
"university": "test_microsite",
208+
"platform_name": "Test Microsite",
209+
"logo_image_url": "test_microsite/images/header-logo.png",
210+
"email_from_address": "[email protected]",
211+
"payment_support_email": "[email protected]",
212+
"ENABLE_MKTG_SITE": False,
213+
"SITE_NAME": "test_microsite.localhost",
214+
"course_org_filter": "TestMicrositeX",
215+
"course_about_show_social_links": False,
216+
"css_overrides_file": "test_microsite/css/test_microsite.css",
217+
"show_partners": False,
218+
"show_homepage_promo_video": False,
219+
"course_index_overlay_text": "This is a Test Microsite Overlay Text.",
220+
"course_index_overlay_logo_file": "test_microsite/images/header-logo.png",
221+
"homepage_overlay_html": "<h1>This is a Test Microsite Overlay HTML</h1>"
222+
},
223+
"default": {
224+
"university": "default_university",
225+
"domain_prefix": "www",
226+
}
227+
}
228+
MICROSITE_ROOT_DIR = COMMON_ROOT / 'test' / 'test_microsites'
229+
FEATURES['USE_MICROSITES'] = True

common/djangoapps/edxmako/shortcuts.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from django.http import HttpResponse
1717
import logging
1818

19-
from microsite_configuration.middleware import MicrositeConfiguration
19+
from microsite_configuration import microsite
2020

2121
from edxmako import lookup_template
2222
import edxmako.middleware
@@ -37,7 +37,7 @@ def marketing_link(name):
3737
# link_map maps URLs from the marketing site to the old equivalent on
3838
# the Django site
3939
link_map = settings.MKTG_URL_LINK_MAP
40-
enable_mktg_site = MicrositeConfiguration.get_microsite_configuration_value(
40+
enable_mktg_site = microsite.get_value(
4141
'ENABLE_MKTG_SITE',
4242
settings.FEATURES.get('ENABLE_MKTG_SITE', False)
4343
)
@@ -80,7 +80,7 @@ def marketing_link_context_processor(request):
8080
def render_to_string(template_name, dictionary, context=None, namespace='main'):
8181

8282
# see if there is an override template defined in the microsite
83-
template_name = MicrositeConfiguration.get_microsite_template_path(template_name)
83+
template_name = microsite.get_template_path(template_name)
8484

8585
context_instance = Context(dictionary)
8686
# add dictionary to context_instance
@@ -111,7 +111,7 @@ def render_to_response(template_name, dictionary=None, context_instance=None, na
111111
"""
112112

113113
# see if there is an override template defined in the microsite
114-
template_name = MicrositeConfiguration.get_microsite_template_path(template_name)
114+
template_name = microsite.get_template_path(template_name)
115115

116116
dictionary = dictionary or {}
117117
return HttpResponse(render_to_string(template_name, dictionary, context_instance, namespace), **kwargs)

common/djangoapps/edxmako/startup.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
def run():
99
"""
1010
Setup mako lookup directories.
11+
12+
IMPORTANT: This method can be called multiple times during application startup. Any changes to this method
13+
must be safe for multiple callers during startup phase.
1114
"""
1215
template_locations = settings.MAKO_TEMPLATES
1316
for namespace, directories in template_locations.items():
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
"""
2+
This file implements the Microsite support for the Open edX platform.
3+
A microsite enables the following features:
4+
5+
1) Mapping of sub-domain name to a 'brand', e.g. foo-university.edx.org
6+
2) Present a landing page with a listing of courses that are specific to the 'brand'
7+
3) Ability to swap out some branding elements in the website
8+
"""
9+
import threading
10+
import os.path
11+
12+
from django.conf import settings
13+
14+
CURRENT_REQUEST_CONFIGURATION = threading.local()
15+
CURRENT_REQUEST_CONFIGURATION.data = {}
16+
17+
18+
def has_configuration_set():
19+
"""
20+
Returns whether there is any Microsite configuration settings
21+
"""
22+
return getattr(settings, "MICROSITE_CONFIGURATION", False)
23+
24+
25+
def get_configuration():
26+
"""
27+
Returns the current request's microsite configuration
28+
"""
29+
if not hasattr(CURRENT_REQUEST_CONFIGURATION, 'data'):
30+
return {}
31+
32+
return CURRENT_REQUEST_CONFIGURATION.data
33+
34+
35+
def is_request_in_microsite():
36+
"""
37+
This will return if current request is a request within a microsite
38+
"""
39+
return get_configuration()
40+
41+
42+
def get_value(val_name, default=None):
43+
"""
44+
Returns a value associated with the request's microsite, if present
45+
"""
46+
configuration = get_configuration()
47+
return configuration.get(val_name, default)
48+
49+
50+
def get_template_path(relative_path):
51+
"""
52+
Returns a path (string) to a Mako template, which can either be in
53+
a microsite directory (as an override) or will just return what is passed in which is
54+
expected to be a string
55+
"""
56+
57+
if not is_request_in_microsite():
58+
return relative_path
59+
60+
microsite_template_path = str(get_value('template_dir'))
61+
62+
if microsite_template_path:
63+
search_path = os.path.join(microsite_template_path, relative_path)
64+
65+
if os.path.isfile(search_path):
66+
path = '{0}/templates/{1}'.format(
67+
get_value('microsite_name'),
68+
relative_path
69+
)
70+
return path
71+
72+
return relative_path
73+
74+
75+
def get_value_for_org(org, val_name, default=None):
76+
"""
77+
This returns a configuration value for a microsite which has an org_filter that matches
78+
what is passed in
79+
"""
80+
if not has_configuration_set():
81+
return default
82+
83+
for value in settings.MICROSITE_CONFIGURATION.values():
84+
org_filter = value.get('course_org_filter', None)
85+
if org_filter == org:
86+
return value.get(val_name, default)
87+
return default
88+
89+
90+
def get_all_orgs():
91+
"""
92+
This returns a set of orgs that are considered within a microsite. This can be used,
93+
for example, to do filtering
94+
"""
95+
org_filter_set = set()
96+
if not has_configuration_set():
97+
return org_filter_set
98+
99+
for value in settings.MICROSITE_CONFIGURATION.values():
100+
org_filter = value.get('course_org_filter')
101+
if org_filter:
102+
org_filter_set.add(org_filter)
103+
104+
return org_filter_set
105+
106+
107+
def clear():
108+
"""
109+
Clears out any microsite configuration from the current request/thread
110+
"""
111+
CURRENT_REQUEST_CONFIGURATION.data = {}
112+
113+
114+
def _set_current_microsite(microsite_config_key, subdomain, domain):
115+
"""
116+
Helper internal method to actually put a microsite on the threadlocal
117+
"""
118+
config = settings.MICROSITE_CONFIGURATION[microsite_config_key].copy()
119+
config['subdomain'] = subdomain
120+
config['site_domain'] = domain
121+
CURRENT_REQUEST_CONFIGURATION.data = config
122+
123+
124+
def set_by_domain(domain):
125+
"""
126+
For a given request domain, find a match in our microsite configuration and then assign
127+
it to the thread local so that it is available throughout the entire
128+
Django request processing
129+
"""
130+
if not has_configuration_set() or not domain:
131+
return
132+
133+
for key, value in settings.MICROSITE_CONFIGURATION.items():
134+
subdomain = value.get('domain_prefix')
135+
if subdomain and domain.startswith(subdomain):
136+
_set_current_microsite(key, subdomain, domain)
137+
return
138+
139+
# if no match on subdomain then see if there is a 'default' microsite defined
140+
# if so, then use that
141+
if 'default' in settings.MICROSITE_CONFIGURATION:
142+
_set_current_microsite('default', subdomain, domain)

0 commit comments

Comments
 (0)