def post(self, request): """ Create a new content library. """ if not request.user.has_perm(permissions.CAN_CREATE_CONTENT_LIBRARY): raise PermissionDenied serializer = ContentLibraryMetadataSerializer(data=request.data) serializer.is_valid(raise_exception=True) data = dict(serializer.validated_data) # Converting this over because using the reserved names 'type' and 'license' would shadow the built-in # definitions elsewhere. data['library_type'] = data.pop('type') data['library_license'] = data.pop('license') # Get the organization short_name out of the "key.org" pseudo-field that the serializer added: org_name = data["key"]["org"] # Move "slug" out of the "key.slug" pseudo-field that the serializer added: data["slug"] = data.pop("key")["slug"] try: org = Organization.objects.get(short_name=org_name) except Organization.DoesNotExist: raise ValidationError( detail={ "org": "No such organization '{}' found.".format(org_name) }) try: result = api.create_library(org=org, **data) except api.LibraryAlreadyExists: raise ValidationError( detail={"slug": "A library with that ID already exists."}) # Grant the current user admin permissions on the library: api.set_library_user_permissions(result.key, request.user, api.AccessLevel.ADMIN_LEVEL) return Response(ContentLibraryMetadataSerializer(result).data)
def get(self, request): """ Return a list of all content libraries that the user has permission to view. """ serializer = ContentLibraryFilterSerializer(data=request.query_params) serializer.is_valid(raise_exception=True) org = serializer.validated_data['org'] library_type = serializer.validated_data['type'] text_search = serializer.validated_data['text_search'] paginator = LibraryApiPagination() queryset = api.get_libraries_for_user(request.user, org=org, library_type=library_type) if text_search: result = api.get_metadata_from_index(queryset, text_search=text_search) result = paginator.paginate_queryset(result, request) else: # We can paginate queryset early and prevent fetching unneeded metadata paginated_qs = paginator.paginate_queryset(queryset, request) result = api.get_metadata_from_index(paginated_qs) serializer = ContentLibraryMetadataSerializer(result, many=True) # Verify `pagination` param to maintain compatibility with older # non pagination-aware clients if request.GET.get('pagination', 'false').lower() == 'true': return paginator.get_paginated_response(serializer.data) return Response(serializer.data)
def get(self, request, lib_key_str): """ Get a specific content library """ 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)
def get(self, request): """ Return a list of all content libraries that the user has permission to view. This is a temporary view for development and returns at most 50 libraries. """ result = api.list_libraries_for_user(request.user) return Response(ContentLibraryMetadataSerializer(result, many=True).data)
def patch(self, request, lib_key_str): """ Update a content library """ key = LibraryLocatorV2.from_string(lib_key_str) api.require_permission_for_library_key(key, request.user, permissions.CAN_EDIT_THIS_CONTENT_LIBRARY) serializer = ContentLibraryUpdateSerializer(data=request.data, partial=True) serializer.is_valid(raise_exception=True) api.update_library(key, **serializer.validated_data) result = api.get_library(key) return Response(ContentLibraryMetadataSerializer(result).data)
def get(self, request): """ Return a list of all content libraries that the user has permission to view. """ paginator = LibraryRootPagination() queryset = api.get_libraries_for_user(request.user) paginated_qs = paginator.paginate_queryset(queryset, request) result = api.get_metadata_from_index(paginated_qs) serializer = ContentLibraryMetadataSerializer(result, many=True) # Verify `pagination` param to maintain compatibility with older # non pagination-aware clients if request.GET.get('pagination', 'false').lower() == 'true': return paginator.get_paginated_response(serializer.data) return Response(serializer.data)
def patch(self, request, lib_key_str): """ Update a content library """ key = LibraryLocatorV2.from_string(lib_key_str) api.require_permission_for_library_key(key, request.user, permissions.CAN_EDIT_THIS_CONTENT_LIBRARY) serializer = ContentLibraryUpdateSerializer(data=request.data, partial=True) serializer.is_valid(raise_exception=True) data = dict(serializer.validated_data) if 'type' in data: data['library_type'] = data.pop('type') try: api.update_library(key, **data) except api.IncompatibleTypesError as err: raise ValidationError({'type': str(err)}) result = api.get_library(key) return Response(ContentLibraryMetadataSerializer(result).data)