-
Notifications
You must be signed in to change notification settings - Fork 2
[ACADEMIC-16210] Add endpoints for enabling courses/modules #38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -9,3 +9,4 @@ build/ | |
| dist/ | ||
| .vscode/ | ||
| ai_aside.egg-info/ | ||
| .DS_Store | ||
This file contains hidden or 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 hidden or 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
Empty file.
This file contains hidden or 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,17 @@ | ||
| """ | ||
| implements a simple REST API for updating unit and course settings | ||
| """ | ||
| from django.urls import re_path | ||
|
|
||
| from ai_aside.api.views import CourseEnabledAPIView, UnitEnabledAPIView | ||
| from ai_aside.constants import COURSE_ID_PATTERN, UNIT_ID_PATTERN | ||
|
|
||
| urlpatterns = [ | ||
| re_path(r'^v1/{course_id}/?$'.format( | ||
| course_id=COURSE_ID_PATTERN | ||
| ), CourseEnabledAPIView.as_view(), name='api-course-settings'), | ||
| re_path(r'^v1/{course_id}/{unit_id}/?$'.format( | ||
| course_id=COURSE_ID_PATTERN, | ||
| unit_id=UNIT_ID_PATTERN | ||
| ), UnitEnabledAPIView.as_view(), name='api-unit-settings'), | ||
| ] |
This file contains hidden or 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,191 @@ | ||
| """ | ||
| Implements a simple REST API for updating unit and course settings. | ||
|
|
||
| Setters: | ||
| POST: ai_aside/v1/:course_id - (payload: { enabled: True/False }) | ||
| POST: ai_aside/v1/:course_id/:unit_id - (payload: { enabled: True/False }) | ||
|
|
||
| Getters: | ||
| GET: ai_aside/v1/:course_id - (response: { success: True/False, enabled: True/False }) | ||
| GET: ai_aside/v1/:course_id/:unit_id - (response: { success: True/False, enabled: True/False }) | ||
|
|
||
| Delete: | ||
| DELETE: ai_aside/v1/:course_id - (response: { success: True/False }) | ||
| DELETE: ai_aside/v1/:course_id/:unit_id - (response: { success: True/False }) | ||
|
|
||
| Both GET and DELETE methods respond with a 404 if the setting cannot be found. | ||
| """ | ||
| import ast | ||
|
|
||
| from opaque_keys import InvalidKeyError | ||
| from opaque_keys.edx.keys import CourseKey, UsageKey | ||
| from rest_framework import status | ||
| from rest_framework.response import Response | ||
| from rest_framework.views import APIView | ||
|
|
||
| from ai_aside.models import AIAsideCourseEnabled, AIAsideUnitEnabled | ||
|
|
||
|
|
||
| class APIResponse(Response): | ||
| """API Response""" | ||
| def __init__(self, data=None, http_status=None, content_type=None, success=False): | ||
| _status = http_status or status.HTTP_200_OK | ||
| data = data or {} | ||
| reply = {'response': {'success': success}} | ||
| reply['response'].update(data) | ||
| super().__init__(data=reply, status=_status, content_type=content_type) | ||
|
|
||
|
|
||
| class CourseEnabledAPIView(APIView): | ||
| """Handlers for course level settings""" | ||
|
|
||
| def get(self, request, course_id=None): | ||
| """Gets the enabled state for a course""" | ||
| if course_id is None: | ||
| return APIResponse(http_status=status.HTTP_404_NOT_FOUND) | ||
|
|
||
| try: | ||
| course_key = CourseKey.from_string(course_id) | ||
| except InvalidKeyError: | ||
| data = {'message': 'Invalid Key'} | ||
| return APIResponse(http_status=status.HTTP_400_BAD_REQUEST, data=data) | ||
|
|
||
| try: | ||
| record = AIAsideCourseEnabled.objects.get( | ||
| course_key=course_key, | ||
| ) | ||
| except AIAsideCourseEnabled.DoesNotExist: | ||
| return APIResponse(http_status=status.HTTP_404_NOT_FOUND) | ||
|
|
||
| enabled = record.enabled | ||
|
|
||
| data = {'enabled': enabled} | ||
| return APIResponse(success=True, data=data) | ||
|
|
||
| def post(self, request, course_id=None): | ||
| """Sets the enabled state for a course""" | ||
| if (enabledStr := request.data.get('enabled')) is None: | ||
| data = {'message': 'Invalid parameters'} | ||
| return APIResponse(http_status=status.HTTP_400_BAD_REQUEST, data=data) | ||
|
|
||
| enabled = ast.literal_eval(enabledStr) | ||
|
|
||
| if course_id is None: | ||
| return APIResponse(http_status=status.HTTP_404_NOT_FOUND) | ||
|
|
||
| try: | ||
| course_key = CourseKey.from_string(course_id) | ||
| except InvalidKeyError: | ||
| data = {'message': 'Invalid Key'} | ||
| return APIResponse(http_status=status.HTTP_400_BAD_REQUEST, data=data) | ||
|
|
||
| AIAsideCourseEnabled.objects.update_or_create( | ||
| course_key=course_key, | ||
| defaults={'enabled': enabled} | ||
| ) | ||
|
|
||
| data = {'enabled': enabled} | ||
|
|
||
| return APIResponse(success=True, data=data) | ||
|
|
||
| def delete(self, request, course_id=None): | ||
| """Deletes the settings for a module""" | ||
| if course_id is None: | ||
| return APIResponse(http_status=status.HTTP_404_NOT_FOUND) | ||
|
|
||
| try: | ||
| course_key = CourseKey.from_string(course_id) | ||
| except InvalidKeyError: | ||
| data = {'message': 'Invalid Key'} | ||
| return APIResponse(http_status=status.HTTP_400_BAD_REQUEST, data=data) | ||
|
|
||
| try: | ||
| AIAsideCourseEnabled.objects.get( | ||
| course_key=course_key, | ||
| ).delete() | ||
| except AIAsideCourseEnabled.DoesNotExist: | ||
| return APIResponse(http_status=status.HTTP_404_NOT_FOUND) | ||
|
|
||
| return APIResponse(success=True) | ||
|
|
||
|
|
||
| class UnitEnabledAPIView(APIView): | ||
| """Handlers for module level settings""" | ||
| def get(self, request, course_id=None, unit_id=None): | ||
| """Gets the enabled state for a module""" | ||
| if course_id is None or unit_id is None: | ||
| return APIResponse(http_status=status.HTTP_404_NOT_FOUND) | ||
|
|
||
| try: | ||
| course_key = CourseKey.from_string(course_id) | ||
| unit_key = UsageKey.from_string(unit_id) | ||
| except InvalidKeyError: | ||
| data = {'message': 'Invalid Key'} | ||
| return APIResponse(http_status=status.HTTP_400_BAD_REQUEST, data=data) | ||
|
|
||
| try: | ||
| record = AIAsideUnitEnabled.objects.get( | ||
| course_key=course_key, | ||
| unit_key=unit_key, | ||
| ) | ||
| except AIAsideUnitEnabled.DoesNotExist: | ||
| return APIResponse(http_status=status.HTTP_404_NOT_FOUND) | ||
|
|
||
| if record is None: | ||
| return APIResponse(http_status=status.HTTP_404_NOT_FOUND) | ||
|
|
||
| enabled = record.enabled | ||
|
|
||
| data = {'enabled': enabled} | ||
| return APIResponse(success=True, data=data) | ||
|
|
||
| def post(self, request, course_id=None, unit_id=None): | ||
|
|
||
| """Sets the enabled state for a module""" | ||
| if course_id is None or unit_id is None: | ||
| return APIResponse(http_status=status.HTTP_404_NOT_FOUND) | ||
|
|
||
| if (enabledStr := request.data.get('enabled')) is None: | ||
| data = {'message': 'Invalid parameters'} | ||
| return APIResponse(http_status=status.HTTP_400_BAD_REQUEST, data=data) | ||
|
|
||
| enabled = ast.literal_eval(enabledStr) | ||
|
|
||
| try: | ||
| course_key = CourseKey.from_string(course_id) | ||
| unit_key = UsageKey.from_string(unit_id) | ||
| except InvalidKeyError: | ||
| data = {'message': 'Invalid Key'} | ||
| return APIResponse(http_status=status.HTTP_400_BAD_REQUEST, data=data) | ||
|
|
||
| AIAsideUnitEnabled.objects.update_or_create( | ||
| course_key=course_key, | ||
| unit_key=unit_key, | ||
| defaults={'enabled': enabled} | ||
| ) | ||
|
|
||
| data = {'enabled': enabled} | ||
|
|
||
| return APIResponse(success=True, data=data) | ||
|
|
||
| def delete(self, request, course_id=None, unit_id=None): | ||
| """Deletes the settings for a module""" | ||
| if course_id is None or unit_id is None: | ||
| return APIResponse(http_status=status.HTTP_404_NOT_FOUND) | ||
|
|
||
| try: | ||
| course_key = CourseKey.from_string(course_id) | ||
| unit_key = UsageKey.from_string(unit_id) | ||
| except InvalidKeyError: | ||
| data = {'message': 'Invalid Key'} | ||
| return APIResponse(http_status=status.HTTP_400_BAD_REQUEST, data=data) | ||
|
|
||
| try: | ||
| AIAsideUnitEnabled.objects.get( | ||
| course_key=course_key, | ||
| unit_key=unit_key, | ||
| ).delete() | ||
| except AIAsideUnitEnabled.DoesNotExist: | ||
| return APIResponse(http_status=status.HTTP_404_NOT_FOUND) | ||
|
|
||
| return APIResponse(success=True) |
This file contains hidden or 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 hidden or 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,9 @@ | ||
| """Constants for AI-Aside.""" | ||
|
|
||
| # Regex for Course ID URL patterns | ||
| COURSE_ID_REGEX = r'[^/+]+(/|\+)[^/+]+(/|\+)[^/]+' | ||
| COURSE_ID_PATTERN = r'(?P<course_id>{})'.format(COURSE_ID_REGEX) | ||
|
|
||
| # Regex for Usage ID URL patterns (Unit IDs) | ||
| UNIT_ID_REGEX = r'(?:i4x://?[^/]+/[^/]+/[^/]+/[^@]+(?:@[^/]+)?)|(?:[^/]+)' | ||
| UNIT_ID_PATTERN = r'(?P<unit_id>{})'.format(UNIT_ID_REGEX) |
This file contains hidden or 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,31 @@ | ||
| # Generated by Django 3.2.20 on 2023-07-20 15:44 | ||
|
|
||
| from django.db import migrations | ||
| import opaque_keys.edx.django.models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| dependencies = [ | ||
| ('ai_aside', '0001_initial'), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.AlterModelOptions( | ||
| name='aiasidecourseenabled', | ||
| options={}, | ||
| ), | ||
| migrations.AlterModelOptions( | ||
| name='aiasideunitenabled', | ||
| options={}, | ||
| ), | ||
| migrations.AlterField( | ||
| model_name='aiasidecourseenabled', | ||
| name='course_key', | ||
| field=opaque_keys.edx.django.models.CourseKeyField(db_index=True, max_length=255, unique=True), | ||
| ), | ||
| migrations.AlterUniqueTogether( | ||
| name='aiasideunitenabled', | ||
| unique_together={('course_key', 'unit_key')}, | ||
| ), | ||
| ] | ||
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,10 +1,13 @@ | ||
| """ | ||
| URLs for ai_aside. | ||
| """ | ||
| from django.urls import re_path # pylint: disable=unused-import | ||
| from django.urls import path, re_path # pylint: disable=unused-import | ||
| from django.views.generic import TemplateView # pylint: disable=unused-import | ||
|
|
||
| urlpatterns = [ | ||
| # TODO: Fill in URL patterns and views here. | ||
| # re_path(r'', TemplateView.as_view(template_name="ai_aside/base.html")), | ||
| ] | ||
| from ai_aside.api.urls import urlpatterns as apipatterns | ||
|
|
||
| app_name = 'ai_aside' | ||
|
|
||
| urlpatterns = [] | ||
|
|
||
| urlpatterns += apipatterns |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -5,3 +5,4 @@ Django # Web application framework | |
| edx_django_utils | ||
| xblock | ||
| edx-opaque-keys | ||
| djangorestframework | ||
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was trying to figure out why it was doing this and it must be removing the default sort 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, probably