Skip to content
Open
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
6 changes: 6 additions & 0 deletions openedx/core/djangoapps/content_libraries/api/containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
from functools import cache
from uuid import UUID, uuid4

from django.conf import settings
from django.core.exceptions import ObjectDoesNotExist
from django.db.models import F
from django.utils.text import slugify
from django.utils.translation import gettext as _
from opaque_keys.edx.locator import LibraryContainerLocator, LibraryLocatorV2, LibraryUsageLocatorV2
from openedx_content import api as content_api
from openedx_content.models_api import Container, PublishLogRecord
Expand All @@ -41,6 +43,7 @@
get_entity_from_key,
library_container_locator,
)
from .exceptions import BlockLimitReachedError
from .serializers import ContainerSerializer

if typing.TYPE_CHECKING:
Expand Down Expand Up @@ -128,6 +131,9 @@ def create_container(
assert isinstance(library_key, LibraryLocatorV2)
content_library = ContentLibrary.objects.get_by_key(library_key)
assert content_library.learning_package_id # Should never happen but we made this a nullable field so need to check
component_count = content_api.get_all_drafts(content_library.learning_package_id).count()
if component_count + 1 > settings.MAX_BLOCKS_PER_CONTENT_LIBRARY:
raise BlockLimitReachedError(_("Library cannot have more than {} Components.").format(settings.MAX_BLOCKS_PER_CONTENT_LIBRARY))
if slug is None:
# Automatically generate a slug. Append a random suffix so it should be unique.
slug = slugify(title, allow_unicode=True) + "-" + uuid4().hex[-6:]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,20 @@ def test_library_blocks_limit(self):
# Second block should throw error
self._add_block_to_library(lib_id, "problem", "problem1", expect_response=400)

def test_library_container_blocks_limit(self):
"""
Test that container creation respects MAX_BLOCKS_PER_CONTENT_LIBRARY.
"""
with self.settings(MAX_BLOCKS_PER_CONTENT_LIBRARY=1):
lib = self._create_library(
slug="test_lib_container_limits",
title="Container Limits Test Library",
description="Testing container limits in a library",
)
lib_id = lib["id"]
self._add_block_to_library(lib_id, "html", "html1")
self._create_container(lib_id, "unit", None, "Unit 1", expect_response=400)

def test_library_paste_xblock(self):
"""
Check the a new block is created in the library after pasting from clipboard.
Expand Down