Merge pull request #31112 from open-craft/0x29a/bb6734/remove_prefer_xmodules

refactor: Remove prefer_xmodules() [BD-13]
This commit is contained in:
Piotr Surowiec
2022-11-16 18:34:08 +01:00
committed by GitHub
11 changed files with 6 additions and 87 deletions

View File

@@ -95,7 +95,7 @@ def _load_mixed_class(category):
"""
Load an XBlock by category name, and apply all defined mixins
"""
component_class = XBlock.load_class(category, select=settings.XBLOCK_SELECT_FUNCTION)
component_class = XBlock.load_class(category)
mixologist = Mixologist(settings.XBLOCK_MIXINS)
return mixologist.mix(component_class)

View File

@@ -5,7 +5,6 @@ Helper methods for Studio views.
import urllib
from uuid import uuid4
from django.conf import settings
from django.http import HttpResponse
from django.utils.translation import gettext as _
from opaque_keys.edx.keys import UsageKey
@@ -134,7 +133,7 @@ def xblock_type_display_name(xblock, default_display_name=None):
return _('Subsection')
elif category == 'vertical':
return _('Unit')
component_class = XBlock.load_class(category, select=settings.XBLOCK_SELECT_FUNCTION)
component_class = XBlock.load_class(category)
if hasattr(component_class, 'display_name') and component_class.display_name.default:
return _(component_class.display_name.default) # lint-amnesty, pylint: disable=translation-of-non-string
else:

View File

@@ -926,7 +926,6 @@ P3P_HEADER = 'CP="Open EdX does not have a P3P policy."'
# Import after sys.path fixup
from xmodule.modulestore.inheritance import InheritanceMixin
from xmodule.modulestore import prefer_xmodules
from xmodule.x_module import XModuleMixin
# These are the Mixins that should be added to every XBlock.
@@ -941,8 +940,6 @@ XBLOCK_MIXINS = (
)
XBLOCK_EXTRA_MIXINS = ()
XBLOCK_SELECT_FUNCTION = prefer_xmodules
# Paths to wrapper methods which should be applied to every XBlock's FieldData.
XBLOCK_FIELD_DATA_WRAPPERS = ()

View File

@@ -1,57 +0,0 @@
"""
Export all xml courses in a diffable format.
This command loads all of the xml courses in the configured DATA_DIR.
For each of the courses, it loops through all of the modules, and dumps
each as a separate output file containing the json representation
of each of its fields (including those fields that are set as default values).
"""
import json
from django.conf import settings
from django.core.management.base import BaseCommand, CommandError
from path import Path as path
from xmodule.modulestore.xml import XMLModuleStore
class Command(BaseCommand):
"""
Django management command to export diffable representations of all xml courses
"""
help = '''Dump the in-memory representation of all xml courses in a diff-able format'''
args = '<export path>'
def handle(self, *args, **options):
if len(args) != 1:
raise CommandError(f'Must called with arguments: {self.args}')
xml_module_store = XMLModuleStore(
data_dir=settings.DATA_DIR,
default_class='xmodule.hidden_module.HiddenDescriptor',
load_error_modules=True,
xblock_mixins=settings.XBLOCK_MIXINS,
xblock_select=settings.XBLOCK_SELECT_FUNCTION,
)
export_dir = path(args[0])
for course_id, course_modules in xml_module_store.modules.items():
course_path = course_id.replace('/', '_')
for location, descriptor in course_modules.items():
location_path = str(location).replace('/', '_')
data = {}
for field_name, field in descriptor.fields.items():
try:
data[field_name] = field.read_json(descriptor)
except Exception as exc: # pylint: disable=broad-except
data[field_name] = {
'$type': str(type(exc)),
'$value': descriptor._field_data.get(descriptor, field_name) # pylint: disable=protected-access
}
outdir = export_dir / course_path
outdir.makedirs_p()
with open(outdir / location_path + '.json', 'w') as outfile:
json.dump(data, outfile, sort_keys=True, indent=4)
print('', file=outfile)

View File

@@ -1519,7 +1519,6 @@ COURSE_LISTINGS = {}
# Import after sys.path fixup
from xmodule.modulestore.edit_info import EditInfoMixin # lint-amnesty, pylint: disable=wrong-import-order, wrong-import-position
from xmodule.modulestore.inheritance import InheritanceMixin # lint-amnesty, pylint: disable=wrong-import-order, wrong-import-position
from xmodule.modulestore import prefer_xmodules # lint-amnesty, pylint: disable=wrong-import-order, wrong-import-position
from xmodule.x_module import XModuleMixin # lint-amnesty, pylint: disable=wrong-import-order, wrong-import-position
# These are the Mixins that should be added to every XBlock.
@@ -1528,14 +1527,6 @@ from xmodule.x_module import XModuleMixin # lint-amnesty, pylint: disable=wrong
XBLOCK_MIXINS = (LmsBlockMixin, InheritanceMixin, XModuleMixin, EditInfoMixin)
XBLOCK_EXTRA_MIXINS = ()
# .. setting_name: XBLOCK_SELECT_FUNCTION
# .. setting_default: prefer_xmodules
# .. setting_description: Function used to select an XBlock from the python package EntryPoints.
# Some alternatives are `prefer_xmodules` and `default_select`. The `prefer_modules` function
# will choose the first "xmodule" if there is one, otherwise, it will act like `default_select`.
# The `default_select` function will simply choose the first match found.
XBLOCK_SELECT_FUNCTION = prefer_xmodules
# .. setting_name: XBLOCK_FIELD_DATA_WRAPPERS
# .. setting_default: ()
# .. setting_description: Paths to wrapper methods which should be applied to every XBlock's FieldData.

View File

@@ -6,7 +6,6 @@ Common views dedicated to rendering xblocks.
import logging
import mimetypes
from django.conf import settings
from django.http import Http404, HttpResponse
from xblock.core import XBlock
@@ -20,7 +19,7 @@ def xblock_resource(request, block_type, uri): # pylint: disable=unused-argumen
try:
# Figure out what the XBlock class is from the block type, and
# then open whatever resource has been requested.
xblock_class = XBlock.load_class(block_type, select=settings.XBLOCK_SELECT_FUNCTION)
xblock_class = XBlock.load_class(block_type)
content = xblock_class.open_local_resource(uri)
except OSError:
log.info('Failed to load xblock resource', exc_info=True)

View File

@@ -1271,7 +1271,7 @@ class ModuleStoreWriteBase(ModuleStoreReadBase, ModuleStoreWrite):
result = defaultdict(dict)
if fields is None:
return result
classes = XBlock.load_class(category, select=prefer_xmodules)
classes = XBlock.load_class(category)
cls = self.mixologist.mix(classes)
for field_name, value in fields.items():
field = getattr(cls, field_name)
@@ -1393,12 +1393,3 @@ def only_xmodules(identifier, entry_points):
from_xmodule = [entry_point for entry_point in entry_points if entry_point.dist.key == 'xmodule']
return default_select(identifier, from_xmodule)
def prefer_xmodules(identifier, entry_points):
"""Prefer entry_points from the xmodule package"""
from_xmodule = [entry_point for entry_point in entry_points if entry_point.dist.key == 'xmodule']
if from_xmodule:
return default_select(identifier, from_xmodule)
else:
return default_select(identifier, entry_points)

View File

@@ -305,7 +305,6 @@ def create_modulestore_instance(
metadata_inheritance_cache_subsystem=metadata_inheritance_cache,
request_cache=request_cache,
xblock_mixins=getattr(settings, 'XBLOCK_MIXINS', ()),
xblock_select=getattr(settings, 'XBLOCK_SELECT_FUNCTION', None),
xblock_field_data_wrappers=xblock_field_data_wrappers,
disabled_xblock_types=fetch_disabled_xblock_types,
doc_store_config=doc_store_config,

View File

@@ -22,7 +22,7 @@ from opaque_keys.edx.locator import BlockUsageLocator
from xblock.core import XBlock
from xmodule.course_module import Textbook
from xmodule.modulestore import ModuleStoreEnum, prefer_xmodules
from xmodule.modulestore import ModuleStoreEnum
from xmodule.modulestore.mixed import strip_key
from xmodule.modulestore.tests.sample_courses import TOY_BLOCK_INFO_TREE, default_block_info_tree
from xmodule.tabs import CourseTab
@@ -389,7 +389,7 @@ class ItemFactory(XModuleFactory):
if 'boilerplate' in kwargs:
template_id = kwargs.pop('boilerplate')
clz = XBlock.load_class(category, select=prefer_xmodules)
clz = XBlock.load_class(category)
template = clz.get_template(template_id)
assert template is not None
metadata.update(template.get('metadata', {}))