def process_objects(self, app: FlaskUnchained, routes: Iterable[Route]):
        for route in _reduce_routes(routes):
            if route.should_register(app):
                if route.module_name and route in self.bundle.endpoints[
                        route.endpoint]:
                    import warnings
                    warnings.warn(f'Duplicate route found: {route}')
                    continue

                self.bundle.endpoints[route.endpoint].append(route)
                if route._controller_cls:
                    key = f'{route._controller_cls.__name__}.{route.method_name}'
                    self.bundle.controller_endpoints[key].append(route)

        # build up a list of bundles with views:
        bundle_module_names = [
        ]  # [tuple(top_bundle_module_name, hierarchy_module_names)]
        for bundle in app.unchained.bundles.values():
            hierarchy = [
                bundle_super.module_name
                for bundle_super in bundle._iter_class_hierarchy()
                if bundle_super._has_views
            ]
            if hierarchy:
                bundle_module_names.append((bundle.module_name, hierarchy))

        # for each route, figure out which bundle hierarchy it's from, and assign the
        # route to the top bundle for that hierarchy
        bundle_route_endpoints = set()
        for endpoint, routes in self.bundle.endpoints.items():
            for route in routes:
                if not route.module_name:
                    continue

                # FIXME would be nice for routes to know which bundle they're from...
                for top_level_bundle_module_name, hierarchy in bundle_module_names:
                    for bundle_module_name in hierarchy:
                        if route.module_name.startswith(bundle_module_name):
                            self.bundle.bundle_routes[
                                top_level_bundle_module_name].append(route)
                            bundle_route_endpoints.add(endpoint)
                            break

        # get all the remaining routes not belonging to a bundle
        self.bundle.other_routes = itertools.chain.from_iterable([
            routes for endpoint, routes in self.bundle.endpoints.items()
            if endpoint not in bundle_route_endpoints
        ])

        # we register non-bundle routes with the app here, and
        # the RegisterBundleBlueprintsHook registers the bundle routes
        for route in self.bundle.other_routes:
            app.add_url_rule(route.full_rule,
                             defaults=route.defaults,
                             endpoint=route.endpoint,
                             methods=route.methods,
                             view_func=route.view_func,
                             **route.rule_options)
    def process_objects(self, app: FlaskUnchained, routes: Iterable[Route]):
        for route in _reduce_routes(routes):
            # FIXME maybe validate routes first? (eg for duplicates?)
            # Flask doesn't complain; it will match the first route found,
            # but maybe we should at least warn the user?
            if route.should_register(app):
                self.bundle.endpoints[route.endpoint].append(route)
                if route._controller_cls:
                    key = f'{route._controller_cls.__name__}.{route.method_name}'
                    self.bundle.controller_endpoints[key].append(route)

        bundle_names = [(
            bundle.module_name,
            [
                bundle_super.module_name
                for bundle_super in bundle._iter_class_hierarchy(
                    include_self=False) if bundle_super._has_views()
            ],
        ) for bundle in app.unchained.bundles.values()]

        bundle_route_endpoints = set()
        for endpoint, routes in self.bundle.endpoints.items():
            for route in routes:
                module_name = route.module_name
                for top_level_bundle_name, hierarchy in bundle_names:
                    for bundle_name in hierarchy:
                        if module_name and module_name.startswith(bundle_name):
                            self.bundle.bundle_routes[
                                top_level_bundle_name].append(route)
                            bundle_route_endpoints.add(endpoint)
                            break

        self.bundle.other_routes = itertools.chain.from_iterable([
            routes for endpoint, routes in self.bundle.endpoints.items()
            if endpoint not in bundle_route_endpoints
        ])

        for route in self.bundle.other_routes:
            app.add_url_rule(route.full_rule,
                             defaults=route.defaults,
                             endpoint=route.endpoint,
                             methods=route.methods,
                             view_func=route.view_func,
                             **route.rule_options)
예제 #3
0
    def after_init_app(self, app: FlaskUnchained):
        if app.config.GRAPHENE_URL:
            app.add_url_rule(app.config.GRAPHENE_URL,
                             view_func=GraphQLView.as_view(
                                 'graphql',
                                 schema=self.root_schema,
                                 graphiql=app.config.GRAPHENE_ENABLE_GRAPHIQL,
                                 pretty=app.config.GRAPHENE_PRETTY_JSON,
                                 batch=False,
                             ))

        if app.config.GRAPHENE_BATCH_URL:
            app.add_url_rule(app.config.GRAPHENE_BATCH_URL,
                             view_func=GraphQLView.as_view(
                                 'graphql',
                                 schema=self.root_schema,
                                 graphiql=app.config.GRAPHENE_ENABLE_GRAPHIQL,
                                 pretty=app.config.GRAPHENE_PRETTY_JSON,
                                 batch=True,
                             ))
예제 #4
0
 def add_url_rule(self, app: FlaskUnchained, rule: str, **kwargs):
     if app.config.ENABLE_URL_LANG_CODE_PREFIX:
         app.add_url_rule(self.get_url_rule(rule),
                          register_with_babel=False,
                          **kwargs)