Add linters configs

This commit is contained in:
2022-01-01 20:54:59 +03:00
parent 4a78d4f987
commit cbba30f2af
30 changed files with 580 additions and 298 deletions

View File

@@ -1,16 +1,22 @@
from random import choice as random_choice
from fastapi import APIRouter, Depends, Request, HTTPException, status
from fastapi_pagination import Params
from fastapi_pagination.ext.ormar import paginate
from app.utils.pagination import CustomPage
from app.models import Author as AuthorDB, AuthorAnnotation as AuthorAnnotationDB, Book as BookDB
from app.serializers.author import Author, CreateAuthor, UpdateAuthor, AuthorBook, TranslatedBook
from app.depends import check_token
from app.models import Author as AuthorDB
from app.models import AuthorAnnotation as AuthorAnnotationDB
from app.models import Book as BookDB
from app.serializers.author import (
Author,
CreateAuthor,
UpdateAuthor,
AuthorBook,
TranslatedBook,
)
from app.serializers.author_annotation import AuthorAnnotation
from app.services.author import AuthorTGRMSearchService, GetRandomAuthorService
from app.depends import check_token
from app.utils.pagination import CustomPage
author_router = APIRouter(
@@ -20,22 +26,19 @@ author_router = APIRouter(
)
PREFETCH_RELATED = ["source", "annotations"]
@author_router.get("/", response_model=CustomPage[Author], dependencies=[Depends(Params)])
@author_router.get(
"/", response_model=CustomPage[Author], dependencies=[Depends(Params)]
)
async def get_authors():
return await paginate(
AuthorDB.objects.prefetch_related(PREFETCH_RELATED)
)
return await paginate(AuthorDB.objects.prefetch_related(PREFETCH_RELATED))
@author_router.post("/", response_model=Author, dependencies=[Depends(Params)])
async def create_author(data: CreateAuthor):
author = await AuthorDB.objects.create(
**data.dict()
)
author = await AuthorDB.objects.create(**data.dict())
return await AuthorDB.objects.prefetch_related(PREFETCH_RELATED).get(id=author.id)
@@ -49,7 +52,9 @@ async def get_random_author():
@author_router.get("/{id}", response_model=Author)
async def get_author(id: int):
author = await AuthorDB.objects.prefetch_related(PREFETCH_RELATED).get_or_none(id=id)
author = await AuthorDB.objects.prefetch_related(PREFETCH_RELATED).get_or_none(
id=id
)
if author is None:
raise HTTPException(status.HTTP_404_NOT_FOUND)
@@ -75,24 +80,32 @@ async def get_author_annotation(id: int):
if annotation is None:
raise HTTPException(status.HTTP_404_NOT_FOUND)
return annotation
@author_router.get("/{id}/books", response_model=CustomPage[AuthorBook], dependencies=[Depends(Params)])
@author_router.get(
"/{id}/books", response_model=CustomPage[AuthorBook], dependencies=[Depends(Params)]
)
async def get_author_books(id: int):
return await paginate(
BookDB.objects.select_related(["source", "annotations", "translators"]).filter(authors__id=id).order_by('title')
BookDB.objects.select_related(["source", "annotations", "translators"])
.filter(authors__id=id)
.order_by("title")
)
@author_router.get("/{id}/translated_books", response_model=CustomPage[TranslatedBook])
async def get_translated_books(id: int):
return await paginate(
BookDB.objects.select_related(["source", "annotations", "translators"]).filter(translations__translator__id=id)
BookDB.objects.select_related(["source", "annotations", "translators"]).filter(
translations__translator__id=id
)
)
@author_router.get("/search/{query}", response_model=CustomPage[Author], dependencies=[Depends(Params)])
@author_router.get(
"/search/{query}", response_model=CustomPage[Author], dependencies=[Depends(Params)]
)
async def search_authors(query: str, request: Request):
return await AuthorTGRMSearchService.get(query, request.app.state.redis)