Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Unreleased

* Added ``context_key`` to LibraryContainerLocator and LibraryCollectionLocator

# 2.13.0

* Breaking change to the new LibraryContainerLocator and
Expand Down
9 changes: 9 additions & 0 deletions opaque_keys/edx/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,15 @@
def library_key(self):
return self.lib_key

@property
@abstractmethod
def context_key(self) -> LearningContextKey:
"""
Get the learning context key (LearningContextKey) for this item.
Will be a library key in this case.
"""
raise NotImplementedError()

Check warning on line 124 in opaque_keys/edx/keys.py

View check run for this annotation

Codecov / codecov/patch

opaque_keys/edx/keys.py#L124

Added line #L124 was not covered by tests


class DefinitionKey(OpaqueKey):
"""
Expand Down
8 changes: 8 additions & 0 deletions opaque_keys/edx/locator.py
Original file line number Diff line number Diff line change
Expand Up @@ -1675,6 +1675,10 @@ def _from_string(cls, serialized: str) -> Self:
except (ValueError, TypeError) as error:
raise InvalidKeyError(cls, serialized) from error

@property
def context_key(self) -> LibraryLocatorV2:
return self.lib_key


class LibraryContainerLocator(CheckFieldMixin, LibraryItemKey):
"""
Expand Down Expand Up @@ -1714,6 +1718,10 @@ def org(self) -> str | None: # pragma: no cover
"""
return self.lib_key.org

@property
def context_key(self) -> LibraryLocatorV2:
return self.lib_key

def _to_string(self) -> str:
"""
Serialize this key as a string
Expand Down
12 changes: 7 additions & 5 deletions opaque_keys/edx/tests/test_collection_locators.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,14 @@ def test_coll_key_from_string(self):
code = 'test-problem-bank'
str_key = f"lib-collection:{org}:{lib}:{code}"
coll_key = LibraryCollectionLocator.from_string(str_key)
assert str(coll_key) == str_key
assert coll_key.org == org
assert coll_key.collection_id == code
lib_key = coll_key.lib_key
self.assertEqual(str(coll_key), str_key)
self.assertEqual(coll_key.org, org)
self.assertEqual(coll_key.collection_id, code)
self.assertEqual(lib_key.org, org)
self.assertEqual(lib_key.slug, lib)
assert isinstance(lib_key, LibraryLocatorV2)
assert lib_key.org == org
assert lib_key.slug == lib
assert coll_key.context_key == lib_key

def test_coll_key_invalid_from_string(self):
with self.assertRaises(InvalidKeyError):
Expand Down
14 changes: 8 additions & 6 deletions opaque_keys/edx/tests/test_container_locators.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,12 @@ def test_key_from_string(self):
container_id = 'test-container'
str_key = f"lct:{org}:{lib}:{container_type}:{container_id}"
container_key = LibraryContainerLocator.from_string(str_key)
assert str(container_key) == str_key
assert container_key.org == org
assert container_key.container_type == container_type
assert container_key.container_id == container_id
lib_key = container_key.lib_key
self.assertEqual(str(container_key), str_key)
self.assertEqual(container_key.org, org)
self.assertEqual(container_key.container_type, container_type)
self.assertEqual(container_key.container_id, container_id)
self.assertEqual(lib_key.org, org)
self.assertEqual(lib_key.slug, lib)
assert isinstance(lib_key, LibraryLocatorV2)
assert lib_key.org == org
assert lib_key.slug == lib
assert container_key.context_key == lib_key