diff --git a/djangocms_frontend/cms_plugins.py b/djangocms_frontend/cms_plugins.py index 45071764..d4610163 100644 --- a/djangocms_frontend/cms_plugins.py +++ b/djangocms_frontend/cms_plugins.py @@ -2,7 +2,7 @@ from cms.plugin_base import CMSPluginBase from django.utils.encoding import force_str -from djangocms_frontend.helpers import get_related_object +from djangocms_frontend.helpers import get_related if hasattr(CMSPluginBase, "edit_field"): # FrontendEditable functionality already implemented in core? @@ -12,15 +12,6 @@ from .helpers import FrontendEditableAdminMixin -def _get_related(instance, key): - def get_related(): - obj = get_related_object(instance.config, key) - setattr(instance, key, obj) - return obj - get_related.__name__ = key - return get_related - - class CMSUIPlugin(FrontendEditableAdminMixin, CMSPluginBase): render_template = "djangocms_frontend/html_container.html" change_form_template = "djangocms_frontend/admin/base.html" @@ -32,7 +23,7 @@ def render(self, context, instance, placeholder): for key, value in instance.config.items(): if isinstance(value, dict) and set(value.keys()) == {"pk", "model"}: if key not in instance.__dir__(): # hasattr would return the value in the config dict - setattr(instance, key, _get_related(instance, key)) + setattr(instance.__class__, key, get_related(key)) return super().render(context, instance, placeholder) def get_plugin_urls(self): diff --git a/djangocms_frontend/helpers.py b/djangocms_frontend/helpers.py index cf7d56a7..dd007707 100644 --- a/djangocms_frontend/helpers.py +++ b/djangocms_frontend/helpers.py @@ -31,6 +31,18 @@ def get_related_object(scope, field_name): return relobj +class get_related: + """Descriptor lazily getting related objects from the config dict.""" + def __init__(self, key): + self.key = key + + def __get__(self, instance, owner): + obj = get_related_object(instance.config, self.key) + if obj is not None: + setattr(instance, self.key, obj) + return obj + + def insert_fields(fieldsets, new_fields, block=None, position=-1, blockname=None, blockattrs=None): """ creates a copy of fieldsets inserting the new fields either in the indexed block at the position, diff --git a/tests/image/test_plugins.py b/tests/image/test_plugins.py index fc1e9d17..01697e5d 100644 --- a/tests/image/test_plugins.py +++ b/tests/image/test_plugins.py @@ -45,7 +45,7 @@ def test_plugin(self): plugin_type=ImagePlugin.__name__, language=self.language, config={ - "image": {"pk": self.image.id, "model": "filer.Image"}, + "picture": {"pk": self.image.id, "model": "filer.Image"}, "picture_fluid": False, "picture_rounded": True, "picture_thumbnail": True, diff --git a/tests/link/test_plugins.py b/tests/link/test_plugins.py index 285c0253..62646e82 100644 --- a/tests/link/test_plugins.py +++ b/tests/link/test_plugins.py @@ -71,6 +71,8 @@ def test_plugin(self): self.assertEqual(response.status_code, 200) self.assertContains(response, "btn-primary") self.assertContains(response, 'href="/content/"') + # Finally, test the descriptor + self.assertEqual(plugin.internal_link, self.page) # alternate version broken link plugin = add_plugin(