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

@@ -103,6 +103,7 @@ class BookGenres(ormar.Model):
class BookSequences(ormar.Model): class BookSequences(ormar.Model):
class Meta(BaseMeta): class Meta(BaseMeta):
tablename = "book_sequences" tablename = "book_sequences"
orders_by = ["position", ]
id: int = ormar.Integer(primary_key=True, nullable=False) # type: ignore id: int = ormar.Integer(primary_key=True, nullable=False) # type: ignore
@@ -112,6 +113,7 @@ class BookSequences(ormar.Model):
class Translation(ormar.Model): class Translation(ormar.Model):
class Meta(BaseMeta): class Meta(BaseMeta):
tablename = "translations" tablename = "translations"
orders_by = ["position", ]
id: int = ormar.Integer(primary_key=True, nullable=False) # type: ignore id: int = ormar.Integer(primary_key=True, nullable=False) # type: ignore

View File

@@ -47,27 +47,21 @@ class AuthorBook(BaseModel):
file_type: str file_type: str
available_types: list[str] available_types: list[str]
uploaded: date uploaded: date
translators: list[Author]
annotation_exists: bool annotation_exists: bool
class Config(ORJSONConfig): class Config(ORJSONConfig):
pass pass
class Translation(BaseModel):
translator: Author
position: int
class Config(ORJSONConfig):
pass
class TranslatedBook(BaseModel): class TranslatedBook(BaseModel):
id: int id: int
title: str title: str
lang: str lang: str
file_type: str file_type: str
available_types: list[str]
authors: list[Author] authors: list[Author]
translations: list[Translation] annotation_exists: bool
class Config(ORJSONConfig): class Config(ORJSONConfig):
pass pass

View File

@@ -19,6 +19,7 @@ class Book(BaseModel):
available_types: list[str] available_types: list[str]
uploaded: date uploaded: date
authors: list[Author] authors: list[Author]
translators: list[Author]
annotation_exists: bool annotation_exists: bool
class Config(ORJSONConfig): class Config(ORJSONConfig):

View File

@@ -41,6 +41,7 @@ class Book(BaseModel):
available_types: list[str] available_types: list[str]
uploaded: date uploaded: date
authors: list[Author] authors: list[Author]
translators: list[Author]
annotation_exists: bool annotation_exists: bool
class Config(ORJSONConfig): class Config(ORJSONConfig):

View File

@@ -39,5 +39,5 @@ SELECT ARRAY(
class AuthorTGRMSearchService(TRGMSearchService): class AuthorTGRMSearchService(TRGMSearchService):
MODEL_CLASS = Author MODEL_CLASS = Author
PREFETCH_RELATED = ["source", "annotations"] PREFETCH_RELATED = ["source", "annotations", "translators"]
GET_OBJECT_IDS_QUERY = GET_OBJECT_IDS_QUERY GET_OBJECT_IDS_QUERY = GET_OBJECT_IDS_QUERY

View File

@@ -23,7 +23,7 @@ SELECT ARRAY(
class BookTGRMSearchService(TRGMSearchService): class BookTGRMSearchService(TRGMSearchService):
MODEL_CLASS = BookDB MODEL_CLASS = BookDB
PREFETCH_RELATED = ["source", "authors", "annotations"] PREFETCH_RELATED = ["source", "authors", "translators", "annotations"]
GET_OBJECT_IDS_QUERY = GET_OBJECT_IDS_QUERY GET_OBJECT_IDS_QUERY = GET_OBJECT_IDS_QUERY

View File

@@ -18,10 +18,14 @@ 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(): async def get_authors():
return await paginate( 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() **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) @author_router.get("/{id}", response_model=Author)
async def get_author(id: int): 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: if author is None:
raise HTTPException(status.HTTP_404_NOT_FOUND) 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)]) @author_router.get("/{id}/books", response_model=CustomPage[AuthorBook], dependencies=[Depends(Params)])
async def get_author_books(id: int): async def get_author_books(id: int):
return await paginate( 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]) @author_router.get("/{id}/translated_books", response_model=CustomPage[TranslatedBook])
async def get_translated_books(id: int): async def get_translated_books(id: int):
return await paginate( 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)]) @book_router.get("/", response_model=CustomPage[RemoteBook], dependencies=[Depends(Params)])
async def get_books(book_filter: dict = Depends(get_book_filter)): async def get_books(book_filter: dict = Depends(get_book_filter)):
return await paginate( 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]): async def create_book(data: Union[CreateBook, CreateRemoteBook]):
book = await BookCreator.create(data) 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) @book_router.get("/{id}", response_model=BookDetail)
async def get_book(id: int): 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: if book is None:
raise HTTPException(status.HTTP_404_NOT_FOUND) 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) @book_router.get("/remote/{source_id}/{remote_id}", response_model=Book)
async def get_remote_book(source_id: int, remote_id: int): 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, source=source_id,
remote_id=remote_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) @book_router.put("/{id}", response_model=Book)
async def update_book(id: int, data: UpdateBook): 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: if book is None:
raise HTTPException(status.HTTP_404_NOT_FOUND) 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)]) @sequence_router.get("/{id}/books", response_model=CustomPage[SequenceBook], dependencies=[Depends(Params)])
async def get_sequence_books(id: int): async def get_sequence_books(id: int):
return await paginate( 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") .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)]) @translation_router.get("/", response_model=CustomPage[Translation], dependencies=[Depends(Params)])
async def get_translations(): async def get_translations():
return await paginate( 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]): async def create_translation(data: Union[CreateTranslation, CreateRemoteTranslation]):
translation = await TranslationCreator.create(data) 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) @translation_router.delete("/{id}", response_model=Translation)
async def delete_translation(id: int): 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: if translation is None:
raise HTTPException(status.HTTP_404_NOT_FOUND) raise HTTPException(status.HTTP_404_NOT_FOUND)