Skip to content

Commit

Permalink
refactor: Create correlative key in create collection rest api view
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisChV committed Sep 9, 2024
1 parent 57bb313 commit 44144c4
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions openedx/core/djangoapps/content_libraries/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -864,16 +864,23 @@ def post(self, request, lib_key_str):

key = slugify(title)

try:
result = authoring_api.create_collection(
learning_package_id=library.learning_package_id,
key=key,
title=title,
description=serializer.validated_data['description'],
created_by=request.user.id,
)
except IntegrityError:
return Response(status=status.HTTP_409_CONFLICT)
attempt = 0
result = None

# It's possible that the key is not unique in the database
# So to avoid that, we add a correlative number in the key
while not result:
modified_key = key if attempt == 0 else key + str(attempt)
try:
result = authoring_api.create_collection(
learning_package_id=library.learning_package_id,
key=modified_key,
title=title,
description=serializer.validated_data['description'],
created_by=request.user.id,
)
except IntegrityError:
attempt += 1

return Response(LibraryCollectionMetadataSerializer(result).data)

Expand Down

0 comments on commit 44144c4

Please sign in to comment.