From 9a0b6a214bbe8dc1a00e47dd217b80a75a082a81 Mon Sep 17 00:00:00 2001 From: hamed Date: Mon, 25 Mar 2024 09:45:43 +0300 Subject: [PATCH] Replace 'generate_name' with 'unique_route_name_generate' - add new test_case --- sanic/app.py | 14 +++++++------- sanic/blueprints.py | 6 +++--- sanic/mixins/base.py | 12 ++++++------ sanic/mixins/routes.py | 4 +++- tests/test_blueprints.py | 11 ++++++++--- 5 files changed, 27 insertions(+), 20 deletions(-) diff --git a/sanic/app.py b/sanic/app.py index 4052f6404d..a0d114b083 100644 --- a/sanic/app.py +++ b/sanic/app.py @@ -223,7 +223,7 @@ class to use for the application. Defaults to `None`. "strict_slashes", "websocket_enabled", "websocket_tasks", - "generate_name", + "unique_route_name_generate", ) _app_registry: ClassVar[Dict[str, "Sanic"]] = {} @@ -248,7 +248,7 @@ def __init__( inspector: bool = False, inspector_class: Optional[Type[Inspector]] = None, certloader_class: Optional[Type[CertLoader]] = None, - generate_name: bool = False, + unique_route_name_generate: bool = False, ) -> None: ... @@ -271,7 +271,7 @@ def __init__( inspector: bool = False, inspector_class: Optional[Type[Inspector]] = None, certloader_class: Optional[Type[CertLoader]] = None, - generate_name: bool = False, + unique_route_name_generate: bool = False, ) -> None: ... @@ -294,7 +294,7 @@ def __init__( inspector: bool = False, inspector_class: Optional[Type[Inspector]] = None, certloader_class: Optional[Type[CertLoader]] = None, - generate_name: bool = False, + unique_route_name_generate: bool = False, ) -> None: ... @@ -317,7 +317,7 @@ def __init__( inspector: bool = False, inspector_class: Optional[Type[Inspector]] = None, certloader_class: Optional[Type[CertLoader]] = None, - generate_name: bool = False, + unique_route_name_generate: bool = False, ) -> None: ... @@ -339,7 +339,7 @@ def __init__( inspector: bool = False, inspector_class: Optional[Type[Inspector]] = None, certloader_class: Optional[Type[CertLoader]] = None, - generate_name: bool = False, + unique_route_name_generate: bool = False, ) -> None: super().__init__(name=name) # logging @@ -396,7 +396,7 @@ def __init__( self.strict_slashes: bool = strict_slashes self.websocket_enabled: bool = False self.websocket_tasks: Set[Future[Any]] = set() - self.generate_name = generate_name + self.unique_route_name_generate = unique_route_name_generate # Register alternative method names self.go_fast = self.run diff --git a/sanic/blueprints.py b/sanic/blueprints.py index 7b9bd641fb..adec685735 100644 --- a/sanic/blueprints.py +++ b/sanic/blueprints.py @@ -117,7 +117,7 @@ class Blueprint(BaseSanic): "version", "version_prefix", "websocket_routes", - "generate_name", + "unique_route_name_generate", ) def __init__( @@ -128,7 +128,7 @@ def __init__( version: Optional[Union[int, str, float]] = None, strict_slashes: Optional[bool] = None, version_prefix: str = "/v", - generate_name: bool = False, + unique_route_name_generate: bool = False, ): super().__init__(name=name) self.reset() @@ -144,7 +144,7 @@ def __init__( ) self.version = version self.version_prefix = version_prefix - self.generate_name = generate_name + self.unique_route_name_generate = unique_route_name_generate def __repr__(self) -> str: args = ", ".join( diff --git a/sanic/mixins/base.py b/sanic/mixins/base.py index 0053cf599e..ad702bf694 100644 --- a/sanic/mixins/base.py +++ b/sanic/mixins/base.py @@ -9,15 +9,15 @@ class BaseMixin(metaclass=SanicMeta): name: str strict_slashes: Optional[bool] - def _generate_name(self, *objects, route_generate=False, methods=None, uri=None) -> str: + def _generate_name(self, *objects, unique_route_name_generate=False, methods=None, uri=None) -> str: name = None - named_route = False + _named_route = False for obj in objects: if obj: if isinstance(obj, str): name = obj - named_route = True + _named_route = True break try: @@ -34,12 +34,12 @@ def _generate_name(self, *objects, route_generate=False, methods=None, uri=None) raise ValueError("Could not generate a name for handler") if not name.startswith(f"{self.name}."): - if route_generate and not named_route: + if unique_route_name_generate and not _named_route: if methods: - methods = "_".join(methods) + methods = "-".join(methods) name = f"{name}_{methods}" if uri: - # uri = uri.replace("/", "/") + uri = uri.replace("/", "S") name = f"{name}_{uri}" name = f"{self.name}.{name}" diff --git a/sanic/mixins/routes.py b/sanic/mixins/routes.py index 6b5d2ddae5..b30db765c5 100644 --- a/sanic/mixins/routes.py +++ b/sanic/mixins/routes.py @@ -139,7 +139,9 @@ def decorator(handler): # variable will be a tuple of (existing routes, handler fn) _, handler = handler - name = self._generate_name(name, handler, route_generate=self.generate_name, methods=methods, uri=uri) + name = self._generate_name( + name, handler, unique_route_name_generate=self.unique_route_name_generate, methods=methods, uri=uri + ) if isinstance(host, str): host = frozenset([host]) diff --git a/tests/test_blueprints.py b/tests/test_blueprints.py index fd7867f2d8..f4133a9b4f 100644 --- a/tests/test_blueprints.py +++ b/tests/test_blueprints.py @@ -313,7 +313,7 @@ def test_bp_with_auto_name_generate(app: Sanic): "test_bp_host", url_prefix="/test1", host=["example.com", "sub.example.com"], - generate_name=True, + unique_route_name_generate=True, ) @bp.route("/") @@ -324,6 +324,10 @@ def handler1(request): def handler2(request): return text("Hello subdomain!") + @bp.route("/route_multiple_method", methods=["GET", "POST"]) + def handler3(request): + return text("Hello subdomain!") + @bp.route("/route_with_name", methods=["GET", "POST"], name="handler3") def handler3(request): return text("Hello subdomain!") @@ -331,8 +335,9 @@ def handler3(request): app.blueprint(bp) route_names = [r.name for r in app.router.routes] - assert "test_bp_with_auto_name_generate.test_bp_host.handler1_GET_/" in route_names - assert "test_bp_with_auto_name_generate.test_bp_host.handler2_GET_/" in route_names + assert "test_bp_with_auto_name_generate.test_bp_host.handler1_GET_S" in route_names + assert "test_bp_with_auto_name_generate.test_bp_host.handler2_GET_S" in route_names + assert "test_bp_with_auto_name_generate.test_bp_host.handler3_GET-POST_Sroute_multiple_method" in route_names assert "test_bp_with_auto_name_generate.test_bp_host.handler3" in route_names