add(chore): bulk requests
This commit is contained in:
@@ -13,9 +13,9 @@ router = APIRouter(prefix="/api/import", tags=["import"])
|
||||
|
||||
|
||||
@router.get("/scan")
|
||||
def scan(session: Session = Depends(get_session)):
|
||||
def scan(split_dirs: bool = False, session: Session = Depends(get_session)):
|
||||
"""Scan the download dir and suggest matches against open requests."""
|
||||
items = scanner.scan(Path(config.DOWNLOAD_DIR))
|
||||
items = scanner.scan(Path(config.DOWNLOAD_DIR), split_dirs=split_dirs)
|
||||
missing = session.scalars(
|
||||
select(BookRequest).where(BookRequest.status == "missing")
|
||||
).all()
|
||||
|
||||
@@ -61,6 +61,46 @@ def create_request(data: RequestIn, session: Session = Depends(get_session)):
|
||||
return _out(req)
|
||||
|
||||
|
||||
class BulkRequestIn(BaseModel):
|
||||
library_id: int
|
||||
series: str
|
||||
authors: str = ""
|
||||
volume_from: int
|
||||
volume_to: int
|
||||
year: int | None = None
|
||||
cover_url: str = ""
|
||||
|
||||
|
||||
@router.post("/bulk", response_model=list[RequestOut])
|
||||
def create_bulk(data: BulkRequestIn, session: Session = Depends(get_session)):
|
||||
lib = session.get(Library, data.library_id)
|
||||
if not lib:
|
||||
raise HTTPException(404, "library not found")
|
||||
if data.volume_from > data.volume_to:
|
||||
raise HTTPException(400, "volume_from must be <= volume_to")
|
||||
if data.volume_to - data.volume_from > 999:
|
||||
raise HTTPException(400, "at most 1000 requests per bulk call")
|
||||
width = max(2, len(str(data.volume_to)))
|
||||
reqs = []
|
||||
for n in range(data.volume_from, data.volume_to + 1):
|
||||
req = BookRequest(
|
||||
media_type=lib.media_type,
|
||||
library_id=lib.id,
|
||||
title=f"{data.series} Folge {n:0{width}d}",
|
||||
authors=data.authors,
|
||||
series=data.series,
|
||||
volume=n,
|
||||
year=data.year,
|
||||
cover_url=data.cover_url,
|
||||
)
|
||||
session.add(req)
|
||||
reqs.append(req)
|
||||
session.commit()
|
||||
for r in reqs:
|
||||
session.refresh(r)
|
||||
return [_out(r) for r in reqs]
|
||||
|
||||
|
||||
@router.delete("/{request_id}")
|
||||
def delete_request(request_id: int, session: Session = Depends(get_session)):
|
||||
req = session.get(BookRequest, request_id)
|
||||
|
||||
Reference in New Issue
Block a user