|
2 | 2 |
|
3 | 3 | import edx_api_doc_tools as apidocs |
4 | 4 | from django.conf import settings |
| 5 | +from edx_rest_framework_extensions.auth.jwt.authentication import JwtAuthentication |
| 6 | +from edx_rest_framework_extensions.auth.session.authentication import SessionAuthenticationAllowInactiveUser |
5 | 7 | from organizations import api as org_api |
| 8 | +from rest_framework import viewsets |
| 9 | +from rest_framework.decorators import action |
| 10 | +from rest_framework.permissions import IsAuthenticated |
6 | 11 | from rest_framework.request import Request |
7 | 12 | from rest_framework.response import Response |
8 | 13 | from rest_framework.views import APIView |
|
13 | 18 | from ..serializers import CourseHomeTabSerializer, LibraryTabSerializer, StudioHomeSerializer |
14 | 19 |
|
15 | 20 |
|
| 21 | +# ADR 0028 – consolidated from HomePageView, HomePageCoursesView, HomePageLibrariesView |
| 22 | +class HomeViewSet(viewsets.ViewSet): |
| 23 | + """ |
| 24 | + ViewSet for the Studio home page. Registered via DefaultRouter (basename ``home``). |
| 25 | +
|
| 26 | + Router-generated URLs: |
| 27 | + GET /api/contentstore/v1/home/ → list (aggregated home context) |
| 28 | + GET /api/contentstore/v1/home/courses/ → courses (course list only) |
| 29 | + GET /api/contentstore/v1/home/libraries/→ libraries (library list only) |
| 30 | +
|
| 31 | + ADR 0025 compliance notes: |
| 32 | + - Three different serializers are returned by ``get_serializer_class()`` depending |
| 33 | + on the action. ``serializer_class`` is set to the default (``StudioHomeSerializer``). |
| 34 | + - All response formatting is handled by the respective serializer class. |
| 35 | + """ |
| 36 | + |
| 37 | + authentication_classes = (JwtAuthentication, SessionAuthenticationAllowInactiveUser) |
| 38 | + permission_classes = (IsAuthenticated,) # ADR 0026 |
| 39 | + serializer_class = StudioHomeSerializer # default; overridden by get_serializer_class() |
| 40 | + |
| 41 | + def get_serializer_class(self): |
| 42 | + """Return the appropriate serializer class for the current action.""" |
| 43 | + if self.action == 'courses': |
| 44 | + return CourseHomeTabSerializer |
| 45 | + if self.action == 'libraries': |
| 46 | + return LibraryTabSerializer |
| 47 | + return StudioHomeSerializer |
| 48 | + |
| 49 | + def get_serializer(self, *args, **kwargs): |
| 50 | + """Return a serializer instance using the action-appropriate class.""" |
| 51 | + return self.get_serializer_class()(*args, **kwargs) |
| 52 | + |
| 53 | + @apidocs.schema( |
| 54 | + parameters=[ |
| 55 | + apidocs.string_parameter( |
| 56 | + "org", |
| 57 | + apidocs.ParameterLocation.QUERY, |
| 58 | + description="Query param to filter by course org", |
| 59 | + )], |
| 60 | + responses={ |
| 61 | + 200: StudioHomeSerializer, |
| 62 | + 401: "The requester is not authenticated.", |
| 63 | + }, |
| 64 | + ) |
| 65 | + def list(self, request: Request): |
| 66 | + """ |
| 67 | + Get an object containing all courses and libraries on home page. |
| 68 | +
|
| 69 | + **Example Request** |
| 70 | +
|
| 71 | + GET /api/contentstore/v1/home/ |
| 72 | + """ |
| 73 | + home_context = get_home_context(request, True) |
| 74 | + home_context.update({ |
| 75 | + # 'allow_to_create_new_org' is actually about auto-creating organizations |
| 76 | + # (e.g. when creating a course or library), so we add an additional test. |
| 77 | + 'allow_to_create_new_org': ( |
| 78 | + home_context['can_create_organizations'] and |
| 79 | + org_api.is_autocreate_enabled() |
| 80 | + ), |
| 81 | + 'studio_name': settings.STUDIO_NAME, |
| 82 | + 'studio_short_name': settings.STUDIO_SHORT_NAME, |
| 83 | + 'studio_request_email': settings.FEATURES.get('STUDIO_REQUEST_EMAIL', ''), |
| 84 | + 'tech_support_email': settings.TECH_SUPPORT_EMAIL, |
| 85 | + 'platform_name': settings.PLATFORM_NAME, |
| 86 | + 'user_is_active': request.user.is_active, |
| 87 | + }) |
| 88 | + serializer = self.get_serializer(home_context) |
| 89 | + return Response(serializer.data) |
| 90 | + |
| 91 | + @apidocs.schema( |
| 92 | + parameters=[ |
| 93 | + apidocs.string_parameter( |
| 94 | + "org", |
| 95 | + apidocs.ParameterLocation.QUERY, |
| 96 | + description="Query param to filter by course org", |
| 97 | + )], |
| 98 | + responses={ |
| 99 | + 200: CourseHomeTabSerializer, |
| 100 | + 401: "The requester is not authenticated.", |
| 101 | + }, |
| 102 | + ) |
| 103 | + @action(detail=False, methods=['get'], url_path='courses', url_name='courses') |
| 104 | + def courses(self, request: Request): |
| 105 | + """ |
| 106 | + Get an object containing all courses. |
| 107 | +
|
| 108 | + **Example Request** |
| 109 | +
|
| 110 | + GET /api/contentstore/v1/home/courses/ |
| 111 | + """ |
| 112 | + active_courses, archived_courses, in_process_course_actions = get_course_context(request) |
| 113 | + courses_context = { |
| 114 | + "courses": active_courses, |
| 115 | + "archived_courses": archived_courses, |
| 116 | + "in_process_course_actions": in_process_course_actions, |
| 117 | + } |
| 118 | + serializer = self.get_serializer(courses_context) |
| 119 | + return Response(serializer.data) |
| 120 | + |
| 121 | + @apidocs.schema( |
| 122 | + parameters=[ |
| 123 | + apidocs.string_parameter( |
| 124 | + "org", |
| 125 | + apidocs.ParameterLocation.QUERY, |
| 126 | + description="Query param to filter by course org", |
| 127 | + ), |
| 128 | + apidocs.query_parameter( |
| 129 | + "is_migrated", |
| 130 | + bool, |
| 131 | + description=( |
| 132 | + "Query param to filter by migrated status of library." |
| 133 | + " If present (true or false), it will filter by migration status" |
| 134 | + " else it will return all legacy libraries." |
| 135 | + ), |
| 136 | + ) |
| 137 | + ], |
| 138 | + responses={ |
| 139 | + 200: LibraryTabSerializer, |
| 140 | + 401: "The requester is not authenticated.", |
| 141 | + }, |
| 142 | + ) |
| 143 | + @action(detail=False, methods=['get'], url_path='libraries', url_name='libraries') |
| 144 | + def libraries(self, request: Request): |
| 145 | + """ |
| 146 | + Get an object containing all libraries on home page. |
| 147 | +
|
| 148 | + **Example Request** |
| 149 | +
|
| 150 | + GET /api/contentstore/v1/home/libraries/ |
| 151 | + """ |
| 152 | + library_context = get_library_context(request) |
| 153 | + serializer = self.get_serializer(library_context) |
| 154 | + return Response(serializer.data) |
| 155 | + |
| 156 | + |
| 157 | +# DEPRECATED (ADR 0028): Use HomeViewSet instead. |
| 158 | +# Will be removed after one named release. |
| 159 | +# Use GET home/, home/courses/, home/libraries/ instead. |
16 | 160 | @view_auth_classes(is_authenticated=True) |
17 | 161 | class HomePageView(APIView): |
18 | 162 | """ |
@@ -99,11 +243,13 @@ def get(self, request: Request): |
99 | 243 | return Response(serializer.data) |
100 | 244 |
|
101 | 245 |
|
102 | | -@view_auth_classes(is_authenticated=True) |
103 | 246 | class HomePageCoursesView(APIView): |
104 | 247 | """ |
105 | 248 | View for getting all courses and libraries available to the logged in user. |
106 | 249 | """ |
| 250 | + authentication_classes = (JwtAuthentication, SessionAuthenticationAllowInactiveUser) |
| 251 | + permission_classes = (IsAuthenticated,) |
| 252 | + serializer_class = CourseHomeTabSerializer |
107 | 253 | @apidocs.schema( |
108 | 254 | parameters=[ |
109 | 255 | apidocs.string_parameter( |
@@ -170,7 +316,7 @@ def get(self, request: Request): |
170 | 316 | "archived_courses": archived_courses, |
171 | 317 | "in_process_course_actions": in_process_course_actions, |
172 | 318 | } |
173 | | - serializer = CourseHomeTabSerializer(courses_context) |
| 319 | + serializer = self.serializer_class(courses_context) |
174 | 320 | return Response(serializer.data) |
175 | 321 |
|
176 | 322 |
|
|
0 commit comments