Skip to content

Commit 2200b12

Browse files
committed
Add custom prefix setting to View.
1 parent 8c55b42 commit 2200b12

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

starlette_plus/core.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import inspect
2020
import logging
2121
from collections.abc import Callable, Coroutine, Iterator, Sequence
22-
from typing import TYPE_CHECKING, Any, Self, TypeAlias, TypedDict, Unpack
22+
from typing import TYPE_CHECKING, Any, ClassVar, Self, TypeAlias, TypedDict, Unpack
2323

2424
from starlette.applications import Starlette
2525
from starlette.requests import Request
@@ -223,10 +223,15 @@ def add_view(self, view: View | Self) -> None:
223223

224224
class View:
225225
__routes__: list[Route | WebSocketRoute]
226+
__prefix__: ClassVar[str | None] = None
227+
228+
def __init_subclass__(cls, *, prefix: str | None = None) -> None:
229+
cls.__prefix__ = prefix
226230

227231
def __new__(cls, *args: Any, **kwargs: Any) -> Self:
228232
self = super().__new__(cls)
229233
name = cls.__name__
234+
prefix = cls.__prefix__ or name
230235

231236
self.__routes__ = []
232237

@@ -235,7 +240,7 @@ def __new__(cls, *args: Any, **kwargs: Any) -> Self:
235240
path: str = member._path
236241

237242
if member._prefix:
238-
path = f"/{name.lower()}/{path.lstrip('/')}"
243+
path = f"/{prefix.lower()}/{path.lstrip('/')}"
239244

240245
for method in member._methods:
241246
method = method.lower()

0 commit comments

Comments
 (0)