|
| 1 | +""" |
| 2 | +REST API views for containers (sections, subsections, units) in content libraries |
| 3 | +""" |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +from functools import wraps |
| 7 | +import logging |
| 8 | + |
| 9 | +from django.conf import settings |
| 10 | +from django.contrib.auth import authenticate, get_user_model, login |
| 11 | +from django.contrib.auth.models import Group |
| 12 | +from django.core.exceptions import ObjectDoesNotExist |
| 13 | +from django.db.transaction import atomic, non_atomic_requests |
| 14 | +from django.http import Http404, HttpResponse, HttpResponseBadRequest, JsonResponse, StreamingHttpResponse |
| 15 | +from django.shortcuts import get_object_or_404 |
| 16 | +from django.urls import reverse |
| 17 | +from django.utils.decorators import method_decorator |
| 18 | +from django.utils.translation import gettext as _ |
| 19 | +from django.views.decorators.clickjacking import xframe_options_exempt |
| 20 | +from django.views.decorators.csrf import csrf_exempt |
| 21 | +from django.views.generic.base import TemplateResponseMixin, View |
| 22 | +from drf_yasg.utils import swagger_auto_schema |
| 23 | +from pylti1p3.contrib.django import DjangoCacheDataStorage, DjangoDbToolConf, DjangoMessageLaunch, DjangoOIDCLogin |
| 24 | +from pylti1p3.exception import LtiException, OIDCException |
| 25 | + |
| 26 | +import edx_api_doc_tools as apidocs |
| 27 | +from opaque_keys import InvalidKeyError |
| 28 | +from opaque_keys.edx.locator import LibraryLocatorV2, LibraryContainerLocator |
| 29 | +from rest_framework import status |
| 30 | +from rest_framework.exceptions import NotFound, PermissionDenied, ValidationError |
| 31 | +from rest_framework.generics import GenericAPIView |
| 32 | +from rest_framework.parsers import MultiPartParser |
| 33 | +from rest_framework.response import Response |
| 34 | +from rest_framework.views import APIView |
| 35 | +from rest_framework.viewsets import GenericViewSet |
| 36 | + |
| 37 | +from openedx.core.djangoapps.content_libraries import api, permissions |
| 38 | +from openedx.core.lib.api.view_utils import view_auth_classes |
| 39 | +from openedx.core.djangoapps.xblock import api as xblock_api |
| 40 | +from openedx.core.types.http import RestRequest |
| 41 | +from . import serializers |
| 42 | +from .utils import convert_exceptions |
| 43 | + |
| 44 | +User = get_user_model() |
| 45 | +log = logging.getLogger(__name__) |
| 46 | + |
| 47 | + |
| 48 | +@method_decorator(non_atomic_requests, name="dispatch") |
| 49 | +@view_auth_classes() |
| 50 | +class LibraryContainersView(GenericAPIView): |
| 51 | + """ |
| 52 | + Views to work with Containers in a specific content library. |
| 53 | + """ |
| 54 | + serializer_class = serializers.LibraryContainerMetadataSerializer |
| 55 | + |
| 56 | + @convert_exceptions |
| 57 | + @swagger_auto_schema( |
| 58 | + request_body=serializers.LibraryContainerMetadataSerializer, |
| 59 | + responses={200: serializers.LibraryContainerMetadataSerializer} |
| 60 | + ) |
| 61 | + def post(self, request, lib_key_str): |
| 62 | + """ |
| 63 | + Create a new Container in this content library |
| 64 | + """ |
| 65 | + library_key = LibraryLocatorV2.from_string(lib_key_str) |
| 66 | + api.require_permission_for_library_key(library_key, request.user, permissions.CAN_EDIT_THIS_CONTENT_LIBRARY) |
| 67 | + serializer = serializers.LibraryContainerMetadataSerializer(data=request.data) |
| 68 | + serializer.is_valid(raise_exception=True) |
| 69 | + |
| 70 | + # Create a new regular top-level block: |
| 71 | + container_type = serializer.validated_data['container_type'] |
| 72 | + _key, container = api.create_container(container_type) |
| 73 | + |
| 74 | + return Response(serializers.LibraryContainerMetadataSerializer(container).data) |
0 commit comments