Exemplo n.º 1
0
    def group(*blueprints, url_prefix="", version=None, strict_slashes=None):
        """
        Create a list of blueprints, optionally grouping them under a
        general URL prefix.

        :param blueprints: blueprints to be registered as a group
        :param url_prefix: URL route to be prepended to all sub-prefixes
        :param version: API Version to be used for Blueprint group
        :param strict_slashes: Indicate strict slash termination behavior
            for URL
        """

        def chain(nested) -> Iterable[Blueprint]:
            """itertools.chain() but leaves strings untouched"""
            for i in nested:
                if isinstance(i, (list, tuple)):
                    yield from chain(i)
                elif isinstance(i, BlueprintGroup):
                    yield from i.blueprints
                else:
                    yield i

        bps = BlueprintGroup(
            url_prefix=url_prefix,
            version=version,
            strict_slashes=strict_slashes,
        )
        for bp in chain(blueprints):
            bps.append(bp)
        return bps
Exemplo n.º 2
0
    def group(*blueprints, url_prefix=""):
        """
        Create a list of blueprints, optionally grouping them under a
        general URL prefix.

        :param blueprints: blueprints to be registered as a group
        :param url_prefix: URL route to be prepended to all sub-prefixes
        """

        def chain(nested):
            """itertools.chain() but leaves strings untouched"""
            for i in nested:
                if isinstance(i, (list, tuple)):
                    yield from chain(i)
                elif isinstance(i, BlueprintGroup):
                    yield from i.blueprints
                else:
                    yield i

        bps = BlueprintGroup(url_prefix=url_prefix)
        for bp in chain(blueprints):
            if bp.url_prefix is None:
                bp.url_prefix = ""
            bp.url_prefix = url_prefix + bp.url_prefix
            bps.append(bp)
        return bps
Exemplo n.º 3
0
    def group(*blueprints, url_prefix=""):
        """
        Create a list of blueprints, optionally grouping them under a
        general URL prefix.

        :param blueprints: blueprints to be registered as a group
        :param url_prefix: URL route to be prepended to all sub-prefixes
        """

        def chain(nested):
            """itertools.chain() but leaves strings untouched"""
            for i in nested:
                if isinstance(i, (list, tuple)):
                    yield from chain(i)
                elif isinstance(i, BlueprintGroup):
                    yield from i.blueprints
                else:
                    yield i

        bps = BlueprintGroup(url_prefix=url_prefix)
        for bp in chain(blueprints):
            if bp.url_prefix is None:
                bp.url_prefix = ""
            bp.url_prefix = url_prefix + bp.url_prefix
            bps.append(bp)
        return bps
Exemplo n.º 4
0
def test_blueprint_group_insert():
    blueprint_1 = Blueprint(
        "blueprint_1", url_prefix="/bp1", strict_slashes=True, version=1
    )
    blueprint_2 = Blueprint("blueprint_2", url_prefix="/bp2")
    blueprint_3 = Blueprint("blueprint_3", url_prefix=None)
    group = BlueprintGroup(
        url_prefix="/test", version=1.3, strict_slashes=False
    )
    group.insert(0, blueprint_1)
    group.insert(0, blueprint_2)
    group.insert(0, blueprint_3)

    @blueprint_1.route("/")
    def blueprint_1_default_route(request):
        return text("BP1_OK")

    @blueprint_2.route("/")
    def blueprint_2_default_route(request):
        return text("BP2_OK")

    @blueprint_3.route("/")
    def blueprint_3_default_route(request):
        return text("BP3_OK")

    app = Sanic("PropTest")
    app.blueprint(group)
    app.router.finalize()

    routes = [(route.path, route.strict) for route in app.router.routes]

    assert len(routes) == 3
    assert ("v1/test/bp1/", True) in routes
    assert ("v1.3/test/bp2", False) in routes
    assert ("v1.3/test", False) in routes
Exemplo n.º 5
0
def test_blueprint_group_insert():
    blueprint_1 = Blueprint(
        "blueprint_1", url_prefix="/bp1", strict_slashes=True, version=1
    )
    blueprint_2 = Blueprint("blueprint_2", url_prefix="/bp2")
    blueprint_3 = Blueprint("blueprint_3", url_prefix=None)
    group = BlueprintGroup(
        url_prefix="/test", version=1.3, strict_slashes=False
    )
    group.insert(0, blueprint_1)
    group.insert(0, blueprint_2)
    group.insert(0, blueprint_3)
    assert group.blueprints[1].strict_slashes is False
    assert group.blueprints[2].strict_slashes is True
    assert group.blueprints[0].url_prefix == "/test"