Skip to content

Commit 265a303

Browse files
committed
refactor: LibraryUsageLocatorV2 as LibraryElementKey
1 parent b113388 commit 265a303

2 files changed

Lines changed: 15 additions & 8 deletions

File tree

opaque_keys/edx/locator.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1550,16 +1550,15 @@ def for_branch(self, branch: str | None):
15501550
return self
15511551

15521552

1553-
class LibraryUsageLocatorV2(CheckFieldMixin, UsageKeyV2):
1553+
class LibraryUsageLocatorV2(CheckFieldMixin, UsageKeyV2, LibraryItemKey):
15541554
"""
15551555
An XBlock in a Learning-Core-based content library.
15561556
15571557
When serialized, these keys look like:
15581558
lb:MITx:reallyhardproblems:problem:problem1
15591559
"""
15601560
CANONICAL_NAMESPACE = 'lb' # "Library Block"
1561-
KEY_FIELDS = ('lib_key', 'block_type', 'usage_id')
1562-
lib_key: LibraryLocatorV2
1561+
KEY_FIELDS = ('library_key', 'block_type', 'usage_id')
15631562
usage_id: str
15641563

15651564
__slots__ = KEY_FIELDS
@@ -1577,14 +1576,14 @@ def __init__(self, lib_key: LibraryLocatorV2, block_type: str, usage_id: str):
15771576
self._check_key_string_field("block_type", block_type)
15781577
self._check_key_string_field("usage_id", usage_id, regexp=self.USAGE_ID_REGEXP)
15791578
super().__init__(
1580-
lib_key=lib_key,
1579+
library_key=lib_key,
15811580
block_type=block_type,
15821581
usage_id=usage_id,
15831582
)
15841583

15851584
@property
15861585
def context_key(self) -> LibraryLocatorV2:
1587-
return self.lib_key
1586+
return self.library_key
15881587

15891588
@property
15901589
def block_id(self) -> str:
@@ -1593,11 +1592,18 @@ def block_id(self) -> str:
15931592
"""
15941593
return self.usage_id
15951594

1595+
@property
1596+
def org(self) -> str | None: # pragma: no cover
1597+
"""
1598+
The organization that this XBlock belongs to.
1599+
"""
1600+
raise self.library_key.org
1601+
15961602
def _to_string(self) -> str:
15971603
"""
15981604
Serialize this key as a string
15991605
"""
1600-
return ":".join((self.lib_key.org, self.lib_key.slug, self.block_type, self.usage_id))
1606+
return ":".join((self.library_key.org, self.library_key.slug, self.block_type, self.usage_id))
16011607

16021608
@classmethod
16031609
def _from_string(cls, serialized: str) -> Self:
@@ -1654,7 +1660,7 @@ def __init__(self, library_key: LibraryLocatorV2, collection_id: str):
16541660
@property
16551661
def org(self) -> str | None: # pragma: no cover
16561662
"""
1657-
The organization that this object belongs to.
1663+
The organization that this collection belongs to.
16581664
"""
16591665
return self.library_key.org
16601666

@@ -1711,7 +1717,7 @@ def __init__(self, library_key: LibraryLocatorV2, container_type: str, container
17111717
@property
17121718
def org(self) -> str | None: # pragma: no cover
17131719
"""
1714-
The organization that this object belongs to.
1720+
The organization that this Container belongs to.
17151721
"""
17161722
return self.library_key.org
17171723

opaque_keys/edx/tests/test_library_usage_locators.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ def test_constructor(self, org, lib, block_type, block_id):
5555
self.assertEqual(lib_usage_key, lib_usage_key2)
5656
self.assertEqual(lib_usage_key.library_key, lib_key)
5757
self.assertEqual(lib_usage_key.library_key, lib_key)
58+
self.assertEqual(lib_usage_key.org, org)
5859
self.assertEqual(lib_usage_key.branch, None)
5960
self.assertEqual(lib_usage_key.run, LibraryLocator.RUN)
6061
self.assertIsInstance(lib_usage_key2, LibraryUsageLocator)

0 commit comments

Comments
 (0)