views.py 3.33 KB
Newer Older
1 2
import logging

3
from django.contrib.auth.models import User
4
from django.http import Http404
5
from edx_rest_api_client import exceptions
6
from edx_rest_framework_extensions.authentication import JwtAuthentication
7
from rest_framework.authentication import SessionAuthentication
8
from rest_framework.generics import ListAPIView, RetrieveUpdateAPIView
9
from rest_framework.permissions import IsAuthenticated
10
from rest_framework.views import APIView
11
from rest_framework_oauth.authentication import OAuth2Authentication
12 13

from commerce.api.v1.models import Course
14
from commerce.api.v1.permissions import ApiKeyOrModelPermission, IsAuthenticatedOrActivationOverridden
15
from commerce.api.v1.serializers import CourseSerializer
16
from commerce.utils import is_account_activation_requirement_disabled
17
from course_modes.models import CourseMode
18
from openedx.core.djangoapps.commerce.utils import ecommerce_api_client
19
from openedx.core.lib.api.mixins import PutAsCreateMixin
20
from util.json_request import JsonResponse
21 22 23 24 25 26

log = logging.getLogger(__name__)


class CourseListView(ListAPIView):
    """ List courses and modes. """
27
    authentication_classes = (JwtAuthentication, OAuth2Authentication, SessionAuthentication,)
28 29
    permission_classes = (IsAuthenticated,)
    serializer_class = CourseSerializer
30
    pagination_class = None
31 32

    def get_queryset(self):
33
        return list(Course.iterator())
34 35


36
class CourseRetrieveUpdateView(PutAsCreateMixin, RetrieveUpdateAPIView):
37 38 39 40
    """ Retrieve, update, or create courses/modes. """
    lookup_field = 'id'
    lookup_url_kwarg = 'course_id'
    model = CourseMode
41
    authentication_classes = (JwtAuthentication, OAuth2Authentication, SessionAuthentication,)
42 43 44
    permission_classes = (ApiKeyOrModelPermission,)
    serializer_class = CourseSerializer

45 46 47 48 49
    # Django Rest Framework v3 requires that we provide a queryset.
    # Note that we're overriding `get_object()` below to return a `Course`
    # rather than a CourseMode, so this isn't really used.
    queryset = CourseMode.objects.all()

50 51 52 53 54 55 56 57
    def get_object(self, queryset=None):
        course_id = self.kwargs.get(self.lookup_url_kwarg)
        course = Course.get(course_id)

        if course:
            return course

        raise Http404
58 59 60 61 62

    def pre_save(self, obj):
        # There is nothing to pre-save. The default behavior changes the Course.id attribute from
        # a CourseKey to a string, which is not desired.
        pass
63 64 65 66 67


class OrderView(APIView):
    """ Retrieve order details. """

68
    authentication_classes = (JwtAuthentication, SessionAuthentication,)
69
    permission_classes = (IsAuthenticatedOrActivationOverridden,)
70

71
    def get(self, request, number):
72
        """ HTTP handler. """
73 74 75 76 77
        # If the account activation requirement is disabled for this installation, override the
        # anonymous user object attached to the request with the actual user object (if it exists)
        if not request.user.is_authenticated() and is_account_activation_requirement_disabled():
            try:
                request.user = User.objects.get(id=request.session._session_cache['_auth_user_id'])
78
            except User.DoesNotExist:
79
                return JsonResponse(status=403)
80 81 82 83 84
        try:
            order = ecommerce_api_client(request.user).orders(number).get()
            return JsonResponse(order)
        except exceptions.HttpNotFoundError:
            return JsonResponse(status=404)