Skip to content
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

added snippets changelog to the toolbar under admin menu #181

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

svandeneertwegh
Copy link

@svandeneertwegh svandeneertwegh commented Mar 5, 2025

Description

This PR adds the snippets manage url to the cms toolbar admin menu.

Related resources

Summary by Sourcery

Adds a link to the snippets changelist in the admin menu of the CMS toolbar, allowing users to manage snippets directly from the toolbar.

Copy link
Contributor

sourcery-ai bot commented Mar 5, 2025

Reviewer's Guide by Sourcery

This pull request adds a 'Snippets' link to the admin menu in the CMS toolbar. This allows users to quickly access and manage snippets from the toolbar.

Sequence diagram for adding Snippets link to admin menu

sequenceDiagram
    participant User
    participant CMSToolbar
    participant AdminMenu

    User->>CMSToolbar: Open CMS Toolbar
    CMSToolbar->>AliasToolbar: populate()
    AliasToolbar->>AliasToolbar: add_aliases_link_to_admin_menu()
    AliasToolbar->>CMSToolbar: get_or_create_menu(ADMIN_MENU_IDENTIFIER)
    CMSToolbar->>AdminMenu: Get or create Admin Menu
    AdminMenu-->>CMSToolbar: Returns AdminMenu
    AliasToolbar->>AliasToolbar: admin_reverse('djangocms_snippet_snippet_changelist')
    AliasToolbar->>AdminMenu: add_sideframe_item(_("Snippets"), url, position)
    AdminMenu-->>AliasToolbar: Returns
    AliasToolbar-->>CMSToolbar: Returns
    CMSToolbar-->>User: Renders toolbar with Snippets link
Loading

File-Level Changes

Change Details Files
Added a toolbar to include a link to the snippet changelist in the admin menu.
  • Created AliasToolbar class that inherits from CMSToolbar.
  • Implemented populate method to call add_aliases_link_to_admin_menu.
  • Implemented add_aliases_link_to_admin_menu to add a sideframe item for Snippets in the admin menu.
  • Implemented get_insert_position to determine the alphabetical position of the Snippets link within the admin menu.
src/djangocms_snippet/cms_toolbars.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!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

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

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 @svandeneertwegh - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider adding a permission check to add_aliases_link_to_admin_menu to ensure only authorized users see the link.
Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 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.

position=self.get_insert_position(admin_menu, self.plural_name),
)

@classmethod
Copy link
Contributor

Choose a reason for hiding this comment

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

issue (complexity): Consider refactoring to separate the break setup and insertion point calculation into distinct helper functions, and use hasattr to check for attributes instead of try/except blocks, to improve code organization and readability..

You can reduce complexity by separating the two concerns: ensuring the breaks are set up and calculating the alphabetical insertion point. For instance, you could extract the break logic into its own helper and refactor the loop to check for the attribute rather than handling an exception. For example:

@classmethod
def ensure_shortcuts_break(cls, admin_menu):
    start = admin_menu.find_first(Break, identifier=SHORTCUTS_BREAK)
    if not start:
        end = admin_menu.find_first(Break, identifier=ADMINISTRATION_BREAK)
        admin_menu.add_break(SHORTCUTS_BREAK, position=end.index)
        start = admin_menu.find_first(Break, identifier=SHORTCUTS_BREAK)
    end = admin_menu.find_first(Break, identifier=ADMINISTRATION_BREAK)
    return start, end

@classmethod
def compute_insert_index(cls, admin_menu, start, end, item_name):
    items = admin_menu.get_items()[start.index + 1: end.index]
    for idx, item in enumerate(items):
        # Check if item has a name attribute instead of using try/except.
        if hasattr(item, 'name'):
            if force_str(item_name.lower()) < force_str(item.name.lower()):
                return idx + start.index + 1
    return end.index

@classmethod
def get_insert_position(cls, admin_menu, item_name):
    start, end = cls.ensure_shortcuts_break(admin_menu)
    return cls.compute_insert_index(admin_menu, start, end, item_name)

This refactoring keeps functionality intact while isolating break management and order computation, increasing clarity and maintainability.

@fsbraun
Copy link
Member

fsbraun commented Mar 5, 2025

@svandeneertwegh Thanks for the PR!

Can you add a check if the user has add or change permissions for snippets? Only then the menu entry makes sense...

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