Add translators to Book serializers

This commit is contained in:
2021-12-10 18:00:02 +03:00
parent 1baae19702
commit 1117e36046
10 changed files with 30 additions and 25 deletions

View File

@@ -18,10 +18,14 @@ author_router = APIRouter(
)
PREFETCH_RELATED = ["source", "annotations"]
@author_router.get("/", response_model=CustomPage[Author], dependencies=[Depends(Params)])
async def get_authors():
return await paginate(
AuthorDB.objects.prefetch_related(["source", "annotations"])
AuthorDB.objects.prefetch_related(PREFETCH_RELATED)
)
@@ -31,12 +35,12 @@ async def create_author(data: CreateAuthor):
**data.dict()
)
return await AuthorDB.objects.prefetch_related(["source", "annotations"]).get(id=author.id)
return await AuthorDB.objects.prefetch_related(PREFETCH_RELATED).get(id=author.id)
@author_router.get("/{id}", response_model=Author)
async def get_author(id: int):
author = await AuthorDB.objects.prefetch_related(["source", "annotations"]).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)
@@ -69,14 +73,14 @@ async def get_author_annotation(id: int):
@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"]).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(["translations", "translations__translator"]).filter(translations__translator__id=id)
BookDB.objects.select_related(["source", "annotations", "translators"]).filter(translations__translator__id=id)
)

View File

@@ -21,10 +21,13 @@ book_router = APIRouter(
)
SELECT_RELATED_FIELDS = ["source", "authors", "translators", "annotations"]
@book_router.get("/", response_model=CustomPage[RemoteBook], dependencies=[Depends(Params)])
async def get_books(book_filter: dict = Depends(get_book_filter)):
return await paginate(
BookDB.objects.select_related(["source", "authors", "annotations"]).filter(**book_filter)
BookDB.objects.select_related(SELECT_RELATED_FIELDS).filter(**book_filter)
)
@@ -32,12 +35,12 @@ async def get_books(book_filter: dict = Depends(get_book_filter)):
async def create_book(data: Union[CreateBook, CreateRemoteBook]):
book = await BookCreator.create(data)
return await BookDB.objects.select_related(["source", "authors", "annotations"]).get(id=book.id)
return await BookDB.objects.select_related(SELECT_RELATED_FIELDS).get(id=book.id)
@book_router.get("/{id}", response_model=BookDetail)
async def get_book(id: int):
book = await BookDB.objects.select_related(["source", "authors", "annotations"]).get_or_none(id=id)
book = await BookDB.objects.select_related(SELECT_RELATED_FIELDS).get_or_none(id=id)
if book is None:
raise HTTPException(status.HTTP_404_NOT_FOUND)
@@ -47,7 +50,7 @@ async def get_book(id: int):
@book_router.get("/remote/{source_id}/{remote_id}", response_model=Book)
async def get_remote_book(source_id: int, remote_id: int):
book = await BookDB.objects.select_related(["source", "authors", "annotations"]).get_or_none(
book = await BookDB.objects.select_related(SELECT_RELATED_FIELDS).get_or_none(
source=source_id,
remote_id=remote_id
)
@@ -60,7 +63,7 @@ async def get_remote_book(source_id: int, remote_id: int):
@book_router.put("/{id}", response_model=Book)
async def update_book(id: int, data: UpdateBook):
book = await BookDB.objects.select_related(["source", "authors", "annotations"]).get_or_none(id=id)
book = await BookDB.objects.select_related(SELECT_RELATED_FIELDS).get_or_none(id=id)
if book is None:
raise HTTPException(status.HTTP_404_NOT_FOUND)

View File

@@ -32,7 +32,7 @@ async def get_sequence(id: int):
@sequence_router.get("/{id}/books", response_model=CustomPage[SequenceBook], dependencies=[Depends(Params)])
async def get_sequence_books(id: int):
return await paginate(
BookDB.objects.select_related(["source", "annotations", "authors"])
BookDB.objects.select_related(["source", "annotations", "authors", "translators"])
.filter(sequences__id=id).order_by("sequences__booksequences__position")
)

View File

@@ -22,7 +22,7 @@ translation_router = APIRouter(
@translation_router.get("/", response_model=CustomPage[Translation], dependencies=[Depends(Params)])
async def get_translations():
return await paginate(
TranslationDB.objects.prefetch_related(["book", "translator"])
TranslationDB.objects.prefetch_related(["book", "author"])
)
@@ -30,12 +30,12 @@ async def get_translations():
async def create_translation(data: Union[CreateTranslation, CreateRemoteTranslation]):
translation = await TranslationCreator.create(data)
return await TranslationDB.objects.prefetch_related(["book", "translator"]).get(id=translation.id)
return await TranslationDB.objects.prefetch_related(["book", "author"]).get(id=translation.id)
@translation_router.delete("/{id}", response_model=Translation)
async def delete_translation(id: int):
translation = await TranslationDB.objects.prefetch_related(["book", "translator"]).get_or_none(id=id)
translation = await TranslationDB.objects.prefetch_related(["book", "author"]).get_or_none(id=id)
if translation is None:
raise HTTPException(status.HTTP_404_NOT_FOUND)