Skip to content

Commit

Permalink
Add an unregister method to renderers
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiask committed Jun 7, 2024
1 parent 0d72e6e commit 068e97f
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ Change log
Next version
~~~~~~~~~~~~

- Added ``RegionRenderer.copy()`` and ``RegionRenderer.unregister`` methods to
allow modifying existing renderers without having to reach into internal
attributes.


5.1 (2024-06-06)
~~~~~~~~~~~~~~~~
Expand Down
17 changes: 17 additions & 0 deletions feincms3/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,23 @@ def copy(self):
obj._marks = dict(self._marks)
return obj

def unregister(self, *plugins, keep=()):
if bool(plugins) == bool(keep):
raise ImproperlyConfigured(
"Only ever provide either a list of plugins or a list of plugins to keep."
)

test = (
(lambda cls: not issubclass(cls, plugins))
if plugins
else (lambda cls: issubclass(cls, tuple(keep)))
)

self._fetch = [p for p in self._fetch if test(p)]
self._renderers = {p: r for p, r in self._renderers.items() if test(p)}
self._subregions = {p: s for p, s in self._subregions.items() if test(p)}
self._marks = {p: m for p, m in self._marks.items() if test(p)}

def register(
self,
plugin,
Expand Down
76 changes: 76 additions & 0 deletions tests/testapp/test_region_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,79 @@ def test_invalid_renderer(self):
ImproperlyConfigured, r"has less than the two required arguments"
):
r.register(1, lambda plugin: "")

def test_register_unregister(self):
richtext_renderer = template_renderer("renderer/richtext.html")
html_renderer = template_renderer("renderer/html.html")

renderer = RegionRenderer()
renderer.register(RichText, richtext_renderer)
renderer.register(HTML, html_renderer)

r2 = renderer.copy()
r2.unregister(RichText)

r3 = renderer.copy()
r3.unregister(keep=[HTML])

self.assertEqual(renderer._fetch, [RichText, HTML])
self.assertEqual(
renderer._renderers,
{
RichText: richtext_renderer,
HTML: html_renderer,
},
)
self.assertEqual(
renderer._subregions,
{RichText: "default", HTML: "default"},
)
self.assertEqual(
renderer._subregions,
{RichText: "default", HTML: "default"},
)
self.assertEqual(
renderer._marks,
{RichText: {"default"}, HTML: {"default"}},
)

self.assertEqual(
r2._renderers,
{
HTML: html_renderer,
},
)
self.assertEqual(
r2._subregions,
{HTML: "default"},
)
self.assertEqual(
r2._subregions,
{HTML: "default"},
)
self.assertEqual(
r2._marks,
{HTML: {"default"}},
)

self.assertEqual(
r3._renderers,
{
HTML: html_renderer,
},
)
self.assertEqual(
r3._subregions,
{HTML: "default"},
)
self.assertEqual(
r3._subregions,
{HTML: "default"},
)
self.assertEqual(
r3._marks,
{HTML: {"default"}},
)

with self.assertRaises(ImproperlyConfigured):
renderer.unregister(HTML, keep=[RichText])

0 comments on commit 068e97f

Please sign in to comment.