-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Create DRF for course team (#32782)
- Loading branch information
1 parent
59782fa
commit ddb092c
Showing
8 changed files
with
213 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
cms/djangoapps/contentstore/rest_api/v1/serializers/course_team.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
""" | ||
API Serializers for course team | ||
""" | ||
|
||
from rest_framework import serializers | ||
|
||
|
||
class UserCourseTeamSerializer(serializers.Serializer): | ||
"""Serializer for user in course team""" | ||
email = serializers.CharField() | ||
id = serializers.IntegerField() | ||
role = serializers.CharField() | ||
username = serializers.CharField() | ||
|
||
|
||
class CourseTeamSerializer(serializers.Serializer): | ||
"""Serializer for course team context data""" | ||
show_transfer_ownership_hint = serializers.BooleanField() | ||
users = UserCourseTeamSerializer(many=True) | ||
allow_actions = serializers.BooleanField() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
cms/djangoapps/contentstore/rest_api/v1/views/course_team.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
""" API Views for course team """ | ||
|
||
import edx_api_doc_tools as apidocs | ||
from opaque_keys.edx.keys import CourseKey | ||
from rest_framework.request import Request | ||
from rest_framework.response import Response | ||
from rest_framework.views import APIView | ||
|
||
from cms.djangoapps.contentstore.utils import get_course_team | ||
from common.djangoapps.student.auth import STUDIO_VIEW_USERS, get_user_permissions | ||
from openedx.core.lib.api.view_utils import DeveloperErrorViewMixin, verify_course_exists, view_auth_classes | ||
|
||
from ..serializers import CourseTeamSerializer | ||
|
||
|
||
@view_auth_classes(is_authenticated=True) | ||
class CourseTeamView(DeveloperErrorViewMixin, APIView): | ||
""" | ||
View for getting data for course team. | ||
""" | ||
@apidocs.schema( | ||
parameters=[ | ||
apidocs.string_parameter("course_id", apidocs.ParameterLocation.PATH, description="Course ID"), | ||
], | ||
responses={ | ||
200: CourseTeamSerializer, | ||
401: "The requester is not authenticated.", | ||
403: "The requester cannot access the specified course.", | ||
404: "The requested course does not exist.", | ||
}, | ||
) | ||
@verify_course_exists() | ||
def get(self, request: Request, course_id: str): | ||
""" | ||
Get all CMS users who are editors for the specified course. | ||
**Example Request** | ||
GET /api/contentstore/v1/course_team/{course_id} | ||
**Response Values** | ||
If the request is successful, an HTTP 200 "OK" response is returned. | ||
The HTTP 200 response contains a single dict that contains keys that | ||
are the course's team info. | ||
**Example Response** | ||
```json | ||
{ | ||
"show_transfer_ownership_hint": true, | ||
"users": [ | ||
{ | ||
"email": "[email protected]", | ||
"id": "3", | ||
"role": "instructor", | ||
"username": "edx" | ||
}, | ||
], | ||
"allow_actions": true | ||
} | ||
``` | ||
""" | ||
user = request.user | ||
course_key = CourseKey.from_string(course_id) | ||
|
||
user_perms = get_user_permissions(user, course_key) | ||
if not user_perms & STUDIO_VIEW_USERS: | ||
self.permission_denied(request) | ||
|
||
course_team_context = get_course_team(user, course_key, user_perms) | ||
serializer = CourseTeamSerializer(course_team_context) | ||
return Response(serializer.data) |
78 changes: 78 additions & 0 deletions
78
cms/djangoapps/contentstore/rest_api/v1/views/tests/test_course_team.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
""" | ||
Unit tests for course team. | ||
""" | ||
import ddt | ||
from django.urls import reverse | ||
from rest_framework import status | ||
|
||
from common.djangoapps.student.roles import CourseInstructorRole, CourseStaffRole | ||
from common.djangoapps.student.tests.factories import UserFactory | ||
from cms.djangoapps.contentstore.tests.utils import CourseTestCase | ||
|
||
from ...mixins import PermissionAccessMixin | ||
|
||
|
||
@ddt.ddt | ||
class CourseTeamViewTest(CourseTestCase, PermissionAccessMixin): | ||
""" | ||
Tests for CourseTeamView. | ||
""" | ||
|
||
def setUp(self): | ||
super().setUp() | ||
self.url = reverse( | ||
"cms.djangoapps.contentstore:v1:course_team", | ||
kwargs={"course_id": self.course.id}, | ||
) | ||
|
||
def get_expected_course_data(self, instructor=None, staff=None): | ||
"""Utils is used to get expected data for course team""" | ||
users = [] | ||
|
||
if instructor: | ||
users.append({ | ||
"email": instructor.email, | ||
"id": instructor.id, | ||
"role": "instructor", | ||
"username": instructor.username | ||
}) | ||
|
||
if staff: | ||
users.append({ | ||
"email": staff.email, | ||
"id": staff.id, | ||
"role": "staff", | ||
"username": staff.username | ||
}) | ||
|
||
return { | ||
"show_transfer_ownership_hint": False, | ||
"users": users, | ||
"allow_actions": True, | ||
} | ||
|
||
def create_course_user_roles(self, course_id): | ||
"""Get course staff and instructor roles user""" | ||
instructor = UserFactory() | ||
CourseInstructorRole(course_id).add_users(instructor) | ||
staff = UserFactory() | ||
CourseStaffRole(course_id).add_users(staff) | ||
|
||
return instructor, staff | ||
|
||
def test_course_team_response(self): | ||
"""Check successful response content""" | ||
response = self.client.get(self.url) | ||
expected_response = self.get_expected_course_data() | ||
|
||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertDictEqual(expected_response, response.data) | ||
|
||
def test_users_response(self): | ||
"""Test the response for users in the course.""" | ||
instructor, staff = self.create_course_user_roles(self.course.id) | ||
response = self.client.get(self.url) | ||
users_response = [dict(item) for item in response.data["users"]] | ||
expected_response = self.get_expected_course_data(instructor, staff) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertListEqual(expected_response["users"], users_response) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters