Python flask.current_app.view_functions() Examples

The following are 7 code examples of flask.current_app.view_functions(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module flask.current_app , or try the search function .
Example #1
Source File: alias.py    From maple-blog with GNU General Public License v3.0 6 votes vote down vote up
def get_view_function(url, method='GET'):
    adapter = current_app.url_map.bind('localhost')
    try:
        match = adapter.match(url, method=method)
    except RequestRedirect as e:
        # recursively match redirects
        return get_view_function(e.new_url, method)
    except (MethodNotAllowed, NotFound):
        # no match
        return None

    try:
        # return the view function and arguments
        return current_app.view_functions[match[0]], match[1]
    except KeyError:
        # no view is associated with the endpoint
        return None 
Example #2
Source File: routes.py    From fastlane with MIT License 6 votes vote down vote up
def routes():  # pragma: no cover
    """Print available functions."""
    if not current_app.debug:
        abort(404)

    func_list = []
    for rule in current_app.url_map.iter_rules():
        endpoint = rule.rule
        methods = ", ".join(list(rule.methods))
        doc = current_app.view_functions[rule.endpoint].__doc__

        route = {
            "endpoint": endpoint,
            "methods": methods
        }
        if doc:
            route["doc"] = doc
        func_list.append(route)

    func_list = sorted(func_list, key=lambda k: k['endpoint'])
    return jsonify(func_list) 
Example #3
Source File: __init__.py    From prometheus_flask_exporter with MIT License 5 votes vote down vote up
def register_default(self, *metric_wrappers, **kwargs):
        """
        Registers metric wrappers to track all endpoints,
        similar to `export_defaults` but with user defined metrics.
        Call this function after all routes have been set up.

        Use the metric wrappers as arguments:
          - metrics.counter(..)
          - metrics.gauge(..)
          - metrics.summary(..)
          - metrics.histogram(..)

        :param metric_wrappers: one or more metric wrappers to register
            for all available endpoints
        :param app: the Flask application to register the default metric for
            (by default it is the application registered with this class)
        """

        app = kwargs.get('app')
        if app is None:
            app = self.app or current_app

        for endpoint, view_func in app.view_functions.items():
            for wrapper in metric_wrappers:
                view_func = wrapper(view_func)
                app.view_functions[endpoint] = view_func 
Example #4
Source File: views.py    From flask-allows with MIT License 5 votes vote down vote up
def _get_executing_handler():
    return current_app.view_functions[request.endpoint] 
Example #5
Source File: flask_bouncer.py    From flask-bouncer with MIT License 5 votes vote down vote up
def method_is_explictly_overwritten(self):
        view_func = current_app.view_functions[request.endpoint]
        return hasattr(view_func, '_explict_rule_set') and view_func._explict_rule_set is True 
Example #6
Source File: urls.py    From flask-react-spa with MIT License 5 votes vote down vote up
def _get_rule_view(rule):
    view_fn = current_app.view_functions[rule.endpoint]
    view_module = inspect.getmodule(view_fn)
    view_fn_name = view_fn.__name__
    if 'View.as_view' in view_fn.__qualname__:
        view_fn_name = view_fn.__dict__['view_class'].__name__
    return f'{view_module.__name__}:{view_fn_name}' 
Example #7
Source File: autodoc.py    From syntheticmass with Apache License 2.0 4 votes vote down vote up
def generate(self, groups='all', sort=None):
        """Return a list of dict describing the routes specified by the
        doc() method

        Each dict contains:
         - methods: the set of allowed methods (ie ['GET', 'POST'])
         - rule: relative url (ie '/user/<int:id>')
         - endpoint: function name (ie 'show_user')
         - doc: docstring of the function
         - args: function arguments
         - defaults: defaults values for the arguments

        By specifying the group or groups arguments, only routes belonging to
        those groups will be returned.

        Routes are sorted alphabetically based on the rule.
        """
        groups_to_generate = list()
        if type(groups) is list:
            groups_to_generate = groups
        elif type(groups) is str:
            groups_to_generate.append(groups)

        links = []
        for rule in current_app.url_map.iter_rules():

            if rule.endpoint == 'static':
                continue

            func = current_app.view_functions[rule.endpoint]
            arguments = rule.arguments if rule.arguments else ['None']
            func_groups = self.func_groups[func]
            location = self.func_locations.get(func, None)

            if func_groups.intersection(groups_to_generate):
                links.append(
                    dict(
                        methods=rule.methods,
                        rule="%s" % rule,
                        endpoint=rule.endpoint,
                        docstring=func.__doc__,
                        args=arguments,
                        defaults=rule.defaults,
                        location=location,
                    )
                )
        if sort:
            return sort(links)
        else:
            return sorted(links, key=itemgetter('rule'))