Skip to content

Commit

Permalink
fix: display available libraries for user
Browse files Browse the repository at this point in the history
  • Loading branch information
ruzniaievdm authored and UvgenGen committed Sep 7, 2022
1 parent a9779d2 commit 6a38f81
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 15 deletions.
12 changes: 11 additions & 1 deletion cms/djangoapps/contentstore/views/organization.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from django.views.generic import View
from organizations.api import get_organizations

from cms.djangoapps.course_creators.models import CourseCreator
from openedx.core.djangolib.js_utils import dump_js_escaped_json


Expand All @@ -20,6 +21,15 @@ class OrganizationListView(View):
@method_decorator(login_required)
def get(self, request, *args, **kwargs): # lint-amnesty, pylint: disable=unused-argument
"""Returns organization list as json."""
org_names_list = []

course_creator = CourseCreator.objects.filter(user=request.user).last()
is_state_granted = course_creator and course_creator.state == CourseCreator.GRANTED
organizations = get_organizations()
org_names_list = [(org["short_name"]) for org in organizations]

if request.user.is_staff or (is_state_granted and course_creator.all_organizations):
org_names_list = [(org["short_name"]) for org in organizations]
elif is_state_granted:
org_names_list = list(course_creator.member_of_organizations_name())

return HttpResponse(dump_js_escaped_json(org_names_list), content_type='application/json; charset=utf-8') # lint-amnesty, pylint: disable=http-response-with-content-type-json
3 changes: 3 additions & 0 deletions cms/djangoapps/course_creators/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ class CourseCreator(models.Model):
def __str__(self):
return f"{self.user} | {self.state} [{self.state_changed}]"

def member_of_organizations_name(self):
return self.organizations.values_list("short_name", flat=True)


@receiver(post_init, sender=CourseCreator)
def post_init_callback(sender, **kwargs): # lint-amnesty, pylint: disable=unused-argument
Expand Down
6 changes: 1 addition & 5 deletions xmodule/library_content_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from copy import copy
from gettext import ngettext

import bleach
from django.conf import settings
from django.utils.functional import classproperty
from lazy import lazy
Expand Down Expand Up @@ -699,10 +698,7 @@ def source_library_values(self):
"""
lib_tools = self.runtime.service(self, 'library_tools')
user_perms = self.runtime.service(self, 'studio_user_permissions')
all_libraries = [
(key, bleach.clean(name)) for key, name in lib_tools.list_available_libraries()
if user_perms.can_read(key) or self.source_library_id == str(key)
]
all_libraries = lib_tools.list_available_libraries(user_perms)
all_libraries.sort(key=lambda entry: entry[1]) # Sort by name
if self.source_library_id and self.source_library_key not in [entry[0] for entry in all_libraries]:
all_libraries.append((self.source_library_id, _("Invalid Library")))
Expand Down
5 changes: 1 addition & 4 deletions xmodule/library_sourced_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,10 +285,7 @@ def source_library_values(self):
Return a list of possible values for self.source_library_id
"""
user_perms = self.runtime.service(self, 'studio_user_permissions')
all_libraries = [
(key, bleach.clean(name)) for key, name in self.tools.list_available_libraries()
if user_perms.can_read(key) or self.source_library_id == str(key)
]
all_libraries = self.tools.list_available_libraries(user_perms)
all_libraries.sort(key=lambda entry: entry[1]) # Sort by name
if self.source_library_id and self.source_library_key not in [entry[0] for entry in all_libraries]:
all_libraries.append((self.source_library_id, _("Invalid Library")))
Expand Down
3 changes: 2 additions & 1 deletion xmodule/library_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def is_loading(self, location):
"""
return self.import_task_status(location) == UserTaskStatus.IN_PROGRESS

def list_available_libraries(self):
def list_available_libraries(self, user_perms):
"""
List all known libraries.
Expand All @@ -177,6 +177,7 @@ def list_available_libraries(self):
v1_libs = [
(lib.location.library_key.replace(version_guid=None, branch=None), lib.display_name)
for lib in self.store.get_library_summaries()
if user_perms.can_read(lib.location.library_key.replace(version_guid=None, branch=None))
]
v2_query = library_api.get_libraries_for_user(user)
v2_libs_with_meta = library_api.get_metadata_from_index(v2_query)
Expand Down
12 changes: 8 additions & 4 deletions xmodule/tests/test_library_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ def setUp(self):
UserFactory(is_staff=True, id=self.user_id)
self.tools = LibraryToolsService(self.store, self.user_id)

def test_list_available_libraries(self):
@patch('cms.djangoapps.contentstore.views.item.StudioPermissionsService')
def test_list_available_libraries(self, mock_user_perm):
"""
Test listing of libraries.
Expand All @@ -41,17 +42,20 @@ def test_list_available_libraries(self):
_ = LibraryFactory.create(modulestore=self.store)
# create V2 library
self._create_library(slug="testlib1_preview", title="Test Library 1", description="Testing XBlocks")
all_libraries = self.tools.list_available_libraries()
all_libraries = self.tools.list_available_libraries(mock_user_perm)
assert all_libraries
assert mock_user_perm.called
assert len(all_libraries) == 2

@patch('xmodule.modulestore.split_mongo.split.SplitMongoModuleStore.get_library_summaries')
def test_list_available_libraries_fetch(self, mock_get_library_summaries):
@patch('cms.djangoapps.contentstore.views.item.StudioPermissionsService')
def test_list_available_libraries_fetch(self, mock_get_library_summaries, mock_user_perm):
"""
Test that library list is compiled using light weight library summary objects.
"""
_ = self.tools.list_available_libraries()
_ = self.tools.list_available_libraries(mock_user_perm)
assert mock_get_library_summaries.called
assert mock_user_perm.called

def test_import_from_blockstore(self):
# Create a blockstore content library
Expand Down

0 comments on commit 6a38f81

Please sign in to comment.