Skip to content

Commit 6995de8

Browse files
committed
refactor!: use LibraryCollectionLocator in LibraryCollectionData
The library_key can be inferred from the collection_key
1 parent 5bf28db commit 6995de8

6 files changed

Lines changed: 29 additions & 19 deletions

openedx_events/content_authoring/data.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
import attr
1414
from opaque_keys.edx.keys import CourseKey, UsageKey
15-
from opaque_keys.edx.locator import LibraryLocatorV2, LibraryUsageLocatorV2
15+
from opaque_keys.edx.locator import LibraryCollectionLocator, LibraryLocatorV2, LibraryUsageLocatorV2
1616

1717

1818
@attr.s(frozen=True)
@@ -218,14 +218,12 @@ class LibraryCollectionData:
218218
Data related to a library collection that has changed.
219219
220220
Attributes:
221-
library_key (LibraryLocatorV2): a key that represents a content library.
222-
collection_key (str): identifies the collection within the library's learning package
221+
collection_key (LibraryCollectionLocator): identifies the collection within the library's learning package
223222
background (bool): indicate whether the sender doesn't want to wait for handler to finish execution,
224223
i.e., the handler can run the task in background. By default it is False.
225224
"""
226225

227-
library_key = attr.ib(type=LibraryLocatorV2)
228-
collection_key = attr.ib(type=str)
226+
collection_key = attr.ib(type=LibraryCollectionLocator)
229227
background = attr.ib(type=bool, default=False)
230228

231229

openedx_events/event_bus/avro/custom_serializers.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from ccx_keys.locator import CCXLocator
1010
from opaque_keys.edx.keys import CourseKey, UsageKey
11-
from opaque_keys.edx.locator import LibraryLocatorV2, LibraryUsageLocatorV2
11+
from opaque_keys.edx.locator import LibraryCollectionLocator, LibraryLocatorV2, LibraryUsageLocatorV2
1212

1313
from openedx_events.event_bus.avro.types import PYTHON_TYPE_TO_AVRO_MAPPING
1414

@@ -112,6 +112,25 @@ def deserialize(data: str):
112112
return UsageKey.from_string(data)
113113

114114

115+
class LibraryCollectionLocatorAvroSerializer(BaseCustomTypeAvroSerializer):
116+
"""
117+
CustomTypeAvroSerializer for LibraryCollectionLocator class.
118+
"""
119+
120+
cls = LibraryCollectionLocator
121+
field_type = PYTHON_TYPE_TO_AVRO_MAPPING[str]
122+
123+
@staticmethod
124+
def serialize(obj) -> str:
125+
"""Serialize obj into string."""
126+
return str(obj)
127+
128+
@staticmethod
129+
def deserialize(data: str):
130+
"""Deserialize string into obj."""
131+
return LibraryCollectionLocator.from_string(data)
132+
133+
115134
class LibraryLocatorV2AvroSerializer(BaseCustomTypeAvroSerializer):
116135
"""
117136
CustomTypeAvroSerializer for LibraryLocatorV2 class.
@@ -175,6 +194,7 @@ def deserialize(data: str):
175194
CourseKeyAvroSerializer,
176195
CcxCourseLocatorAvroSerializer,
177196
DatetimeAvroSerializer,
197+
LibraryCollectionLocatorAvroSerializer,
178198
LibraryLocatorV2AvroSerializer,
179199
LibraryUsageLocatorV2AvroSerializer,
180200
UsageKeyAvroSerializer,

openedx_events/event_bus/avro/tests/schemas/org+openedx+content_authoring+content_library+collection+created+v1_schema.avsc

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
99
"name": "LibraryCollectionData",
1010
"type": "record",
1111
"fields": [
12-
{
13-
"name": "library_key",
14-
"type": "string"
15-
},
1612
{
1713
"name": "collection_key",
1814
"type": "string"

openedx_events/event_bus/avro/tests/schemas/org+openedx+content_authoring+content_library+collection+deleted+v1_schema.avsc

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
99
"name": "LibraryCollectionData",
1010
"type": "record",
1111
"fields": [
12-
{
13-
"name": "library_key",
14-
"type": "string"
15-
},
1612
{
1713
"name": "collection_key",
1814
"type": "string"

openedx_events/event_bus/avro/tests/schemas/org+openedx+content_authoring+content_library+collection+updated+v1_schema.avsc

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
99
"name": "LibraryCollectionData",
1010
"type": "record",
1111
"fields": [
12-
{
13-
"name": "library_key",
14-
"type": "string"
15-
},
1612
{
1713
"name": "collection_key",
1814
"type": "string"

openedx_events/event_bus/avro/tests/test_avro.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from fastavro.repository.base import SchemaRepositoryError
1212
from fastavro.schema import load_schema
1313
from opaque_keys.edx.keys import CourseKey, UsageKey
14-
from opaque_keys.edx.locator import LibraryLocatorV2, LibraryUsageLocatorV2
14+
from opaque_keys.edx.locator import LibraryCollectionLocator, LibraryLocatorV2, LibraryUsageLocatorV2
1515

1616
from openedx_events.event_bus.avro.deserializer import AvroSignalDeserializer, deserialize_bytes_to_event_data
1717
from openedx_events.event_bus.avro.serializer import AvroSignalSerializer, serialize_event_data_to_bytes
@@ -109,6 +109,7 @@ def generate_test_event_data_for_data_type(data_type): # pragma: no cover
109109
UsageKey: UsageKey.from_string(
110110
"block-v1:edx+DemoX+Demo_course+type@video+block@UaEBjyMjcLW65gaTXggB93WmvoxGAJa0JeHRrDThk",
111111
),
112+
LibraryCollectionLocator: LibraryCollectionLocator.from_string('lib-collection:MITx:reallyhardproblems:col1'),
112113
LibraryLocatorV2: LibraryLocatorV2.from_string('lib:MITx:reallyhardproblems'),
113114
LibraryUsageLocatorV2: LibraryUsageLocatorV2.from_string('lb:MITx:reallyhardproblems:problem:problem1'),
114115
List[int]: [1, 2, 3],
@@ -125,6 +126,9 @@ def generate_test_event_data_for_data_type(data_type): # pragma: no cover
125126
"block-v1:edx+DemoX+Demo_course+type@video+block@UaEBjyMjcLW65gaTXggB93WmvoxGAJa0JeHRrDThk",
126127
)},
127128
dict[str, LibraryLocatorV2]: {'key': LibraryLocatorV2.from_string('lib:MITx:reallyhardproblems')},
129+
dict[str, LibraryCollectionLocator]: {
130+
'key': LibraryCollectionLocator.from_string('lib-collection:MITx:reallyhardproblems:col1'),
131+
},
128132
dict[str, LibraryUsageLocatorV2]: {
129133
'key': LibraryUsageLocatorV2.from_string('lb:MITx:reallyhardproblems:problem:problem1'),
130134
},

0 commit comments

Comments
 (0)