Skip to content

Commit

Permalink
Replace 'generate_name' with 'unique_route_name_generate'
Browse files Browse the repository at this point in the history
- add new test_case
  • Loading branch information
hamed committed Mar 25, 2024
1 parent 8c74b6c commit 9a0b6a2
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 20 deletions.
14 changes: 7 additions & 7 deletions sanic/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]] = {}
Expand All @@ -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:
...

Expand All @@ -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:
...

Expand All @@ -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:
...

Expand All @@ -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:
...

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions sanic/blueprints.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class Blueprint(BaseSanic):
"version",
"version_prefix",
"websocket_routes",
"generate_name",
"unique_route_name_generate",
)

def __init__(
Expand All @@ -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()
Expand All @@ -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(
Expand Down
12 changes: 6 additions & 6 deletions sanic/mixins/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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}"

Expand Down
4 changes: 3 additions & 1 deletion sanic/mixins/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
11 changes: 8 additions & 3 deletions tests/test_blueprints.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("/")
Expand All @@ -324,15 +324,20 @@ 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!")

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


Expand Down

0 comments on commit 9a0b6a2

Please sign in to comment.