Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Create content in library permission added to API response #34934

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ChrisChV @bradenmacdonald: Did you mean to leave a commented out line of code? Shouldn't it just be deleted?

I only happened to look at this PR because the tests failed on master (at first) due to some issue uploading an artifact, but it passed after I re-ran (or maybe after Braden had)?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, just an oversight on my part. I should have asked for that line to be deleted during the review. We're doing lots of work in this area though, so we'll remove it in a follow-up pass.

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
Loading