Add support for multi-org sites

WL-926
This commit is contained in:
Douglas Hall
2017-02-04 15:12:35 -05:00
parent 6626725781
commit 58f0154ee2
12 changed files with 77 additions and 71 deletions

View File

@@ -446,12 +446,12 @@ class CourseOverview(TimeStampedModel):
return course_overviews
@classmethod
def get_all_courses(cls, org=None, filter_=None):
def get_all_courses(cls, orgs=None, filter_=None):
"""
Returns all CourseOverview objects in the database.
Arguments:
org (string): Optional parameter that allows case-insensitive
orgs (list[string]): Optional parameter that allows case-insensitive
filtering by organization.
filter_ (dict): Optional parameter that allows custom filtering.
"""
@@ -460,11 +460,11 @@ class CourseOverview(TimeStampedModel):
# created. For tests using CourseFactory, use emit_signals=True.
course_overviews = CourseOverview.objects.all()
if org:
if orgs:
# In rare cases, courses belonging to the same org may be accidentally assigned
# an org code with a different casing (e.g., Harvardx as opposed to HarvardX).
# Case-insensitive exact matching allows us to deal with this kind of dirty data.
course_overviews = course_overviews.filter(org__iexact=org)
# Case-insensitive matching allows us to deal with this kind of dirty data.
course_overviews = course_overviews.filter(org__iregex=r'(' + '|'.join(orgs) + ')')
if filter_:
course_overviews = course_overviews.filter(**filter_)

View File

@@ -470,7 +470,7 @@ class CourseOverviewTestCase(ModuleStoreTestCase):
def test_get_all_courses_by_org(self):
org_courses = [] # list of lists of courses
for index in range(2):
for index in range(3):
org_courses.append([
CourseFactory.create(org='test_org_' + unicode(index), emit_signals=True)
for __ in range(3)
@@ -478,18 +478,18 @@ class CourseOverviewTestCase(ModuleStoreTestCase):
self.assertEqual(
{c.id for c in CourseOverview.get_all_courses()},
{c.id for c in org_courses[0] + org_courses[1]},
{c.id for c in org_courses[0] + org_courses[1] + org_courses[2]},
)
self.assertEqual(
{c.id for c in CourseOverview.get_all_courses(org='test_org_1')},
{c.id for c in org_courses[1]},
{c.id for c in CourseOverview.get_all_courses(orgs=['test_org_1', 'test_org_2'])},
{c.id for c in org_courses[1] + org_courses[2]},
)
# Test case-insensitivity.
self.assertEqual(
{c.id for c in CourseOverview.get_all_courses(org='TEST_ORG_1')},
{c.id for c in org_courses[1]},
{c.id for c in CourseOverview.get_all_courses(orgs=['TEST_ORG_1', 'TEST_ORG_2'])},
{c.id for c in org_courses[1] + org_courses[2]},
)
def test_get_all_courses_by_mobile_available(self):

View File

@@ -188,6 +188,21 @@ def get_value_for_org(org, val_name, default=None):
return microsite.get_value_for_org(org, val_name, default)
def get_current_site_orgs():
"""
This returns the orgs configured in site configuration or microsite configuration for the current site.
Returns:
list: A list of organization names.
"""
course_org_filter = get_value('course_org_filter')
# Make sure we have a list
if course_org_filter and not isinstance(course_org_filter, list):
course_org_filter = [course_org_filter]
return course_org_filter
def get_all_orgs():
"""
This returns all of the orgs that are considered in site configurations or microsite configuration,

View File

@@ -77,8 +77,8 @@ class SiteConfiguration(models.Model):
Configuration value for the given key.
"""
for configuration in cls.objects.filter(values__contains=org, enabled=True).all():
org_filter = configuration.get_value('course_org_filter', None)
if org_filter == org:
course_org_filter = configuration.get_value('course_org_filter', None)
if org in course_org_filter:
return configuration.get_value(name, default)
return default
@@ -94,9 +94,10 @@ class SiteConfiguration(models.Model):
org_filter_set = set()
for configuration in cls.objects.filter(values__contains='course_org_filter', enabled=True).all():
org_filter = configuration.get_value('course_org_filter', None)
if org_filter:
org_filter_set.add(org_filter)
course_org_filter = configuration.get_value('course_org_filter', [])
if not isinstance(course_org_filter, list):
course_org_filter = [course_org_filter]
org_filter_set.update(course_org_filter)
return org_filter_set
@classmethod

View File

@@ -36,6 +36,10 @@ test_config = { # pylint: disable=invalid-name
},
}
test_config_multi_org = { # pylint: disable=invalid-name
"course_org_filter": ["FooOrg", "BarOrg", "FooBarOrg"]
}
class TestHelpers(TestCase):
"""
@@ -189,3 +193,11 @@ class TestHelpers(TestCase):
list(configuration_helpers.get_all_orgs()),
test_orgs,
)
@with_site_configuration(configuration=test_config_multi_org)
def test_get_current_site_orgs(self):
test_orgs = test_config_multi_org['course_org_filter']
self.assertItemsEqual(
list(configuration_helpers.get_current_site_orgs()),
test_orgs
)