Skip to content

Commit be03938

Browse files
committed
refactor: update to use Learning Core's new public API
This also bumps our openedx-learning dependency to 0.10.0 (the first version with the new openedx_learning.api package).
1 parent f820961 commit be03938

File tree

13 files changed

+59
-56
lines changed

13 files changed

+59
-56
lines changed

cms/envs/common.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1881,9 +1881,9 @@
18811881
'openedx_events',
18821882

18831883
# Learning Core Apps, used by v2 content libraries (content_libraries app)
1884-
"openedx_learning.core.components",
1885-
"openedx_learning.core.contents",
1886-
"openedx_learning.core.publishing",
1884+
"openedx_learning.apps.authoring.components",
1885+
"openedx_learning.apps.authoring.contents",
1886+
"openedx_learning.apps.authoring.publishing",
18871887
]
18881888

18891889

lms/envs/common.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3385,9 +3385,9 @@ def _make_locale_paths(settings): # pylint: disable=missing-function-docstring
33853385
'openedx_events',
33863386

33873387
# Learning Core Apps, used by v2 content libraries (content_libraries app)
3388-
"openedx_learning.core.components",
3389-
"openedx_learning.core.contents",
3390-
"openedx_learning.core.publishing",
3388+
"openedx_learning.apps.authoring.components",
3389+
"openedx_learning.apps.authoring.contents",
3390+
"openedx_learning.apps.authoring.publishing",
33913391
]
33923392

33933393

openedx/core/djangoapps/content_libraries/api.py

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,8 @@
8585
LIBRARY_BLOCK_DELETED,
8686
LIBRARY_BLOCK_UPDATED,
8787
)
88-
from openedx_learning.core.publishing import api as publishing_api
89-
from openedx_learning.core.contents import api as contents_api
90-
from openedx_learning.core.components import api as components_api
91-
from openedx_learning.core.components.models import Component
88+
from openedx_learning.api import authoring as authoring_api
89+
from openedx_learning.api.authoring_models import Component, MediaType
9290
from organizations.models import Organization
9391
from xblock.core import XBlock
9492
from xblock.exceptions import XBlockNotFoundError
@@ -327,18 +325,18 @@ def get_library(library_key):
327325
"""
328326
ref = ContentLibrary.objects.get_by_key(library_key)
329327
learning_package = ref.learning_package
330-
num_blocks = publishing_api.get_all_drafts(learning_package.id).count()
331-
last_publish_log = publishing_api.get_last_publish(learning_package.id)
332-
has_unpublished_changes = publishing_api.get_entities_with_unpublished_changes(learning_package.id) \
333-
.exists()
328+
num_blocks = authoring_api.get_all_drafts(learning_package.id).count()
329+
last_publish_log = authoring_api.get_last_publish(learning_package.id)
330+
has_unpublished_changes = authoring_api.get_entities_with_unpublished_changes(learning_package.id) \
331+
.exists()
334332

335333
# TODO: I'm doing this one to match already-existing behavior, but this is
336334
# something that we should remove. It exists to accomodate some complexities
337335
# with how Blockstore staged changes, but Learning Core works differently,
338336
# and has_unpublished_changes should be sufficient.
339337
# Ref: https://github.com/openedx/edx-platform/issues/34283
340-
has_unpublished_deletes = publishing_api.get_entities_with_unpublished_deletes(learning_package.id) \
341-
.exists()
338+
has_unpublished_deletes = authoring_api.get_entities_with_unpublished_deletes(learning_package.id) \
339+
.exists()
342340

343341
# Learning Core doesn't really have a notion of a global version number,but
344342
# we can sort of approximate it by using the primary key of the last publish
@@ -415,7 +413,7 @@ def create_library(
415413
allow_public_read=allow_public_read,
416414
license=library_license,
417415
)
418-
learning_package = publishing_api.create_learning_package(
416+
learning_package = authoring_api.create_learning_package(
419417
key=str(ref.library_key),
420418
title=title,
421419
description=description,
@@ -556,7 +554,7 @@ def update_library(
556554
content_lib.save()
557555

558556
if learning_pkg_changed:
559-
publishing_api.update_learning_package(
557+
authoring_api.update_learning_package(
560558
content_lib.learning_package_id,
561559
title=title,
562560
description=description,
@@ -614,7 +612,7 @@ def get_library_components(library_key, text_search=None, block_types=None) -> Q
614612
"""
615613
lib = ContentLibrary.objects.get_by_key(library_key) # type: ignore[attr-defined]
616614
learning_package = lib.learning_package
617-
components = components_api.get_components(
615+
components = authoring_api.get_components(
618616
learning_package.id,
619617
draft=True,
620618
namespace='xblock.v1',
@@ -693,13 +691,13 @@ def set_library_block_olx(usage_key, new_olx_str):
693691
now = datetime.now(tz=timezone.utc)
694692

695693
with transaction.atomic():
696-
new_content = contents_api.get_or_create_text_content(
694+
new_content = authoring_api.get_or_create_text_content(
697695
component.learning_package_id,
698696
get_or_create_olx_media_type(usage_key.block_type).id,
699697
text=new_olx_str,
700698
created=now,
701699
)
702-
components_api.create_next_version(
700+
authoring_api.create_next_version(
703701
component.pk,
704702
title=new_title,
705703
content_to_replace={
@@ -736,7 +734,7 @@ def create_library_block(library_key, block_type, definition_id):
736734
)
737735

738736
# If adding a component would take us over our max, return an error.
739-
component_count = publishing_api.get_all_drafts(ref.learning_package.id).count()
737+
component_count = authoring_api.get_all_drafts(ref.learning_package.id).count()
740738
if component_count + 1 > settings.MAX_BLOCKS_PER_CONTENT_LIBRARY:
741739
raise BlockLimitReachedError(
742740
_("Library cannot have more than {} Components").format(
@@ -785,14 +783,14 @@ def _component_exists(usage_key: UsageKeyV2) -> bool:
785783
return True
786784

787785

788-
def get_or_create_olx_media_type(block_type: str) -> contents_api.MediaType:
786+
def get_or_create_olx_media_type(block_type: str) -> MediaType:
789787
"""
790788
Get or create a MediaType for the block type.
791789
792790
Learning Core stores all Content with a Media Type (a.k.a. MIME type). For
793791
OLX, we use the "application/vnd.*" convention, per RFC 6838.
794792
"""
795-
return contents_api.get_or_create_media_type(
793+
return authoring_api.get_or_create_media_type(
796794
f"application/vnd.openedx.xblock.v1.{block_type}+xml"
797795
)
798796

@@ -819,24 +817,24 @@ def _create_component_for_block(content_lib, usage_key):
819817
learning_package = content_lib.learning_package
820818

821819
with transaction.atomic():
822-
component_type = components_api.get_or_create_component_type(
820+
component_type = authoring_api.get_or_create_component_type(
823821
"xblock.v1", usage_key.block_type
824822
)
825-
component, component_version = components_api.create_component_and_version(
823+
component, component_version = authoring_api.create_component_and_version(
826824
learning_package.id,
827825
component_type=component_type,
828826
local_key=usage_key.block_id,
829827
title=display_name,
830828
created=now,
831829
created_by=None,
832830
)
833-
content = contents_api.get_or_create_text_content(
831+
content = authoring_api.get_or_create_text_content(
834832
learning_package.id,
835833
get_or_create_olx_media_type(usage_key.block_type).id,
836834
text=xml_text,
837835
created=now,
838836
)
839-
components_api.create_component_version_content(
837+
authoring_api.create_component_version_content(
840838
component_version.pk,
841839
content.id,
842840
key="block.xml",
@@ -849,7 +847,7 @@ def delete_library_block(usage_key, remove_from_parent=True):
849847
Delete the specified block from this library (soft delete).
850848
"""
851849
component = get_component_from_usage_key(usage_key)
852-
publishing_api.soft_delete_draft(component.pk)
850+
authoring_api.soft_delete_draft(component.pk)
853851

854852
LIBRARY_BLOCK_DELETED.send_event(
855853
library_block=LibraryBlockData(
@@ -938,7 +936,7 @@ def publish_changes(library_key):
938936
"""
939937
learning_package = ContentLibrary.objects.get_by_key(library_key).learning_package
940938

941-
publishing_api.publish_all_drafts(learning_package.id)
939+
authoring_api.publish_all_drafts(learning_package.id)
942940

943941
CONTENT_LIBRARY_UPDATED.send_event(
944942
content_library=ContentLibraryData(
@@ -954,7 +952,7 @@ def revert_changes(library_key):
954952
last published version.
955953
"""
956954
learning_package = ContentLibrary.objects.get_by_key(library_key).learning_package
957-
publishing_api.reset_drafts_to_published(learning_package.id)
955+
authoring_api.reset_drafts_to_published(learning_package.id)
958956

959957
CONTENT_LIBRARY_UPDATED.send_event(
960958
content_library=ContentLibraryData(

openedx/core/djangoapps/content_libraries/library_context.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from openedx.core.djangoapps.content_libraries.models import ContentLibrary
1111
from openedx.core.djangoapps.xblock.api import LearningContext
1212

13-
from openedx_learning.core.components import api as components_api
13+
from openedx_learning.api import authoring as authoring_api
1414

1515
log = logging.getLogger(__name__)
1616

@@ -87,7 +87,7 @@ def block_exists(self, usage_key):
8787
if learning_package is None:
8888
return False
8989

90-
return components_api.component_exists_by_key(
90+
return authoring_api.component_exists_by_key(
9191
learning_package.id,
9292
namespace='xblock.v1',
9393
type_name=usage_key.block_type,

openedx/core/djangoapps/content_libraries/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
LIBRARY_TYPES, COMPLEX, LICENSE_OPTIONS,
5858
ALL_RIGHTS_RESERVED,
5959
)
60-
from openedx_learning.core.publishing.models import LearningPackage
60+
from openedx_learning.api.authoring_models import LearningPackage
6161
from organizations.models import Organization # lint-amnesty, pylint: disable=wrong-import-order
6262

6363
from .apps import ContentLibrariesConfig

openedx/core/djangoapps/xblock/api.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,21 @@
1515

1616
from django.urls import reverse
1717
from django.utils.translation import gettext as _
18-
from openedx_learning.core.components import api as components_api
19-
from openedx_learning.core.components.models import Component
20-
from openedx_learning.core.publishing import api as publishing_api
18+
from openedx_learning.api import authoring as authoring_api
19+
from openedx_learning.api.authoring_models import Component
2120
from opaque_keys.edx.keys import UsageKeyV2
2221
from opaque_keys.edx.locator import BundleDefinitionLocator, LibraryUsageLocatorV2
23-
2422
from rest_framework.exceptions import NotFound
2523
from xblock.core import XBlock
2624
from xblock.exceptions import NoSuchViewError
2725
from xblock.plugin import PluginMissingError
2826

2927
from openedx.core.djangoapps.xblock.apps import get_xblock_app_config
3028
from openedx.core.djangoapps.xblock.learning_context.manager import get_learning_context_impl
31-
3229
from openedx.core.djangoapps.xblock.runtime.learning_core_runtime import (
3330
LearningCoreFieldData,
3431
LearningCoreXBlockRuntime,
3532
)
36-
37-
3833
from openedx.core.djangoapps.xblock.runtime.runtime import XBlockRuntimeSystem as _XBlockRuntimeSystem
3934
from .utils import get_secure_token_for_xblock_handler, get_xblock_id_for_anonymous_user
4035

@@ -192,10 +187,10 @@ def get_component_from_usage_key(usage_key: UsageKeyV2) -> Component:
192187
This is a lower-level function that will return a Component even if there is
193188
no current draft version of that Component (because it's been soft-deleted).
194189
"""
195-
learning_package = publishing_api.get_learning_package_by_key(
190+
learning_package = authoring_api.get_learning_package_by_key(
196191
str(usage_key.context_key)
197192
)
198-
return components_api.get_component_by_key(
193+
return authoring_api.get_component_by_key(
199194
learning_package.id,
200195
namespace='xblock.v1',
201196
type_name=usage_key.block_type,

openedx/core/djangoapps/xblock/runtime/learning_core_runtime.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010
from django.core.exceptions import ObjectDoesNotExist
1111
from django.db.transaction import atomic
1212

13-
from openedx_learning.core.components import api as components_api
14-
from openedx_learning.core.contents import api as contents_api
15-
from openedx_learning.core.publishing import api as publishing_api
13+
from openedx_learning.api import authoring as authoring_api
1614

1715
from lxml import etree
1816

@@ -239,16 +237,16 @@ def save_block(self, block):
239237
usage_key = block.scope_ids.usage_id
240238
with atomic():
241239
component = self._get_component_from_usage_key(usage_key)
242-
block_media_type = contents_api.get_or_create_media_type(
240+
block_media_type = authoring_api.get_or_create_media_type(
243241
f"application/vnd.openedx.xblock.v1.{usage_key.block_type}+xml"
244242
)
245-
content = contents_api.get_or_create_text_content(
243+
content = authoring_api.get_or_create_text_content(
246244
component.learning_package_id,
247245
block_media_type.id,
248246
text=serialized.olx_str,
249247
created=now,
250248
)
251-
components_api.create_next_version(
249+
authoring_api.create_next_version(
252250
component.pk,
253251
title=block.display_name,
254252
content_to_replace={
@@ -267,9 +265,9 @@ def _get_component_from_usage_key(self, usage_key):
267265
TODO: This is the third place where we're implementing this. Figure out
268266
where the definitive place should be and have everything else call that.
269267
"""
270-
learning_package = publishing_api.get_learning_package_by_key(str(usage_key.lib_key))
268+
learning_package = authoring_api.get_learning_package_by_key(str(usage_key.lib_key))
271269
try:
272-
component = components_api.get_component_by_key(
270+
component = authoring_api.get_component_by_key(
273271
learning_package.id,
274272
namespace='xblock.v1',
275273
type_name=usage_key.block_type,

requirements/constraints.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ libsass==0.10.0
9797
click==8.1.6
9898

9999
# pinning this version to avoid updates while the library is being developed
100-
openedx-learning==0.9.4
100+
openedx-learning==0.10.0
101101

102102
# Open AI version 1.0.0 dropped support for openai.ChatCompletion which is currently in use in enterprise.
103103
openai<=0.28.1

requirements/edx/base.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,7 @@ openedx-filters==1.8.1
775775
# -r requirements/edx/kernel.in
776776
# lti-consumer-xblock
777777
# ora2
778-
openedx-learning==0.9.4
778+
openedx-learning==0.10.0
779779
# via
780780
# -c requirements/edx/../constraints.txt
781781
# -r requirements/edx/kernel.in

requirements/edx/development.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1331,7 +1331,7 @@ openedx-filters==1.8.1
13311331
# -r requirements/edx/testing.txt
13321332
# lti-consumer-xblock
13331333
# ora2
1334-
openedx-learning==0.9.4
1334+
openedx-learning==0.10.0
13351335
# via
13361336
# -c requirements/edx/../constraints.txt
13371337
# -r requirements/edx/doc.txt

requirements/edx/doc.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -906,7 +906,7 @@ openedx-filters==1.8.1
906906
# -r requirements/edx/base.txt
907907
# lti-consumer-xblock
908908
# ora2
909-
openedx-learning==0.9.4
909+
openedx-learning==0.10.0
910910
# via
911911
# -c requirements/edx/../constraints.txt
912912
# -r requirements/edx/base.txt

requirements/edx/testing.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -991,7 +991,7 @@ openedx-filters==1.8.1
991991
# -r requirements/edx/base.txt
992992
# lti-consumer-xblock
993993
# ora2
994-
openedx-learning==0.9.4
994+
openedx-learning==0.10.0
995995
# via
996996
# -c requirements/edx/../constraints.txt
997997
# -r requirements/edx/base.txt

setup.cfg

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ root_packages =
6262
lms
6363
cms
6464
openedx
65+
openedx_learning
6566
include_external_packages = True
6667
contract_types =
6768
# Our custom contract which checks that we're only importing from 'api.py'
@@ -185,3 +186,14 @@ allowed_modules =
185186
# Only imports from api.py are allowed elsewhere in the code
186187
# See https://open-edx-proposals.readthedocs.io/en/latest/best-practices/oep-0049-django-app-patterns.html#api-py
187188
api
189+
190+
[importlinter:contract:3]
191+
name = Do not import apps from openedx-learning (only import from openedx_learning.api.* and openedx_learning.lib.*).
192+
type = forbidden
193+
source_modules =
194+
cms
195+
lms
196+
openedx
197+
forbidden_modules =
198+
openedx_learning.apps
199+
allow_indirect_imports = True

0 commit comments

Comments
 (0)