Skip to content

Commit

Permalink
refactor: Create collection Rest API to auto-generate key
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisChV committed Sep 10, 2024
1 parent 3c79543 commit 8118b77
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
8 changes: 0 additions & 8 deletions openedx/core/djangoapps/content_libraries/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,14 +271,6 @@ class ContentLibraryCollectionUpdateSerializer(serializers.Serializer):
description = serializers.CharField(allow_blank=True)


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

key = serializers.CharField()


class UsageKeyV2Serializer(serializers.Serializer):
"""
Serializes a UsageKeyV2.
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 8118b77

Please sign in to comment.