forked from openedx/edx-platform
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: [AXIMST-64] create API for group configurations (#2492)
- Loading branch information
1 parent
bd39293
commit 1a7b858
Showing
8 changed files
with
314 additions
and
79 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
57 changes: 57 additions & 0 deletions
57
cms/djangoapps/contentstore/rest_api/v1/serializers/group_configurations.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,57 @@ | ||
""" | ||
API Serializers for course's settings group configurations. | ||
""" | ||
|
||
from rest_framework import serializers | ||
|
||
|
||
class GroupConfigurationUsageSerializer(serializers.Serializer): | ||
""" | ||
Serializer for representing nested usage inside configuration. | ||
""" | ||
|
||
label = serializers.CharField() | ||
url = serializers.CharField() | ||
|
||
|
||
class GroupConfigurationGroupSerializer(serializers.Serializer): | ||
""" | ||
Serializer for representing nested group inside configuration. | ||
""" | ||
|
||
id = serializers.IntegerField() | ||
name = serializers.CharField() | ||
usage = GroupConfigurationUsageSerializer(allow_null=True, many=True) | ||
version = serializers.IntegerField() | ||
|
||
|
||
class GroupConfigurationItemSerializer(serializers.Serializer): | ||
""" | ||
Serializer for representing group configurations item. | ||
""" | ||
|
||
active = serializers.BooleanField() | ||
description = serializers.CharField() | ||
groups = GroupConfigurationGroupSerializer(allow_null=True, many=True) | ||
id = serializers.IntegerField() | ||
name = serializers.CharField() | ||
parameters = serializers.DictField() | ||
read_only = serializers.BooleanField(required=False) | ||
scheme = serializers.CharField() | ||
version = serializers.IntegerField() | ||
|
||
|
||
class CourseGroupConfigurationsSerializer(serializers.Serializer): | ||
""" | ||
Serializer for representing course's settings group configurations. | ||
""" | ||
|
||
all_group_configurations = GroupConfigurationItemSerializer(many=True) | ||
experiment_group_configurations = GroupConfigurationItemSerializer( | ||
allow_null=True, many=True | ||
) | ||
mfe_proctored_exam_settings_url = serializers.CharField( | ||
required=False, allow_null=True, allow_blank=True | ||
) | ||
should_show_enrollment_track = serializers.BooleanField() | ||
should_show_experiment_groups = 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
98 changes: 98 additions & 0 deletions
98
cms/djangoapps/contentstore/rest_api/v1/views/group_configurations.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,98 @@ | ||
""" API Views for course's settings group configurations """ | ||
|
||
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_group_configurations_context | ||
from cms.djangoapps.contentstore.rest_api.v1.serializers import ( | ||
CourseGroupConfigurationsSerializer, | ||
) | ||
from common.djangoapps.student.auth import has_studio_read_access | ||
from openedx.core.lib.api.view_utils import ( | ||
DeveloperErrorViewMixin, | ||
verify_course_exists, | ||
view_auth_classes, | ||
) | ||
from xmodule.modulestore.django import modulestore | ||
|
||
|
||
@view_auth_classes(is_authenticated=True) | ||
class CourseGroupConfigurationsView(DeveloperErrorViewMixin, APIView): | ||
""" | ||
View for course's settings group configurations. | ||
""" | ||
|
||
@apidocs.schema( | ||
parameters=[ | ||
apidocs.string_parameter( | ||
"course_id", apidocs.ParameterLocation.PATH, description="Course ID" | ||
), | ||
], | ||
responses={ | ||
200: CourseGroupConfigurationsSerializer, | ||
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 an object containing course's settings group configurations. | ||
**Example Request** | ||
GET /api/contentstore/v1/group_configurations/{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 settings group configurations. | ||
**Example Response** | ||
```json | ||
{ | ||
"all_group_configurations": [ | ||
{ | ||
"active": true, | ||
"description": "The groups in this configuration can be mapped to cohorts in the Instructor.", | ||
"groups": [ | ||
{ | ||
"id": 593758473, | ||
"name": "My Content Group", | ||
"usage": [], | ||
"version": 1 | ||
} | ||
], | ||
"id": 1791848226, | ||
"name": "Content Groups", | ||
"parameters": {}, | ||
"read_only": false, | ||
"scheme": "cohort", | ||
"version": 3 | ||
} | ||
], | ||
"experiment_group_configurations": null, | ||
"mfe_proctored_exam_settings_url": "", | ||
"should_show_enrollment_track": false, | ||
"should_show_experiment_groups": false | ||
} | ||
``` | ||
""" | ||
course_key = CourseKey.from_string(course_id) | ||
store = modulestore() | ||
|
||
if not has_studio_read_access(request.user, course_key): | ||
self.permission_denied(request) | ||
|
||
with store.bulk_operations(course_key): | ||
course = modulestore().get_course(course_key) | ||
group_configurations_context = get_group_configurations_context(course, store) | ||
serializer = CourseGroupConfigurationsSerializer(group_configurations_context) | ||
return Response(serializer.data) |
58 changes: 58 additions & 0 deletions
58
cms/djangoapps/contentstore/rest_api/v1/views/tests/test_group_configurations.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,58 @@ | ||
""" | ||
Unit tests for the course's setting group configuration. | ||
""" | ||
from django.urls import reverse | ||
from rest_framework import status | ||
|
||
from cms.djangoapps.contentstore.course_group_config import ( | ||
CONTENT_GROUP_CONFIGURATION_NAME, | ||
) | ||
from cms.djangoapps.contentstore.tests.utils import CourseTestCase | ||
from cms.djangoapps.contentstore.views.tests.test_certificates import HelperMethods | ||
from xmodule.partitions.partitions import ( | ||
Group, | ||
UserPartition, | ||
) # lint-amnesty, pylint: disable=wrong-import-order | ||
|
||
from ...mixins import PermissionAccessMixin | ||
|
||
|
||
class CourseGroupConfigurationsViewTest( | ||
CourseTestCase, PermissionAccessMixin, HelperMethods | ||
): | ||
""" | ||
Tests for CourseGroupConfigurationsView. | ||
""" | ||
|
||
def setUp(self): | ||
super().setUp() | ||
self.url = reverse( | ||
"cms.djangoapps.contentstore:v1:group_configurations", | ||
kwargs={"course_id": self.course.id}, | ||
) | ||
|
||
def test_success_response(self): | ||
""" | ||
Check that endpoint is valid and success response. | ||
""" | ||
self.course.user_partitions = [ | ||
UserPartition( | ||
0, | ||
"First name", | ||
"First description", | ||
[Group(0, "Group A"), Group(1, "Group B"), Group(2, "Group C")], | ||
), # lint-amnesty, pylint: disable=line-too-long | ||
] | ||
self.save_course() | ||
|
||
if "split_test" not in self.course.advanced_modules: | ||
self.course.advanced_modules.append("split_test") | ||
self.store.update_item(self.course, self.user.id) | ||
|
||
response = self.client.get(self.url) | ||
self.assertEqual(len(response.data["all_group_configurations"]), 1) | ||
self.assertEqual(len(response.data["experiment_group_configurations"]), 1) | ||
self.assertContains(response, "First name", count=1) | ||
self.assertContains(response, "Group C") | ||
self.assertContains(response, CONTENT_GROUP_CONFIGURATION_NAME) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) |
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
Oops, something went wrong.