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
15 changes: 13 additions & 2 deletions opaque_keys/edx/django/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
If Django is unavailable, none of the classes below will work as intended.
"""
from __future__ import annotations

import logging
import warnings

Expand All @@ -17,8 +18,7 @@
IsNull = object

from opaque_keys import OpaqueKey
from opaque_keys.edx.keys import BlockTypeKey, CourseKey, LearningContextKey, UsageKey

from opaque_keys.edx.keys import BlockTypeKey, CourseKey, LearningContextKey, LibraryItemKey, UsageKey

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -224,6 +224,17 @@ class UsageKeyField(OpaqueKeyField):
_pyi_private_get_type: UsageKey | None


class LibraryItemField(OpaqueKeyField):
"""
A django Field that stores a LibraryItemKey object as a string.
"""
description = "A Location object, saved to the DB in the form of a string"
KEY_CLASS = LibraryItemKey
# Declare the field types for the django-stubs mypy type hint plugin:
_pyi_private_set_type: LibraryItemKey | str | None
_pyi_private_get_type: LibraryItemKey | None


class LocationKeyField(UsageKeyField):
"""
A django Field that stores a UsageKey object as a string.
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 XBlock usage.
May be a course key, library key, or some other LearningContextKey type.
"""
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 @@
except (ValueError, TypeError) as error:
raise InvalidKeyError(cls, serialized) from error

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

Check warning on line 1680 in opaque_keys/edx/locator.py

View check run for this annotation

Codecov / codecov/patch

opaque_keys/edx/locator.py#L1680

Added line #L1680 was not covered by tests


class LibraryContainerLocator(CheckFieldMixin, LibraryItemKey):
"""
Expand Down Expand Up @@ -1714,6 +1718,10 @@
"""
return self.lib_key.org

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

Check warning on line 1723 in opaque_keys/edx/locator.py

View check run for this annotation

Codecov / codecov/patch

opaque_keys/edx/locator.py#L1723

Added line #L1723 was not covered by tests

def _to_string(self) -> str:
"""
Serialize this key as a string
Expand Down