Skip to content
Open
80 changes: 74 additions & 6 deletions openlibrary/fastapi/lists.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
from __future__ import annotations

from typing import TYPE_CHECKING, Annotated, Literal
Comment thread
harshgupta2125 marked this conversation as resolved.
Outdated

from fastapi import APIRouter, Depends, HTTPException, Path, Query, Request, status
from fastapi import APIRouter, Body, Depends, HTTPException, Path, Query, Request, status
Comment thread
harshgupta2125 marked this conversation as resolved.
Outdated

from infogami.infobase import client
from openlibrary.accounts import get_current_user
from openlibrary.fastapi.auth import AuthenticatedUser, require_authenticated_user
from openlibrary.plugins.openlibrary.lists import ListEditionsModel, ListSubjectsModel, get_list, get_list_editions, get_list_subjects
from openlibrary.plugins.openlibrary.lists import ListEditionsModel, ListSubjectsModel, get_list, get_list_editions, get_list_seeds, get_list_subjects
from openlibrary.plugins.openlibrary.lists import list_seeds as _LegacyListSeeds
from openlibrary.plugins.openlibrary.lists import lists_delete as _LegacyListsDelete
from openlibrary.utils.request_context import site, web_ctx_ip

Expand Down Expand Up @@ -114,8 +113,77 @@ async def lists_json():
pass


async def list_seeds():
pass
def _get_list_seeds_or_404(key: str) -> dict:
lst = get_list_seeds(key)
if lst is None:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="List or Series not found")
return lst


def _update_list_seeds(key: str, payload: dict) -> dict:
s = site.get()
lst = s.get(key)

if lst is None:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="List or Series not found")

if not s.can_write(key):
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Permission denied.")

try:
# Pass the payload safely directly to the legacy processor
data = {"add": payload.get("add", []), "remove": payload.get("remove", [])}
return _LegacyListSeeds.process_seeds_update(lst, data, key)
Comment thread
harshgupta2125 marked this conversation as resolved.
except ValueError as e:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))


@router.get("/people/{username}/lists/{olid}/seeds.json")
def list_seeds_json_user(username: UsernamePath, olid: ListOLID) -> dict:
return _get_list_seeds_or_404(f"/people/{username}/lists/{olid}")


Comment thread
harshgupta2125 marked this conversation as resolved.
@router.get("/people/{username}/series/{olid}/seeds.json")
def series_seeds_json_user(username: UsernamePath, olid: ListOLID) -> dict:
return _get_list_seeds_or_404(f"/people/{username}/series/{olid}")


@router.get("/lists/{olid}/seeds.json")
def list_seeds_json_public(olid: ListOLID) -> dict:
return _get_list_seeds_or_404(f"/lists/{olid}")


@router.get("/series/{olid}/seeds.json")
def series_seeds_json_public(olid: ListOLID) -> dict:
return _get_list_seeds_or_404(f"/series/{olid}")


@router.post("/people/{username}/lists/{olid}/seeds.json")
def update_list_seeds_json_user(
username: UsernamePath, olid: ListOLID, payload: Annotated[dict, Body(...)], _: Annotated[AuthenticatedUser, Depends(require_authenticated_user)]
) -> dict:
return _update_list_seeds(f"/people/{username}/lists/{olid}", payload)


@router.post("/people/{username}/series/{olid}/seeds.json")
def update_series_seeds_json_user(
username: UsernamePath, olid: ListOLID, payload: Annotated[dict, Body(...)], _: Annotated[AuthenticatedUser, Depends(require_authenticated_user)]
) -> dict:
return _update_list_seeds(f"/people/{username}/series/{olid}", payload)


@router.post("/lists/{olid}/seeds.json")
def update_list_seeds_json_public(
olid: ListOLID, payload: Annotated[dict, Body(...)], _: Annotated[AuthenticatedUser, Depends(require_authenticated_user)]
) -> dict:
return _update_list_seeds(f"/lists/{olid}", payload)


@router.post("/series/{olid}/seeds.json")
def update_series_seeds_json_public(
olid: ListOLID, payload: Annotated[dict, Body(...)], _: Annotated[AuthenticatedUser, Depends(require_authenticated_user)]
Comment thread
harshgupta2125 marked this conversation as resolved.
) -> dict:
return _update_list_seeds(f"/series/{olid}", payload)


class GetListEditionsParams:
Expand Down
2 changes: 1 addition & 1 deletion openlibrary/plugins/openlibrary/lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,7 @@ class list_view_yaml(list_view_json):


def get_list_seeds(key):
if lst := web.ctx.site.get(key):
if lst := site.get().get(key):
seeds = [seed.dict() for seed in lst.get_seeds()]
return {
"links": {"self": key + "/seeds", "list": key},
Expand Down