diff --git a/openedx/core/djangoapps/content_libraries/serializers.py b/openedx/core/djangoapps/content_libraries/serializers.py index 13c6a756fd31..ee27e4464365 100644 --- a/openedx/core/djangoapps/content_libraries/serializers.py +++ b/openedx/core/djangoapps/content_libraries/serializers.py @@ -12,9 +12,11 @@ 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): diff --git a/openedx/core/djangoapps/content_libraries/tests/base.py b/openedx/core/djangoapps/content_libraries/tests/base.py index 2bb94e3d87c7..ef6cc180f19a 100644 --- a/openedx/core/djangoapps/content_libraries/tests/base.py +++ b/openedx/core/djangoapps/content_libraries/tests/base.py @@ -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 @@ def _create_library( "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): diff --git a/openedx/core/djangoapps/content_libraries/views.py b/openedx/core/djangoapps/content_libraries/views.py index 78bc5ac205d7..fba8d42689cb 100644 --- a/openedx/core/djangoapps/content_libraries/views.py +++ b/openedx/core/djangoapps/content_libraries/views.py @@ -250,14 +250,6 @@ def post(self, request): ) 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 @@ def get(self, request, lib_key_str): 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):