88
99from openedx_events .content_authoring .data import LibraryBlockData , LibraryContainerData
1010from openedx_events .content_authoring .signals import LIBRARY_BLOCK_UPDATED , LIBRARY_CONTAINER_UPDATED
11- from opaque_keys .edx .keys import UsageKeyV2 , OpaqueKey
12- from opaque_keys .edx .locator import LibraryContainerLocator , LibraryLocatorV2 , LibraryUsageLocatorV2
11+ from opaque_keys .edx .keys import UsageKeyV2
12+ from opaque_keys .edx .locator import LibraryUsageLocatorV2 , LibraryLocatorV2
1313from openedx_learning .api import authoring as authoring_api
1414
1515from openedx .core .djangoapps .content_libraries import api , permissions
2020log = logging .getLogger (__name__ )
2121
2222
23- class LibraryContextPermissionBase :
24- locator_type = OpaqueKey
23+ class LibraryContextImpl (LearningContext ):
24+ """
25+ Implements content libraries as a learning context.
26+
27+ This is the *new* content libraries based on Learning Core, not the old content
28+ libraries based on modulestore.
29+ """
2530
2631 def __init__ (self , ** kwargs ):
2732 super ().__init__ (** kwargs )
2833 self .use_draft = kwargs .get ('use_draft' , None )
2934
30- def get_library_key (self , opaque_key : OpaqueKey ) :
35+ def can_edit_block (self , user : UserType , usage_key : UsageKeyV2 ) -> bool :
3136 """
32- Get library key from given opaque_key.
33- """
34- raise NotImplementedError ()
35-
36- def can_edit_block (self , user : UserType , opaque_key : OpaqueKey ) -> bool :
37- """
38- Assuming a block with the specified ID (opaque_key) exists, does the
37+ Assuming a block with the specified ID (usage_key) exists, does the
3938 specified user have permission to edit it (make changes to the
4039 fields / authored data store)?
4140
4241 May raise ContentLibraryNotFound if the library does not exist.
4342 """
44- assert isinstance (opaque_key , self .locator_type )
45- return self ._check_perm (
46- user ,
47- self .get_library_key (opaque_key ),
48- permissions .CAN_EDIT_THIS_CONTENT_LIBRARY
49- )
43+ assert isinstance (usage_key , LibraryUsageLocatorV2 )
44+ return self ._check_perm (user , usage_key .lib_key , permissions .CAN_EDIT_THIS_CONTENT_LIBRARY )
5045
51- def can_view_block_for_editing (self , user : UserType , opaque_key : OpaqueKey ) -> bool :
46+ def can_view_block_for_editing (self , user : UserType , usage_key : UsageKeyV2 ) -> bool :
5247 """
53- Assuming a block with the specified ID (opaque_key ) exists, does the
48+ Assuming a block with the specified ID (usage_key ) exists, does the
5449 specified user have permission to view its fields and OLX details (but
5550 not necessarily to make changes to it)?
5651
5752 May raise ContentLibraryNotFound if the library does not exist.
5853 """
59- assert isinstance (opaque_key , self .locator_type )
60- return self ._check_perm (
61- user ,
62- self .get_library_key (opaque_key ),
63- permissions .CAN_VIEW_THIS_CONTENT_LIBRARY
64- )
54+ assert isinstance (usage_key , LibraryUsageLocatorV2 )
55+ return self ._check_perm (user , usage_key .lib_key , permissions .CAN_VIEW_THIS_CONTENT_LIBRARY )
6556
66- def can_view_block (self , user : UserType , opaque_key : OpaqueKey ) -> bool :
57+ def can_view_block (self , user : UserType , usage_key : UsageKeyV2 ) -> bool :
6758 """
6859 Does the specified usage key exist in its context, and if so, does the
6960 specified user have permission to view it and interact with it (call
7061 handlers, save user state, etc.)?
7162
7263 May raise ContentLibraryNotFound if the library does not exist.
7364 """
74- assert isinstance (opaque_key , self .locator_type )
75- return self ._check_perm (
76- user ,
77- self .get_library_key (opaque_key ),
78- permissions .CAN_LEARN_FROM_THIS_CONTENT_LIBRARY
79- )
65+ assert isinstance (usage_key , LibraryUsageLocatorV2 )
66+ return self ._check_perm (user , usage_key .lib_key , permissions .CAN_LEARN_FROM_THIS_CONTENT_LIBRARY )
8067
8168 def _check_perm (self , user : UserType , lib_key : LibraryLocatorV2 , perm ) -> bool :
8269 """ Helper method to check a permission for the various can_ methods"""
@@ -89,23 +76,6 @@ def _check_perm(self, user: UserType, lib_key: LibraryLocatorV2, perm) -> bool:
8976 # A 404 is probably what you want in this case, not a 500 error, so do that by default.
9077 raise NotFound (f"Content Library '{ lib_key } ' does not exist" ) from exc
9178
92-
93- class LibraryContextImpl (LibraryContextPermissionBase , LearningContext ):
94- """
95- Implements content libraries as a learning context.
96-
97- This is the *new* content libraries based on Learning Core, not the old content
98- libraries based on modulestore.
99- """
100-
101- locator_type = LibraryUsageLocatorV2
102-
103- def get_library_key (self , opaque_key : OpaqueKey ):
104- """
105- Get library key from given opaque_key.
106- """
107- return opaque_key .lib_key
108-
10979 def block_exists (self , usage_key : LibraryUsageLocatorV2 ):
11080 """
11181 Does the block for this usage_key exist in this Library?
@@ -133,82 +103,29 @@ def block_exists(self, usage_key: LibraryUsageLocatorV2):
133103 local_key = usage_key .block_id ,
134104 )
135105
136- def send_block_updated_event (self , opaque_key : OpaqueKey ):
106+ def send_block_updated_event (self , usage_key : UsageKeyV2 ):
137107 """
138108 Send a "block updated" event for the library block with the given usage_key.
139109 """
140- assert isinstance (opaque_key , self . locator_type )
110+ assert isinstance (usage_key , LibraryUsageLocatorV2 )
141111 LIBRARY_BLOCK_UPDATED .send_event (
142112 library_block = LibraryBlockData (
143- library_key = opaque_key .lib_key ,
144- usage_key = opaque_key ,
113+ library_key = usage_key .lib_key ,
114+ usage_key = usage_key ,
145115 )
146116 )
147117
148- def send_container_updated_events (self , opaque_key : OpaqueKey ):
118+ def send_container_updated_events (self , usage_key : UsageKeyV2 ):
149119 """
150120 Send "container updated" events for containers that contains the library block
151121 with the given usage_key.
152122 """
153- assert isinstance (opaque_key , self . locator_type )
154- affected_containers = api .get_containers_contains_component (opaque_key )
123+ assert isinstance (usage_key , LibraryUsageLocatorV2 )
124+ affected_containers = api .get_containers_contains_component (usage_key )
155125 for container in affected_containers :
156126 LIBRARY_CONTAINER_UPDATED .send_event (
157127 library_container = LibraryContainerData (
158128 container_key = container .container_key ,
159129 background = True ,
160130 )
161131 )
162-
163-
164- class LibraryContextContainerImpl (LibraryContextPermissionBase , LearningContext ):
165- """
166- Implements content libraries as a learning context for containers in libraries.
167-
168- This is the *new* content libraries based on Learning Core, not the old content
169- libraries based on modulestore.
170- """
171-
172- locator_type = LibraryContainerLocator
173-
174- def get_library_key (self , opaque_key : OpaqueKey ):
175- """
176- Get library key from given opaque_key.
177- """
178- return opaque_key .library_key
179-
180- def container_exists (self , container_key : LibraryContainerLocator ):
181- """
182- Does the container for this key exist in this Library?
183-
184- Note that this applies to all versions, i.e. you can put a container key for
185- a piece of content that has been soft-deleted (removed from Drafts), and
186- it will still return True here. That's because for the purposes of
187- permission checking, we just want to know whether that block has ever
188- existed in this Library, because we could be looking at any older
189- version of it.
190- """
191- try :
192- content_lib = ContentLibrary .objects .get_by_key (container_key .library_key ) # type: ignore[attr-defined]
193- except ContentLibrary .DoesNotExist :
194- return False
195-
196- learning_package = content_lib .learning_package
197- if learning_package is None :
198- return False
199-
200- return authoring_api .container_exists_by_key (
201- learning_package .id ,
202- container_key ,
203- )
204-
205- def send_block_updated_event (self , opaque_key : OpaqueKey ):
206- """
207- Send a "block updated" event for the library block with the given usage_key.
208- """
209- assert isinstance (opaque_key , self .locator_type )
210- LIBRARY_CONTAINER_UPDATED .send_event (
211- library_container = LibraryContainerData (
212- container_key = opaque_key ,
213- )
214- )
0 commit comments