Skip to content

Commit

Permalink
feat: Create content in library permission added to API response (#34934
Browse files Browse the repository at this point in the history
)
  • Loading branch information
ChrisChV authored Jun 24, 2024
1 parent 5e3181c commit 4fd60c4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
24 changes: 22 additions & 2 deletions openedx/core/djangoapps/content_libraries/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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)
Expand All @@ -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):
Expand Down
5 changes: 0 additions & 5 deletions openedx/core/djangoapps/content_libraries/tests/base.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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):
Expand Down
11 changes: 2 additions & 9 deletions openedx/core/djangoapps/content_libraries/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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):
Expand Down

0 comments on commit 4fd60c4

Please sign in to comment.