Skip to content

Commit

Permalink
chore: Adds Waffle flags to roll out the extracted XBlocks
Browse files Browse the repository at this point in the history
  • Loading branch information
farhan committed Oct 14, 2024
1 parent dc4db71 commit cca34ec
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 32 deletions.
6 changes: 6 additions & 0 deletions lms/djangoapps/courseware/views/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
from django.views.generic import View
from edx_django_utils.monitoring import set_custom_attribute, set_custom_attributes_for_course_key
from ipware.ip import get_client_ip
from xblock.core import XBlock

from lms.djangoapps.static_template_view.views import render_500
from markupsafe import escape
from opaque_keys import InvalidKeyError
Expand Down Expand Up @@ -1551,6 +1553,10 @@ def render_xblock(request, usage_key_string, check_if_enrolled=True, disable_sta
set_custom_attributes_for_course_key(course_key)
set_custom_attribute('usage_key', usage_key_string)
set_custom_attribute('block_type', usage_key.block_type)
block_class = XBlock.load_class(usage_key.block_type)
if hasattr(block_class, 'is_extracted'):
is_extracted = getattr(block_class, 'is_extracted')
set_custom_attribute('block_extracted', is_extracted)

requested_view = request.GET.get('view', 'student_view')
if requested_view != 'student_view' and requested_view != 'public_view': # lint-amnesty, pylint: disable=consider-using-in
Expand Down
14 changes: 12 additions & 2 deletions xmodule/annotatable_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@
from web_fragments.fragment import Fragment
from xblock.core import XBlock
from xblock.fields import Scope, String
from xblocks_contrib.annotatable import AnnotatableBlock as _ExtractedAnnotatableBlock

from openedx.core.djangolib.markup import HTML, Text
from xmodule.editing_block import EditingMixin
from xmodule.raw_block import RawMixin
from xmodule.toggles import USE_EXTRACTED_ANNOTATABLE_BLOCK
from xmodule.util.builtin_assets import add_webpack_js_to_fragment, add_sass_to_fragment
from xmodule.xml_block import XmlMixin
from xmodule.x_module import (
ResourceTemplates,
shim_xmodule_js,
XModuleMixin,
XModuleToXBlockMixin,
)
from xmodule.xml_block import XmlMixin

log = logging.getLogger(__name__)

Expand All @@ -28,7 +30,7 @@


@XBlock.needs('mako')
class AnnotatableBlock(
class _BuiltInAnnotatableBlock(
RawMixin,
XmlMixin,
EditingMixin,
Expand All @@ -40,6 +42,8 @@ class AnnotatableBlock(
Annotatable XBlock.
"""

is_extracted = False

data = String(
help=_("XML data for the annotation"),
scope=Scope.content,
Expand Down Expand Up @@ -197,3 +201,9 @@ def studio_view(self, _context):
add_webpack_js_to_fragment(fragment, 'AnnotatableBlockEditor')
shim_xmodule_js(fragment, self.studio_js_module_name)
return fragment


AnnotatableBlock = (
_ExtractedAnnotatableBlock if USE_EXTRACTED_ANNOTATABLE_BLOCK.is_enabled()
else _BuiltInAnnotatableBlock
)
27 changes: 18 additions & 9 deletions xmodule/capa_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,14 @@
from xblock.core import XBlock
from xblock.fields import Boolean, Dict, Float, Integer, Scope, String, XMLString, List
from xblock.scorable import ScorableXBlockMixin, Score
from xblocks_contrib.problem import ProblemBlock as _ExtractedProblemBlock

from common.djangoapps.xblock_django.constants import (
ATTR_KEY_DEPRECATED_ANONYMOUS_USER_ID,
ATTR_KEY_USER_IS_STAFF,
ATTR_KEY_USER_ID,
)
from openedx.core.djangolib.markup import HTML, Text
from xmodule.capa import responsetypes
from xmodule.capa.capa_problem import LoncapaProblem, LoncapaSystem
from xmodule.capa.inputtypes import Status
Expand All @@ -36,23 +43,17 @@
from xmodule.exceptions import NotFoundError, ProcessingError
from xmodule.graders import ShowCorrectness
from xmodule.raw_block import RawMixin
from xmodule.util.sandboxing import SandboxService
from xmodule.toggles import USE_EXTRACTED_PROBLEM_BLOCK
from xmodule.util.builtin_assets import add_webpack_js_to_fragment, add_sass_to_fragment
from xmodule.util.sandboxing import SandboxService
from xmodule.x_module import (
ResourceTemplates,
XModuleMixin,
XModuleToXBlockMixin,
shim_xmodule_js
)
from xmodule.xml_block import XmlMixin
from common.djangoapps.xblock_django.constants import (
ATTR_KEY_DEPRECATED_ANONYMOUS_USER_ID,
ATTR_KEY_USER_IS_STAFF,
ATTR_KEY_USER_ID,
)
from openedx.core.djangolib.markup import HTML, Text
from .capa.xqueue_interface import XQueueService

from .fields import Date, ListScoreField, ScoreField, Timedelta
from .progress import Progress

Expand Down Expand Up @@ -134,7 +135,7 @@ def from_json(self, value):
@XBlock.needs('sandbox')
@XBlock.needs('replace_urls')
@XBlock.wants('call_to_action')
class ProblemBlock(
class _BuiltInProblemBlock(
ScorableXBlockMixin,
RawMixin,
XmlMixin,
Expand All @@ -161,6 +162,8 @@ class ProblemBlock(
"""
INDEX_CONTENT_TYPE = 'CAPA'

is_extracted = False

resources_dir = None

has_score = True
Expand Down Expand Up @@ -2509,3 +2512,9 @@ def randomization_bin(seed, problem_id):
r_hash.update(str(problem_id).encode())
# get the first few digits of the hash, convert to an int, then mod.
return int(r_hash.hexdigest()[:7], 16) % NUM_RANDOMIZATION_BINS


ProblemBlock = (
_ExtractedProblemBlock if USE_EXTRACTED_PROBLEM_BLOCK.is_enabled()
else _BuiltInProblemBlock
)
12 changes: 10 additions & 2 deletions xmodule/discussion_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@
from xblock.fields import UNIQUE_ID, Scope, String
from xblock.utils.resources import ResourceLoader
from xblock.utils.studio_editable import StudioEditableXBlockMixin
from xblocks_contrib.discussion import DiscussionXBlock as _ExtractedDiscussionXBlock

from lms.djangoapps.discussion.django_comment_client.permissions import has_permission
from openedx.core.djangoapps.discussions.models import DiscussionsConfiguration, Provider
from openedx.core.djangolib.markup import HTML, Text
from openedx.core.lib.xblock_utils import get_css_dependencies, get_js_dependencies
from xmodule.toggles import USE_EXTRACTED_DISCUSSION_BLOCK
from xmodule.xml_block import XmlMixin


log = logging.getLogger(__name__)
loader = ResourceLoader(__name__) # pylint: disable=invalid-name

Expand All @@ -36,10 +37,11 @@ def _(text):
@XBlock.needs('user') # pylint: disable=abstract-method
@XBlock.needs('i18n')
@XBlock.needs('mako')
class DiscussionXBlock(XBlock, StudioEditableXBlockMixin, XmlMixin): # lint-amnesty, pylint: disable=abstract-method
class _BuiltInDiscussionXBlock(XBlock, StudioEditableXBlockMixin, XmlMixin): # lint-amnesty, pylint: disable=abstract-method
"""
Provides a discussion forum that is inline with other content in the courseware.
"""
is_extracted = False
completion_mode = XBlockCompletionMode.EXCLUDED

discussion_id = String(scope=Scope.settings, default=UNIQUE_ID)
Expand Down Expand Up @@ -275,3 +277,9 @@ def _apply_metadata_and_policy(cls, block, node, runtime):
for field_name, value in metadata.items():
if field_name in block.fields:
setattr(block, field_name, value)


DiscussionXBlock = (
_ExtractedDiscussionXBlock if USE_EXTRACTED_DISCUSSION_BLOCK.is_enabled()
else _BuiltInDiscussionXBlock
)
14 changes: 12 additions & 2 deletions xmodule/html_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@
from web_fragments.fragment import Fragment
from xblock.core import XBlock
from xblock.fields import Boolean, List, Scope, String
from xblocks_contrib.html import HtmlBlock as _ExtractedHtmlBlock

from common.djangoapps.xblock_django.constants import ATTR_KEY_DEPRECATED_ANONYMOUS_USER_ID
from xmodule.contentstore.content import StaticContent
from xmodule.editing_block import EditingMixin
from xmodule.edxnotes_utils import edxnotes
from xmodule.html_checker import check_html
from xmodule.stringify import stringify_children
from xmodule.util.misc import escape_html_characters
from xmodule.toggles import USE_EXTRACTED_HTML_BLOCK
from xmodule.util.builtin_assets import add_webpack_js_to_fragment, add_sass_to_fragment
from xmodule.util.misc import escape_html_characters
from xmodule.x_module import (
ResourceTemplates,
shim_xmodule_js,
Expand All @@ -50,6 +52,8 @@ class HtmlBlockMixin( # lint-amnesty, pylint: disable=abstract-method
The HTML XBlock mixin.
This provides the base class for all Html-ish blocks (including the HTML XBlock).
"""
is_extracted = False

display_name = String(
display_name=_("Display Name"),
help=_("The display name for this component."),
Expand Down Expand Up @@ -353,7 +357,7 @@ def index_dictionary(self):


@edxnotes
class HtmlBlock(HtmlBlockMixin): # lint-amnesty, pylint: disable=abstract-method
class _BuiltInHtmlBlock(HtmlBlockMixin): # lint-amnesty, pylint: disable=abstract-method
"""
This is the actual HTML XBlock.
Nothing extra is required; this is just a wrapper to include edxnotes support.
Expand Down Expand Up @@ -489,3 +493,9 @@ def safe_parse_date(date):
return datetime.strptime(date, '%B %d, %Y')
except ValueError: # occurs for ill-formatted date values
return datetime.today()


HtmlBlock = (
_ExtractedHtmlBlock if USE_EXTRACTED_HTML_BLOCK.is_enabled()
else _BuiltInHtmlBlock
)
25 changes: 16 additions & 9 deletions xmodule/lti_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,40 +59,40 @@
import hashlib
import logging
import textwrap
from xml.sax.saxutils import escape
from unittest import mock
from urllib import parse
from xml.sax.saxutils import escape

import nh3
import oauthlib.oauth1
from django.conf import settings
from lxml import etree
from oauthlib.oauth1.rfc5849 import signature
from pytz import UTC
from webob import Response
from web_fragments.fragment import Fragment
from webob import Response
from xblock.core import List, Scope, String, XBlock
from xblock.fields import Boolean, Float
from xmodule.mako_block import MakoTemplateBlockBase

from openedx.core.djangolib.markup import HTML, Text
from xmodule.editing_block import EditingMixin
from xblocks_contrib.lti import LTIBlock as _ExtractedLTIBlock

from common.djangoapps.xblock_django.constants import (
ATTR_KEY_ANONYMOUS_USER_ID,
ATTR_KEY_USER_ROLE,
)
from openedx.core.djangolib.markup import HTML, Text
from xmodule.editing_block import EditingMixin
from xmodule.lti_2_util import LTI20BlockMixin, LTIError
from xmodule.mako_block import MakoTemplateBlockBase
from xmodule.raw_block import EmptyDataRawMixin
from xmodule.toggles import USE_EXTRACTED_LTI_BLOCK
from xmodule.util.builtin_assets import add_webpack_js_to_fragment, add_sass_to_fragment
from xmodule.xml_block import XmlMixin
from xmodule.x_module import (
ResourceTemplates,
shim_xmodule_js,
XModuleMixin,
XModuleToXBlockMixin,
)

from xmodule.xml_block import XmlMixin

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -274,7 +274,7 @@ class LTIFields:
@XBlock.needs("mako")
@XBlock.needs("user")
@XBlock.needs("rebind_user")
class LTIBlock(
class _BuiltInLTIBlock(
LTIFields,
LTI20BlockMixin,
EmptyDataRawMixin,
Expand Down Expand Up @@ -366,6 +366,7 @@ class LTIBlock(
Otherwise error message from LTI provider is generated.
"""
is_extracted = False
resources_dir = None
uses_xmodule_styles_setup = True

Expand Down Expand Up @@ -984,3 +985,9 @@ def is_past_due(self):
else:
close_date = due_date
return close_date is not None and datetime.datetime.now(UTC) > close_date


LTIBlock = (
_ExtractedLTIBlock if USE_EXTRACTED_LTI_BLOCK.is_enabled()
else _BuiltInLTIBlock
)
17 changes: 13 additions & 4 deletions xmodule/poll_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@
from collections import OrderedDict
from copy import deepcopy

from web_fragments.fragment import Fragment

from lxml import etree
from web_fragments.fragment import Fragment
from xblock.core import XBlock
from xblock.fields import Boolean, Dict, List, Scope, String # lint-amnesty, pylint: disable=wrong-import-order
from xblocks_contrib.poll import PollBlock as _ExtractedPollBlock

from openedx.core.djangolib.markup import Text, HTML
from xmodule.mako_block import MakoTemplateBlockBase
from xmodule.stringify import stringify_children
from xmodule.toggles import USE_EXTRACTED_POLL_BLOCK
from xmodule.util.builtin_assets import add_webpack_js_to_fragment, add_sass_to_fragment
from xmodule.x_module import (
ResourceTemplates,
Expand All @@ -30,20 +32,22 @@
)
from xmodule.xml_block import XmlMixin


log = logging.getLogger(__name__)
_ = lambda text: text


@XBlock.needs('mako')
class PollBlock(
class _BuiltInPollBlock(
MakoTemplateBlockBase,
XmlMixin,
XModuleToXBlockMixin,
ResourceTemplates,
XModuleMixin,
): # pylint: disable=abstract-method
"""Poll Block"""

is_extracted = False

# Name of poll to use in links to this poll
display_name = String(
help=_("The display name for this component."),
Expand Down Expand Up @@ -244,3 +248,8 @@ def add_child(xml_obj, answer): # lint-amnesty, pylint: disable=unused-argument
add_child(xml_object, answer)

return xml_object

PollBlock = (
_ExtractedPollBlock if USE_EXTRACTED_POLL_BLOCK.is_enabled()
else _BuiltInPollBlock
)
23 changes: 23 additions & 0 deletions xmodule/toggles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""
Add Waffle flags to roll out the extracted XBlocks.
Flags will use to toggle between the old and new block quickly
without putting course content or user state at risk.
Ticket: https://github.com/openedx/edx-platform/issues/35308
"""
from edx_toggles.toggles import WaffleFlag

# .. toggle_name: USE_EXTRACTED_WORD_CLOUD_BLOCK
# .. toggle_description: Enables the use of the extracted Word Cloud XBlock, which has been shifted to the 'openedx/xblocks-contrib' repo.
# .. toggle_warning: Not production-ready until https://github.com/openedx/edx-platform/issues/34840 is done.
# .. toggle_use_cases: temporary
# .. toggle_default: False
USE_EXTRACTED_WORD_CLOUD_BLOCK = WaffleFlag('xmodule.use_extracted_block.word_cloud', __name__)
# TODO: Add flag docs once above approved
USE_EXTRACTED_ANNOTATABLE_BLOCK = WaffleFlag('xmodule.use_extracted_block.annotatable', __name__)
USE_EXTRACTED_POLL_BLOCK = WaffleFlag('xmodule.use_extracted_block.poll', __name__)
USE_EXTRACTED_LTI_BLOCK = WaffleFlag('xmodule.use_extracted_block.lti', __name__)
USE_EXTRACTED_HTML_BLOCK = WaffleFlag('xmodule.use_extracted_block.html', __name__)
USE_EXTRACTED_DISCUSSION_BLOCK = WaffleFlag('xmodule.use_extracted_block.discussion', __name__)
USE_EXTRACTED_PROBLEM_BLOCK = WaffleFlag('xmodule.use_extracted_block.problem', __name__)
USE_EXTRACTED_VIDEO_BLOCK = WaffleFlag('xmodule.use_extracted_block.video', __name__)
Loading

0 comments on commit cca34ec

Please sign in to comment.