Add annotation_exists field to Book and Author

This commit is contained in:
2021-12-05 19:14:05 +03:00
parent aacdf51f17
commit 5fbe26c0f2
7 changed files with 23 additions and 12 deletions

View File

@@ -53,6 +53,10 @@ class Author(ormar.Model):
last_name: str = ormar.String(max_length=256, nullable=False) # type: ignore last_name: str = ormar.String(max_length=256, nullable=False) # type: ignore
middle_name: str = ormar.String(max_length=256, nullable=True) # type: ignore middle_name: str = ormar.String(max_length=256, nullable=True) # type: ignore
@ormar.property_field
def annotation_exists(self) -> bool:
return len(self.annotations) != 0
class AuthorAnnotation(ormar.Model): class AuthorAnnotation(ormar.Model):
class Meta(BaseMeta): class Meta(BaseMeta):
@@ -60,7 +64,7 @@ class AuthorAnnotation(ormar.Model):
id = ormar.Integer(primary_key=True, nullable=False) id = ormar.Integer(primary_key=True, nullable=False)
author: Author = ormar.ForeignKey(Author, nullable=False, unique=True) author: Author = ormar.ForeignKey(Author, nullable=False, unique=True, related_name="annotations")
title: str = ormar.String(max_length=256, nullable=False, default="") # type: ignore title: str = ormar.String(max_length=256, nullable=False, default="") # type: ignore
text: str = ormar.Text(nullable=False, default="") # type: ignore text: str = ormar.Text(nullable=False, default="") # type: ignore
@@ -147,6 +151,10 @@ class Book(ormar.Model):
return [self.file_type] return [self.file_type]
@ormar.property_field
def annotation_exists(self) -> bool:
return len(self.annotations) != 0
class BookAnnotation(ormar.Model): class BookAnnotation(ormar.Model):
class Meta(BaseMeta): class Meta(BaseMeta):
@@ -154,7 +162,7 @@ class BookAnnotation(ormar.Model):
id = ormar.Integer(primary_key=True, nullable=False) id = ormar.Integer(primary_key=True, nullable=False)
book: Book = ormar.ForeignKey(Book, nullable=False, unique=True) book: Book = ormar.ForeignKey(Book, nullable=False, unique=True, related_name="annotations")
title: str = ormar.String(max_length=256, nullable=False, default="") # type: ignore title: str = ormar.String(max_length=256, nullable=False, default="") # type: ignore
text: str = ormar.Text(nullable=False, default="") # type: ignore text: str = ormar.Text(nullable=False, default="") # type: ignore

View File

@@ -12,6 +12,8 @@ class Author(BaseModel):
last_name: str last_name: str
middle_name: Optional[str] middle_name: Optional[str]
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]
annotation_exists: bool
class Config(ORJSONConfig): class Config(ORJSONConfig):
pass pass

View File

@@ -10,4 +10,4 @@ class AuthorTGRMSearchService(TRGMSearchService):
Author.Meta.table.c.first_name, Author.Meta.table.c.first_name,
Author.Meta.table.c.middle_name Author.Meta.table.c.middle_name
] ]
PREFETCH_RELATED = ["source"] PREFETCH_RELATED = ["source", "annotations"]

View File

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

View File

@@ -21,7 +21,7 @@ author_router = APIRouter(
@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") AuthorDB.objects.prefetch_related(["source", "annotations"])
) )
@@ -31,12 +31,12 @@ async def create_author(data: CreateAuthor):
**data.dict() **data.dict()
) )
return await AuthorDB.objects.prefetch_related("source").get(id=author.id) return await AuthorDB.objects.prefetch_related(["source", "annotations"]).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").get_or_none(id=id) author = await AuthorDB.objects.prefetch_related(["source", "annotations"]).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)

View File

@@ -23,7 +23,7 @@ book_router = APIRouter(
@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"]).filter(**book_filter) BookDB.objects.select_related(["source", "authors", "annotations"]).filter(**book_filter)
) )
@@ -31,12 +31,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"]).get(id=book.id) return await BookDB.objects.select_related(["source", "authors", "annotations"]).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"]).get_or_none(id=id) book = await BookDB.objects.select_related(["source", "authors", "annotations"]).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)
@@ -46,7 +46,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"]).get_or_none( book = await BookDB.objects.select_related(["source", "authors", "annotations"]).get_or_none(
source=source_id, source=source_id,
remote_id=remote_id remote_id=remote_id
) )
@@ -59,7 +59,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"]).get_or_none(id=id) book = await BookDB.objects.select_related(["source", "authors", "annotations"]).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)