-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathviews_base.py
51 lines (36 loc) · 1.38 KB
/
views_base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from typing import ClassVar, Dict
from fastapi import Depends
from pydantic import BaseModel, ConfigDict
from sqlalchemy.ext.asyncio import AsyncSession
from examples.api_for_sqlalchemy.extensions.sqlalchemy import Connector
from fastapi_jsonapi.data_layers.sqla_orm import SqlalchemyDataLayer
from fastapi_jsonapi.misc.sqla.generics.base import DetailViewBaseGeneric, ListViewBaseGeneric
from fastapi_jsonapi.views.utils import HTTPMethod, HTTPMethodConfig
from fastapi_jsonapi.views.view_base import ViewBase
class SessionDependency(BaseModel):
session: AsyncSession = Depends(Connector.get_session)
model_config = ConfigDict(arbitrary_types_allowed=True)
def handler(view: ViewBase, dto: SessionDependency) -> Dict:
return {"session": dto.session}
class DetailViewBase(DetailViewBaseGeneric):
"""
Generic view base (detail)
"""
data_layer_cls = SqlalchemyDataLayer
method_dependencies: ClassVar = {
HTTPMethod.ALL: HTTPMethodConfig(
dependencies=SessionDependency,
prepare_data_layer_kwargs=handler,
),
}
class ListViewBase(ListViewBaseGeneric):
"""
Generic view base (list)
"""
data_layer_cls = SqlalchemyDataLayer
method_dependencies: ClassVar = {
HTTPMethod.ALL: HTTPMethodConfig(
dependencies=SessionDependency,
prepare_data_layer_kwargs=handler,
),
}