Skip to content

Commit

Permalink
feat: added get/set_draft_version to publishing API
Browse files Browse the repository at this point in the history
  • Loading branch information
ormsbee committed Oct 23, 2023
1 parent 261f7fc commit 6c86503
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 12 deletions.
39 changes: 34 additions & 5 deletions openedx_learning/core/publishing/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,40 @@ def publish_from_drafts(
return publish_log


def delete_drafts(
learning_package_id: int,
draft_qset: QuerySet,
):
pass
def get_draft_version(publishable_entity_id: int) -> PublishableEntityVersion | None:
"""
Return current draft PublishableEntityVersion for this PublishableEntity.
This function will return None if there is no current draft.
"""
try:
draft = Draft.objects.select_related("version").get(
entity_id=publishable_entity_id
)
except Draft.DoesNotExist:
# No draft was ever created.
return None

# draft.version could be None if it was set that way by set_draft_version.
# Setting the Draft.version to None is how we show that we've "deleted" the
# content in Studio.
return draft.version


def set_draft_version(publishable_entity_id: int, publishable_entity_version_pk: int | None) -> None:
"""
Modify the Draft of a PublishableEntity to be a PublishableEntityVersion.
This would most commonly be used to set the Draft to point to a newly
created PublishableEntityVersion that was created in Studio (because someone
edited some content). Setting a Draft's version to None is like deleting it
from Studio's editing point of view. We don't actually delete the Draft row
because we'll need that for publishing purposes (i.e. to delete content from
the published branch).
"""
draft = Draft.objects.get(entity_id=publishable_entity_id)
draft.version_id = publishable_entity_version_pk
draft.save()


def register_content_models(
Expand Down
52 changes: 45 additions & 7 deletions tests/openedx_learning/core/publishing/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
from django.core.exceptions import ValidationError
from django.test import TestCase

from openedx_learning.core.publishing.api import create_learning_package

from openedx_learning.core.publishing import api as publishing_api
from openedx_learning.core.publishing.models import Draft

class CreateLearningPackageTestCase(TestCase):
"""
Expand All @@ -22,7 +22,7 @@ def test_normal(self) -> None: # Note: we must specify '-> None' to opt in to t
key = "my_key"
title = "My Excellent Title with Emoji 🔥"
created = datetime(2023, 4, 2, 15, 9, 0, tzinfo=timezone.utc)
package = create_learning_package(key, title, created)
package = publishing_api.create_learning_package(key, title, created)

assert package.key == "my_key"
assert package.title == "My Excellent Title with Emoji 🔥"
Expand All @@ -41,7 +41,7 @@ def test_auto_datetime(self) -> None:
"""
key = "my_key"
title = "My Excellent Title with Emoji 🔥"
package = create_learning_package(key, title)
package = publishing_api.create_learning_package(key, title)

assert package.key == "my_key"
assert package.title == "My Excellent Title with Emoji 🔥"
Expand All @@ -62,7 +62,7 @@ def test_non_utc_time(self) -> None:
Require UTC timezone for created.
"""
with pytest.raises(ValidationError) as excinfo:
create_learning_package("my_key", "A Title", datetime(2023, 4, 2))
publishing_api.create_learning_package("my_key", "A Title", datetime(2023, 4, 2))
message_dict = excinfo.value.message_dict

# Both datetime fields should be marked as invalid
Expand All @@ -73,8 +73,46 @@ def test_already_exists(self) -> None:
"""
Raises ValidationError for duplicate keys.
"""
create_learning_package("my_key", "Original")
publishing_api.create_learning_package("my_key", "Original")
with pytest.raises(ValidationError) as excinfo:
create_learning_package("my_key", "Duplicate")
publishing_api.create_learning_package("my_key", "Duplicate")
message_dict = excinfo.value.message_dict
assert "key" in message_dict


class DraftTestCase(TestCase):

def test_draft_lifecycle(self):
"""
Test basic lifecycle of a Draft.
"""
created = datetime(2023, 4, 2, 15, 9, 0, tzinfo=timezone.utc)
package = publishing_api.create_learning_package(
"my_package_key",
"Draft Testing LearningPackage 🔥",
created=created,
)
entity = publishing_api.create_publishable_entity(
package.id,
"my_entity",
created,
created_by=None,
)
# Drafts are NOT created when a PublishableEntity is created, only when
# its first PublisahbleEntityVersion is.
assert publishing_api.get_draft_version(entity.id) is None

entity_version = publishing_api.create_publishable_entity_version(
entity_id=entity.id,
version_num=1,
title="An Entity 🌴",
created=created,
created_by=None,
)
assert entity_version == publishing_api.get_draft_version(entity.id)

# We never really remove rows from the table holding Drafts. We just
# mark the version as None.
publishing_api.set_draft_version(entity.id, None)
entity_version = publishing_api.get_draft_version(entity.id)
assert entity_version is None

0 comments on commit 6c86503

Please sign in to comment.