Skip to content

Commit 8cfbb4b

Browse files
committed
Fixing all samples
1 parent 10aae68 commit 8cfbb4b

File tree

11 files changed

+251
-23
lines changed

11 files changed

+251
-23
lines changed

samples/01-carapp/carapp/apps/car/routers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,4 @@ async def get_car_html(repo: Inject[CarRepository]):
5151
async def get_car_html_with_render(
5252
repo: Inject[CarRepository], request: Inject[Request]
5353
):
54-
return render_template("car/list.html", request=request, model=repo.get_all())
54+
return render_template("car/list.html", model=repo.get_all())

samples/01-carapp/carapp/config.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
11
from ellar.core.conf import ConfigDefaultTypesMixin
22

33

4-
class DevelopmentConfig(ConfigDefaultTypesMixin):
4+
class BaseConfig(ConfigDefaultTypesMixin):
5+
MIDDLEWARE = [
6+
"ellar.core.middleware.trusted_host:trusted_host_middleware",
7+
"ellar.core.middleware.cors:cors_middleware",
8+
"ellar.core.middleware.errors:server_error_middleware",
9+
"ellar.core.middleware.versioning:versioning_middleware",
10+
"ellar.auth.middleware.session:session_middleware",
11+
"ellar.auth.middleware.auth:identity_middleware",
12+
"ellar.core.middleware.exceptions:exception_middleware",
13+
]
14+
15+
16+
class DevelopmentConfig(BaseConfig):
517
DEBUG = True
618

719

8-
class TestingConfig(ConfigDefaultTypesMixin):
20+
class TestingConfig(BaseConfig):
921
DEBUG = False

samples/02-socketio-app/socketio_app/apps/events/gateways.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ async def my_broadcast_event(self, message: MessageData = WsBody()):
2222

2323
@subscribe_message("join")
2424
async def join(self, message: MessageRoom = WsBody()):
25-
self.context.server.enter_room(self.context.sid, message.room)
25+
await self.context.server.enter_room(self.context.sid, message.room)
2626
await self.context.server.emit(
2727
"my_response",
2828
{"data": "Entered room: " + message.room},

samples/02-socketio-app/socketio_app/config.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from ellar.core.versioning import BaseAPIVersioning, DefaultAPIVersioning
1414
from ellar.pydantic import ENCODERS_BY_TYPE as encoders_by_type
1515
from starlette.middleware import Middleware
16+
from starlette.requests import Request
1617

1718

1819
class BaseConfig(ConfigDefaultTypesMixin):
@@ -29,6 +30,15 @@ class BaseConfig(ConfigDefaultTypesMixin):
2930
# https://jinja.palletsprojects.com/en/3.0.x/api/#high-level-api
3031
JINJA_TEMPLATES_OPTIONS: t.Dict[str, t.Any] = {}
3132

33+
# Injects context to jinja templating context values
34+
TEMPLATES_CONTEXT_PROCESSORS: t.List[
35+
t.Union[str, t.Callable[[t.Union[Request]], t.Dict[str, t.Any]]]
36+
] = [
37+
"ellar.core.templating.context_processors:request_context",
38+
"ellar.core.templating.context_processors:user",
39+
"ellar.core.templating.context_processors:request_state",
40+
]
41+
3242
# Application route versioning scheme
3343
VERSIONING_SCHEME: BaseAPIVersioning = DefaultAPIVersioning()
3444

@@ -51,14 +61,24 @@ class BaseConfig(ConfigDefaultTypesMixin):
5161
ALLOWED_HOSTS: t.List[str] = ["*"]
5262

5363
# Application middlewares
54-
MIDDLEWARE: t.Sequence[Middleware] = []
64+
MIDDLEWARE: t.Union[str, Middleware] = [
65+
"ellar.core.middleware.trusted_host:trusted_host_middleware",
66+
"ellar.core.middleware.cors:cors_middleware",
67+
"ellar.core.middleware.errors:server_error_middleware",
68+
"ellar.core.middleware.versioning:versioning_middleware",
69+
"ellar.auth.middleware.session:session_middleware",
70+
"ellar.auth.middleware.auth:identity_middleware",
71+
"ellar.core.middleware.exceptions:exception_middleware",
72+
]
5573

5674
# A dictionary mapping either integer status codes,
5775
# or exception class types onto callables which handle the exceptions.
5876
# Exception handler callables should be of the form
5977
# `handler(context:IExecutionContext, exc: Exception) -> response`
6078
# and may be either standard functions, or async functions.
61-
EXCEPTION_HANDLERS: t.List[IExceptionHandler] = []
79+
EXCEPTION_HANDLERS: t.Union[str, IExceptionHandler] = [
80+
"ellar.core.exceptions:error_404_handler"
81+
]
6282

6383
# Object Serializer custom encoders
6484
SERIALIZER_CUSTOM_ENCODER: t.Dict[t.Any, t.Callable[[t.Any], t.Any]] = (

samples/03-auth-with-guards/auth_project/config.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from ellar.core.versioning import BaseAPIVersioning, DefaultAPIVersioning
1414
from ellar.pydantic import ENCODERS_BY_TYPE as encoders_by_type
1515
from starlette.middleware import Middleware
16+
from starlette.requests import Request
1617

1718

1819
class BaseConfig(ConfigDefaultTypesMixin):
@@ -29,6 +30,15 @@ class BaseConfig(ConfigDefaultTypesMixin):
2930
# https://jinja.palletsprojects.com/en/3.0.x/api/#high-level-api
3031
JINJA_TEMPLATES_OPTIONS: t.Dict[str, t.Any] = {}
3132

33+
# Injects context to jinja templating context values
34+
TEMPLATES_CONTEXT_PROCESSORS: t.List[
35+
t.Union[str, t.Callable[[t.Union[Request]], t.Dict[str, t.Any]]]
36+
] = [
37+
"ellar.core.templating.context_processors:request_context",
38+
"ellar.core.templating.context_processors:user",
39+
"ellar.core.templating.context_processors:request_state",
40+
]
41+
3242
# Application route versioning scheme
3343
VERSIONING_SCHEME: BaseAPIVersioning = DefaultAPIVersioning()
3444

@@ -51,15 +61,24 @@ class BaseConfig(ConfigDefaultTypesMixin):
5161
ALLOWED_HOSTS: t.List[str] = ["*"]
5262

5363
# Application middlewares
54-
MIDDLEWARE: t.Sequence[Middleware] = []
64+
MIDDLEWARE: t.Union[str, Middleware] = [
65+
"ellar.core.middleware.trusted_host:trusted_host_middleware",
66+
"ellar.core.middleware.cors:cors_middleware",
67+
"ellar.core.middleware.errors:server_error_middleware",
68+
"ellar.core.middleware.versioning:versioning_middleware",
69+
"ellar.auth.middleware.session:session_middleware",
70+
"ellar.auth.middleware.auth:identity_middleware",
71+
"ellar.core.middleware.exceptions:exception_middleware",
72+
]
5573

5674
# A dictionary mapping either integer status codes,
5775
# or exception class types onto callables which handle the exceptions.
5876
# Exception handler callables should be of the form
5977
# `handler(context:IExecutionContext, exc: Exception) -> response`
6078
# and may be either standard functions, or async functions.
61-
EXCEPTION_HANDLERS: t.List[IExceptionHandler] = []
62-
79+
EXCEPTION_HANDLERS: t.Union[str, IExceptionHandler] = [
80+
"ellar.core.exceptions:error_404_handler"
81+
]
6382
# Object Serializer custom encoders
6483
SERIALIZER_CUSTOM_ENCODER: t.Dict[t.Any, t.Callable[[t.Any], t.Any]] = (
6584
encoders_by_type

samples/04-auth-with-handlers/auth_project_with_handler/config.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from ellar.core.versioning import BaseAPIVersioning, DefaultAPIVersioning
1414
from ellar.pydantic import ENCODERS_BY_TYPE as encoders_by_type
1515
from starlette.middleware import Middleware
16+
from starlette.requests import Request
1617

1718

1819
class BaseConfig(ConfigDefaultTypesMixin):
@@ -29,6 +30,15 @@ class BaseConfig(ConfigDefaultTypesMixin):
2930
# https://jinja.palletsprojects.com/en/3.0.x/api/#high-level-api
3031
JINJA_TEMPLATES_OPTIONS: t.Dict[str, t.Any] = {}
3132

33+
# Injects context to jinja templating context values
34+
TEMPLATES_CONTEXT_PROCESSORS: t.List[
35+
t.Union[str, t.Callable[[t.Union[Request]], t.Dict[str, t.Any]]]
36+
] = [
37+
"ellar.core.templating.context_processors:request_context",
38+
"ellar.core.templating.context_processors:user",
39+
"ellar.core.templating.context_processors:request_state",
40+
]
41+
3242
# Application route versioning scheme
3343
VERSIONING_SCHEME: BaseAPIVersioning = DefaultAPIVersioning()
3444

@@ -51,14 +61,24 @@ class BaseConfig(ConfigDefaultTypesMixin):
5161
ALLOWED_HOSTS: t.List[str] = ["*"]
5262

5363
# Application middlewares
54-
MIDDLEWARE: t.Sequence[Middleware] = []
64+
MIDDLEWARE: t.Union[str, Middleware] = [
65+
"ellar.core.middleware.trusted_host:trusted_host_middleware",
66+
"ellar.core.middleware.cors:cors_middleware",
67+
"ellar.core.middleware.errors:server_error_middleware",
68+
"ellar.core.middleware.versioning:versioning_middleware",
69+
"ellar.auth.middleware.session:session_middleware",
70+
"ellar.auth.middleware.auth:identity_middleware",
71+
"ellar.core.middleware.exceptions:exception_middleware",
72+
]
5573

5674
# A dictionary mapping either integer status codes,
5775
# or exception class types onto callables which handle the exceptions.
5876
# Exception handler callables should be of the form
5977
# `handler(context:IExecutionContext, exc: Exception) -> response`
6078
# and may be either standard functions, or async functions.
61-
EXCEPTION_HANDLERS: t.List[IExceptionHandler] = []
79+
EXCEPTION_HANDLERS: t.Union[str, IExceptionHandler] = [
80+
"ellar.core.exceptions:error_404_handler"
81+
]
6282

6383
# Object Serializer custom encoders
6484
SERIALIZER_CUSTOM_ENCODER: t.Dict[t.Any, t.Callable[[t.Any], t.Any]] = (

samples/05-ellar-with-sqlalchemy/db_learning/config.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from ellar.core.versioning import BaseAPIVersioning, DefaultAPIVersioning
1414
from ellar.pydantic import ENCODERS_BY_TYPE as encoders_by_type
1515
from starlette.middleware import Middleware
16+
from starlette.requests import Request
1617

1718

1819
class BaseConfig(ConfigDefaultTypesMixin):
@@ -29,6 +30,15 @@ class BaseConfig(ConfigDefaultTypesMixin):
2930
# https://jinja.palletsprojects.com/en/3.0.x/api/#high-level-api
3031
JINJA_TEMPLATES_OPTIONS: t.Dict[str, t.Any] = {}
3132

33+
# Injects context to jinja templating context values
34+
TEMPLATES_CONTEXT_PROCESSORS: t.List[
35+
t.Union[str, t.Callable[[t.Union[Request]], t.Dict[str, t.Any]]]
36+
] = [
37+
"ellar.core.templating.context_processors:request_context",
38+
"ellar.core.templating.context_processors:user",
39+
"ellar.core.templating.context_processors:request_state",
40+
]
41+
3242
# Application route versioning scheme
3343
VERSIONING_SCHEME: BaseAPIVersioning = DefaultAPIVersioning()
3444

@@ -51,14 +61,24 @@ class BaseConfig(ConfigDefaultTypesMixin):
5161
ALLOWED_HOSTS: t.List[str] = ["*"]
5262

5363
# Application middlewares
54-
MIDDLEWARE: t.Sequence[Middleware] = []
64+
MIDDLEWARE: t.Union[str, Middleware] = [
65+
"ellar.core.middleware.trusted_host:trusted_host_middleware",
66+
"ellar.core.middleware.cors:cors_middleware",
67+
"ellar.core.middleware.errors:server_error_middleware",
68+
"ellar.core.middleware.versioning:versioning_middleware",
69+
"ellar.auth.middleware.session:session_middleware",
70+
"ellar.auth.middleware.auth:identity_middleware",
71+
"ellar.core.middleware.exceptions:exception_middleware",
72+
]
5573

5674
# A dictionary mapping either integer status codes,
5775
# or exception class types onto callables which handle the exceptions.
5876
# Exception handler callables should be of the form
5977
# `handler(context:IExecutionContext, exc: Exception) -> response`
6078
# and may be either standard functions, or async functions.
61-
EXCEPTION_HANDLERS: t.List[IExceptionHandler] = []
79+
EXCEPTION_HANDLERS: t.Union[str, IExceptionHandler] = [
80+
"ellar.core.exceptions:error_404_handler"
81+
]
6282

6383
# Object Serializer custom encoders
6484
SERIALIZER_CUSTOM_ENCODER: t.Dict[t.Any, t.Callable[[t.Any], t.Any]] = (
Lines changed: 81 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,86 @@
1-
from ellar.core.conf import ConfigDefaultTypesMixin
1+
import typing as t
22

3+
from ellar.common import IExceptionHandler, JSONResponse
4+
from ellar.core import ConfigDefaultTypesMixin
5+
from ellar.core.versioning import BaseAPIVersioning, DefaultAPIVersioning
6+
from ellar.pydantic import ENCODERS_BY_TYPE as encoders_by_type
7+
from starlette.middleware import Middleware
8+
from starlette.requests import Request
39

4-
class DevelopmentConfig(ConfigDefaultTypesMixin):
5-
DEBUG = True
610

11+
class BaseConfig(ConfigDefaultTypesMixin):
12+
DEBUG: bool = False
713

8-
class TestingConfig(ConfigDefaultTypesMixin):
14+
DEFAULT_JSON_CLASS: t.Type[JSONResponse] = JSONResponse
15+
SECRET_KEY: str = "ellar_QdZwHTfLkZQWQtAot-V6gbTHONMn3ekrl5jdcb5AOC8"
16+
17+
# injector auto_bind = True allows you to resolve types that are not registered on the container
18+
# For more info, read: https://injector.readthedocs.io/en/latest/index.html
19+
INJECTOR_AUTO_BIND = False
20+
21+
# jinja Environment options
22+
# https://jinja.palletsprojects.com/en/3.0.x/api/#high-level-api
23+
JINJA_TEMPLATES_OPTIONS: t.Dict[str, t.Any] = {}
24+
25+
# Injects context to jinja templating context values
26+
TEMPLATES_CONTEXT_PROCESSORS: t.List[
27+
t.Union[str, t.Callable[[t.Union[Request]], t.Dict[str, t.Any]]]
28+
] = [
29+
"ellar.core.templating.context_processors:request_context",
30+
"ellar.core.templating.context_processors:user",
31+
"ellar.core.templating.context_processors:request_state",
32+
]
33+
34+
# Application route versioning scheme
35+
VERSIONING_SCHEME: BaseAPIVersioning = DefaultAPIVersioning()
36+
37+
# Enable or Disable Application Router route searching by appending backslash
38+
REDIRECT_SLASHES: bool = False
39+
40+
# Define references to static folders in python packages.
41+
# eg STATIC_FOLDER_PACKAGES = [('boostrap4', 'statics')]
42+
STATIC_FOLDER_PACKAGES: t.Optional[t.List[t.Union[str, t.Tuple[str, str]]]] = []
43+
44+
# Define references to static folders defined within the project
45+
STATIC_DIRECTORIES: t.Optional[t.List[t.Union[str, t.Any]]] = []
46+
47+
# static route path
48+
STATIC_MOUNT_PATH: str = "/static"
49+
50+
CORS_ALLOW_ORIGINS: t.List[str] = ["*"]
51+
CORS_ALLOW_METHODS: t.List[str] = ["*"]
52+
CORS_ALLOW_HEADERS: t.List[str] = ["*"]
53+
ALLOWED_HOSTS: t.List[str] = ["*"]
54+
55+
# Application middlewares
56+
MIDDLEWARE: t.Union[str, Middleware] = [
57+
"ellar.core.middleware.trusted_host:trusted_host_middleware",
58+
"ellar.core.middleware.cors:cors_middleware",
59+
"ellar.core.middleware.errors:server_error_middleware",
60+
"ellar.core.middleware.versioning:versioning_middleware",
61+
"ellar.auth.middleware.session:session_middleware",
62+
"ellar.auth.middleware.auth:identity_middleware",
63+
"ellar.core.middleware.exceptions:exception_middleware",
64+
]
65+
66+
# A dictionary mapping either integer status codes,
67+
# or exception class types onto callables which handle the exceptions.
68+
# Exception handler callables should be of the form
69+
# `handler(context:IExecutionContext, exc: Exception) -> response`
70+
# and may be either standard functions, or async functions.
71+
EXCEPTION_HANDLERS: t.Union[str, IExceptionHandler] = [
72+
"ellar.core.exceptions:error_404_handler"
73+
]
74+
75+
# Object Serializer custom encoders
76+
SERIALIZER_CUSTOM_ENCODER: t.Dict[t.Any, t.Callable[[t.Any], t.Any]] = (
77+
encoders_by_type
78+
)
79+
80+
81+
class DevelopmentConfig(BaseConfig):
82+
DEBUG: bool = True
83+
84+
85+
class TestingConfig(BaseConfig):
986
DEBUG = False

samples/07-caching/ellar_catching/config.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from ellar.core.versioning import BaseAPIVersioning, DefaultAPIVersioning
1414
from ellar.pydantic import ENCODERS_BY_TYPE as encoders_by_type
1515
from starlette.middleware import Middleware
16+
from starlette.requests import Request
1617

1718

1819
class BaseConfig(ConfigDefaultTypesMixin):
@@ -29,6 +30,15 @@ class BaseConfig(ConfigDefaultTypesMixin):
2930
# https://jinja.palletsprojects.com/en/3.0.x/api/#high-level-api
3031
JINJA_TEMPLATES_OPTIONS: t.Dict[str, t.Any] = {}
3132

33+
# Injects context to jinja templating context values
34+
TEMPLATES_CONTEXT_PROCESSORS: t.List[
35+
t.Union[str, t.Callable[[t.Union[Request]], t.Dict[str, t.Any]]]
36+
] = [
37+
"ellar.core.templating.context_processors:request_context",
38+
"ellar.core.templating.context_processors:user",
39+
"ellar.core.templating.context_processors:request_state",
40+
]
41+
3242
# Application route versioning scheme
3343
VERSIONING_SCHEME: BaseAPIVersioning = DefaultAPIVersioning()
3444

@@ -51,14 +61,24 @@ class BaseConfig(ConfigDefaultTypesMixin):
5161
ALLOWED_HOSTS: t.List[str] = ["*"]
5262

5363
# Application middlewares
54-
MIDDLEWARE: t.Sequence[Middleware] = []
64+
MIDDLEWARE: t.Union[str, Middleware] = [
65+
"ellar.core.middleware.trusted_host:trusted_host_middleware",
66+
"ellar.core.middleware.cors:cors_middleware",
67+
"ellar.core.middleware.errors:server_error_middleware",
68+
"ellar.core.middleware.versioning:versioning_middleware",
69+
"ellar.auth.middleware.session:session_middleware",
70+
"ellar.auth.middleware.auth:identity_middleware",
71+
"ellar.core.middleware.exceptions:exception_middleware",
72+
]
5573

5674
# A dictionary mapping either integer status codes,
5775
# or exception class types onto callables which handle the exceptions.
5876
# Exception handler callables should be of the form
5977
# `handler(context:IExecutionContext, exc: Exception) -> response`
6078
# and may be either standard functions, or async functions.
61-
EXCEPTION_HANDLERS: t.List[IExceptionHandler] = []
79+
EXCEPTION_HANDLERS: t.Union[str, IExceptionHandler] = [
80+
"ellar.core.exceptions:error_404_handler"
81+
]
6282

6383
# Object Serializer custom encoders
6484
SERIALIZER_CUSTOM_ENCODER: t.Dict[t.Any, t.Callable[[t.Any], t.Any]] = (

0 commit comments

Comments
 (0)