forked from openedx/edx-platform
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #201 from appsembler/appsembler/ficus/feature/taxoman
Conditionally enable Taxoman for Ficus
- Loading branch information
Showing
12 changed files
with
250 additions
and
0 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
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,2 @@ | ||
|
||
API_VERSION = 'v0' |
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,24 @@ | ||
|
||
from contentstore.courseware_index import CoursewareSearchIndexer | ||
from opaque_keys.edx.keys import CourseKey | ||
from xmodule.modulestore.django import modulestore | ||
|
||
def reindex_course(course_id): | ||
""" | ||
Arguments: | ||
course_id - The course id for a course. This is the 'course_id' property | ||
for the course as returend from: | ||
<lms-host>/api/courses/v1/courses/ | ||
Raises: | ||
InvalidKeyError - if the opaque course | ||
SearchIndexingError - If the reindexing fails | ||
References | ||
course.py#reindex_course_and_check_access | ||
""" | ||
course_key = CourseKey.from_string(course_id) | ||
with modulestore().bulk_operations(course_key): | ||
return CoursewareSearchIndexer.do_course_reindex(modulestore(), | ||
course_key) |
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,10 @@ | ||
from rest_framework.permissions import BasePermission | ||
|
||
|
||
class IsStaffUser(BasePermission): | ||
""" | ||
Allow access to only staff users | ||
""" | ||
def has_permission(self, request, view): | ||
return request.user and request.user.is_active and ( | ||
request.user.is_staff or request.user.is_superuser) |
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,9 @@ | ||
from django.conf.urls import url | ||
|
||
from . import API_VERSION, views | ||
|
||
urlpatterns = [ | ||
url(r'^$', views.SearchIndex.as_view(), name='search_api_index'), | ||
url(r'^{}/reindex-course'.format(API_VERSION), | ||
views.CourseIndexer.as_view(), name='reindex-course'), | ||
] |
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,97 @@ | ||
|
||
import json | ||
|
||
from django.conf import settings | ||
from django.http import HttpResponse | ||
|
||
from rest_framework.authentication import ( | ||
BasicAuthentication, | ||
SessionAuthentication, | ||
TokenAuthentication, | ||
) | ||
from rest_framework.decorators import api_view, authentication_classes, permission_classes | ||
from django.views.decorators.csrf import csrf_exempt | ||
from django.views.decorators.http import require_http_methods | ||
from rest_framework.permissions import IsAuthenticated | ||
from rest_framework.response import Response | ||
from rest_framework.views import APIView | ||
|
||
from opaque_keys import InvalidKeyError | ||
|
||
from . import api | ||
from .permissions import IsStaffUser | ||
|
||
# Restrict access to the LMS server | ||
ALLOWED_ORIGIN = settings.LMS_BASE | ||
|
||
description = """ | ||
Appembler Open edX search api. | ||
Opens up access to Open edX'sa search infrastructure via HTTP (REST) API interfaces. | ||
""" | ||
|
||
class SearchIndex(APIView): | ||
authentication_classes = ( | ||
BasicAuthentication, | ||
SessionAuthentication, | ||
TokenAuthentication | ||
) | ||
|
||
permission_classes = ( IsAuthenticated, IsStaffUser, ) | ||
def get(self, request, format=None): | ||
return Response({ | ||
'message': 'CMS Search API', | ||
}) | ||
|
||
|
||
class CourseIndexer(APIView): | ||
authentication_classes = ( | ||
BasicAuthentication, | ||
SessionAuthentication, | ||
TokenAuthentication | ||
) | ||
|
||
permission_classes = ( IsAuthenticated, IsStaffUser, ) | ||
|
||
def get(self, request, format=None): | ||
return Response({ | ||
'message': 'Course Indexer', | ||
}) | ||
|
||
def post(self, request, format=None): | ||
|
||
request_data = json.loads(request.body) | ||
course_id = request_data.get('course_id') | ||
try: | ||
results = api.reindex_course(course_id) | ||
response_data = { | ||
'course_id': course_id, | ||
'status': 'OK', | ||
'message': 'course reindex initiated', | ||
'results': results, | ||
} | ||
response = Response(response_data) | ||
response['Access-Control-Allow-Origin'] = ALLOWED_ORIGIN | ||
response['Access-Control-Allow-Methods'] = 'GET, POST, OPTIONS' | ||
response['Access-Control-Allow-Headers'] = '*' | ||
return response | ||
except Exception as e: | ||
if isinstance(e, InvalidKeyError): | ||
message = 'InvalidKeyError: Cannot find key for course string ' + \ | ||
'"{}"'.format(course_id) | ||
status = 400 | ||
else: | ||
message = 'Exception "{}" msg: {}'.format(e.__class__, e.message) | ||
status = 500 | ||
return Response(json.dumps({ | ||
'course_id': course_id, | ||
'status': 'ERROR', | ||
'message': message, | ||
}), status=status) | ||
|
||
def options(self, request, format=None): | ||
response = Response() | ||
response['Access-Control-Allow-Origin'] = ALLOWED_ORIGIN | ||
response['Access-Control-Allow-Methods'] = 'GET, POST, OPTIONS' | ||
# Options do not allow wildcard for access-control-allow-headers | ||
response['Access-Control-Allow-Headers'] = 'Content-Type' | ||
return 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
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
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