Skip to content

Commit e1443a4

Browse files
committed
Removed JSONAPI schemes to the library
1 parent d8e8eb9 commit e1443a4

File tree

16 files changed

+79
-133
lines changed

16 files changed

+79
-133
lines changed

examples/api_for_tortoise_orm/api/user.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,18 @@
1010
from examples.api_for_tortoise_orm.helpers.factories.user import UserFactory, ErrorCreateUserObject
1111
from examples.api_for_tortoise_orm.helpers.updaters.exceptions import ObjectNotFound
1212
from examples.api_for_tortoise_orm.helpers.updaters.update_user import UpdateUser, ErrorUpdateUserObject
13-
from examples.api_for_tortoise_orm.models.pydantic import UserSchema, UserPatchJSONAPISchema, UserJSONAPIListSchema, \
14-
UserPostJSONAPISchema
13+
from examples.api_for_tortoise_orm.models.pydantic import UserSchema, UserPatchSchema
14+
from examples.api_for_tortoise_orm.models.pydantic.user import UserInSchema
1515
from examples.api_for_tortoise_orm.models.tortoise import User
1616
from fastapi_rest_jsonapi import json_api_pagination
1717

1818
from fastapi_rest_jsonapi.exceptions import (
1919
BadRequest,
2020
HTTPException,
2121
)
22-
from fastapi_rest_jsonapi.filter import json_api_filter
22+
from fastapi_rest_jsonapi.data_layers.filter import json_api_filter
2323
from fastapi_rest_jsonapi.querystring import QueryStringManager
24+
from fastapi_rest_jsonapi.schema import JSONAPIResultListSchema
2425

2526

2627
class UserDetail(object):
@@ -51,12 +52,12 @@ async def get(cls, obj_id, query_params: QueryStringManager) -> UserSchema:
5152
return UserSchema.from_orm(user)
5253

5354
@classmethod
54-
async def patch(cls, obj_id, data: UserPatchJSONAPISchema, query_params: QueryStringManager) -> UserSchema:
55+
async def patch(cls, obj_id, data: UserPatchSchema, query_params: QueryStringManager) -> UserSchema:
5556
user_obj: User
5657
try:
5758
user_obj = await UpdateUser.update(
5859
obj_id,
59-
data.attributes.dict(exclude_unset=True),
60+
data.dict(exclude_unset=True),
6061
query_params.headers,
6162
)
6263
except ErrorUpdateUserObject as ex:
@@ -70,7 +71,7 @@ async def patch(cls, obj_id, data: UserPatchJSONAPISchema, query_params: QuerySt
7071

7172
class UserList(object):
7273
@classmethod
73-
async def get(cls, query_params: QueryStringManager) -> Union[QuerySet, UserJSONAPIListSchema]:
74+
async def get(cls, query_params: QueryStringManager) -> Union[QuerySet, JSONAPIResultListSchema]:
7475
extended_fields: List[str] = query_params.fields.get("users", [])
7576
if not extended_fields:
7677
device_query = User.filter().order_by("-id")
@@ -82,16 +83,16 @@ async def get(cls, query_params: QueryStringManager) -> Union[QuerySet, UserJSON
8283
users_db: List[User] = await query.all()
8384
users: List[UserSchema] = [UserSchema.from_orm(i_user) for i_user in users_db]
8485

85-
return UserJSONAPIListSchema(
86+
return JSONAPIResultListSchema(
8687
meta={"count": count, "totalPages": total_pages},
8788
data=[{"id": i_obj.id, "type": "Device", "attributes": i_obj.dict()} for i_obj in users],
8889
)
8990

9091
@classmethod
91-
async def post(cls, data: UserPostJSONAPISchema, query_params: QueryStringManager) -> UserSchema:
92+
async def post(cls, data: UserInSchema, query_params: QueryStringManager) -> UserSchema:
9293
try:
9394
device_obj = await UserFactory.create(
94-
data=data.attributes.dict(),
95+
data=data.dict(),
9596
mode=FactoryUseMode.production,
9697
header=query_params.headers,
9798
)

examples/api_for_tortoise_orm/models/pydantic/__init__.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,12 @@
22

33

44
from .user import (
5-
UserJSONAPIDetailSchema,
6-
UserJSONAPIListSchema,
7-
UserJSONAPIObjectSchema,
8-
UserPatchJSONAPISchema,
95
UserPatchSchema,
10-
UserPostJSONAPISchema,
116
UserSchema,
127
)
138

149
__all__ = [
1510
"UserSchema",
1611
"UserPatchSchema",
17-
"UserPatchJSONAPISchema",
18-
"UserJSONAPIListSchema",
19-
"UserJSONAPIDetailSchema",
20-
"UserJSONAPIObjectSchema",
2112
"UserSchema",
22-
"UserPostJSONAPISchema",
2313
]

examples/api_for_tortoise_orm/models/pydantic/user/base.py renamed to examples/api_for_tortoise_orm/models/pydantic/user.py

File renamed without changes.

examples/api_for_tortoise_orm/models/pydantic/user/__init__.py

Lines changed: 0 additions & 25 deletions
This file was deleted.

examples/api_for_tortoise_orm/models/pydantic/user/json_api.py

Lines changed: 0 additions & 58 deletions
This file was deleted.

examples/api_for_tortoise_orm/urls.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,9 @@
1212
)
1313

1414
from fastapi_rest_jsonapi import RoutersJSONAPI
15+
from .models.pydantic import UserPatchSchema
1516
from .models.pydantic.user import (
16-
UserJSONAPIDetailSchema,
17-
UserJSONAPIListSchema,
18-
UserPatchJSONAPISchema,
19-
UserPostJSONAPISchema,
20-
UserSchema,
17+
UserSchema, UserInSchema,
2118
)
2219
from .api.user import (
2320
UserDetail,
@@ -42,10 +39,8 @@ def add_routes(app: FastAPI) -> List[Dict[str, Any]]:
4239
class_list=UserList,
4340
schema=UserSchema,
4441
type_resource="users",
45-
schema_in_patch=UserPatchJSONAPISchema,
46-
schema_in_post=UserPostJSONAPISchema,
47-
resp_schema_detail=UserJSONAPIDetailSchema,
48-
resp_schema_list=UserJSONAPIListSchema,
42+
schema_in_patch=UserPatchSchema,
43+
schema_in_post=UserInSchema,
4944
)
5045

5146
app.include_router(routers, prefix="")

fastapi_rest_jsonapi/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from fastapi_rest_jsonapi.api import RoutersJSONAPI
44
from fastapi_rest_jsonapi.exceptions import BadRequest
5-
from fastapi_rest_jsonapi.filter import json_api_filter
5+
from fastapi_rest_jsonapi.data_layers.filter import json_api_filter
66
from fastapi_rest_jsonapi.pagination import json_api_pagination
77
from fastapi_rest_jsonapi.querystring import QueryStringManager
88
from fastapi_rest_jsonapi.sorting import json_api_sort

fastapi_rest_jsonapi/api.py

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
Union,
1010
)
1111

12+
import pydantic
1213
from fastapi import APIRouter
13-
from pydantic import BaseModel
14+
from pydantic import BaseModel, Field
1415

1516
from fastapi_rest_jsonapi.exceptions import ExceptionResponseSchema
1617
from fastapi_rest_jsonapi.methods import (
@@ -20,6 +21,8 @@
2021
patch_detail_jsonapi,
2122
post_list_jsonapi,
2223
)
24+
from fastapi_rest_jsonapi.schema import BasePatchJSONAPISchema, BasePostJSONAPISchema, JSONAPIObjectSchema, \
25+
JSONAPIResultDetailSchema
2326

2427
JSON_API_RESPONSE_TYPE = Optional[Dict[Union[int, str], Dict[str, Any]]]
2528

@@ -38,8 +41,6 @@ def __init__( # noqa: WPS211
3841
type_resource: str,
3942
schema_in_patch: Type[BaseModel],
4043
schema_in_post: Type[BaseModel],
41-
resp_schema_detail: Type[BaseModel],
42-
resp_schema_list: Type[BaseModel],
4344
) -> None:
4445
"""Initialize router items."""
4546
self._routers: APIRouter = routers
@@ -49,10 +50,43 @@ def __init__( # noqa: WPS211
4950
self.class_list: Any = class_list
5051
self._type: str = type_resource
5152
self._schema: Type[BaseModel] = schema
52-
self._schema_in_patch: Type[BaseModel] = schema_in_patch
53-
self._schema_in_post: Type[BaseModel] = schema_in_post
54-
self._resp_schema_detail: Type[BaseModel] = resp_schema_detail
55-
self._resp_schema_list: Type[BaseModel] = resp_schema_list
53+
54+
patch_jsonapi_schema = pydantic.create_model(
55+
"{base_name}JSONAPI".format(base_name=schema_in_patch.__name__),
56+
attributes=(schema_in_patch, ...),
57+
type=(str, Field(default=self._type, description="Тип ресурса")),
58+
__base__=BasePatchJSONAPISchema,
59+
)
60+
self._schema_in_patch_base: Type[BaseModel] = schema_in_patch
61+
self._schema_in_patch: Type[BaseModel] = patch_jsonapi_schema
62+
63+
post_jsonapi_schema = pydantic.create_model(
64+
"{base_name}JSONAPI".format(base_name=schema_in_post.__name__),
65+
attributes=(schema_in_post, ...),
66+
type=(str, Field(default=self._type, description="Тип ресурса")),
67+
__base__=BasePostJSONAPISchema,
68+
)
69+
self._schema_in_post_base: Type[BaseModel] = schema_in_post
70+
self._schema_in_post: Type[BaseModel] = post_jsonapi_schema
71+
72+
object_jsonapi_schema = pydantic.create_model(
73+
"{base_name}ObjectJSONAPI".format(base_name=schema.__name__),
74+
attributes=(schema, ...),
75+
type=(str, Field(default=self._type, description="Тип ресурса")),
76+
__base__=JSONAPIObjectSchema,
77+
)
78+
detail_jsonapi_schema = pydantic.create_model(
79+
"{base_name}DetailJSONAPI".format(base_name=schema.__name__),
80+
data=(object_jsonapi_schema, ...),
81+
__base__=JSONAPIResultDetailSchema,
82+
)
83+
self._resp_schema_detail: Type[BaseModel] = detail_jsonapi_schema
84+
list_jsonapi_schema = pydantic.create_model(
85+
"{base_name}ListJSONAPI".format(base_name=schema.__name__),
86+
data=(List[object_jsonapi_schema], ...),
87+
__base__=JSONAPIResultDetailSchema,
88+
)
89+
self._resp_schema_list: Type[BaseModel] = list_jsonapi_schema
5690

5791
if isinstance(self._path, list):
5892
for i_path in self._path:

fastapi_rest_jsonapi/filter/__init__.py renamed to fastapi_rest_jsonapi/data_layers/filter/__init__.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
"""Base filtering functions package."""
2-
3-
from fastapi_rest_jsonapi.filter.base import (
4-
create_q_tortoise,
5-
orm_and_or,
6-
val_to_query,
7-
validate_q_tortoise,
8-
)
9-
from fastapi_rest_jsonapi.filter.json_api import (
2+
from fastapi_rest_jsonapi.data_layers.filter.base import orm_and_or, create_q_tortoise, val_to_query, \
3+
validate_q_tortoise
4+
from fastapi_rest_jsonapi.data_layers.filter.json_api import (
105
json_api_filter,
116
json_api_filter_converter,
127
)
13-
from fastapi_rest_jsonapi.filter.preparing import (
8+
from fastapi_rest_jsonapi.data_layers.filter.preparing import (
149
prepare_field_name_for_filtering,
1510
prepare_filter_event,
1611
prepare_filter_pair,
File renamed without changes.

0 commit comments

Comments
 (0)