Skip to content

Commit

Permalink
Update collections crud rest api (#683)
Browse files Browse the repository at this point in the history
* refactor: Update description as optional in ContentLibraryCollectionUpdateSerializer
* refactor: Create collection Rest API to auto-generate key
  • Loading branch information
ChrisChV authored Sep 11, 2024
1 parent d5aeff8 commit 48a1667
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 22 deletions.
10 changes: 1 addition & 9 deletions openedx/core/djangoapps/content_libraries/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,15 +268,7 @@ class ContentLibraryCollectionUpdateSerializer(serializers.Serializer):
"""

title = serializers.CharField()
description = serializers.CharField()


class ContentLibraryCollectionCreateSerializer(ContentLibraryCollectionUpdateSerializer):
"""
Serializer for adding a Collection in a Content Library
"""

key = serializers.CharField()
description = serializers.CharField(allow_blank=True)


class UsageKeyV2Serializer(serializers.Serializer):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,16 +162,15 @@ def test_create_library_collection(self):
Test creating a Content Library Collection
"""
post_data = {
"key": "COL4",
"title": "Collection 4",
"description": "Description for Collection 4",
}
resp = self.client.post(
URL_LIB_COLLECTIONS.format(lib_key=self.lib1.library_key), post_data, format="json"
)

# Check that the new Content Library Collection is returned in response and created in DB
assert resp.status_code == 200
post_data["key"] = 'collection-4'
self.assertDictContainsEntries(resp.data, post_data)

created_collection = Collection.objects.get(id=resp.data["id"])
Expand All @@ -183,7 +182,6 @@ def test_create_library_collection(self):

with self.as_user(reader):
post_data = {
"key": "COL5",
"title": "Collection 5",
"description": "Description for Collection 5",
}
Expand All @@ -193,6 +191,31 @@ def test_create_library_collection(self):

assert resp.status_code == 403

def test_create_collection_same_key(self):
"""
Test collection creation with same key
"""
post_data = {
"title": "Same Collection",
"description": "Description for Collection 4",
}
self.client.post(
URL_LIB_COLLECTIONS.format(lib_key=self.lib1.library_key), post_data, format="json"
)

for i in range(100):
resp = self.client.post(
URL_LIB_COLLECTIONS.format(lib_key=self.lib1.library_key), post_data, format="json"
)
expected_data = {
"key": f"same-collection-{i + 1}",
"title": "Same Collection",
"description": "Description for Collection 4",
}

assert resp.status_code == 200
self.assertDictContainsEntries(resp.data, expected_data)

def test_create_invalid_library_collection(self):
"""
Test creating an invalid Content Library Collection
Expand Down
34 changes: 24 additions & 10 deletions openedx/core/djangoapps/content_libraries/views_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from __future__ import annotations

from django.db.models import QuerySet
from django.utils.text import slugify
from django.db import transaction

from rest_framework.decorators import action
from rest_framework.response import Response
Expand All @@ -21,7 +23,6 @@
from openedx.core.djangoapps.content_libraries.serializers import (
ContentLibraryCollectionSerializer,
ContentLibraryCollectionComponentsUpdateSerializer,
ContentLibraryCollectionCreateSerializer,
ContentLibraryCollectionUpdateSerializer,
)

Expand Down Expand Up @@ -109,17 +110,30 @@ def create(self, request, *args, **kwargs) -> Response:
Create a Collection that belongs to a Content Library
"""
content_library = self.get_content_library()
create_serializer = ContentLibraryCollectionCreateSerializer(data=request.data)
create_serializer = ContentLibraryCollectionUpdateSerializer(data=request.data)
create_serializer.is_valid(raise_exception=True)

collection = api.create_library_collection(
library_key=content_library.library_key,
content_library=content_library,
collection_key=create_serializer.validated_data["key"],
title=create_serializer.validated_data["title"],
description=create_serializer.validated_data["description"],
created_by=request.user.id,
)
title = create_serializer.validated_data['title']
key = slugify(title)

attempt = 0
collection = None
while not collection:
modified_key = key if attempt == 0 else key + '-' + str(attempt)
try:
# Add transaction here to avoid TransactionManagementError on retry
with transaction.atomic():
collection = api.create_library_collection(
library_key=content_library.library_key,
content_library=content_library,
collection_key=modified_key,
title=title,
description=create_serializer.validated_data["description"],
created_by=request.user.id,
)
except api.LibraryCollectionAlreadyExists:
attempt += 1

serializer = self.get_serializer(collection)

return Response(serializer.data)
Expand Down

0 comments on commit 48a1667

Please sign in to comment.