Commit 344e13b1 by Robert Raposa Committed by GitHub

Merge pull request #16179 from edx/robrap/refactor-site-enrollment-filter

Refactor site course enrollment filtering.
parents 0702df51 112938e4
...@@ -275,15 +275,14 @@ def reverification_info(statuses): ...@@ -275,15 +275,14 @@ def reverification_info(statuses):
return reverifications return reverifications
def get_course_enrollments(user, orgs_to_include, orgs_to_exclude): def get_course_enrollments(user, org_whitelist, org_blacklist):
""" """
Given a user, return a filtered set of his or her course enrollments. Given a user, return a filtered set of his or her course enrollments.
Arguments: Arguments:
user (User): the user in question. user (User): the user in question.
orgs_to_include (list[str]): If not None, ONLY courses of these orgs will be returned. org_whitelist (list[str]): If not None, ONLY courses of these orgs will be returned.
orgs_to_exclude (list[str]): If orgs_to_include is not None, this org_blacklist (list[str]): Courses of these orgs will be excluded.
argument is ignored. Else, courses of this org will be excluded.
Returns: Returns:
generator[CourseEnrollment]: a sequence of enrollments to be displayed generator[CourseEnrollment]: a sequence of enrollments to be displayed
...@@ -301,12 +300,12 @@ def get_course_enrollments(user, orgs_to_include, orgs_to_exclude): ...@@ -301,12 +300,12 @@ def get_course_enrollments(user, orgs_to_include, orgs_to_exclude):
) )
continue continue
# Filter out anything that is not attributed to the orgs to include. # Filter out anything that is not in the whitelist.
if orgs_to_include and course_overview.location.org not in orgs_to_include: if org_whitelist and course_overview.location.org not in org_whitelist:
continue continue
# Conversely, filter out any enrollments with courses attributed to current ORG. # Conversely, filter out any enrollments in the blacklist.
elif course_overview.location.org in orgs_to_exclude: elif org_blacklist and course_overview.location.org in org_blacklist:
continue continue
# Else, include the enrollment. # Else, include the enrollment.
...@@ -314,6 +313,31 @@ def get_course_enrollments(user, orgs_to_include, orgs_to_exclude): ...@@ -314,6 +313,31 @@ def get_course_enrollments(user, orgs_to_include, orgs_to_exclude):
yield enrollment yield enrollment
def get_org_black_and_whitelist_for_site(user):
"""
Returns the org blacklist and whitelist for the current site.
Returns:
(org_whitelist, org_blacklist): A tuple of lists of orgs that serve as
either a blacklist or a whitelist of orgs for the current site. The
whitelist takes precedence, and the blacklist is used if the
whitelist is None.
"""
# Default blacklist is empty.
org_blacklist = None
# Whitelist the orgs configured for the current site. Each site outside
# of edx.org has a list of orgs associated with its configuration.
org_whitelist = configuration_helpers.get_current_site_orgs()
if not org_whitelist:
# If there is no whitelist, the blacklist will include all orgs that
# have been configured for any other sites. This applies to edx.org,
# where it is easier to blacklist all other orgs.
org_blacklist = configuration_helpers.get_all_orgs()
return (org_whitelist, org_blacklist)
def _cert_info(user, course_overview, cert_status, course_mode): # pylint: disable=unused-argument def _cert_info(user, course_overview, cert_status, course_mode): # pylint: disable=unused-argument
""" """
Implements the logic for cert_info -- split out for testing. Implements the logic for cert_info -- split out for testing.
...@@ -647,21 +671,9 @@ def dashboard(request): ...@@ -647,21 +671,9 @@ def dashboard(request):
'ACTIVATION_EMAIL_SUPPORT_LINK', settings.ACTIVATION_EMAIL_SUPPORT_LINK 'ACTIVATION_EMAIL_SUPPORT_LINK', settings.ACTIVATION_EMAIL_SUPPORT_LINK
) or settings.SUPPORT_SITE_LINK ) or settings.SUPPORT_SITE_LINK
# Let's filter out any courses in an "org" that has been declared to be # get the org whitelist or the org blacklist for the current site
# in a configuration site_org_whitelist, site_org_blacklist = get_org_black_and_whitelist_for_site(user)
org_filter_out_set = configuration_helpers.get_all_orgs() course_enrollments = list(get_course_enrollments(user, site_org_whitelist, site_org_blacklist))
# Remove current site orgs from the "filter out" list, if applicable.
# We want to filter and only show enrollments for courses within
# the organizations defined in configuration for the current site.
course_org_filter = configuration_helpers.get_current_site_orgs()
if course_org_filter:
org_filter_out_set = org_filter_out_set - set(course_org_filter)
# Build our (course, enrollment) list for the user, but ignore any courses that no
# longer exist (because the course IDs have changed). Still, we don't delete those
# enrollments, because it could have been a data push snafu.
course_enrollments = list(get_course_enrollments(user, course_org_filter, org_filter_out_set))
# Record how many courses there are so that we can get a better # Record how many courses there are so that we can get a better
# understanding of usage patterns on prod. # understanding of usage patterns on prod.
...@@ -808,7 +820,7 @@ def dashboard(request): ...@@ -808,7 +820,7 @@ def dashboard(request):
denied_banner = any(item.display for item in reverifications["denied"]) denied_banner = any(item.display for item in reverifications["denied"])
# Populate the Order History for the side-bar. # Populate the Order History for the side-bar.
order_history_list = order_history(user, course_org_filter=course_org_filter, org_filter_out_set=org_filter_out_set) order_history_list = order_history(user, course_org_filter=site_org_whitelist, org_filter_out_set=site_org_blacklist)
# get list of courses having pre-requisites yet to be completed # get list of courses having pre-requisites yet to be completed
courses_having_prerequisites = frozenset( courses_having_prerequisites = frozenset(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment