feat: Create content in library permission added to API response (#34934)
This commit is contained in:
@@ -12,9 +12,11 @@ from openedx.core.djangoapps.content_libraries.constants import (
|
||||
LICENSE_OPTIONS,
|
||||
)
|
||||
from openedx.core.djangoapps.content_libraries.models import (
|
||||
ContentLibraryPermission, ContentLibraryBlockImportTask
|
||||
ContentLibraryPermission, ContentLibraryBlockImportTask,
|
||||
ContentLibrary
|
||||
)
|
||||
from openedx.core.lib.api.serializers import CourseKeyField
|
||||
from . import permissions
|
||||
|
||||
|
||||
DATETIME_FORMAT = '%Y-%m-%dT%H:%M:%SZ'
|
||||
@@ -34,7 +36,7 @@ class ContentLibraryMetadataSerializer(serializers.Serializer):
|
||||
org = serializers.SlugField(source="key.org")
|
||||
slug = serializers.CharField(source="key.slug", validators=(validate_unicode_slug, ))
|
||||
bundle_uuid = serializers.UUIDField(format='hex_verbose', read_only=True)
|
||||
collection_uuid = serializers.UUIDField(format='hex_verbose', write_only=True)
|
||||
#collection_uuid = serializers.UUIDField(format='hex_verbose', write_only=True)
|
||||
title = serializers.CharField()
|
||||
description = serializers.CharField(allow_blank=True)
|
||||
num_blocks = serializers.IntegerField(read_only=True)
|
||||
@@ -46,6 +48,24 @@ class ContentLibraryMetadataSerializer(serializers.Serializer):
|
||||
has_unpublished_changes = serializers.BooleanField(read_only=True)
|
||||
has_unpublished_deletes = serializers.BooleanField(read_only=True)
|
||||
license = serializers.ChoiceField(choices=LICENSE_OPTIONS, default=ALL_RIGHTS_RESERVED)
|
||||
can_edit_library = serializers.SerializerMethodField()
|
||||
|
||||
def get_can_edit_library(self, obj):
|
||||
"""
|
||||
Verifies if the user in request has permission
|
||||
to edit a library.
|
||||
"""
|
||||
request = self.context.get('request', None)
|
||||
if request is None:
|
||||
return False
|
||||
|
||||
user = request.user
|
||||
|
||||
if not user:
|
||||
return False
|
||||
|
||||
library_obj = ContentLibrary.objects.get_by_key(obj.key)
|
||||
return user.has_perm(permissions.CAN_EDIT_THIS_CONTENT_LIBRARY, obj=library_obj)
|
||||
|
||||
|
||||
class ContentLibraryUpdateSerializer(serializers.Serializer):
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
"""
|
||||
Tests for Learning-Core-based Content Libraries
|
||||
"""
|
||||
import uuid
|
||||
from contextlib import contextmanager
|
||||
from io import BytesIO
|
||||
from urllib.parse import urlencode
|
||||
@@ -128,10 +127,6 @@ class ContentLibrariesRestApiTest(APITransactionTestCase):
|
||||
"description": description,
|
||||
"type": library_type,
|
||||
"license": license_type,
|
||||
# We're not actually using this value any more, but we're keeping it
|
||||
# in the API testing for backwards compatibility for just a little
|
||||
# longer. TODO: Remove this once the frontend stops sending it.
|
||||
"collection_uuid": uuid.uuid4(),
|
||||
}, expect_response)
|
||||
|
||||
def _list_libraries(self, query_params_dict=None, expect_response=200):
|
||||
|
||||
@@ -250,14 +250,6 @@ class LibraryRootView(GenericAPIView):
|
||||
)
|
||||
org = Organization.objects.get(short_name=org_name)
|
||||
|
||||
# Backwards compatibility: ignore the no-longer used "collection_uuid"
|
||||
# parameter. This was necessary with Blockstore, but not used for
|
||||
# Learning Core. TODO: This can be removed once the frontend stops
|
||||
# sending it to us. This whole bit of deserialization is kind of weird
|
||||
# though, with the renames and such. Look into this later for clennup.
|
||||
# Ref: https://github.com/openedx/edx-platform/issues/34283
|
||||
data.pop("collection_uuid", None)
|
||||
|
||||
try:
|
||||
with atomic():
|
||||
result = api.create_library(org=org, **data)
|
||||
@@ -283,7 +275,8 @@ class LibraryDetailsView(APIView):
|
||||
key = LibraryLocatorV2.from_string(lib_key_str)
|
||||
api.require_permission_for_library_key(key, request.user, permissions.CAN_VIEW_THIS_CONTENT_LIBRARY)
|
||||
result = api.get_library(key)
|
||||
return Response(ContentLibraryMetadataSerializer(result).data)
|
||||
serializer = ContentLibraryMetadataSerializer(result, context={'request': self.request})
|
||||
return Response(serializer.data)
|
||||
|
||||
@convert_exceptions
|
||||
def patch(self, request, lib_key_str):
|
||||
|
||||
Reference in New Issue
Block a user