Skip to content

Commit

Permalink
Add custom prefix setting to View.
Browse files Browse the repository at this point in the history
  • Loading branch information
EvieePy committed Apr 26, 2024
1 parent 8c55b42 commit 2200b12
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions starlette_plus/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import inspect
import logging
from collections.abc import Callable, Coroutine, Iterator, Sequence
from typing import TYPE_CHECKING, Any, Self, TypeAlias, TypedDict, Unpack
from typing import TYPE_CHECKING, Any, ClassVar, Self, TypeAlias, TypedDict, Unpack

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

class View:
__routes__: list[Route | WebSocketRoute]
__prefix__: ClassVar[str | None] = None

def __init_subclass__(cls, *, prefix: str | None = None) -> None:
cls.__prefix__ = prefix

def __new__(cls, *args: Any, **kwargs: Any) -> Self:
self = super().__new__(cls)
name = cls.__name__
prefix = cls.__prefix__ or name

self.__routes__ = []

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

if member._prefix:
path = f"/{name.lower()}/{path.lstrip('/')}"
path = f"/{prefix.lower()}/{path.lstrip('/')}"

for method in member._methods:
method = method.lower()
Expand Down

0 comments on commit 2200b12

Please sign in to comment.