mirror of
https://github.com/flibusta-apps/book_library_server.git
synced 2025-12-06 15:15:36 +01:00
Add translators to Book serializers
This commit is contained in:
@@ -103,6 +103,7 @@ class BookGenres(ormar.Model):
|
||||
class BookSequences(ormar.Model):
|
||||
class Meta(BaseMeta):
|
||||
tablename = "book_sequences"
|
||||
orders_by = ["position", ]
|
||||
|
||||
id: int = ormar.Integer(primary_key=True, nullable=False) # type: ignore
|
||||
|
||||
@@ -112,6 +113,7 @@ class BookSequences(ormar.Model):
|
||||
class Translation(ormar.Model):
|
||||
class Meta(BaseMeta):
|
||||
tablename = "translations"
|
||||
orders_by = ["position", ]
|
||||
|
||||
id: int = ormar.Integer(primary_key=True, nullable=False) # type: ignore
|
||||
|
||||
|
||||
@@ -47,27 +47,21 @@ class AuthorBook(BaseModel):
|
||||
file_type: str
|
||||
available_types: list[str]
|
||||
uploaded: date
|
||||
translators: list[Author]
|
||||
annotation_exists: bool
|
||||
|
||||
class Config(ORJSONConfig):
|
||||
pass
|
||||
|
||||
|
||||
class Translation(BaseModel):
|
||||
translator: Author
|
||||
position: int
|
||||
|
||||
class Config(ORJSONConfig):
|
||||
pass
|
||||
|
||||
|
||||
class TranslatedBook(BaseModel):
|
||||
id: int
|
||||
title: str
|
||||
lang: str
|
||||
file_type: str
|
||||
available_types: list[str]
|
||||
authors: list[Author]
|
||||
translations: list[Translation]
|
||||
annotation_exists: bool
|
||||
|
||||
class Config(ORJSONConfig):
|
||||
pass
|
||||
|
||||
@@ -19,6 +19,7 @@ class Book(BaseModel):
|
||||
available_types: list[str]
|
||||
uploaded: date
|
||||
authors: list[Author]
|
||||
translators: list[Author]
|
||||
annotation_exists: bool
|
||||
|
||||
class Config(ORJSONConfig):
|
||||
|
||||
@@ -41,6 +41,7 @@ class Book(BaseModel):
|
||||
available_types: list[str]
|
||||
uploaded: date
|
||||
authors: list[Author]
|
||||
translators: list[Author]
|
||||
annotation_exists: bool
|
||||
|
||||
class Config(ORJSONConfig):
|
||||
|
||||
@@ -39,5 +39,5 @@ SELECT ARRAY(
|
||||
|
||||
class AuthorTGRMSearchService(TRGMSearchService):
|
||||
MODEL_CLASS = Author
|
||||
PREFETCH_RELATED = ["source", "annotations"]
|
||||
PREFETCH_RELATED = ["source", "annotations", "translators"]
|
||||
GET_OBJECT_IDS_QUERY = GET_OBJECT_IDS_QUERY
|
||||
|
||||
@@ -23,7 +23,7 @@ SELECT ARRAY(
|
||||
|
||||
class BookTGRMSearchService(TRGMSearchService):
|
||||
MODEL_CLASS = BookDB
|
||||
PREFETCH_RELATED = ["source", "authors", "annotations"]
|
||||
PREFETCH_RELATED = ["source", "authors", "translators", "annotations"]
|
||||
GET_OBJECT_IDS_QUERY = GET_OBJECT_IDS_QUERY
|
||||
|
||||
|
||||
|
||||
@@ -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)
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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")
|
||||
)
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user