Skip to content

Commit

Permalink
Filtered course based on organization of current sites before serving (
Browse files Browse the repository at this point in the history
  • Loading branch information
Muhammad Haseeb authored Sep 1, 2020
1 parent f87256f commit e7b9aca
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
3 changes: 2 additions & 1 deletion cms/djangoapps/contentstore/views/course.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from openedx.core.djangoapps.waffle_utils import WaffleSwitchNamespace
from openedx.features.course_experience.waffle import waffle as course_experience_waffle
from openedx.features.course_experience.waffle import ENABLE_COURSE_ABOUT_SIDEBAR_HTML
from openedx.features.edly.utils import get_edx_org_from_cookie, get_enabled_organizations
from openedx.features.edly.utils import filter_courses_based_on_org, get_edx_org_from_cookie, get_enabled_organizations
from six import text_type

from contentstore.course_group_config import (
Expand Down Expand Up @@ -514,6 +514,7 @@ def filter_ccx(course_access):
staff_courses = UserBasedRole(request.user, CourseStaffRole.ROLE).courses_with_role()
site_courses = UserBasedRole(request.user, GlobalCourseCreatorRole.ROLE).courses_with_role()
all_courses = filter(filter_ccx, instructor_courses | staff_courses | site_courses)
all_courses = filter_courses_based_on_org(request, all_courses)
courses_list = []
course_keys = {}

Expand Down
40 changes: 39 additions & 1 deletion openedx/features/edly/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,28 @@
user_has_edly_organization_access,
user_belongs_to_edly_sub_organization,
user_can_login_on_requested_edly_organization,
filter_courses_based_on_org,
)
from organizations.tests.factories import OrganizationFactory
from student import auth
from student.roles import (
CourseCreatorRole,
GlobalCourseCreatorRole,
)
from student.tests.factories import UserFactory, GroupFactory
from xmodule.modulestore.django import modulestore
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase
from xmodule.modulestore.tests.factories import CourseFactory


def mock_render_to_string(template_name, context):
"""
Return a string that encodes template_name and context
"""
return str((template_name, context))

class UtilsTests(TestCase):

class UtilsTests(ModuleStoreTestCase):
"""
Tests for utility methods.
"""
Expand Down Expand Up @@ -297,3 +304,34 @@ def test_edly_panel_user_has_edly_org_access(self):
edly_panel_user_group = GroupFactory(name=settings.EDLY_PANEL_USERS_GROUP)
self.request.user.groups.add(edly_panel_user_group)
assert edly_panel_user_has_edly_org_access(self.request)

def test_filter_courses_based_on_org(self):
"""
Test course are filtered properly on current site organization.
"""
edx_org_1 = OrganizationFactory()
edx_org_2 = OrganizationFactory()
courses_of_org_1 = CourseFactory.create_batch(2, org=edx_org_1.short_name)
CourseFactory.create(org=edx_org_2.short_name)

assert len(modulestore().get_courses()) == 3

EdlySubOrganizationFactory(
edx_organization=edx_org_1,
lms_site=self.request.site,
studio_site=self.request.site
)
create_user_link_with_edly_sub_organization(self.request, self.request.user)
response = cookies_api.set_logged_in_edly_cookies(
self.request, HttpResponse(),
self.user,
cookie_settings(self.request)
)
self._copy_cookies_to_request(response, self.request)

filtered_courses = filter_courses_based_on_org(self.request, courses_of_org_1)
assert len(filtered_courses) == 2

edx_orgs_of_filterd_courses = [course.org for course in filtered_courses]
for org in edx_orgs_of_filterd_courses:
assert org == edx_org_1.short_name
23 changes: 23 additions & 0 deletions openedx/features/edly/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,3 +290,26 @@ def user_can_login_on_requested_edly_organization(request, user):
return True

return False


def filter_courses_based_on_org(request, all_courses):
"""
Filter courses based on the requested URL site.
Most of our LMS based roles are not organization based we would
need to filter courses manually based on org of current site.
Arguments:
request: HTTP request object,
all_courses (iterator): Iterator object.
Returns:
list: Returns List of courses filtered based on current site organization.
"""

edly_user_info_cookie = request.COOKIES.get(settings.EDLY_USER_INFO_COOKIE_NAME, None)
edx_org = get_edx_org_from_cookie(edly_user_info_cookie)

filtered_courses = [course for course in list(all_courses) if course.org == edx_org]

return filtered_courses

0 comments on commit e7b9aca

Please sign in to comment.