Fix book import failing due to strict validation
This commit is contained in:
@@ -182,7 +182,11 @@ def books_import() -> Response:
|
|||||||
isbn=isbn,
|
isbn=isbn,
|
||||||
authors=book_data.authors,
|
authors=book_data.authors,
|
||||||
publisher=book_data.publisher,
|
publisher=book_data.publisher,
|
||||||
first_published=book_data.publishedDate.year,
|
first_published=(
|
||||||
|
book_data.publishedDate.year
|
||||||
|
if isinstance(book_data.publishedDate, date)
|
||||||
|
else book_data.publishedDate
|
||||||
|
),
|
||||||
genres=book_data.categories,
|
genres=book_data.categories,
|
||||||
)
|
)
|
||||||
db.session.add(book)
|
db.session.add(book)
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
from datetime import date
|
from datetime import date, datetime
|
||||||
|
from typing import Any, Optional
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel, field_validator
|
||||||
|
|
||||||
# {
|
# {
|
||||||
# "title": "Concilio de Sombras (Sombras de Magia 2)",
|
# "title": "Concilio de Sombras (Sombras de Magia 2)",
|
||||||
@@ -50,23 +51,35 @@ from pydantic import BaseModel
|
|||||||
|
|
||||||
class GoogleBook(BaseModel):
|
class GoogleBook(BaseModel):
|
||||||
title: str
|
title: str
|
||||||
authors: list[str]
|
authors: list[str] = []
|
||||||
publisher: str
|
publisher: str = ""
|
||||||
publishedDate: date
|
publishedDate: Optional[date | int] = None
|
||||||
description: str
|
description: str = ""
|
||||||
industryIdentifiers: list[dict[str, str]]
|
industryIdentifiers: list[dict[str, str]] = []
|
||||||
pageCount: int
|
pageCount: int = 0
|
||||||
printType: str
|
printType: str = ""
|
||||||
categories: list[str]
|
categories: list[str] = []
|
||||||
maturityRating: str
|
maturityRating: str = ""
|
||||||
allowAnonLogging: bool
|
allowAnonLogging: bool = False
|
||||||
contentVersion: str
|
contentVersion: str = ""
|
||||||
panelizationSummary: dict[str, bool]
|
panelizationSummary: dict[str, bool] = {}
|
||||||
imageLinks: dict[str, str]
|
imageLinks: dict[str, str] = {}
|
||||||
language: str
|
language: str = ""
|
||||||
previewLink: str
|
previewLink: str = ""
|
||||||
infoLink: str
|
infoLink: str = ""
|
||||||
canonicalVolumeLink: str
|
canonicalVolumeLink: str = ""
|
||||||
|
|
||||||
|
# Validate publishedDate when given in YYYY-MM format and convert to date
|
||||||
|
@field_validator("publishedDate", mode="before")
|
||||||
|
@classmethod
|
||||||
|
def validate_published_date(cls, v: Any) -> Any:
|
||||||
|
if isinstance(v, str):
|
||||||
|
try:
|
||||||
|
return datetime.strptime(v, "%Y-%m").date()
|
||||||
|
except ValueError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
return v
|
||||||
|
|
||||||
|
|
||||||
def fetch_google_book_data(isbn: str) -> GoogleBook:
|
def fetch_google_book_data(isbn: str) -> GoogleBook:
|
||||||
|
|||||||
Reference in New Issue
Block a user