コード例 #1
0
ファイル: modulemanager.py プロジェクト: qbx2/opencampus
    def decorator(f):
        @wraps(f)
        def decorated(*args, **kwargs):
            kwargs.pop('subdomain', None)
            request.campus = get_campus()
            if not request.campus:
                return abort(404)
            return f(*args, **kwargs)
        endpoint = options.pop('endpoint', None)
        menu = options.pop('menu', None)
        parent = options.pop('parent', None)
        app.add_url_rule(rule, endpoint, decorated, **options)
        options.pop('subdomain', None)
        app.add_url_rule(rule, endpoint, decorated, subdomain="<string:subdomain>", **options)
        if menu:
            from flask.helpers import _endpoint_from_view_func
            CAMPUS_SERIVCE_MENUS.append({
                'name': menu,
                'endpoint': (endpoint if endpoint else _endpoint_from_view_func(decorated))
            })
            CAMPUS_SERIVCE_PARNET.append({
            })

        if parent:
            CAMPUS_SERIVCE_PARNET.append({
            })

        return decorated
コード例 #2
0
ファイル: api.py プロジェクト: pbecotte/flask-restplus
    def _blueprint_setup_add_url_rule_patch(blueprint_setup,
                                            rule,
                                            endpoint=None,
                                            view_func=None,
                                            **options):
        '''
        Method used to patch BlueprintSetupState.add_url_rule for setup
        state instance corresponding to this Api instance.  Exists primarily
        to enable _complete_url's function.

        :param blueprint_setup: The BlueprintSetupState instance (self)
        :param rule: A string or callable that takes a string and returns a
            string(_complete_url) that is the url rule for the endpoint
            being registered
        :param endpoint: See BlueprintSetupState.add_url_rule
        :param view_func: See BlueprintSetupState.add_url_rule
        :param **options: See BlueprintSetupState.add_url_rule
        '''

        if callable(rule):
            rule = rule(blueprint_setup.url_prefix)
        elif blueprint_setup.url_prefix:
            rule = blueprint_setup.url_prefix + rule
        options.setdefault('subdomain', blueprint_setup.subdomain)
        if endpoint is None:
            endpoint = _endpoint_from_view_func(view_func)
        defaults = blueprint_setup.url_defaults
        if 'defaults' in options:
            defaults = dict(defaults, **options.pop('defaults'))
        blueprint_setup.app.add_url_rule(
            rule,
            '%s.%s' % (blueprint_setup.blueprint.name, endpoint),
            view_func,
            defaults=defaults,
            **options)
コード例 #3
0
ファイル: app.py プロジェクト: seraphln/wheel
 def add_wheel_url_rule(self, rule, endpoint=None,
                         view_func=None, **options):
     if endpoint is None:
         endpoint = _endpoint_from_view_func(view_func)
     if not endpoint.startswith('wheel.'):
         endpoint = 'wheel.core.' + endpoint
     self.add_url_rule(rule, endpoint, view_func, **options)
コード例 #4
0
 def add_unifispot_url_rule(self, rule, endpoint=None,
                         view_func=None, **options):
     if endpoint is None:
         endpoint = _endpoint_from_view_func(view_func)
     if not endpoint.startswith('unifispot.'):
         endpoint = 'unifispot.core.' + endpoint
     self.add_url_rule(rule, endpoint, view_func, **options)
コード例 #5
0
ファイル: modulemanager.py プロジェクト: qbx2/opencampus
    def decorator(f):
        @wraps(f)
        def decorated(*args, **kwargs):
            campus_id = kwargs.pop('campus_id', None)

            try:
                request.campus = Campus.objects(id=campus_id).get()
            except Campus.DoesNotExist:
                return abort(404)

            if not is_super_user() and session.get_admin_account() not in request.campus.admins:
                return abort(403)

            return f(*args, **kwargs)

        endpoint = options.pop('endpoint', None)
        menu = options.pop('menu', None)
        console_blueprint.add_url_rule(rule, endpoint, decorated, **options)

        if menu:
            from flask.helpers import _endpoint_from_view_func

            get_manager_menus().append({
                'name': menu,
                'endpoint': 'console.%s' % (endpoint if endpoint else _endpoint_from_view_func(decorated))
            })

        return decorated
コード例 #6
0
ファイル: __init__.py プロジェクト: feigner/flask-restful
    def _blueprint_setup_add_url_rule_patch(blueprint_setup, rule, endpoint=None, view_func=None, **options):
        """Method used to patch BlueprintSetupState.add_url_rule for setup
        state instance corresponding to this Api instance.  Exists primarily
        to enable _complete_url's function.
        :param blueprint_setup: The BlueprintSetupState instance (self)
        :param rule: A string or callable that takes a string and returns a
            string(_complete_url) that is the url rule for the endpoint
            being registered
        :param endpoint: See BlueprintSetupState.add_url_rule
        :param view_func: See BlueprintSetupState.add_url_rule
        :param **options: See BlueprintSetupState.add_url_rule
        """

        if callable(rule):
            rule = rule(blueprint_setup.url_prefix)
        elif blueprint_setup.url_prefix:
            rule = blueprint_setup.url_prefix + rule
        options.setdefault('subdomain', blueprint_setup.subdomain)
        if endpoint is None:
            endpoint = _endpoint_from_view_func(view_func)
        defaults = blueprint_setup.url_defaults
        if 'defaults' in options:
            defaults = dict(defaults, **options.pop('defaults'))
        blueprint_setup.app.add_url_rule(rule, '%s.%s' % (blueprint_setup.blueprint.name, endpoint),
                                         view_func, defaults=defaults, **options)
コード例 #7
0
ファイル: app.py プロジェクト: gmsjy/quokka_ng
 def add_quokka_url_rule(self, rule, endpoint=None,
                         view_func=None, **options):
     """Builds urls using quokka. prefix to avoid conflicts
     with external modules urls."""
     if endpoint is None:
         endpoint = _endpoint_from_view_func(view_func)
     if not endpoint.startswith('quokka.'):
         endpoint = 'quokka.' + endpoint
     self.add_url_rule(rule, endpoint, view_func, **options)
コード例 #8
0
ファイル: app.py プロジェクト: AquaBindi/quokka
 def add_quokka_url_rule(self, rule, endpoint=None,
                         view_func=None, **options):
     """Builds urls using quokka. prefix to avoid conflicts
     with external modules urls."""
     if endpoint is None:
         endpoint = _endpoint_from_view_func(view_func)
     if not endpoint.startswith('quokka.'):
         endpoint = 'quokka.' + endpoint
     self.add_url_rule(rule, endpoint, view_func, **options)
コード例 #9
0
    def add_url_rule(self, rule, endpoint, f, **options):
        if endpoint is None:
            endpoint = _endpoint_from_view_func(f)

        methods = options.pop('methods', None)
        options.setdefault('defaults', {}).setdefault('ws', None)

        self.url_map.add(Rule(rule, endpoint=endpoint, **options))
        self.view_functions[endpoint] = f

        if methods is None:
            methods = []
        self.app.add_url_rule(rule, endpoint, f, methods=methods, **options)
コード例 #10
0
    def add_url_rule(self, rule, endpoint=None, view_func=None, **options):
        if endpoint is None:
            endpoint = _endpoint_from_view_func(view_func)
        options['endpoint'] = endpoint
        methods = options.pop('methods', None)

        if methods is None:
            methods = getattr(view_func, 'methods', None) or ('GET', )
        methods = set(methods)

        required_methods = set(getattr(view_func, 'required_methods', ()))

        provide_automatic_options = getattr(view_func,
                                            'provide_automatic_options', None)
        if provide_automatic_options is None:
            if "OPTIONS" not in methods:
                provide_automatic_options = True
                required_methods.add('OPTIONS')
            else:
                provide_automatic_options = False

        # 设置 version_dict
        if self.version_dict.get(rule) is None:
            self.version_dict[rule] = []

        version_list = self.version_dict.get(rule.strip())

        # 添加version list
        version = options.get('version')
        if version and isinstance(version, list):
            for item in version:
                if item not in version_list:
                    version_list.append(item)

        # 增加回调时 的 methods
        methods |= required_methods

        rule = self.url_rule_class(rule, methods=methods, **options)
        rule.provide_automatic_options = provide_automatic_options

        self.url_map.add(rule)
        if view_func is not None:
            old_func = self.view_functions.get(endpoint)
            if old_func is not None and old_func != view_func:
                raise AssertionError('View function mapping is overwriting an '
                                     'existing endpoint function: %s' %
                                     endpoint)

            self.view_functions[endpoint] = view_func
コード例 #11
0
 def add_url_rule(self, rule, endpoint=None, view_func=None, **options):
     if self.url_prefix is not None:
         if rule:
             rule = f'{self.blueprint.get_full_url_prefix()}{rule}'
         else:
             rule = self.url_prefix
     options.setdefault('subdomain', self.subdomain)
     if endpoint is None:
         endpoint = _endpoint_from_view_func(view_func)
     else:
         endpoint = f'{self.blueprint.get_full_endpoint()}.{endpoint}'
     defaults = self.url_defaults
     if 'defaults' in options:
         defaults = dict(defaults, **options.pop('defaults'))
     self.app.add_url_rule(rule, endpoint, view_func, defaults=defaults, **options)
コード例 #12
0
 def add_url_rule(self, rule, endpoint=None, view_func=None, **options):
     """
     A helper method to register a rule (and optionally a view function)
     to the application.  The endpoint is automatically prefixed with the
     blueprint's name.
     """
     if self.url_prefix:
         rule = self.url_prefix + rule
     options.setdefault('subdomain', self.subdomain)
     if endpoint is None:
         endpoint = _endpoint_from_view_func(view_func)
     defaults = self.url_defaults
     if 'defaults' in options:
         defaults = dict(defaults, **options.pop('defaults'))
     self.app.add_url_rule(rule, endpoint, view_func, defaults=defaults, **options)
コード例 #13
0
ファイル: extension.py プロジェクト: abilian/abilian-core
        def add_url_rule(rule, endpoint=None, view_func=None, **kwargs):
            if not rule:
                # '' is already used for panel get/post
                raise ValueError("Invalid additional url rule: {}".format(repr(rule)))

            if endpoint is None:
                endpoint = _endpoint_from_view_func(view_func)

            if not endpoint.startswith(base_endpoint):
                endpoint = base_endpoint + "_" + endpoint

            extension._panels_endpoints["admin." + endpoint] = panel
            return self.blueprint.add_url_rule(
                base_url + rule, endpoint=endpoint, view_func=view_func, **kwargs
            )
コード例 #14
0
    def add_url_rule(self, rule, endpoint=None, view_func=None, **options):
        if endpoint is None:
            endpoint = _endpoint_from_view_func(view_func)
        options['endpoint'] = endpoint
        methods = options.pop('methods', None)

        # if the methods are not given and the view_func object knows its
        # methods we can use that instead.  If neither exists, we go with
        # a tuple of only ``GET`` as default.
        if methods is None:
            methods = getattr(view_func, 'methods', None) or ('GET', )
        if isinstance(methods, string_types):
            raise TypeError('Allowed methods have to be iterables of strings, '
                            'for example: @app.route(..., methods=["POST"])')
        methods = set(item.upper() for item in methods)

        # Methods that should always be added
        required_methods = set(getattr(view_func, 'required_methods', ()))

        # starting with Flask 0.8 the view_func object can disable and
        # force-enable the automatic options handling.
        provide_automatic_options = getattr(view_func,
                                            'provide_automatic_options', None)

        if provide_automatic_options is None:
            if 'OPTIONS' not in methods:
                provide_automatic_options = True
                required_methods.add('OPTIONS')
            else:
                provide_automatic_options = False

        # Add the required methods now.
        methods |= required_methods

        if not isinstance(rule, Rule):
            rule = self.url_rule_class(rule, methods=methods, **options)
            rule.provide_automatic_options = provide_automatic_options

        self.url_map.add(rule)
        if view_func is not None:
            old_func = self.view_functions.get(endpoint)
            if old_func is not None and old_func != view_func:
                raise AssertionError('View function mapping is overwriting an '
                                     'existing endpoint function: %s' %
                                     endpoint)
            self.view_functions[endpoint] = view_func
コード例 #15
0
ファイル: extension.py プロジェクト: rmoorman/abilian-core
        def add_url_rule(rule, endpoint=None, view_func=None, *args, **kwargs):
            if not rule:
                # '' is already used for panel get/post
                raise ValueError('Invalid additional url rule: {}'.format(
                    repr(rule)))

            if endpoint is None:
                endpoint = _endpoint_from_view_func(view_func)

            if not endpoint.startswith(base_endpoint):
                endpoint = base_endpoint + '_' + endpoint

            extension._panels_endpoints['admin.' + endpoint] = panel
            return self.blueprint.add_url_rule(base_url + rule,
                                               endpoint=endpoint,
                                               view_func=view_func,
                                               *args,
                                               **kwargs)
コード例 #16
0
        def add_url_rule(
            rule: str,
            endpoint: Optional[Any] = None,
            view_func: Optional[Callable] = None,
            **kwargs: Any,
        ) -> None:
            if not rule:
                # '' is already used for panel get/post
                raise ValueError(f"Invalid additional url rule: {repr(rule)}")

            if endpoint is None:
                endpoint = _endpoint_from_view_func(view_func)

            if not endpoint.startswith(base_endpoint):
                endpoint = base_endpoint + "_" + endpoint

            extension._panels_endpoints["admin." + endpoint] = panel
            self.blueprint.add_url_rule(base_url + rule,
                                        endpoint=endpoint,
                                        view_func=view_func,
                                        **kwargs)
コード例 #17
0
 def add_flaskpress_url_rule(self, rule, endpoint=None, view_func=None, **options):
     if endpoint is None:
         endpoint = _endpoint_from_view_func(view_func)
     if not endpoint.startswith('flaskpress.'):
         endpoint = 'flaskpress.core.' + endpoint
     self.add_url_rule(rule, endpoint, view_func, **options)
コード例 #18
0
ファイル: app.py プロジェクト: zhangst23/tutorial
 def add_quokka_url_rule(self, rule, endpoint=None, view_func=None, **options):
     if endpoint is None:
         endpoint = _endpoint_from_view_func(view_func)
     if not endpoint.startswitch("quokka."):
         endpoint = "quokka.core." + endpoint
     self.add_url_rule(rule, endpoint, view_func, **options)
コード例 #19
0
ファイル: helpers.py プロジェクト: mbr/flask-arrest
 def _(f):
     self.add_mimetype(extra_type, _endpoint_from_view_func(f))
     return f
コード例 #20
0
ファイル: helpers.py プロジェクト: mbr/flask-arrest
 def _(f):
     self.set_mimetypes(only_types, _endpoint_from_view_func(f))
     return f
コード例 #21
0
    def add_url_rule(self, rule, endpoint=None, view_func=None, **options):
        if endpoint is None:
            endpoint = _endpoint_from_view_func(view_func)

        self.url_map.add(Rule(rule, endpoint=endpoint, **options))
        self.view_functions[endpoint] = view_func
コード例 #22
0
def add_url_rule_no_options(self,
                            rule,
                            endpoint=None,
                            view_func=None,
                            **options):
    """Connects a URL rule.  Works exactly like the :meth:`route`
    decorator.  If a view_func is provided it will be registered with the
    endpoint.

    Basically this example::

        @app.route('/')
        def index():
            pass

    Is equivalent to the following::

        def index():
            pass
        app.add_url_rule('/', 'index', index)

    If the view_func is not provided you will need to connect the endpoint
    to a view function like so::

        app.view_functions['index'] = index

    Internally :meth:`route` invokes :meth:`add_url_rule` so if you want
    to customize the behavior via subclassing you only need to change
    this method.

    For more information refer to :ref:`url-route-registrations`.

    .. versionchanged:: 0.2
       `view_func` parameter added.

    .. versionchanged:: 0.6
       ``OPTIONS`` is added automatically as method.

    :param rule: the URL rule as string
    :param endpoint: the endpoint for the registered URL rule.  Flask
                     itself assumes the name of the view function as
                     endpoint
    :param view_func: the function to call when serving a request to the
                      provided endpoint
    :param options: the options to be forwarded to the underlying
                    :class:`~werkzeug.routing.Rule` object.  A change
                    to Werkzeug is handling of method options.  methods
                    is a list of methods this rule should be limited
                    to (``GET``, ``POST`` etc.).  By default a rule
                    just listens for ``GET`` (and implicitly ``HEAD``).
                    Starting with Flask 0.6, ``OPTIONS`` is implicitly
                    added and handled by the standard request handling.
    """
    if endpoint is None:
        endpoint = _endpoint_from_view_func(view_func)
    options['endpoint'] = endpoint
    methods = options.pop('methods', None)

    # if the methods are not given and the view_func object knows its
    # methods we can use that instead.  If neither exists, we go with
    # a tuple of only ``GET`` as default.
    if methods is None:
        methods = getattr(view_func, 'methods', None) or ('GET', )
    if isinstance(methods, string_types):
        raise TypeError('Allowed methods have to be iterables of strings, '
                        'for example: @app.route(..., methods=["POST"])')
    methods = set(item.upper() for item in methods)

    # Methods that should always be added
    required_methods = set(getattr(view_func, 'required_methods', ()))

    # starting with Flask 0.8 the view_func object can disable and
    # force-enable the automatic options handling.
    provide_automatic_options = getattr(view_func, 'provide_automatic_options',
                                        None)

    if provide_automatic_options is None:
        if 'OPTIONS' not in methods:
            provide_automatic_options = False

        else:
            provide_automatic_options = True
            required_methods.add('OPTIONS')

    # Add the required methods now.
    methods |= required_methods

    rule = self.url_rule_class(rule, methods=methods, **options)
    rule.provide_automatic_options = provide_automatic_options

    self.url_map.add(rule)
    if view_func is not None:
        old_func = self.view_functions.get(endpoint)
        if old_func is not None and old_func != view_func:
            raise AssertionError('View function mapping is overwriting an '
                                 'existing endpoint function: %s' % endpoint)
        self.view_functions[endpoint] = view_func
コード例 #23
0
ファイル: flask_raas.py プロジェクト: aspinuso/s-provenance
def add_url_rule_no_options(self, rule, endpoint=None, view_func=None, **options):
    """Connects a URL rule.  Works exactly like the :meth:`route`
    decorator.  If a view_func is provided it will be registered with the
    endpoint.

    Basically this example::

        @app.route('/')
        def index():
            pass

    Is equivalent to the following::

        def index():
            pass
        app.add_url_rule('/', 'index', index)

    If the view_func is not provided you will need to connect the endpoint
    to a view function like so::

        app.view_functions['index'] = index

    Internally :meth:`route` invokes :meth:`add_url_rule` so if you want
    to customize the behavior via subclassing you only need to change
    this method.

    For more information refer to :ref:`url-route-registrations`.

    .. versionchanged:: 0.2
       `view_func` parameter added.

    .. versionchanged:: 0.6
       ``OPTIONS`` is added automatically as method.

    :param rule: the URL rule as string
    :param endpoint: the endpoint for the registered URL rule.  Flask
                     itself assumes the name of the view function as
                     endpoint
    :param view_func: the function to call when serving a request to the
                      provided endpoint
    :param options: the options to be forwarded to the underlying
                    :class:`~werkzeug.routing.Rule` object.  A change
                    to Werkzeug is handling of method options.  methods
                    is a list of methods this rule should be limited
                    to (``GET``, ``POST`` etc.).  By default a rule
                    just listens for ``GET`` (and implicitly ``HEAD``).
                    Starting with Flask 0.6, ``OPTIONS`` is implicitly
                    added and handled by the standard request handling.
    """
    if endpoint is None:
        endpoint = _endpoint_from_view_func(view_func)
    options['endpoint'] = endpoint
    methods = options.pop('methods', None)

    # if the methods are not given and the view_func object knows its
    # methods we can use that instead.  If neither exists, we go with
    # a tuple of only ``GET`` as default.
    if methods is None:
        methods = getattr(view_func, 'methods', None) or ('GET',)
    if isinstance(methods, string_types):
        raise TypeError('Allowed methods have to be iterables of strings, '
                        'for example: @app.route(..., methods=["POST"])')
    methods = set(item.upper() for item in methods)

    # Methods that should always be added
    required_methods = set(getattr(view_func, 'required_methods', ()))

    # starting with Flask 0.8 the view_func object can disable and
    # force-enable the automatic options handling.
    provide_automatic_options = getattr(view_func,
        'provide_automatic_options', None)

    if provide_automatic_options is None:
        if 'OPTIONS' not in methods:
            provide_automatic_options = False
            
        else:
            provide_automatic_options = True
            required_methods.add('OPTIONS')

    # Add the required methods now.
    methods |= required_methods

    rule = self.url_rule_class(rule, methods=methods, **options)
    rule.provide_automatic_options = provide_automatic_options

    self.url_map.add(rule)
    if view_func is not None:
        old_func = self.view_functions.get(endpoint)
        if old_func is not None and old_func != view_func:
            raise AssertionError('View function mapping is overwriting an '
                                 'existing endpoint function: %s' % endpoint)
        self.view_functions[endpoint] = view_func
コード例 #24
0
def _add_log(level, func, endpoint=None):
    if not isinstance(level, int):
        level = DEBUG
    if not endpoint:
        endpoint = _endpoint_from_view_func(func)
    SpecificLevelLog[endpoint] = level