Add sequences to BookDetail

This commit is contained in:
2022-03-05 23:42:01 +03:00
parent 6e307b8067
commit 42181b6d4e
9 changed files with 271 additions and 269 deletions

View File

@@ -3,6 +3,7 @@ from datetime import date
from pydantic import BaseModel
from app.serializers.author import Author
from app.serializers.sequence import Sequence
from app.serializers.orjson_config import ORJSONConfig
@@ -32,6 +33,7 @@ class RemoteBook(Book):
class BookDetail(RemoteBook):
sequences: list[Sequence]
is_deleted: bool

View File

@@ -43,8 +43,8 @@ SELECT ARRAY(
class AuthorTGRMSearchService(TRGMSearchService):
MODEL_CLASS = Author
SELECT_RELATED = ["source"]
PREFETCH_RELATED = ["annotations"]
PREFETCH_RELATED = ["source"]
SELECT_RELATED = ["annotations"]
GET_OBJECT_IDS_QUERY = GET_OBJECT_IDS_QUERY
@@ -70,8 +70,8 @@ class GetRandomAuthorService(GetRandomService):
class AuthorMeiliSearchService(MeiliSearchService):
MODEL_CLASS = Author
SELECT_RELATED = ["source"]
PREFETCH_RELATED = ["annotations"]
PREFETCH_RELATED = ["source"]
SELECT_RELATED = ["annotations"]
MS_INDEX_NAME = "authors"
MS_INDEX_LANG_KEY = "author_langs"

View File

@@ -24,8 +24,8 @@ SELECT ARRAY(
class BookTGRMSearchService(TRGMSearchService):
MODEL_CLASS = BookDB
SELECT_RELATED = ["source"]
PREFETCH_RELATED = ["authors", "translators", "annotations"]
PREFETCH_RELATED = ["source"]
SELECT_RELATED = ["authors", "translators", "annotations"]
GET_OBJECT_IDS_QUERY = GET_OBJECT_IDS_QUERY
@@ -95,8 +95,8 @@ class GetRandomBookService(GetRandomService):
class BookMeiliSearchService(MeiliSearchService):
MODEL_CLASS = BookDB
SELECT_RELATED = ["source"]
PREFETCH_RELATED = ["authors", "translators", "annotations"]
PREFETCH_RELATED = ["source"]
SELECT_RELATED = ["authors", "translators", "annotations"]
MS_INDEX_NAME = "books"
MS_INDEX_LANG_KEY = "lang"

View File

@@ -32,7 +32,7 @@ SELECT ARRAY (
class SequenceTGRMSearchService(TRGMSearchService):
MODEL_CLASS = Sequence
SELECT_RELATED = ["source"]
PREFETCH_RELATED = ["source"]
GET_OBJECT_IDS_QUERY = GET_OBJECT_IDS_QUERY
@@ -60,7 +60,7 @@ class GetRandomSequenceService(GetRandomService):
class SequenceMeiliSearchService(MeiliSearchService):
MODEL_CLASS = Sequence
SELECT_RELATED = ["source"]
PREFETCH_RELATED = ["source"]
MS_INDEX_NAME = "sequences"
MS_INDEX_LANG_KEY = "langs"

View File

@@ -44,16 +44,16 @@ SELECT ARRAY(
class TranslatorTGRMSearchService(TRGMSearchService):
MODEL_CLASS = Author
CUSTOM_CACHE_PREFIX = "translator"
SELECT_RELATED = ["source"]
PREFETCH_RELATED = ["annotations"]
PREFETCH_RELATED = ["source"]
SELECT_RELATED = ["annotations"]
GET_OBJECT_IDS_QUERY = GET_OBJECT_IDS_QUERY
class TranslatorMeiliSearchService(MeiliSearchService):
MODEL_CLASS = Author
CUSTOM_CACHE_PREFIX = "translator"
SELECT_RELATED = ["source"]
PREFETCH_RELATED = ["annotations"]
PREFETCH_RELATED = ["source"]
SELECT_RELATED = ["annotations"]
MS_INDEX_NAME = "authors"
MS_INDEX_LANG_KEY = "translator_langs"

View File

@@ -27,8 +27,8 @@ author_router = APIRouter(
)
SELECT_RELATED_FIELDS = ["source"]
PREFETCH_RELATED_FIELDS = ["annotations"]
PREFETCH_RELATED_FIELDS = ["source"]
SELECT_RELATED_FIELDS = ["annotations"]
@author_router.get(
@@ -107,8 +107,8 @@ async def get_author_books(
id: int, allowed_langs: list[str] = Depends(get_allowed_langs)
):
return await paginate(
BookDB.objects.select_related(["source"])
.prefetch_related(["annotations", "translators"])
BookDB.objects.prefetch_related(["source"])
.select_related(["annotations", "translators"])
.filter(authors__id=id, lang__in=allowed_langs, is_deleted=False)
.order_by("title")
)
@@ -137,8 +137,8 @@ async def get_translated_books(
id: int, allowed_langs: list[str] = Depends(get_allowed_langs)
):
return await paginate(
BookDB.objects.select_related(["source"])
.prefetch_related(["annotations", "authors"])
BookDB.objects.prefetch_related(["source"])
.select_related(["annotations", "authors"])
.filter(
translators__id=id,
lang__in=allowed_langs,

View File

@@ -29,8 +29,8 @@ book_router = APIRouter(
dependencies=[Depends(check_token)],
)
SELECT_RELATED_FIELDS = ["source"]
PREFETCH_RELATED_FIELDS = ["authors", "translators", "annotations"]
PREFETCH_RELATED_FIELDS = ["source"]
SELECT_RELATED_FIELDS = ["authors", "translators", "annotations"]
@book_router.get(
@@ -59,17 +59,19 @@ async def create_book(data: Union[CreateBook, CreateRemoteBook]):
async def get_random_book(allowed_langs: list[str] = Depends(get_allowed_langs)):
book_id = await GetRandomBookService.get_random_id(allowed_langs)
return (
await BookDB.objects.select_related(SELECT_RELATED_FIELDS)
book = (
await BookDB.objects.select_related(SELECT_RELATED_FIELDS + ["sequences"])
.prefetch_related(PREFETCH_RELATED_FIELDS)
.get(id=book_id)
)
return book
@book_router.get("/{id}", response_model=BookDetail)
async def get_book(id: int):
book = (
await BookDB.objects.select_related(SELECT_RELATED_FIELDS)
await BookDB.objects.select_related(SELECT_RELATED_FIELDS + ["sequences"])
.prefetch_related(PREFETCH_RELATED_FIELDS)
.get_or_none(id=id)
)

View File

@@ -47,8 +47,8 @@ async def get_sequence_books(
id: int, allowed_langs: list[str] = Depends(get_allowed_langs)
):
return await paginate(
BookDB.objects.select_related(["source"])
.prefetch_related(["annotations", "authors", "translators"])
BookDB.objects.prefetch_related(["source"])
.select_related(["annotations", "authors", "translators"])
.filter(sequences__id=id, lang__in=allowed_langs, is_deleted=False)
.order_by("sequences__booksequences__position")
)