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

MenuItem's kwargs should be checked to be callable #83

Open
nerdoc opened this issue May 9, 2022 · 1 comment
Open

MenuItem's kwargs should be checked to be callable #83

nerdoc opened this issue May 9, 2022 · 1 comment
Labels
enhancement Something can be improved

Comments

@nerdoc
Copy link
Contributor

nerdoc commented May 9, 2022

As stated in #78, MenuItem's __init__() adds all additionally added kwargs as instance variables. This is great for new args lico icon etc., as long as they are "static", meaning they do not change over time in the template's context.
There are many variables I can think of (even icon!) like

  • badge (which should return e.g. a number of new emails), or
  • disabled (which should be true if a certain condition is met)
  • ...

where you would need to add it to MenuItem, and it would be easy with kwargs, but MenuItem doesn't check if it is callable.

So I would propose to

  1. put kwargs in a dict self.kwargs to have a reference later
  2. nstead of making reference vars of them in the first place, do this after 1., to hold the same reference. Please correct me if this is not correct in Python...
    (untested, just as idea here)
def __init__(..., **kwargs)
    # ...
    self.kwargs = kwargs
    # keep a link of all kwargs in the instance, for convenience/template context
    for k in self.kwargs:
        setattr(self, k, self.kwargs[k])    

def process(self, request):
    # ...
    # if kwargs are callable, call them and save the results in the actual instance variables
    for key, value in self.kwargs:
        if callable(value):
            setattr(self, k, self.kwargs[k]())    
@kytta kytta added the enhancement Something can be improved label May 10, 2022
@kytta kytta added this to the v2.0.0 milestone May 10, 2022
@nerdoc
Copy link
Contributor Author

nerdoc commented Oct 27, 2022

I don't want to create another issue: This should also be applied to the url attr, which is a "core" attribute and not dynamicaly generated using kwargs: url should be potentially callable too.

use case: I want to point to a URL like /person/<int:pk/edit/ in a menu item. The only way I can think of is to subclass MenuItem again and add the same functionality like for the title attr, to let users link to /person/{{request.user.id}}/edit, which is generated in a callable.

@kytta kytta removed this from the v2.1.0 milestone Nov 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Something can be improved
Projects
None yet
Development

No branches or pull requests

2 participants