Add available types field

This commit is contained in:
2021-11-21 14:17:44 +03:00
parent e504fc4cc8
commit 66a166a72a
4 changed files with 14 additions and 6 deletions

View File

@@ -140,6 +140,13 @@ class Book(ormar.Model):
genres = ormar.ManyToMany(Genre, through=BookGenres)
sequences = ormar.ManyToMany(Sequence, through=BookSequences)
@ormar.property_field
def available_types(self) -> list[str]:
if self.file_type == 'fb2' and self.source.name == 'flibusta':
return ['fb2', 'epub', 'mobi']
return [self.file_type]
class BookAnnotation(ormar.Model):
class Meta(BaseMeta):

View File

@@ -10,6 +10,7 @@ class Book(BaseModel):
title: str
lang: str
file_type: str
available_types: list[str]
uploaded: date
authors: list[Author]

View File

@@ -13,7 +13,7 @@ class BookTGRMSearchService(TRGMSearchService):
FIELDS = [
BookDB.Meta.table.c.title
]
PREFETCH_RELATED = ["source"]
PREFETCH_RELATED = ["source", "authors"]
FILTERS = [
BookDB.Meta.table.c.is_deleted == False,
]

View File

@@ -22,7 +22,7 @@ book_router = APIRouter(
@book_router.get("/", response_model=CustomPage[Book], dependencies=[Depends(Params)])
async def get_books():
return await paginate(
BookDB.objects.select_related("authors")
BookDB.objects.select_related(["source", "authors"])
)
@@ -30,12 +30,12 @@ async def get_books():
async def create_book(data: Union[CreateBook, CreateRemoteBook]):
book = await BookCreator.create(data)
return await BookDB.objects.select_related("authors").get(id=book.id)
return await BookDB.objects.select_related(["source", "authors"]).get(id=book.id)
@book_router.get("/{id}", response_model=Book)
async def get_book(id: int):
book = await BookDB.objects.select_related("authors").get_or_none(id=id)
book = await BookDB.objects.select_related(["source", "authors"]).get_or_none(id=id)
if book is None:
raise HTTPException(status.HTTP_404_NOT_FOUND)
@@ -45,7 +45,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("authors").get_or_none(
book = await BookDB.objects.select_related(["source", "authors"]).get_or_none(
source=source_id,
remote_id=remote_id
)
@@ -58,7 +58,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("authors").get_or_none(id=id)
book = await BookDB.objects.select_related(["source", "authors"]).get_or_none(id=id)
if book is None:
raise HTTPException(status.HTTP_404_NOT_FOUND)