Skip to content

Commit ae1e55c

Browse files
committed
Refactored guards and versioning decorator to work for both class and route functions
1 parent 1dc89db commit ae1e55c

File tree

13 files changed

+40
-55
lines changed

13 files changed

+40
-55
lines changed

ellar/common/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55

66
from .decorators import (
77
Controller,
8+
Guards,
89
Module,
10+
Version,
911
command,
1012
exception_handler,
1113
extra_args,
1214
file,
13-
guards,
1415
middleware,
1516
on_shutdown,
1617
on_startup,
@@ -20,7 +21,6 @@
2021
set_metadata,
2122
template_filter,
2223
template_global,
23-
version,
2424
)
2525
from .routing import (
2626
Body,
@@ -57,13 +57,13 @@
5757
"ModuleRouter",
5858
"render",
5959
"Module",
60-
"guards",
60+
"Guards",
6161
"Param",
6262
"ParamTypes",
6363
"set_metadata",
6464
"Controller",
6565
"openapi_info",
66-
"version",
66+
"Version",
6767
"delete",
6868
"get",
6969
"head",

ellar/common/decorators/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@
66
from .exception import exception_handler
77
from .extra_args import extra_args
88
from .file import file
9-
from .guards import guards
9+
from .guards import Guards
1010
from .html import render, template_filter, template_global
1111
from .middleware import middleware
1212
from .modules import Module
1313
from .openapi import openapi_info
1414
from .request import on_shutdown, on_startup
1515
from .serializer import serializer_filter
16-
from .versioning import version
16+
from .versioning import Version
1717

1818
__all__ = [
1919
"serializer_filter",
2020
"Controller",
21-
"version",
22-
"guards",
21+
"Version",
22+
"Guards",
2323
"template_filter",
2424
"template_global",
2525
"file",

ellar/common/decorators/controller.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@
99
CONTROLLER_METADATA,
1010
CONTROLLER_OPERATION_HANDLER_KEY,
1111
CONTROLLER_WATERMARK,
12-
GUARDS_KEY,
1312
NOT_SET,
1413
OPERATION_ENDPOINT_KEY,
1514
REFLECT_TYPE,
16-
VERSIONING_KEY,
1715
)
1816
from ellar.core import ControllerBase
1917
from ellar.core.controller import ControllerType
@@ -22,9 +20,6 @@
2220
from ellar.di import RequestScope, injectable
2321
from ellar.reflect import reflect
2422

25-
if t.TYPE_CHECKING: # pragma: no cover
26-
from ellar.core.guard import GuardCanActivate
27-
2823

2924
def get_route_functions(
3025
cls: t.Type,
@@ -79,8 +74,6 @@ def Controller(
7974
external_doc_description: str = None,
8075
external_doc_url: str = None,
8176
name: str = None,
82-
version: t.Union[t.Tuple, str] = (),
83-
guards: t.List[t.Union[t.Type["GuardCanActivate"], "GuardCanActivate"]] = None,
8477
include_in_schema: bool = True,
8578
) -> t.Union[t.Type[ControllerBase], t.Callable[..., t.Any], t.Any]: # pragma: no cover
8679
"""
@@ -109,8 +102,6 @@ def Controller(
109102
external_doc_description: str = None,
110103
external_doc_url: str = None,
111104
name: str = None,
112-
version: t.Union[t.Tuple, str] = (),
113-
guards: t.List[t.Union[t.Type["GuardCanActivate"], "GuardCanActivate"]] = None,
114105
include_in_schema: bool = True,
115106
) -> t.Union[t.Type[ControllerBase], t.Callable[..., t.Any], t.Any]:
116107
"""
@@ -121,8 +112,6 @@ def Controller(
121112
:param external_doc_description: OPENAPI External Doc Description
122113
:param external_doc_url: OPENAPI External Document URL
123114
:param name: route name prefix for url reversing, eg name:route_name default=controller_name
124-
:param version: default URL versioning for all defined route in a controller
125-
:param guards: default guard for all routes defined under this controller
126115
:param include_in_schema: include controller in OPENAPI schema
127116
:return: t.Type[ControllerBase]
128117
"""
@@ -144,8 +133,6 @@ def Controller(
144133
),
145134
path=_prefix,
146135
name=name,
147-
version=set([version] if isinstance(version, str) else version),
148-
guards=guards or [],
149136
include_in_schema=include_in_schema,
150137
)
151138

@@ -190,9 +177,6 @@ def _decorator(cls: t.Type) -> t.Type[ControllerBase]:
190177
for key in CONTROLLER_METADATA.keys:
191178
reflect.define_metadata(key, kwargs[key], _controller_type)
192179

193-
reflect.define_metadata(VERSIONING_KEY, kwargs.version, _controller_type)
194-
reflect.define_metadata(GUARDS_KEY, kwargs.guards, _controller_type)
195-
196180
if new_cls:
197181
# if we forced cls to inherit from ControllerBase, we need to block it from been processed
198182
setattr(cls, "__CONTROLLER_WATERMARK__", True)

ellar/common/decorators/guards.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
from ellar.core.guard import GuardCanActivate
99

1010

11-
def guards(
11+
def Guards(
1212
*_guards: t.Union[t.Type["GuardCanActivate"], "GuardCanActivate"]
1313
) -> t.Callable:
1414
"""
15-
=========ROUTE FUNCTION DECORATOR ==============
15+
=========CONTROLLER AND ROUTE FUNCTION DECORATOR ==============
1616
1717
Defines list of guards for a route function
1818
:param _guards: Guard Type or Instance

ellar/common/decorators/versioning.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
from .base import set_metadata as set_meta
66

77

8-
def version(*_version: str) -> t.Callable:
8+
def Version(*_version: str) -> t.Callable:
99
"""
10-
========= ROUTE FUNCTION DECORATOR ==============
10+
========= CONTROLLER AND ROUTE FUNCTION DECORATOR ==============
1111
1212
Defines route function version
1313
:param _version: allowed versions

ellar/core/guard/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class GuardCanActivate(ABC, metaclass=ABCMeta):
1515
t.Type[HTTPException], t.Type[APIException]
1616
] = HTTPException
1717
status_code: int = HTTP_403_FORBIDDEN
18-
detail: str = "Not authenticated"
18+
detail: str = "Forbidden"
1919

2020
@abstractmethod
2121
async def can_activate(self, context: IExecutionContext) -> bool:

ellar/core/routing/router/module.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ def controller_router_factory(
4242
app = Router()
4343
app.routes = RouteCollection(routes) # type:ignore
4444

45-
reflect.get_metadata_or_raise_exception(VERSIONING_KEY, controller)
4645
include_in_schema = reflect.get_metadata_or_raise_exception(
4746
CONTROLLER_METADATA.INCLUDE_IN_SCHEMA, controller
4847
)

tests/test_common/test_decorators/test_controller.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pytest
22

3-
from ellar.common import Controller, set_metadata
3+
from ellar.common import Controller, Guards, Version, set_metadata
44
from ellar.constants import CONTROLLER_METADATA, GUARDS_KEY, NOT_SET, VERSIONING_KEY
55
from ellar.core import ControllerBase
66
from ellar.core.exceptions import ImproperConfiguration
@@ -12,12 +12,12 @@
1212
prefix="/decorator",
1313
description="Some description",
1414
external_doc_description="external",
15-
guards=[],
16-
version=("v1",),
1715
tag="dec",
1816
external_doc_url="https://example.com",
1917
name="test",
2018
)
19+
@Version("v1")
20+
@Guards()
2121
class ControllerDecorationTest:
2222
pass
2323

@@ -51,8 +51,8 @@ def test_controller_decoration_default():
5151
"external_doc_description": None,
5252
"external_doc_url": None,
5353
}
54-
assert reflect.get_metadata(GUARDS_KEY, ControllerDefaultTest) == []
55-
assert reflect.get_metadata(VERSIONING_KEY, ControllerDefaultTest) == set()
54+
assert reflect.get_metadata(GUARDS_KEY, ControllerDefaultTest) is None
55+
assert reflect.get_metadata(VERSIONING_KEY, ControllerDefaultTest) is None
5656
assert (
5757
reflect.get_metadata(CONTROLLER_METADATA.PATH, ControllerDefaultTest)
5858
== "/defaulttest"

tests/test_common/test_decorators/test_guards.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from ellar.common import guards
1+
from ellar.common import Guards
22
from ellar.constants import GUARDS_KEY
33
from ellar.core import ExecutionContext
44
from ellar.core.connection import Request
@@ -16,12 +16,12 @@ async def can_activate(self, context: ExecutionContext) -> bool:
1616
return False # pragma: no cover
1717

1818

19-
@guards(SomeGuard)
19+
@Guards(SomeGuard)
2020
def endpoint(request: Request):
2121
return "foo" # pragma: no cover
2222

2323

24-
@guards(SomeGuard, SomeGuard2)
24+
@Guards(SomeGuard, SomeGuard2)
2525
def endpoint2(request: Request):
2626
return "foo" # pragma: no cover
2727

tests/test_common/test_decorators/test_versioning.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
from ellar.common import version
1+
from ellar.common import Version
22
from ellar.constants import VERSIONING_KEY
33
from ellar.reflect import reflect
44

55

6-
@version(1, 2, 3)
6+
@Version(1, 2, 3)
77
def endpoint_versioning_func():
88
pass # pragma: no cover
99

0 commit comments

Comments
 (0)