1

I want to somehow check if a endpoint has a decorator like login_required, is there a way I could do it? This is how I get all url_rules:

def has_no_empty_params(rule):
    defaults = rule.defaults if rule.defaults is not None else ()
    arguments = rule.arguments if rule.arguments is not None else ()
    return len(defaults) >= len(arguments)


def sitemap():
    links = []
    for rule in 
        if "GET" in rule.methods and has_no_empty_params(rule):
            url = flask.url_for(rule.endpoint, **(rule.defaults or {}))
            links.append((url, rule.endpoint))
    return links

This is my navigation bar in which I use these, but I want to somehow not display the views who have the decorator login_required.

{% for url, endpoint in endpoints %}
            {% if request.url_rule != endpoint %}
                <a  href="{{ url }}">
                <button class="tablecontent">{{ endpoint.split('.', 1)[1] }}</button>
            </a>
            {% endif %}
        {% endfor %}

I am sorry for the late edit, but if someone is still here, any help would be appreciated. Or maybe I should better hard code the view names in a dictionary?

MareksNo
  • 140
  • 2
  • 12
  • Can you elaborate? – Patch Jun 04 '20 at 11:07
  • Like something that I could use to see if a view for example add_photo has a decorator called login_required, that could look something like this url.needs_login – MareksNo Jun 04 '20 at 11:33
  • I still don't understand "like something that I could use to see if a view" makes no sense. you can do if statements. Also to login required has it. You can check if the user is authenticated instead. – Patch Jun 04 '20 at 11:41
  • 2
    @MareksNo It would be better if you elaborate why you want to do this and show some code if possible – KcH Jun 04 '20 at 11:42
  • Does this answer your question? [Check if a function has a decorator](https://stackoverflow.com/questions/5489649/check-if-a-function-has-a-decorator) – Ken Kinder Jun 04 '20 at 13:28
  • If flask has func_dict I guess it shpuld work, I will try this out when I get on my computer, thank you – MareksNo Jun 04 '20 at 15:34

0 Answers0