def test_get_courses_by_org(self): """ Verify that org filtering performs as expected, and that an empty result is returned if the org passed by the caller does not match the designated org. """ primary = 'primary' alternate = 'alternate' def _fake_get_value(value, default=None): """Used to stub out site_configuration.helpers.get_value().""" if value == 'course_org_filter': return alternate return default user = UserFactory.create() # Pass `emit_signals=True` so that these courses are cached with CourseOverviews. primary_course = CourseFactory.create(org=primary, emit_signals=True) alternate_course = CourseFactory.create(org=alternate, emit_signals=True) self.assertNotEqual(primary_course.org, alternate_course.org) unfiltered_courses = get_courses(user) for org in [primary_course.org, alternate_course.org]: self.assertTrue( any(course.org == org for course in unfiltered_courses)) filtered_courses = get_courses(user, org=primary) self.assertTrue( all(course.org == primary_course.org for course in filtered_courses)) with mock.patch( 'openedx.core.djangoapps.site_configuration.helpers.get_value', autospec=True, ) as mock_get_value: mock_get_value.side_effect = _fake_get_value # Request filtering for an org distinct from the designated org. no_courses = get_courses(user, org=primary) self.assertEqual(list(no_courses), []) # Request filtering for an org matching the designated org. site_courses = get_courses(user, org=alternate) self.assertTrue( all(course.org == alternate_course.org for course in site_courses))
def list_courses(request, username, org=None, filter_=None, search_term=None): """ Yield all available courses. The courses returned are all be visible to the user identified by `username` and the logged in user should have permission to view courses available to that user. Arguments: request (HTTPRequest): Used to identify the logged-in user and to instantiate the course module to retrieve the course about description username (string): The name of the user the logged-in user would like to be identified as Keyword Arguments: org (string): If specified, visible `CourseOverview` objects are filtered such that only those belonging to the organization with the provided org code (e.g., "HarvardX") are returned. Case-insensitive. filter_ (dict): If specified, visible `CourseOverview` objects are filtered by the given key-value pairs. search_term (string): Search term to filter courses (used by ElasticSearch). Return value: Yield `CourseOverview` objects representing the collection of courses. """ user = get_effective_user(request.user, username) course_qs = get_courses(user, org=org, filter_=filter_) course_qs = _filter_by_search(course_qs, search_term) return course_qs
def list_courses(request, username): """ Return a list of available courses. The courses returned are all be visible to the user identified by `username` and the logged in user should have permission to view courses available to that user. Arguments: request (HTTPRequest): Used to identify the logged-in user and to instantiate the course module to retrieve the course about description username (string): The name of the user the logged-in user would like to be identified as Return value: A CourseSerializer object representing the collection of courses. """ user = get_effective_user(request.user, username) courses = get_courses(user) return CourseSerializer(courses, context={ 'request': request }, many=True).data
def list_courses(request, username, org=None, filter_=None): """ Return a list of available courses. The courses returned are all be visible to the user identified by `username` and the logged in user should have permission to view courses available to that user. Arguments: request (HTTPRequest): Used to identify the logged-in user and to instantiate the course module to retrieve the course about description username (string): The name of the user the logged-in user would like to be identified as Keyword Arguments: org (string): If specified, visible `CourseOverview` objects are filtered such that only those belonging to the organization with the provided org code (e.g., "HarvardX") are returned. Case-insensitive. filter_ (dict): If specified, visible `CourseOverview` objects are filtered by the given key-value pairs. Return value: List of `CourseOverview` objects representing the collection of courses. """ user = get_effective_user(request.user, username) return get_courses(user, org=org, filter_=filter_)
def courses_custom(request): """ Render "find courses" page. The course selection work is done in courseware.courses. """ courses_list = [] programs_list = [] course_discovery_meanings = getattr(settings, 'COURSE_DISCOVERY_MEANINGS', {}) if not settings.FEATURES.get('ENABLE_COURSE_DISCOVERY'): current_date = datetime.now(utc) courses_list = get_courses( request.user, filter_={'end__gte': current_date.date() - timedelta(days=1)}) if configuration_helpers.get_value( "ENABLE_COURSE_SORTING_BY_START_DATE", settings.FEATURES["ENABLE_COURSE_SORTING_BY_START_DATE"]): courses_list = sort_by_start_date(courses_list) else: courses_list = sort_by_announcement(courses_list) # Getting all the programs from course-catalog service. The programs_list is being added to the context but it's # not being used currently in courseware/courses.html. To use this list, you need to create a custom theme that # overrides courses.html. The modifications to courses.html to display the programs will be done after the support # for edx-pattern-library is added. if configuration_helpers.get_value( "DISPLAY_PROGRAMS_ON_MARKETING_PAGES", settings.FEATURES.get("DISPLAY_PROGRAMS_ON_MARKETING_PAGES")): programs_list = get_programs_data(request.user) if request.user.is_authenticated(): add_tag_to_enrolled_courses(request.user, courses_list) for course in courses_list: course_key = SlashSeparatedCourseKey.from_deprecated_string( course.id.to_deprecated_string()) with modulestore().bulk_operations(course_key): if has_access(request.user, 'load', course): access_link = get_last_accessed_courseware( get_course_by_id(course_key, 0), request, request.user) first_chapter_url, first_section = get_course_related_keys( request, get_course_by_id(course_key, 0)) first_target = reverse('courseware_section', args=[ course.id.to_deprecated_string(), first_chapter_url, first_section ]) course.course_target = access_link if access_link != None else first_target else: course.course_target = '/courses/' + course.id.to_deprecated_string( ) return render_to_response( "courseware/courses.html", { 'courses': courses_list, 'course_discovery_meanings': course_discovery_meanings, 'programs_list': programs_list })
def index(request, extra_context=None, user=AnonymousUser()): """ Render the edX main page. extra_context is used to allow immediate display of certain modal windows, eg signup. """ if extra_context is None: extra_context = {} courses = get_courses(user) if configuration_helpers.get_value( "ENABLE_COURSE_SORTING_BY_START_DATE", settings.FEATURES["ENABLE_COURSE_SORTING_BY_START_DATE"], ): courses = sort_by_start_date(courses) else: courses = sort_by_announcement(courses) context = {'courses': courses} context['homepage_overlay_html'] = configuration_helpers.get_value( 'homepage_overlay_html') # This appears to be an unused context parameter, at least for the master templates... context['show_partners'] = configuration_helpers.get_value( 'show_partners', True) # TO DISPLAY A YOUTUBE WELCOME VIDEO # 1) Change False to True context['show_homepage_promo_video'] = configuration_helpers.get_value( 'show_homepage_promo_video', False) # Maximum number of courses to display on the homepage. context['homepage_course_max'] = configuration_helpers.get_value( 'HOMEPAGE_COURSE_MAX', settings.HOMEPAGE_COURSE_MAX) # 2) Add your video's YouTube ID (11 chars, eg "123456789xX"), or specify via site configuration # Note: This value should be moved into a configuration setting and plumbed-through to the # context via the site configuration workflow, versus living here youtube_video_id = configuration_helpers.get_value( 'homepage_promo_video_youtube_id', "your-youtube-id") context['homepage_promo_video_youtube_id'] = youtube_video_id # allow for theme override of the courses list context['courses_list'] = theming_helpers.get_template_path( 'courses_list.html') # Insert additional context for use in the template context.update(extra_context) # Add marketable programs to the context. context['programs_list'] = get_programs_with_type(request.site, include_hidden=False) return render_to_response('index.html', context)
def test_get_courses_with_filter(self): """ Verify that filtering performs as expected. """ user = UserFactory.create() mobile_course = CourseFactory.create(emit_signals=True) non_mobile_course =\ CourseFactory.create(mobile_available=False, emit_signals=True) test_cases = ( (None, {non_mobile_course.id, mobile_course.id}), (dict(mobile_available=True), {mobile_course.id}), (dict(mobile_available=False), {non_mobile_course.id}), ) for filter_, expected_courses in test_cases: assert {course.id for course in get_courses(user, filter_=filter_)} ==\ expected_courses, f'testing get_courses with filter_={filter_}'
def list_courses(request, username): """ Return a list of available courses. The courses returned are all be visible to the user identified by `username` and the logged in user should have permission to view courses available to that user. Arguments: request (HTTPRequest): Used to identify the logged-in user and to instantiate the course module to retrieve the course about description username (string): The name of the user the logged-in user would like to be identified as Return value: List of `CourseDescriptor` objects representing the collection of courses. """ user = get_effective_user(request.user, username) return get_courses(user)
def index(request, extra_context=None, user=AnonymousUser()): """ Render the edX main page. extra_context is used to allow immediate display of certain modal windows, eg signup. """ if extra_context is None: extra_context = {} courses = get_courses(user) if configuration_helpers.get_value( "ENABLE_COURSE_SORTING_BY_START_DATE", settings.FEATURES["ENABLE_COURSE_SORTING_BY_START_DATE"], ): courses = sort_by_start_date(courses) else: courses = sort_by_announcement(courses) context = {'courses': courses} context['homepage_overlay_html'] = configuration_helpers.get_value( 'homepage_overlay_html') # This appears to be an unused context parameter, at least for the master templates... context['show_partners'] = configuration_helpers.get_value( 'show_partners', True) # TO DISPLAY A YOUTUBE WELCOME VIDEO # 1) Change False to True context['show_homepage_promo_video'] = configuration_helpers.get_value( 'show_homepage_promo_video', False) # Maximum number of courses to display on the homepage. context['homepage_course_max'] = configuration_helpers.get_value( 'HOMEPAGE_COURSE_MAX', settings.HOMEPAGE_COURSE_MAX) # 2) Add your video's YouTube ID (11 chars, eg "123456789xX"), or specify via site configuration # Note: This value should be moved into a configuration setting and plumbed-through to the # context via the site configuration workflow, versus living here youtube_video_id = configuration_helpers.get_value( 'homepage_promo_video_youtube_id', "your-youtube-id") context['homepage_promo_video_youtube_id'] = youtube_video_id # allow for theme override of the courses list context['courses_list'] = theming_helpers.get_template_path( 'courses_list.html') # Insert additional context for use in the template context.update(extra_context) # Add marketable programs to the context. context['programs_list'] = get_programs_with_type(request.site, include_hidden=False) # Added by Mahendra from lms.djangoapps.specialization.models import categories from lms.djangoapps.course_extrainfo.models import course_extrainfo today = datetime.datetime.now(UTC).date() today_date_time = datetime.datetime.now(UTC) context["category_course_count"] = (course_extrainfo.objects.exclude( category="").values("category").annotate( ccount=Count("category")).filter( ccount__gte=1).order_by("-ccount")[:8]) cat_course_count = (course_extrainfo.objects.exclude( category="").values("category").annotate( ccount=Count("category")).filter( ccount__gte=1).order_by("-ccount")[:8]) categoryid = [] for catid in cat_course_count: categoryid.append(catid["category"]) context["categories_list"] = categories.objects.filter( pk__in=categoryid).values() # for lectures list lectures = course_extrainfo.objects.filter(course_type=2).values() cid = [] for courseid in lectures: course_id = CourseKey.from_string(courseid["course_id"]) cid.append(course_id) context["clectures"] = (CourseOverview.objects.all().filter( pk__in=cid, start__gte=today).exclude(catalog_visibility="none").order_by("start")) # for courses list index_courses = course_extrainfo.objects.filter(course_type=1).values() index_cid = [] for index_course in index_courses: index_course_id = CourseKey.from_string(index_course["course_id"]) index_cid.append(index_course_id) # log.info(u'course-id %s',index_course_id) # below query commented for displaying upcoming courses context["index_upcoming_courses"] = (CourseOverview.objects.all().filter( pk__in=index_cid, start__gte=today).order_by("start")[::-1]) # query to display all courses context["index_ongoing_courses"] = (CourseOverview.objects.all().filter( pk__in=index_cid, start__lte=today_date_time, end__gte=today_date_time).order_by("start")[::-1]) context["index_all"] = (CourseOverview.objects.all().exclude( catalog_visibility="none").order_by("start")[::-1][:10]) # for upcoming case studies index_case_studies = course_extrainfo.objects.filter( course_type=3).values() index_csid = [] for index_casestudy in index_case_studies: index_casestudy_id = CourseKey.from_string( index_casestudy["course_id"]) index_csid.append(index_casestudy_id) context["index_upcoming_case_studies"] = ( CourseOverview.objects.all().filter( pk__in=index_csid).order_by("start")[::-1]) return render_to_response('index.html', context)