Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion sanic/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,8 +415,11 @@ def _apply_exception_handler(
def _apply_listener(self, listener: FutureListener):
return self.register_listener(listener.listener, listener.event)

def _apply_route(self, route: FutureRoute) -> List[Route]:
def _apply_route(
self, route: FutureRoute, overwrite: bool = False
) -> List[Route]:
params = route._asdict()
params["overwrite"] = overwrite
websocket = params.pop("websocket", False)
subprotocols = params.pop("subprotocols", None)

Expand Down
13 changes: 11 additions & 2 deletions sanic/blueprints.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class Blueprint(BaseSanic):
"_future_listeners",
"_future_exceptions",
"_future_signals",
"allow_route_overwrite",
"ctx",
"exceptions",
"host",
Expand All @@ -115,9 +116,11 @@ def __init__(
version: Optional[Union[int, str, float]] = None,
strict_slashes: Optional[bool] = None,
version_prefix: str = "/v",
allow_route_overwrite: bool = False,
):
super().__init__(name=name)
self.reset()
self.allow_route_overwrite = allow_route_overwrite
self.ctx = SimpleNamespace()
self.host = host
self.strict_slashes = strict_slashes
Expand Down Expand Up @@ -167,6 +170,7 @@ def registered(self) -> bool:

def reset(self):
self._apps: Set[Sanic] = set()
self.allow_route_overwrite = False
self.exceptions: List[RouteHandler] = []
self.listeners: Dict[str, List[ListenerType[Any]]] = {}
self.middlewares: List[MiddlewareType] = []
Expand All @@ -180,6 +184,7 @@ def copy(
url_prefix: Optional[Union[str, Default]] = _default,
version: Optional[Union[int, str, float, Default]] = _default,
version_prefix: Union[str, Default] = _default,
allow_route_overwrite: bool = _default,
strict_slashes: Optional[Union[bool, Default]] = _default,
with_registration: bool = True,
with_ctx: bool = False,
Expand Down Expand Up @@ -222,6 +227,8 @@ def copy(
new_bp.strict_slashes = strict_slashes
if not isinstance(version_prefix, Default):
new_bp.version_prefix = version_prefix
if not isinstance(allow_route_overwrite, Default):
new_bp.allow_route_overwrite = allow_route_overwrite

for key, value in attrs_backup.items():
setattr(self, key, value)
Expand Down Expand Up @@ -277,7 +284,7 @@ def chain(nested) -> Iterable[Blueprint]:
bps.append(bp)
return bps

def register(self, app, options):
def register(self, app: Sanic, options: Dict[str, Any]):
"""
Register the blueprint to the sanic app.

Expand Down Expand Up @@ -351,7 +358,9 @@ def register(self, app, options):
continue

registered.add(apply_route)
route = app._apply_route(apply_route)
route = app._apply_route(
apply_route, overwrite=self.allow_route_overwrite
)
operation = (
routes.extend if isinstance(route, list) else routes.append
)
Expand Down
2 changes: 2 additions & 0 deletions sanic/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def add( # type: ignore
unquote: bool = False,
static: bool = False,
version_prefix: str = "/v",
overwrite: bool = False,
error_format: Optional[str] = None,
) -> Union[Route, List[Route]]:
"""
Expand Down Expand Up @@ -120,6 +121,7 @@ def add( # type: ignore
name=name,
strict=strict_slashes,
unquote=unquote,
overwrite=overwrite,
)

if isinstance(host, str):
Expand Down