Skip to content

Commit

Permalink
refactor: make code more DRY
Browse files Browse the repository at this point in the history
  • Loading branch information
bradenmacdonald committed Oct 7, 2024
1 parent 3de76a5 commit f1737e1
Showing 1 changed file with 9 additions and 21 deletions.
30 changes: 9 additions & 21 deletions openedx/core/djangoapps/content_libraries/library_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from openedx_events.content_authoring.data import LibraryBlockData
from openedx_events.content_authoring.signals import LIBRARY_BLOCK_UPDATED
from opaque_keys.edx.keys import UsageKeyV2
from opaque_keys.edx.locator import LibraryUsageLocatorV2
from opaque_keys.edx.locator import LibraryUsageLocatorV2, LibraryLocatorV2
from openedx_learning.api import authoring as authoring_api

from openedx.core.djangoapps.content_libraries import api, permissions
Expand Down Expand Up @@ -41,14 +41,7 @@ def can_edit_block(self, user: UserType, usage_key: UsageKeyV2) -> bool:
May raise ContentLibraryNotFound if the library does not exist.
"""
assert isinstance(usage_key, LibraryUsageLocatorV2)
try:
api.require_permission_for_library_key(usage_key.lib_key, user, permissions.CAN_EDIT_THIS_CONTENT_LIBRARY)
return True
except PermissionDenied:
return False
except api.ContentLibraryNotFound as exc:
# A 404 is probably what you want in this case, not a 500 error, so do that by default.
raise NotFound(f"Content Library '{usage_key.lib_key}' does not exist") from exc
return self._check_perm(user, usage_key.lib_key, permissions.CAN_EDIT_THIS_CONTENT_LIBRARY)

def can_view_block_for_editing(self, user: UserType, usage_key: UsageKeyV2) -> bool:
"""
Expand All @@ -59,14 +52,7 @@ def can_view_block_for_editing(self, user: UserType, usage_key: UsageKeyV2) -> b
May raise ContentLibraryNotFound if the library does not exist.
"""
assert isinstance(usage_key, LibraryUsageLocatorV2)
try:
api.require_permission_for_library_key(usage_key.lib_key, user, permissions.CAN_VIEW_THIS_CONTENT_LIBRARY)
return True
except PermissionDenied:
return False
except api.ContentLibraryNotFound as exc:
# A 404 is probably what you want in this case, not a 500 error, so do that by default.
raise NotFound(f"Content Library '{usage_key.lib_key}' does not exist") from exc
return self._check_perm(user, usage_key.lib_key, permissions.CAN_VIEW_THIS_CONTENT_LIBRARY)

def can_view_block(self, user: UserType, usage_key: UsageKeyV2) -> bool:
"""
Expand All @@ -77,16 +63,18 @@ def can_view_block(self, user: UserType, usage_key: UsageKeyV2) -> bool:
May raise ContentLibraryNotFound if the library does not exist.
"""
assert isinstance(usage_key, LibraryUsageLocatorV2)
return self._check_perm(user, usage_key.lib_key, permissions.CAN_LEARN_FROM_THIS_CONTENT_LIBRARY)

def _check_perm(self, user: UserType, lib_key: LibraryLocatorV2, perm) -> bool:
""" Helper method to check a permission for the various can_ methods"""
try:
api.require_permission_for_library_key(
usage_key.lib_key, user, permissions.CAN_LEARN_FROM_THIS_CONTENT_LIBRARY,
)
api.require_permission_for_library_key(lib_key, user, perm)
return True
except PermissionDenied:
return False
except api.ContentLibraryNotFound as exc:
# A 404 is probably what you want in this case, not a 500 error, so do that by default.
raise NotFound(f"Content Library '{usage_key.lib_key}' does not exist") from exc
raise NotFound(f"Content Library '{lib_key}' does not exist") from exc

def block_exists(self, usage_key: LibraryUsageLocatorV2):
"""
Expand Down

0 comments on commit f1737e1

Please sign in to comment.