add python getter in capa system

This commit is contained in:
noraiz-anwar
2018-07-13 11:03:05 +05:00
parent 7f1f3b0dad
commit 593c2d1ae8
13 changed files with 15 additions and 14 deletions

View File

@@ -11,7 +11,9 @@ import dogstats_wrapper as dog_stats_api
from capa import responsetypes
from xmodule.exceptions import NotFoundError, ProcessingError
from xmodule.raw_module import RawDescriptor
from xmodule.contentstore.django import contentstore
from xmodule.util.misc import escape_html_characters
from xmodule.util.sandboxing import get_python_lib_zip
from xmodule.x_module import DEPRECATION_VSCOMPAT_EVENT, XModule, module_attr
from .capa_base import CapaFields, CapaMixin, ComplexEncoder
@@ -331,7 +333,6 @@ class CapaDescriptor(CapaFields, RawDescriptor):
if limit_responses == 0:
# Don't even start collecting answers
return
capa_system = LoncapaSystem(
ajax_url=None,
# TODO set anonymous_student_id to the anonymous ID of the user which answered each problem
@@ -341,7 +342,7 @@ class CapaDescriptor(CapaFields, RawDescriptor):
anonymous_student_id=None,
cache=None,
can_execute_unsafe_code=lambda: None,
get_python_lib_zip=lambda: None,
get_python_lib_zip=(lambda: get_python_lib_zip(contentstore, self.runtime.course_id)),
DEBUG=None,
filestore=self.runtime.resources_fs,
i18n=self.runtime.service(self, "i18n"),

View File

@@ -28,7 +28,7 @@ from openedx.core.djangoapps.request_cache.middleware import RequestCache
from xmodule.contentstore.django import contentstore
from xmodule.modulestore.draft_and_published import BranchSettingMixin
from xmodule.modulestore.mixed import MixedModuleStore
from xmodule.util.django import get_current_request_hostname
from xmodule.util.xmodule_django import get_current_request_hostname
# We also may not always have the current request user (crum) module available
try:

View File

@@ -271,7 +271,7 @@ class ModuleStoreIsolationMixin(CacheIsolationMixin, SignalIsolationMixin):
# List of modulestore signals enabled for this test. Defaults to an empty
# list. The list of signals available is found on the SignalHandler class,
# in /common/lib/xmodule/xmodule/modulestore/django.py
# in /common/lib/xmodule/xmodule/modulestore/xmodule_django.py
#
# You must use the signal itself, and not its name. So for example:
#

View File

@@ -1,5 +1,5 @@
"""Tests for methods defined in util/django.py"""
from xmodule.util.django import get_current_request, get_current_request_hostname
from xmodule.util.xmodule_django import get_current_request, get_current_request_hostname
from nose.tools import assert_is_none
from unittest import TestCase

View File

@@ -0,0 +1,40 @@
import re
from django.conf import settings
DEFAULT_PYTHON_LIB_FILENAME = 'python_lib.zip'
def can_execute_unsafe_code(course_id):
"""
Determine if this course is allowed to run unsafe code.
For use from the ModuleStore. Checks the `course_id` against a list of whitelisted
regexes.
Returns a boolean, true if the course can run outside the sandbox.
"""
# To decide if we can run unsafe code, we check the course id against
# a list of regexes configured on the server.
# If this is not defined in the environment variables then default to the most restrictive, which
# is 'no unsafe courses'
# TODO: This should be a database configuration, where we can mark individual courses as being
# safe/unsafe. Someone in the future should switch us over to that rather than using regexes
# in a settings file
# To others using this: the code as-is is brittle and likely to be changed in the future,
# as per the TODO, so please consider carefully before adding more values to COURSES_WITH_UNSAFE_CODE
for regex in getattr(settings, 'COURSES_WITH_UNSAFE_CODE', []):
if re.match(regex, unicode(course_id)):
return True
return False
def get_python_lib_zip(contentstore, course_id):
"""Return the bytes of the course code library file, if it exists."""
python_lib_filename = getattr(settings, 'PYTHON_LIB_FILENAME', DEFAULT_PYTHON_LIB_FILENAME)
asset_key = course_id.make_asset_key("asset", python_lib_filename)
zip_lib = contentstore().find(asset_key, throw_on_not_found=False)
if zip_lib is not None:
return zip_lib.data
else:
return None