Skip to content

fix: AttributeError CMSPlugin (EditorNotePlugin) object has no attribute config #280

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 22, 2025

Conversation

zbohm
Copy link
Contributor

@zbohm zbohm commented May 21, 2025

Plugin EditorNotePlugin does not inherit form AttributesMixin. This raises AttributeError in function render where is the code instance.config.items(). There could be more such plugins, so it is advisable to insert a condition in the render function. It would also be inappropriate to add AttributesMixin to a class when the class does not need it.

The render function is missing a test for the EditorNotePlugin plugin, otherwise the bug would have been found already. Unfortunately, I am currently unable to complete the test due to time constraints.

Summary by Sourcery

Bug Fixes:

  • Check for instance.config presence before iterating its items in render() to avoid errors for plugins without config attributes

Copy link
Contributor

sourcery-ai bot commented May 21, 2025

Reviewer's Guide

Wrap the plugin render logic in a conditional check to safely handle cases where the plugin instance has no config attribute, preventing AttributeError.

File-Level Changes

Change Details Files
Guard config access in render() to avoid AttributeError for plugins without a config attribute
  • Added if hasattr(instance, "config"): before iterating over instance.config
  • Indented existing loop and context update under the new guard
  • Retained original logic for setting related attributes and updating context only when config exists
djangocms_frontend/ui_plugin_base.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@zbohm zbohm changed the title Fix AttributeError CMSPlugin (EditorNotePlugin) object has no attribute config fix: AttributeError CMSPlugin (EditorNotePlugin) object has no attribute config May 21, 2025
Copy link

codecov bot commented May 21, 2025

Codecov Report

Attention: Patch coverage is 87.50000% with 1 line in your changes missing coverage. Please review.

Project coverage is 88.66%. Comparing base (35d4231) to head (f265e24).
Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
djangocms_frontend/ui_plugin_base.py 87.50% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##           master     #280   +/-   ##
=======================================
  Coverage   88.65%   88.66%           
=======================================
  Files         124      124           
  Lines        3376     3378    +2     
  Branches      287      288    +1     
=======================================
+ Hits         2993     2995    +2     
  Misses        265      265           
  Partials      118      118           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @zbohm - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines 33 to 39
if hasattr(instance, "config"):
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.__class__, key, get_related(key))
if "instance" not in instance.config and isinstance(instance.config, dict):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (bug_risk): Guard for config existence may be too broad

hasattr only checks for the attribute's existence, not its type. If config exists but isn't a dict, .items() will fail. Use getattr with a default and check isinstance(config, dict) before iterating.

Suggested change
if hasattr(instance, "config"):
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.__class__, key, get_related(key))
if "instance" not in instance.config and isinstance(instance.config, dict):
config = getattr(instance, "config", None)
if isinstance(config, dict):
for key, value in config.items():
if isinstance(value, dict) and set(value.keys()) == {"pk", "model"}:
if key not in instance.__dir__():
setattr(instance.__class__, key, get_related(key))
if "instance" not in config:

Comment on lines +35 to +38
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.__class__, key, get_related(key))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (code-quality): Merge nested if conditions (merge-nested-ifs)

Suggested change
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.__class__, key, get_related(key))
if isinstance(value, dict) and set(value.keys()) == {"pk", "model"} and key not in instance.__dir__():
setattr(instance.__class__, key, get_related(key))


ExplanationToo much nesting can make code difficult to understand, and this is especially
true in Python, where there are no brackets to help out with the delineation of
different nesting levels.

Reading deeply nested code is confusing, since you have to keep track of which
conditions relate to which levels. We therefore strive to reduce nesting where
possible, and the situation where two if conditions can be combined using
and is an easy win.

@fsbraun
Copy link
Member

fsbraun commented May 21, 2025

@zbohm Great catch!

I added tests to the master branch - you'll just have to merge the master branch into you PR.

@zbohm zbohm force-pushed the fix-plugin-has-no-config branch from fe8e86f to 0a949bb Compare May 22, 2025 07:07
@fsbraun
Copy link
Member

fsbraun commented May 22, 2025

@zbohm Can you do two more things?

Thank you!

@zbohm zbohm force-pushed the fix-plugin-has-no-config branch from 0a949bb to f265e24 Compare May 22, 2025 13:45
Copy link
Member

@fsbraun fsbraun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work, @zbohm ! Thank you so much! Have you already joined the Django CMS discord server?

@fsbraun fsbraun merged commit 3cd8767 into django-cms:master May 22, 2025
20 of 21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants