15
15
16
16
Both GET and DELETE methods respond with a 404 if the setting cannot be found.
17
17
"""
18
- from opaque_keys import InvalidKeyError
19
- from opaque_keys .edx .keys import CourseKey , UsageKey
20
18
from rest_framework import status
21
- from rest_framework .response import Response
22
- from rest_framework .views import APIView
23
19
20
+ from ai_aside .api import AiAsideAPIView , APIResponse
21
+ from ai_aside .api .permissions import HasStudioWriteAccess
22
+ from ai_aside .api .view_utils import validate_course_key , validate_unit_key
24
23
from ai_aside .config_api .api import (
25
24
NotFoundError ,
26
25
delete_course_settings ,
34
33
)
35
34
36
35
37
- class APIResponse (Response ):
38
- """API Response"""
39
- def __init__ (self , data = None , http_status = None , content_type = None , success = False ):
40
- _status = http_status or status .HTTP_200_OK
41
- data = data or {}
42
- reply = {'response' : {'success' : success }}
43
- reply ['response' ].update (data )
44
- super ().__init__ (data = reply , status = _status , content_type = content_type )
45
-
46
-
47
- class CourseSummaryConfigEnabledAPIView (APIView ):
36
+ class CourseSummaryConfigEnabledAPIView (AiAsideAPIView ):
48
37
"""
49
38
Simple GET endpoint to expose whether the course may use summary config.
50
39
"""
51
40
41
+ permission_classes = (HasStudioWriteAccess ,)
42
+
52
43
def get (self , request , course_id = None ):
53
44
"""Expose whether the course may use summary config"""
54
45
if course_id is None :
55
46
return APIResponse (http_status = status .HTTP_404_NOT_FOUND )
56
47
57
- try :
58
- enabled = is_summary_config_enabled (CourseKey .from_string (course_id ))
59
- return APIResponse (success = True , data = {'enabled' : enabled })
60
- except InvalidKeyError :
61
- data = {'message' : 'Invalid Key' }
62
- return APIResponse (http_status = status .HTTP_400_BAD_REQUEST , data = data )
48
+ course_key = validate_course_key (course_id )
49
+ enabled = is_summary_config_enabled (course_key )
50
+ return APIResponse (success = True , data = {'enabled' : enabled })
63
51
64
52
65
- class CourseEnabledAPIView (APIView ):
53
+ class CourseEnabledAPIView (AiAsideAPIView ):
66
54
"""Handlers for course level settings"""
67
55
56
+ permission_classes = (HasStudioWriteAccess ,)
57
+
68
58
def get (self , request , course_id = None ):
69
59
"""Gets the enabled state for a course"""
70
60
if course_id is None :
71
61
return APIResponse (http_status = status .HTTP_404_NOT_FOUND )
72
62
73
63
try :
74
- settings = get_course_settings (CourseKey .from_string (course_id ))
75
- except InvalidKeyError :
76
- data = {'message' : 'Invalid Key' }
77
- return APIResponse (http_status = status .HTTP_400_BAD_REQUEST , data = data )
64
+ course_key = validate_course_key (course_id )
65
+ settings = get_course_settings (course_key )
78
66
except NotFoundError :
79
67
return APIResponse (http_status = status .HTTP_404_NOT_FOUND )
80
68
@@ -90,13 +78,10 @@ def post(self, request, course_id=None):
90
78
reset = request .data .get ('reset' )
91
79
92
80
try :
93
- course_key = CourseKey . from_string (course_id )
81
+ course_key = validate_course_key (course_id )
94
82
set_course_settings (course_key , {'enabled' : enabled })
95
83
if reset :
96
84
reset_course_unit_settings (course_key )
97
- except InvalidKeyError :
98
- data = {'message' : 'Invalid Key' }
99
- return APIResponse (http_status = status .HTTP_400_BAD_REQUEST , data = data )
100
85
except TypeError :
101
86
data = {'message' : 'Invalid parameters' }
102
87
return APIResponse (http_status = status .HTTP_400_BAD_REQUEST , data = data )
@@ -110,32 +95,28 @@ def delete(self, request, course_id=None):
110
95
return APIResponse (http_status = status .HTTP_404_NOT_FOUND )
111
96
112
97
try :
113
- delete_course_settings (CourseKey .from_string (course_id ))
114
- except InvalidKeyError :
115
- data = {'message' : 'Invalid Key' }
116
- return APIResponse (http_status = status .HTTP_400_BAD_REQUEST , data = data )
98
+ course_key = validate_course_key (course_id )
99
+ delete_course_settings (course_key )
117
100
except NotFoundError :
118
101
return APIResponse (http_status = status .HTTP_404_NOT_FOUND )
119
102
120
103
return APIResponse (success = True )
121
104
122
105
123
- class UnitEnabledAPIView (APIView ):
106
+ class UnitEnabledAPIView (AiAsideAPIView ):
124
107
"""Handlers for module level settings"""
125
108
109
+ permission_classes = (HasStudioWriteAccess ,)
110
+
126
111
def get (self , request , course_id = None , unit_id = None ):
127
112
"""Gets the enabled state for a unit"""
128
113
if course_id is None or unit_id is None :
129
114
return APIResponse (http_status = status .HTTP_404_NOT_FOUND )
130
115
131
116
try :
132
- settings = get_unit_settings (
133
- CourseKey .from_string (course_id ),
134
- UsageKey .from_string (unit_id ),
135
- )
136
- except InvalidKeyError :
137
- data = {'message' : 'Invalid Key' }
138
- return APIResponse (http_status = status .HTTP_400_BAD_REQUEST , data = data )
117
+ course_key = validate_course_key (course_id )
118
+ unit_key = validate_unit_key (unit_id )
119
+ settings = get_unit_settings (course_key , unit_key )
139
120
except NotFoundError :
140
121
return APIResponse (http_status = status .HTTP_404_NOT_FOUND )
141
122
@@ -147,13 +128,9 @@ def post(self, request, course_id=None, unit_id=None):
147
128
enabled = request .data .get ('enabled' )
148
129
149
130
try :
150
- set_unit_settings (
151
- CourseKey .from_string (course_id ),
152
- UsageKey .from_string (unit_id ),
153
- {'enabled' : enabled })
154
- except InvalidKeyError :
155
- data = {'message' : 'Invalid Key' }
156
- return APIResponse (http_status = status .HTTP_400_BAD_REQUEST , data = data )
131
+ course_key = validate_course_key (course_id )
132
+ unit_key = validate_unit_key (unit_id )
133
+ set_unit_settings (course_key , unit_key , {'enabled' : enabled })
157
134
except TypeError :
158
135
data = {'message' : 'Invalid parameters' }
159
136
return APIResponse (http_status = status .HTTP_400_BAD_REQUEST , data = data )
@@ -167,13 +144,9 @@ def delete(self, request, course_id=None, unit_id=None):
167
144
return APIResponse (http_status = status .HTTP_404_NOT_FOUND )
168
145
169
146
try :
170
- delete_unit_settings (
171
- CourseKey .from_string (course_id ),
172
- UsageKey .from_string (unit_id ),
173
- )
174
- except InvalidKeyError :
175
- data = {'message' : 'Invalid Key' }
176
- return APIResponse (http_status = status .HTTP_400_BAD_REQUEST , data = data )
147
+ course_key = validate_course_key (course_id )
148
+ unit_key = validate_unit_key (unit_id )
149
+ delete_unit_settings (course_key , unit_key ,)
177
150
except NotFoundError :
178
151
return APIResponse (http_status = status .HTTP_404_NOT_FOUND )
179
152
0 commit comments