def get_visible_courses(): """ Return the set of CourseDescriptors that should be visible in this branded instance """ filtered_by_org = microsite.get_value('course_org_filter') _courses = modulestore().get_courses(org=filtered_by_org) courses = [c for c in _courses if isinstance(c, CourseDescriptor)] courses = sorted(courses, key=lambda course: course.number) subdomain = microsite.get_value('subdomain', 'default') # See if we have filtered course listings in this domain filtered_visible_ids = None # this is legacy format which is outside of the microsite feature -- also handle dev case, which should not filter if hasattr(settings, 'COURSE_LISTINGS') and subdomain in settings.COURSE_LISTINGS and not settings.DEBUG: filtered_visible_ids = frozenset([SlashSeparatedCourseKey.from_deprecated_string(c) for c in settings.COURSE_LISTINGS[subdomain]]) if filtered_by_org: return [course for course in courses if course.location.org == filtered_by_org] if filtered_visible_ids: return [course for course in courses if course.id in filtered_visible_ids] else: # Let's filter out any courses in an "org" that has been declared to be # in a Microsite org_filter_out_set = microsite.get_all_orgs() return [course for course in courses if course.location.org not in org_filter_out_set]
def process_request(self, request): """ Raise an 404 exception if the course being rendered belongs to an ORG in a microsite, but it is not the current microsite """ path = request.path_info p = re.compile('/courses/{}/'.format(settings.COURSE_ID_PATTERN)) m = p.match(path) # If there is no match, then we are not in a ORG-restricted area if m is None: return None course_id = m.group('course_id') course_key = CourseKey.from_string(course_id) # If the course org is the same as the current microsite org_filter = microsite.get_value('course_org_filter', set([])) if isinstance(org_filter, basestring): org_filter = set([org_filter]) if course_key.org in org_filter: return None # If the course does not belong to an ORG defined in a microsite all_orgs = microsite.get_all_orgs() if course_key.org not in all_orgs: return None # We could log some of the output here for forensic analysis raise Http404
def test_get_all_orgs(self): """ Tests microsite.get_all_orgs works as expected. """ microsite.set_by_domain(self.microsite_subdomain) self.assertEqual( microsite.get_all_orgs(), set(['TestMicrositeX', 'LogistrationX']) ) # if no config is set microsite.clear() with patch('django.conf.settings.MICROSITE_CONFIGURATION', False): self.assertEqual( microsite.get_all_orgs(), set() )
def mobi_forum_course_list(request): ''' get all course what user has talked about ''' nr_transaction = newrelic.agent.current_transaction() user = request.user course_org_filter = microsite.get_value('course_org_filter') # Let's filter out any courses in an "org" that has been declared to be # in a Microsite org_filter_out_set = microsite.get_all_orgs() # remove our current Microsite from the "filter out" list, if applicable if course_org_filter: org_filter_out_set.remove(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_enrollment_pairs = list(get_course_enrollment_pairs(user, course_org_filter, org_filter_out_set)) show_courseware_links_for = frozenset(course.id for course, _enrollment in course_enrollment_pairs if has_access(request.user, course, 'load')) user_info = cc.User.from_django_user(request.user).to_dict() courselist = [] for course_id in show_courseware_links_for: try: user_id = user.id profiled_user = cc.User(id=user_id, course_id=course_id) query_params = { 'page': request.GET.get('page', 1), 'per_page': THREADS_PER_PAGE, # more than threads_per_page to show more activities } threads, page, num_pages = profiled_user.active_threads(query_params) query_params['page'] = page query_params['num_pages'] = num_pages with newrelic.agent.FunctionTrace(nr_transaction, "get_metadata_for_threads"): annotated_content_info = utils.get_metadata_for_threads(course_id, threads, request.user, user_info) if annotated_content_info: courselist.append(course_id) except User.DoesNotExist: raise Http404 course_list = [] for newcourse in courselist: course = course_from_id(newcourse) courseid = course.id.replace('/', '.') newdict = { 'imageurl': request.get_host() + course_image_url(course), 'id': courseid, 'name': course.display_name } course_list.append(newdict) return JsonResponse({"course-list": course_list})
def test_get_all_orgs(self): """ Tests microsite.get_all_orgs works as expected. """ microsite.set_by_domain(self.microsite.site.domain) self.assertEqual( microsite.get_all_orgs(), set(self.microsite.get_organizations()) )
def exclude_dictionary(self, **kwargs): """ If we are not on a microsite, then exclude any microsites that are defined """ exclude_dictionary = super(LmsSearchFilterGenerator, self).exclude_dictionary(**kwargs) course_org_filter = microsite.get_value('course_org_filter') # If we have a course filter we are ensuring that we only get those courses above org_filter_out_set = microsite.get_all_orgs() if not course_org_filter and org_filter_out_set: exclude_dictionary['org'] = list(org_filter_out_set) return exclude_dictionary
def get_all_orgs(): """ This returns all of the orgs that are considered in site configurations or microsite configuration, This can be used, for example, to do filtering. Returns: A list of all organizations present in either microsite configuration or site configuration. """ site_configuration_orgs = SiteConfiguration.get_all_orgs() microsite_orgs = microsite.get_all_orgs() return site_configuration_orgs.union(microsite_orgs)
def get_all_orgs(): """ This returns all of the orgs that are considered in site configurations or microsite configuration, This can be used, for example, to do filtering. Returns: A list of all organizations present in either microsite configuration or site configuration. """ # Import is placed here to avoid model import at project startup. from openedx.core.djangoapps.site_configuration.models import SiteConfiguration site_configuration_orgs = SiteConfiguration.get_all_orgs() microsite_orgs = microsite.get_all_orgs() return site_configuration_orgs.union(microsite_orgs)
def get_visible_courses(org=None, filter_=None): """ Return the set of 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. """ microsite_org = microsite.get_value('course_org_filter') if org and microsite_org: # When called in the context of a microsite, return an empty result if the org # passed by the caller does not match the designated microsite org. courses = CourseOverview.get_all_courses( org=org, filter_=filter_, ) if org == microsite_org else [] else: # We only make it to this point if one of org or microsite_org is defined. # If both org and microsite_org were defined, the code would have fallen into the # first branch of the conditional above, wherein an equality check is performed. target_org = org or microsite_org courses = CourseOverview.get_all_courses(org=target_org, filter_=filter_) courses = sorted(courses, key=lambda course: course.number) # When called in the context of a microsite, filtering can stop here. if microsite_org: return courses # See if we have filtered course listings in this domain filtered_visible_ids = None # this is legacy format which is outside of the microsite feature -- also handle dev case, which should not filter subdomain = microsite.get_value('subdomain', 'default') if hasattr(settings, 'COURSE_LISTINGS') and subdomain in settings.COURSE_LISTINGS and not settings.DEBUG: filtered_visible_ids = frozenset( [SlashSeparatedCourseKey.from_deprecated_string(c) for c in settings.COURSE_LISTINGS[subdomain]] ) if filtered_visible_ids: return [course for course in courses if course.id in filtered_visible_ids] else: # Filter out any courses belonging to a microsite, to avoid leaking these. microsite_orgs = microsite.get_all_orgs() return [course for course in courses if course.location.org not in microsite_orgs]
def post(self, request, format=None): """ """ # Gather all the request data organization_name = request.POST.get('organization_name') # Forbid org already defined in a microsite orgs_in_microsites = microsite.get_all_orgs() if organization_name.lower() in (org.lower() for org in orgs_in_microsites): return JsonResponse("Org taken", status=409) # TODO: # Find orgs that already have courses in them and forbid those too return JsonResponse({"success": True}, status=200)
def get_course_enrollment(request): if not request.user.is_authenticated(): return JsonResponse({ "status": False }) # for microsites, we want to filter and only show enrollments for courses # within the microsites 'ORG' course_org_filter = microsite.get_value('course_org_filter') # Let's filter out any courses in an "org" that has been declared to be # in a Microsite org_filter_out_set = microsite.get_all_orgs() # remove our current Microsite from the "filter out" list, if applicable if course_org_filter: org_filter_out_set.remove(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_enrollment_pairs = list(get_course_enrollment_pairs(request.user, course_org_filter, org_filter_out_set)) enrollment_list = [] for course, enrollment in course_enrollment_pairs: item = { "course_image_url": course_image_url(course), "course_id": course.id.to_deprecated_string(), "display_organization": get_course_about_section(course, 'university'), "display_number": course.display_number_with_default, "display_name": course.display_name_with_default, "course_start": course.start, "course_end": course.end, "enrollment_start": course.enrollment_start, "enrollment_end": course.enrollment_end, "advertised_start": course.advertised_start, "enrollment_date": enrollment.created, "active": enrollment.is_active, } enrollment_list.append(item) return JsonResponse({ "status": True, "enrollment": enrollment_list })
def get_visible_courses(): """ Return the set of CourseDescriptors that should be visible in this branded instance """ # In the event we don't want any course tiles displayed if not getattr(settings, 'DISPLAY_COURSE_TILES', False): return [] filtered_by_org = microsite.get_value('course_org_filter') if filtered_by_org: _courses = modulestore().get_courses(org=filtered_by_org) else: _courses = modulestore().get_courses() courses = [c for c in _courses if isinstance(c, CourseDescriptor)] courses = sorted(courses, key=lambda course: course.number) subdomain = microsite.get_value('subdomain', 'default') # See if we have filtered course listings in this domain filtered_visible_ids = None # this is legacy format which is outside of the microsite feature -- also handle dev case, which should not filter if hasattr(settings, 'COURSE_LISTINGS') and subdomain in settings.COURSE_LISTINGS and not settings.DEBUG: filtered_visible_ids = frozenset([SlashSeparatedCourseKey.from_deprecated_string(c) for c in settings.COURSE_LISTINGS[subdomain]]) filtered_by_db = TileConfiguration.objects.filter( enabled=True, ).values('course_id').order_by('-change_date') if filtered_by_db: filtered_by_db_ids = [course['course_id'] for course in filtered_by_db] filtered_by_db_keys = frozenset([SlashSeparatedCourseKey.from_string(c) for c in filtered_by_db_ids]) return [course for course in courses if course.id in filtered_by_db_keys] if filtered_by_org: return [course for course in courses if course.location.org == filtered_by_org] if filtered_visible_ids: return [course for course in courses if course.id in filtered_visible_ids] else: # Let's filter out any courses in an "org" that has been declared to be # in a Microsite org_filter_out_set = microsite.get_all_orgs() return [course for course in courses if course.location.org not in org_filter_out_set] subdomain = microsite.get_value('subdomain', 'default') # See if we have filtered course listings in this domain filtered_visible_ids = None # this is legacy format which is outside of the microsite feature -- also handle dev case, which should not filter if hasattr(settings, 'COURSE_LISTINGS') and subdomain in settings.COURSE_LISTINGS and not settings.DEBUG: filtered_visible_ids = frozenset([SlashSeparatedCourseKey.from_deprecated_string(c) for c in settings.COURSE_LISTINGS[subdomain]]) if filtered_by_org: return [course for course in courses if course.location.org == filtered_by_org] if filtered_visible_ids: return [course for course in courses if course.id in filtered_visible_ids] else: # Let's filter out any courses in an "org" that has been declared to be # in a Microsite org_filter_out_set = microsite.get_all_orgs() return [course for course in courses if course.location.org not in org_filter_out_set]
def mobi_forum_course_list(request): ''' get all course what user has talked about ''' nr_transaction = newrelic.agent.current_transaction() user = request.user course_org_filter = microsite.get_value('course_org_filter') # Let's filter out any courses in an "org" that has been declared to be # in a Microsite org_filter_out_set = microsite.get_all_orgs() # remove our current Microsite from the "filter out" list, if applicable if course_org_filter: org_filter_out_set.remove(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_enrollment_pairs = list( get_course_enrollment_pairs(user, course_org_filter, org_filter_out_set)) show_courseware_links_for = frozenset( course.id for course, _enrollment in course_enrollment_pairs if has_access(request.user, course, 'load')) user_info = cc.User.from_django_user(request.user).to_dict() courselist = [] for course_id in show_courseware_links_for: try: user_id = user.id profiled_user = cc.User(id=user_id, course_id=course_id) query_params = { 'page': request.GET.get('page', 1), 'per_page': THREADS_PER_PAGE, # more than threads_per_page to show more activities } threads, page, num_pages = profiled_user.active_threads( query_params) query_params['page'] = page query_params['num_pages'] = num_pages with newrelic.agent.FunctionTrace(nr_transaction, "get_metadata_for_threads"): annotated_content_info = utils.get_metadata_for_threads( course_id, threads, request.user, user_info) if annotated_content_info: courselist.append(course_id) except User.DoesNotExist: raise Http404 course_list = [] for newcourse in courselist: course = course_from_id(newcourse) courseid = course.id.replace('/', '.') newdict = { 'imageurl': request.get_host() + course_image_url(course), 'id': courseid, 'name': course.display_name } course_list.append(newdict) return JsonResponse({"course-list": course_list})
def get_visible_courses(org=None, filter_=None): """ Return the set of 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. """ current_site_org = configuration_helpers.get_value('course_org_filter') if isinstance(current_site_org, list): courses = [] for org in current_site_org: _partial = CourseOverview.get_all_courses(org=org, filter_=filter_) courses = courses + list(_partial) elif org and current_site_org: # Return an empty result if the org passed by the caller does not match the designated site org. courses = CourseOverview.get_all_courses( org=org, filter_=filter_, ) if org == current_site_org else [] else: # We only make it to this point if one of org or current_site_org is defined. # If both org and current_site_org were defined, the code would have fallen into the # first branch of the conditional above, wherein an equality check is performed. target_org = org or current_site_org courses = CourseOverview.get_all_courses(org=target_org, filter_=filter_) courses = sorted(courses, key=lambda course: course.number) # Filtering can stop here. if current_site_org: 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( [SlashSeparatedCourseKey.from_deprecated_string(c) for c in settings.COURSE_LISTINGS[subdomain]] ) filtered_by_org = configuration_helpers.get_value('course_org_filter') if filtered_by_org and isinstance(filtered_by_org, basestring): return [course for course in courses if course.location.org == filtered_by_org] if filtered_by_org and isinstance(filtered_by_org, list): return [course for course in courses if course.location.org in filtered_by_org] if filtered_visible_ids: return [course for course in courses if course.id in filtered_visible_ids] else: # Let's filter out any courses in an "org" that has been declared to be # in a Microsite org_filter_out_set = microsite.get_all_orgs() return [course for course in courses if course.location.org not in org_filter_out_set]