Files
edx-platform/lms/djangoapps/branding/__init__.py
Calen Pennington 6c596f7c49 Allow courses api to return data incrementally
Prior to this commit, the course api (/api/courses/v1/courses/)
performed all the work necessary to return all courses available
to the user, and then only actually returned on page's worth of those
courses.

With this change, the api now does the work incrementally, computing
only the data needed to fetch the courses up to and including the page
being returned. This still increases approximately linearly as
the page number accessed being increases, but should be more cache-friendly.
One side effect of this is that the max_page reported by pagination
will be an overestimate (it will include pages that are removed due
to a users access restrictions).

This change also changes the sort-order of courses being returned by the
course_api. By sorting by course-id, rather than course-number, we
can sort in the database, rather than in Python, and defer loading data
from the end of the list until it is requested.

REVMI-90
2019-02-06 10:11:15 -05:00

74 lines
2.6 KiB
Python

"""
EdX Branding package.
Provides a way to retrieve "branded" parts of the site.
This module provides functions to retrieve basic branded parts
such as the site visible courses, university name and logo.
"""
from django.conf import settings
from opaque_keys.edx.keys import CourseKey
from openedx.core.djangoapps.site_configuration import helpers as configuration_helpers
def get_visible_courses(org=None, filter_=None):
"""
Yield the CourseOverviews that should be visible in this branded
instance.
Arguments:
org (string): Optional parameter that allows case-insensitive
filtering by organization.
filter_ (dict): Optional parameter that allows custom filtering by
fields on the course.
"""
# Import is placed here to avoid model import at project startup.
from openedx.core.djangoapps.content.course_overviews.models import CourseOverview
current_site_orgs = configuration_helpers.get_current_site_orgs()
courses = CourseOverview.objects.none()
if org:
# Check the current site's orgs to make sure the org's courses should be displayed
if not current_site_orgs or org in current_site_orgs:
courses = CourseOverview.get_all_courses(orgs=[org], filter_=filter_)
elif current_site_orgs:
# Only display courses that should be displayed on this site
courses = CourseOverview.get_all_courses(orgs=current_site_orgs, filter_=filter_)
else:
courses = CourseOverview.get_all_courses(filter_=filter_)
courses = courses.order_by('id')
# Filtering can stop here.
if current_site_orgs:
return courses
# See if we have filtered course listings in this domain
filtered_visible_ids = None
# this is legacy format, which also handle dev case, which should not filter
subdomain = configuration_helpers.get_value('subdomain', 'default')
if hasattr(settings, 'COURSE_LISTINGS') and subdomain in settings.COURSE_LISTINGS and not settings.DEBUG:
filtered_visible_ids = frozenset(
[CourseKey.from_string(c) for c in settings.COURSE_LISTINGS[subdomain]]
)
if filtered_visible_ids:
return courses.filter(id__in=filtered_visible_ids)
else:
# Filter out any courses based on current org, to avoid leaking these.
orgs = configuration_helpers.get_all_orgs()
return courses.exclude(org__in=orgs)
def get_university_for_request():
"""
Return the university name specified for the domain, or None
if no university was specified
"""
return configuration_helpers.get_value('university')