Skip to content

Commit 5549fe4

Browse files
committed
refactor!: use LibraryContainerLocator in LibraryContainerData
The library_key can be inferred from the container_key
1 parent 6995de8 commit 5549fe4

6 files changed

Lines changed: 46 additions & 19 deletions

openedx_events/content_authoring/data.py

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

1313
import attr
1414
from opaque_keys.edx.keys import CourseKey, UsageKey
15-
from opaque_keys.edx.locator import LibraryCollectionLocator, LibraryLocatorV2, LibraryUsageLocatorV2
15+
from opaque_keys.edx.locator import (
16+
LibraryCollectionLocator,
17+
LibraryContainerLocator,
18+
LibraryLocatorV2,
19+
LibraryUsageLocatorV2,
20+
)
1621

1722

1823
@attr.s(frozen=True)
@@ -233,12 +238,10 @@ class LibraryContainerData:
233238
Data related to a library container that has changed.
234239
235240
Attributes:
236-
library_key (LibraryLocatorV2): a key that represents a content library.
237-
container_key (str): identifies the container within the library's learning package (e.g. unit, section)
241+
container_key (LibraryContainerLocator): identifies the container (e.g. unit, section)
238242
background (bool): indicate whether the sender doesn't want to wait for handler to finish execution,
239243
i.e., the handler can run the task in background. By default it is False.
240244
"""
241245

242-
library_key = attr.ib(type=LibraryLocatorV2)
243-
container_key = attr.ib(type=str)
246+
container_key = attr.ib(type=LibraryContainerLocator)
244247
background = attr.ib(type=bool, default=False)

openedx_events/event_bus/avro/custom_serializers.py

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

99
from ccx_keys.locator import CCXLocator
1010
from opaque_keys.edx.keys import CourseKey, UsageKey
11-
from opaque_keys.edx.locator import LibraryCollectionLocator, LibraryLocatorV2, LibraryUsageLocatorV2
11+
from opaque_keys.edx.locator import (
12+
LibraryCollectionLocator,
13+
LibraryContainerLocator,
14+
LibraryLocatorV2,
15+
LibraryUsageLocatorV2,
16+
)
1217

1318
from openedx_events.event_bus.avro.types import PYTHON_TYPE_TO_AVRO_MAPPING
1419

@@ -131,6 +136,25 @@ def deserialize(data: str):
131136
return LibraryCollectionLocator.from_string(data)
132137

133138

139+
class LibraryContainerLocatorAvroSerializer(BaseCustomTypeAvroSerializer):
140+
"""
141+
CustomTypeAvroSerializer for LibraryContainerLocator class.
142+
"""
143+
144+
cls = LibraryContainerLocator
145+
field_type = PYTHON_TYPE_TO_AVRO_MAPPING[str]
146+
147+
@staticmethod
148+
def serialize(obj) -> str:
149+
"""Serialize obj into string."""
150+
return str(obj)
151+
152+
@staticmethod
153+
def deserialize(data: str):
154+
"""Deserialize string into obj."""
155+
return LibraryContainerLocator.from_string(data)
156+
157+
134158
class LibraryLocatorV2AvroSerializer(BaseCustomTypeAvroSerializer):
135159
"""
136160
CustomTypeAvroSerializer for LibraryLocatorV2 class.
@@ -195,6 +219,7 @@ def deserialize(data: str):
195219
CcxCourseLocatorAvroSerializer,
196220
DatetimeAvroSerializer,
197221
LibraryCollectionLocatorAvroSerializer,
222+
LibraryContainerLocatorAvroSerializer,
198223
LibraryLocatorV2AvroSerializer,
199224
LibraryUsageLocatorV2AvroSerializer,
200225
UsageKeyAvroSerializer,

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

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

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

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

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

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

openedx_events/event_bus/avro/tests/test_avro.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@
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 LibraryCollectionLocator, LibraryLocatorV2, LibraryUsageLocatorV2
14+
from opaque_keys.edx.locator import (
15+
LibraryCollectionLocator,
16+
LibraryContainerLocator,
17+
LibraryLocatorV2,
18+
LibraryUsageLocatorV2,
19+
)
1520

1621
from openedx_events.event_bus.avro.deserializer import AvroSignalDeserializer, deserialize_bytes_to_event_data
1722
from openedx_events.event_bus.avro.serializer import AvroSignalSerializer, serialize_event_data_to_bytes
@@ -110,6 +115,9 @@ def generate_test_event_data_for_data_type(data_type): # pragma: no cover
110115
"block-v1:edx+DemoX+Demo_course+type@video+block@UaEBjyMjcLW65gaTXggB93WmvoxGAJa0JeHRrDThk",
111116
),
112117
LibraryCollectionLocator: LibraryCollectionLocator.from_string('lib-collection:MITx:reallyhardproblems:col1'),
118+
LibraryContainerLocator: LibraryCollectionLocator.from_string(
119+
'lct:MITx:reallyhardproblems:unit:test-container',
120+
),
113121
LibraryLocatorV2: LibraryLocatorV2.from_string('lib:MITx:reallyhardproblems'),
114122
LibraryUsageLocatorV2: LibraryUsageLocatorV2.from_string('lb:MITx:reallyhardproblems:problem:problem1'),
115123
List[int]: [1, 2, 3],
@@ -129,6 +137,9 @@ def generate_test_event_data_for_data_type(data_type): # pragma: no cover
129137
dict[str, LibraryCollectionLocator]: {
130138
'key': LibraryCollectionLocator.from_string('lib-collection:MITx:reallyhardproblems:col1'),
131139
},
140+
dict[str, LibraryContainerLocator]: {
141+
'key': LibraryContainerLocator.from_string('lct:MITx:reallyhardproblems:unit:test-container'),
142+
},
132143
dict[str, LibraryUsageLocatorV2]: {
133144
'key': LibraryUsageLocatorV2.from_string('lb:MITx:reallyhardproblems:problem:problem1'),
134145
},

0 commit comments

Comments
 (0)