|
| 1 | +from http import HTTPStatus |
| 2 | +from typing import ( |
| 3 | + List, |
| 4 | + Union, |
| 5 | +) |
| 6 | +from tortoise.exceptions import DoesNotExist |
| 7 | +from tortoise.queryset import QuerySet |
| 8 | + |
| 9 | +from examples.api_for_tortoise_orm.helpers.factories.meta_base import FactoryUseMode |
| 10 | +from examples.api_for_tortoise_orm.helpers.factories.user import UserFactory, ErrorCreateUserObject |
| 11 | +from examples.api_for_tortoise_orm.helpers.updaters.exceptions import ObjectNotFound |
| 12 | +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 |
| 15 | +from examples.api_for_tortoise_orm.models.tortoise import User |
| 16 | +from fastapi_rest_jsonapi import json_api_pagination |
| 17 | + |
| 18 | +from fastapi_rest_jsonapi.exceptions import ( |
| 19 | + BadRequest, |
| 20 | + HTTPException, |
| 21 | +) |
| 22 | +from fastapi_rest_jsonapi.filter import json_api_filter |
| 23 | +from fastapi_rest_jsonapi.querystring import QueryStringManager |
| 24 | + |
| 25 | + |
| 26 | +class UserDetail(object): |
| 27 | + @classmethod |
| 28 | + async def get_user(cls, user_id, query_params: QueryStringManager) -> User: |
| 29 | + """ |
| 30 | + Get user by id from ORM. |
| 31 | +
|
| 32 | + :param user_id: int |
| 33 | + :param query_params: QueryStringManager |
| 34 | + :return: User model. |
| 35 | + :raises HTTPException: if user not found. |
| 36 | + """ |
| 37 | + user: User |
| 38 | + try: |
| 39 | + user = await User.get(id=user_id) |
| 40 | + except DoesNotExist: |
| 41 | + raise HTTPException( |
| 42 | + status_code=HTTPStatus.FORBIDDEN, |
| 43 | + detail="User with id {id} not found".format(id=user_id), |
| 44 | + ) |
| 45 | + |
| 46 | + return user |
| 47 | + |
| 48 | + @classmethod |
| 49 | + async def get(cls, obj_id, query_params: QueryStringManager) -> UserSchema: |
| 50 | + user: User = await cls.get_user(user_id=obj_id, query_params=query_params) |
| 51 | + return UserSchema.from_orm(user) |
| 52 | + |
| 53 | + @classmethod |
| 54 | + async def patch(cls, obj_id, data: UserPatchJSONAPISchema, query_params: QueryStringManager) -> UserSchema: |
| 55 | + user_obj: User |
| 56 | + try: |
| 57 | + user_obj = await UpdateUser.update( |
| 58 | + obj_id, |
| 59 | + data.attributes.dict(exclude_unset=True), |
| 60 | + query_params.headers, |
| 61 | + ) |
| 62 | + except ErrorUpdateUserObject as ex: |
| 63 | + raise BadRequest(ex.description, ex.field) |
| 64 | + except ObjectNotFound as ex: |
| 65 | + raise HTTPException(status_code=HTTPStatus.NOT_FOUND, detail=ex.description) |
| 66 | + |
| 67 | + device = UserSchema.from_orm(user_obj) |
| 68 | + return device |
| 69 | + |
| 70 | + |
| 71 | +class UserList(object): |
| 72 | + @classmethod |
| 73 | + async def get(cls, query_params: QueryStringManager) -> Union[QuerySet, UserJSONAPIListSchema]: |
| 74 | + extended_fields: List[str] = query_params.fields.get("users", []) |
| 75 | + if not extended_fields: |
| 76 | + device_query = User.filter().order_by("-id") |
| 77 | + return await json_api_filter(query=device_query, schema=UserSchema, query_params=query_params) |
| 78 | + |
| 79 | + user_query = User.filter().order_by("-id") |
| 80 | + query: QuerySet = await json_api_filter(query=user_query, schema=UserSchema, query_params=query_params) |
| 81 | + query, total_pages, count = await json_api_pagination(query=query, query_params=query_params) |
| 82 | + users_db: List[User] = await query.all() |
| 83 | + users: List[UserSchema] = [UserSchema.from_orm(i_user) for i_user in users_db] |
| 84 | + |
| 85 | + return UserJSONAPIListSchema( |
| 86 | + meta={"count": count, "totalPages": total_pages}, |
| 87 | + data=[{"id": i_obj.id, "type": "Device", "attributes": i_obj.dict()} for i_obj in users], |
| 88 | + ) |
| 89 | + |
| 90 | + @classmethod |
| 91 | + async def post(cls, data: UserPostJSONAPISchema, query_params: QueryStringManager) -> UserSchema: |
| 92 | + try: |
| 93 | + device_obj = await UserFactory.create( |
| 94 | + data=data.attributes.dict(), |
| 95 | + mode=FactoryUseMode.production, |
| 96 | + header=query_params.headers, |
| 97 | + ) |
| 98 | + except ErrorCreateUserObject as ex: |
| 99 | + raise BadRequest(ex.description, ex.field) |
| 100 | + |
| 101 | + user = UserSchema.from_orm(device_obj) |
| 102 | + return user |
0 commit comments