From bdff0f9c5a585ffb72affc9d6fc4ba868cd18165 Mon Sep 17 00:00:00 2001 From: connorhaugh Date: Thu, 18 Apr 2024 14:24:28 -0400 Subject: [PATCH] feat: readd fragment as passthrough to webfragement.fragment --- CHANGELOG.rst | 9 +++++++-- xblock/__init__.py | 2 +- xblock/fragment.py | 25 +++++++++++++++++++++++++ xblock/test/test_fragment.py | 21 +++++++++++++++++++++ 4 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 xblock/fragment.py create mode 100644 xblock/test/test_fragment.py diff --git a/CHANGELOG.rst b/CHANGELOG.rst index c91ccab58..f5c4e4a5c 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -5,6 +5,11 @@ Change history for XBlock Unreleased ---------- +4.0.0 - 2024-04-18 +------------------ + +* xblock.fragment has returned as a pass-though component to web_fragments.fragment + 3.0.0 - 2024-03-18 ------------------ @@ -15,7 +20,7 @@ will be unaffected by this change. Some improvements have also been made to the Specific changes: -* **Removed:** +* **Removed:** * ``xblock.XBlockMixin`` (still available as ``xblock.core.XBlockMixin``) * ``xblock.core.SharedBlockBase`` (replaced with ``xblock.core.Blocklike``) @@ -53,7 +58,7 @@ Specific changes: * Various docstrings have been improved, some of which are published in the docs. * XBlockAside will now be represented in the API docs, right below XBlock on the "XBlock API" page. - * XBlockMixin has been removed from the docs. + * XBlockMixin has been removed from the docs. It was only ever documented under the "Fields API" page (which didn't make any sense), and it was barely even documented there. We considered adding it back to the "XBlock API" page, but as noted in the class's new docstring, we do not want to encourage any new use of XBlockMixin. diff --git a/xblock/__init__.py b/xblock/__init__.py index 9737a15cc..ff55f6857 100644 --- a/xblock/__init__.py +++ b/xblock/__init__.py @@ -2,4 +2,4 @@ XBlock Courseware Components """ -__version__ = '3.1.0' +__version__ = '4.0.0' diff --git a/xblock/fragment.py b/xblock/fragment.py new file mode 100644 index 000000000..0a117c4b1 --- /dev/null +++ b/xblock/fragment.py @@ -0,0 +1,25 @@ +""" +Makes the Fragment class available through the old namespace location. +""" +import warnings + +import web_fragments.fragment + + +class Fragment(web_fragments.fragment.Fragment): + """ + A wrapper around web_fragments.fragment.Fragment that provides + backwards compatibility for the old location. + Deprecated. + """ + def __init__(self, *args, **kwargs): + warnings.warn( + 'xblock.fragment is deprecated. Please use web_fragments.fragment instead', + DeprecationWarning, + stacklevel=2 + ) + super().__init__(*args, **kwargs) + + # Provide older names for renamed methods + add_frag_resources = web_fragments.fragment.Fragment.add_fragment_resources + add_frags_resources = web_fragments.fragment.Fragment.add_resources diff --git a/xblock/test/test_fragment.py b/xblock/test/test_fragment.py new file mode 100644 index 000000000..193694334 --- /dev/null +++ b/xblock/test/test_fragment.py @@ -0,0 +1,21 @@ +""" +Unit tests for the Fragment class. +Note: this class has been deprecated in favor of web_fragments.fragment.Fragment +""" +from unittest import TestCase + +from xblock.fragment import Fragment + + +class TestFragment(TestCase): + """ + Unit tests for fragments. + """ + def test_fragment(self): + """ + Test the delegated Fragment class. + """ + TEST_HTML = '

Hello, world!

' # pylint: disable=invalid-name + fragment = Fragment() + fragment.add_content(TEST_HTML) + self.assertEqual(fragment.body_html(), TEST_HTML)