Merge pull request #13686 from edx/openedx_cleanup/misc
Remove few LMS references from openedx/core
This commit is contained in:
@@ -27,8 +27,8 @@ from openedx.core.lib.api.permissions import IsUserInUrl
|
||||
|
||||
from xmodule.modulestore.exceptions import ItemNotFoundError
|
||||
|
||||
from lms.djangoapps.lms_xblock.runtime import unquote_slashes
|
||||
from openedx.core.lib.api.paginators import DefaultPagination
|
||||
from openedx.core.lib.url_utils import unquote_slashes
|
||||
|
||||
from . import DEFAULT_FIELDS, OPTIONAL_FIELDS, api
|
||||
from .serializers import BookmarkSerializer
|
||||
|
||||
@@ -9,7 +9,6 @@ import uuid
|
||||
import pytz
|
||||
from django.db import transaction
|
||||
|
||||
from lms.djangoapps.django_comment_client.utils import JsonResponse
|
||||
from edx_proctoring.api import get_last_exam_completion_date
|
||||
from openedx.core.djangoapps.credit.exceptions import (
|
||||
UserIsNotEligible,
|
||||
@@ -31,6 +30,7 @@ from student.models import (
|
||||
)
|
||||
from openedx.core.djangoapps.credit.signature import signature, get_shared_secret_key
|
||||
from util.date_utils import to_timestamp
|
||||
from util.json_request import JsonResponse
|
||||
|
||||
|
||||
# TODO: Cleanup this mess! ECOM-2908
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
"""
|
||||
Utilities related to API views
|
||||
"""
|
||||
import functools
|
||||
from django.core.exceptions import NON_FIELD_ERRORS, ValidationError, ObjectDoesNotExist
|
||||
from django.http import Http404
|
||||
from django.utils.translation import ugettext as _
|
||||
|
||||
from rest_framework import status, response
|
||||
from rest_framework import status
|
||||
from rest_framework.exceptions import APIException
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
from rest_framework.request import clone_request
|
||||
@@ -14,11 +13,6 @@ from rest_framework.response import Response
|
||||
from rest_framework.mixins import RetrieveModelMixin, UpdateModelMixin
|
||||
from rest_framework.generics import GenericAPIView
|
||||
|
||||
from lms.djangoapps.courseware.courses import get_course_with_access
|
||||
from lms.djangoapps.courseware.courseware_access_exception import CoursewareAccessException
|
||||
from opaque_keys.edx.keys import CourseKey
|
||||
from xmodule.modulestore.django import modulestore
|
||||
|
||||
from openedx.core.lib.api.authentication import (
|
||||
SessionAuthenticationAllowInactiveUser,
|
||||
OAuth2AuthenticationAllowInactiveUser,
|
||||
@@ -89,36 +83,6 @@ class ExpandableFieldViewMixin(object):
|
||||
return result
|
||||
|
||||
|
||||
def view_course_access(depth=0, access_action='load', check_for_milestones=False):
|
||||
"""
|
||||
Method decorator for an API endpoint that verifies the user has access to the course.
|
||||
"""
|
||||
def _decorator(func):
|
||||
"""Outer method decorator."""
|
||||
@functools.wraps(func)
|
||||
def _wrapper(self, request, *args, **kwargs):
|
||||
"""
|
||||
Expects kwargs to contain 'course_id'.
|
||||
Passes the course descriptor to the given decorated function.
|
||||
Raises 404 if access to course is disallowed.
|
||||
"""
|
||||
course_id = CourseKey.from_string(kwargs.pop('course_id'))
|
||||
with modulestore().bulk_operations(course_id):
|
||||
try:
|
||||
course = get_course_with_access(
|
||||
request.user,
|
||||
access_action,
|
||||
course_id,
|
||||
depth=depth,
|
||||
check_if_enrolled=True,
|
||||
)
|
||||
except CoursewareAccessException as error:
|
||||
return response.Response(data=error.to_json(), status=status.HTTP_404_NOT_FOUND)
|
||||
return func(self, request, course=course, *args, **kwargs)
|
||||
return _wrapper
|
||||
return _decorator
|
||||
|
||||
|
||||
def view_auth_classes(is_user=False, is_authenticated=True):
|
||||
"""
|
||||
Function and class decorator that abstracts the authentication and permission checks for api views.
|
||||
|
||||
33
openedx/core/lib/tests/test_url_utils.py
Normal file
33
openedx/core/lib/tests/test_url_utils.py
Normal file
@@ -0,0 +1,33 @@
|
||||
"""
|
||||
Tests for url_utils module.
|
||||
"""
|
||||
from ddt import ddt, data
|
||||
from django.test import TestCase
|
||||
from openedx.core.lib.url_utils import quote_slashes, unquote_slashes
|
||||
|
||||
|
||||
TEST_STRINGS = [
|
||||
'',
|
||||
'foobar',
|
||||
'foo/bar',
|
||||
'foo/bar;',
|
||||
'foo;;bar',
|
||||
'foo;_bar',
|
||||
'foo/',
|
||||
'/bar',
|
||||
'foo//bar',
|
||||
'foo;;;bar',
|
||||
]
|
||||
|
||||
|
||||
@ddt
|
||||
class TestQuoteSlashes(TestCase):
|
||||
"""Test the quote_slashes and unquote_slashes functions"""
|
||||
|
||||
@data(*TEST_STRINGS)
|
||||
def test_inverse(self, test_string):
|
||||
self.assertEquals(test_string, unquote_slashes(quote_slashes(test_string)))
|
||||
|
||||
@data(*TEST_STRINGS)
|
||||
def test_escaped(self, test_string):
|
||||
self.assertNotIn('/', quote_slashes(test_string))
|
||||
@@ -9,7 +9,7 @@ import uuid
|
||||
|
||||
from django.test.client import RequestFactory
|
||||
|
||||
from lms.djangoapps.lms_xblock.runtime import quote_slashes
|
||||
from openedx.core.lib.url_utils import quote_slashes
|
||||
from xblock.fragment import Fragment
|
||||
from xmodule.modulestore import ModuleStoreEnum
|
||||
from xmodule.modulestore.tests.django_utils import SharedModuleStoreTestCase
|
||||
|
||||
53
openedx/core/lib/url_utils.py
Normal file
53
openedx/core/lib/url_utils.py
Normal file
@@ -0,0 +1,53 @@
|
||||
"""
|
||||
Contains common utilities for URL escaping.
|
||||
"""
|
||||
import re
|
||||
|
||||
|
||||
def quote_slashes(text):
|
||||
"""
|
||||
Quote '/' characters so that they aren't visible to
|
||||
django's url quoting, unquoting, or url regex matching.
|
||||
|
||||
Escapes '/'' to the sequence ';_', and ';' to the sequence
|
||||
';;'. By making the escape sequence fixed length, and escaping
|
||||
identifier character ';', we are able to reverse the escaping.
|
||||
"""
|
||||
return re.sub(ur'[;/]', _quote_slashes, text)
|
||||
|
||||
|
||||
def unquote_slashes(text):
|
||||
"""
|
||||
Unquote slashes quoted by `quote_slashes`
|
||||
"""
|
||||
return re.sub(r'(;;|;_)', _unquote_slashes, text)
|
||||
|
||||
|
||||
def _quote_slashes(match):
|
||||
"""
|
||||
Helper function for `quote_slashes`
|
||||
"""
|
||||
matched = match.group(0)
|
||||
# We have to escape ';', because that is our
|
||||
# escape sequence identifier (otherwise, the escaping)
|
||||
# couldn't distinguish between us adding ';_' to the string
|
||||
# and ';_' appearing naturally in the string
|
||||
if matched == ';':
|
||||
return ';;'
|
||||
elif matched == '/':
|
||||
return ';_'
|
||||
else:
|
||||
return matched
|
||||
|
||||
|
||||
def _unquote_slashes(match):
|
||||
"""
|
||||
Helper function for `unquote_slashes`
|
||||
"""
|
||||
matched = match.group(0)
|
||||
if matched == ';;':
|
||||
return ';'
|
||||
elif matched == ';_':
|
||||
return '/'
|
||||
else:
|
||||
return matched
|
||||
@@ -6,16 +6,14 @@ import unittest
|
||||
|
||||
from nose.plugins.attrib import attr
|
||||
|
||||
from django.conf import settings
|
||||
from django.core.urlresolvers import reverse
|
||||
|
||||
from xmodule.modulestore.tests.django_utils import SharedModuleStoreTestCase
|
||||
from xmodule.modulestore.tests.factories import CourseFactory, ItemFactory
|
||||
|
||||
from lms.djangoapps.courseware.tests.helpers import LoginEnrollmentTestCase
|
||||
from lms.djangoapps.courseware.tests.factories import GlobalStaffFactory
|
||||
from lms.djangoapps.lms_xblock.runtime import quote_slashes
|
||||
|
||||
from django.conf import settings
|
||||
from openedx.core.lib.url_utils import quote_slashes
|
||||
from xmodule.modulestore.tests.django_utils import SharedModuleStoreTestCase
|
||||
from xmodule.modulestore.tests.factories import CourseFactory, ItemFactory
|
||||
|
||||
|
||||
class TestCrowdsourceHinter(SharedModuleStoreTestCase, LoginEnrollmentTestCase):
|
||||
|
||||
@@ -20,7 +20,7 @@ from xmodule.modulestore.tests.django_utils import SharedModuleStoreTestCase
|
||||
|
||||
from lms.djangoapps.courseware.tests.helpers import LoginEnrollmentTestCase
|
||||
from lms.djangoapps.courseware.tests.factories import GlobalStaffFactory
|
||||
from lms.djangoapps.lms_xblock.runtime import quote_slashes
|
||||
from openedx.core.lib.url_utils import quote_slashes
|
||||
|
||||
|
||||
class TestRecommender(SharedModuleStoreTestCase, LoginEnrollmentTestCase):
|
||||
|
||||
Reference in New Issue
Block a user