Skip to content

Commit 232bfd2

Browse files
committed
Update view functions
1 parent d16f8d6 commit 232bfd2

File tree

6 files changed

+44
-11
lines changed

6 files changed

+44
-11
lines changed

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,22 @@
44

55
### 1. App API-FOR-TORTOISE-ORM
66

7+
8+
#### Install dependencies
9+
```shell
10+
pip install -r requirements.txt
11+
pip install -r requirements-dev.txt
12+
```
13+
14+
715
#### Start app
816
```shell
917
# in dir fastapi-rest-jsonapi
1018

11-
export PYTHOPATH="${PYTHONPATH}:./"
19+
export PYTHOPATH="${PYTHONPATH}:./"
20+
# reload may not work :(
1221
python examples/api_for_tortoise_orm/main.py
1322
```
14-
http://0.0.0.0:8080/docs
23+
24+
Visit http://0.0.0.0:8080/docs
25+

examples/api_for_tortoise_orm/api/user.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from fastapi_rest_jsonapi.schema import JSONAPIResultListSchema
2525

2626

27-
class UserDetail(object):
27+
class UserDetail:
2828
@classmethod
2929
async def get_user(cls, user_id, query_params: QueryStringManager) -> User:
3030
"""
@@ -69,7 +69,7 @@ async def patch(cls, obj_id, data: UserPatchSchema, query_params: QueryStringMan
6969
return device
7070

7171

72-
class UserList(object):
72+
class UserList:
7373
@classmethod
7474
async def get(cls, query_params: QueryStringManager) -> Union[QuerySet, JSONAPIResultListSchema]:
7575
extended_fields: List[str] = query_params.fields.get("users", [])
@@ -85,7 +85,7 @@ async def get(cls, query_params: QueryStringManager) -> Union[QuerySet, JSONAPIR
8585

8686
return JSONAPIResultListSchema(
8787
meta={"count": count, "totalPages": total_pages},
88-
data=[{"id": i_obj.id, "type": "Device", "attributes": i_obj.dict()} for i_obj in users],
88+
data=[{"id": i_obj.id, "attributes": i_obj.dict()} for i_obj in users],
8989
)
9090

9191
@classmethod

examples/api_for_tortoise_orm/main.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,22 @@
33
44
In module placed db initialization functions, app factory.
55
"""
6+
import sys
7+
from pathlib import Path
68

9+
CURRENT_FILE = Path(__file__).resolve()
10+
CURRENT_DIR = CURRENT_FILE.parent
11+
PROJECT_DIR = CURRENT_DIR.parent.parent
12+
13+
sys.path.append(str(PROJECT_DIR))
714

815
import uvicorn
916
from fastapi import FastAPI
1017
from tortoise import Tortoise
1118

1219
from examples.api_for_tortoise_orm.urls import add_routes
13-
from fastapi_rest_jsonapi.openapi import custom_openapi
1420
from fastapi_rest_jsonapi.schema import collect_app_orm_schemas
21+
from fastapi_rest_jsonapi.openapi import custom_openapi
1522

1623

1724
async def tortoise_init() -> None:
@@ -50,6 +57,7 @@ def create_app() -> FastAPI:
5057
"asgi:app",
5158
host="0.0.0.0",
5259
port=8080,
53-
reload=True, # enable hot reload for local
54-
log_config=None, # remove uvicorn's default logging config
60+
debug=True,
61+
reload=True,
62+
app_dir=str(CURRENT_DIR),
5563
)

examples/api_for_tortoise_orm/urls.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def add_routes(app: FastAPI) -> List[Dict[str, Any]]:
3838
class_detail=UserDetail,
3939
class_list=UserList,
4040
schema=UserSchema,
41-
type_resource="users",
41+
type_resource="user",
4242
schema_in_patch=UserPatchSchema,
4343
schema_in_post=UserInSchema,
4444
)

fastapi_rest_jsonapi/api.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,13 @@ def _add_routers(self, path: str):
104104
500: {"model": ExceptionResponseSchema},
105105
}
106106
if hasattr(self.class_list, "get"):
107-
self._routers.get(path, tags=self._tags, response_model=self._resp_schema_list, responses=error_responses,)(
107+
self._routers.get(
108+
path,
109+
tags=self._tags,
110+
response_model=self._resp_schema_list,
111+
responses=error_responses,
112+
summary=f"Get list of `{self._type}` objects",
113+
)(
108114
get_list_jsonapi(schema=self._schema, type_=self._type, schema_resp=self._resp_schema_list)(
109115
self.class_list.get
110116
)
@@ -116,6 +122,8 @@ def _add_routers(self, path: str):
116122
tags=self._tags,
117123
response_model=self._resp_schema_detail,
118124
responses=error_responses,
125+
summary=f"Create object `{self._type}`"
126+
119127
)(
120128
post_list_jsonapi(
121129
schema=self._schema,
@@ -131,6 +139,7 @@ def _add_routers(self, path: str):
131139
tags=self._tags,
132140
response_model=self._resp_schema_detail,
133141
responses=error_responses,
142+
summary=f"Get object `{self._type}` by id"
134143
)(
135144
get_detail_jsonapi(schema=self._schema, type_=self._type, schema_resp=self._resp_schema_detail)(
136145
self.class_detail.get
@@ -143,6 +152,7 @@ def _add_routers(self, path: str):
143152
tags=self._tags,
144153
response_model=self._resp_schema_detail,
145154
responses=error_responses,
155+
summary=f"Update object `{self._type}` by id"
146156
)(
147157
patch_detail_jsonapi(
148158
schema=self._schema,
@@ -156,4 +166,5 @@ def _add_routers(self, path: str):
156166
self._routers.delete(
157167
path + "/{obj_id}",
158168
tags=self._tags,
169+
summary=f"Delete object of type `{self._type}`"
159170
)(delete_detail_jsonapi(schema=self._schema)(self.class_detail.delete))

fastapi_rest_jsonapi/methods.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,10 @@ def post_list_jsonapi(
155155
type_: str,
156156
schema_resp: Any,
157157
) -> Callable:
158-
"""POST method router (Decorator for JSON API)."""
158+
"""
159+
POST method router (Decorator for JSON API).
160+
TODO: check data in `type` field
161+
"""
159162

160163
def inner(func: Callable) -> Callable:
161164
async def wrapper(request: Request, data: schema_in): # type: ignore

0 commit comments

Comments
 (0)