Skip to content

Commit

Permalink
tests: Add test for related field descriptor
Browse files Browse the repository at this point in the history
  • Loading branch information
fsbraun committed Oct 7, 2024
1 parent ec1bc8d commit 42bc9c7
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
13 changes: 2 additions & 11 deletions djangocms_frontend/cms_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -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"
Expand All @@ -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):
Expand Down
12 changes: 12 additions & 0 deletions djangocms_frontend/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion tests/image/test_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions tests/link/test_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 42bc9c7

Please sign in to comment.