Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ The format is based on [Keep a Changelog 1.1.0](https://keepachangelog.com/en/1.
- A web interface foundation: browser sign-in and sign-out with server-side cookie sessions, CSRF protection, a searchable, paged list of works, and work pages to view, add, edit, and delete.
- Web pages for libraries, copies, and editions: list and open the libraries you belong to, add and change a library and its settings, add copies from an edition, and edit or delete a copy. Each page offers only what your role allows.
- Library member lists in the API now include each member's display name.
- Web pages for tags and shelves: set a copy's tags from its page, create, rename, and delete shelves, put copies on shelves and take them off, and see a library's shelves and tags.
- An API operation, `GET /api/v1/copies/{copy_id}/shelves`, that lists the shelves holding a copy. Only members of the copy's library see them.
94 changes: 94 additions & 0 deletions openapi/openapi.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions src/librelibrary/api/v1/libraries.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,15 @@ def set_copy_tags(
return [TagOut.model_validate(tag) for tag in tags]


@router.get("/copies/{copy_id}/shelves", operation_id="copy_shelves_list")
def list_copy_shelves(
copy_id: PathId, session: DbSession, actor: CurrentActor
) -> list[ShelfOut]:
"""List the shelves that hold a copy. Only members of its library see them."""
shelves = libraries.list_copy_shelves(session, actor, copy_id)
return [ShelfOut.model_validate(shelf) for shelf in shelves]


@router.get("/libraries/{library_id}/tags", operation_id="tags_list")
def list_tags(
library_id: PathId,
Expand Down
2 changes: 2 additions & 0 deletions src/librelibrary/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from librelibrary.ui import copies as copy_pages
from librelibrary.ui import libraries as library_pages
from librelibrary.ui import pages, works
from librelibrary.ui import shelves as shelf_pages
from librelibrary.ui.deps import LoginRequired
from librelibrary.ui.templating import UI_DIRECTORY, templates

Expand Down Expand Up @@ -164,6 +165,7 @@ async def lifespan(_app: FastAPI) -> AsyncIterator[None]:
app.include_router(works.router)
app.include_router(library_pages.router)
app.include_router(copy_pages.router)
app.include_router(shelf_pages.router)
app.mount("/static", StaticFiles(directory=UI_DIRECTORY / "static"), name="static")

@app.middleware("http")
Expand Down
19 changes: 19 additions & 0 deletions src/librelibrary/services/libraries.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,25 @@ def list_copy_tags(session: Session, actor: Actor, copy_id: int) -> list[Tag]:
return list(session.scalars(statement))


@policy_action(Action.COPY_READ)
def list_copy_shelves(session: Session, actor: Actor, copy_id: int) -> list[Shelf]:
"""List the shelves that hold a visible copy.

Shelf names belong to a library's members, so someone who can see the copy
only because it is shared more widely gets an empty list.
"""
copy = _visible_copy(session, actor, copy_id)
if library_role(session, actor, copy.library_id) is None:
return []
statement = (
select(Shelf)
.join(ShelfItem, ShelfItem.shelf_id == Shelf.id)
.where(ShelfItem.copy_id == copy_id)
.order_by(Shelf.name)
)
return list(session.scalars(statement))


@policy_action(Action.LIBRARY_EDIT)
def set_copy_tags(
session: Session, actor: Actor, copy_id: int, names: list[str]
Expand Down
123 changes: 116 additions & 7 deletions src/librelibrary/ui/copies.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,16 @@

from librelibrary.api.deps import DbSession, PathId
from librelibrary.authz.policy import Action, LibraryResource, require
from librelibrary.schemas import CopyIn, CopyOut, CopyPatch, LibraryOut, Page
from librelibrary.schemas import (
CopyIn,
CopyOut,
CopyPatch,
LibraryOut,
Page,
ShelfOut,
TagNamesIn,
TagOut,
)
from librelibrary.services import libraries
from librelibrary.ui import ui_route
from librelibrary.ui.deps import CsrfChecked, CurrentUser
Expand All @@ -18,6 +27,7 @@

router = APIRouter()
ALL = 200
MAX_TAGS = 50
_SEE_OTHER = 303
_ID = Annotated[int, Form(ge=1, le=2_147_483_647)]
_LABELS = {
Expand Down Expand Up @@ -188,19 +198,41 @@ def create_copy( # noqa: PLR0913
return RedirectResponse(f"/copies/{copy.id}", status_code=_SEE_OTHER)


@ui_route(router, "GET", "/copies/{copy_id}", api="copies_get")
def copy_detail(
request: Request, copy_id: PathId, session: DbSession, user: CurrentUser
def _detail( # noqa: PLR0913
request: Request,
session: DbSession,
user: CurrentUser,
copy: CopyOut,
*,
tag_text: str | None = None,
errors: list[str] | None = None,
status_code: int = 200,
) -> Response:
"""Show a copy I can see."""
copy = CopyOut.model_validate(libraries.get_copy(session, user.actor, copy_id))
copy_id = copy.id
label = edition_labels(session, user.actor, [copy.edition_id])[copy.edition_id]
role = libraries.my_roles(session, user.actor).get(copy.library_id)
can_edit = allowed(user.actor, Action.LIBRARY_EDIT, LibraryResource(role))
tags = [
TagOut.model_validate(tag)
for tag in libraries.list_copy_tags(session, user.actor, copy_id)
]
library = None
shelves: list[ShelfOut] = []
shelf_choices: list[ShelfOut] = []
if role is not None:
library = LibraryOut.model_validate(
libraries.get_library(session, user.actor, copy.library_id)
)
shelves = [
ShelfOut.model_validate(shelf)
for shelf in libraries.list_copy_shelves(session, user.actor, copy_id)
]
if can_edit:
on = {shelf.id for shelf in shelves}
every = Page[ShelfOut].model_validate(
libraries.list_shelves(session, user.actor, copy.library_id, ALL, 0)
)
shelf_choices = [shelf for shelf in every.items if shelf.id not in on]
return templates.TemplateResponse(
request,
"copy.html",
Expand All @@ -210,11 +242,88 @@ def copy_detail(
"copy": copy,
"label": label,
"library": library,
"can_edit": allowed(user.actor, Action.LIBRARY_EDIT, LibraryResource(role)),
"can_edit": can_edit,
"tags": tags,
"tag_text": (
tag_text
if tag_text is not None
else ", ".join(tag.name for tag in tags)
),
"shelves": shelves,
"shelf_choices": shelf_choices,
"errors": errors or [],
},
status_code=status_code,
)


@ui_route(router, "GET", "/copies/{copy_id}", api="copies_get")
def copy_detail(
request: Request, copy_id: PathId, session: DbSession, user: CurrentUser
) -> Response:
"""Show a copy I can see, with its tags and shelves."""
copy = CopyOut.model_validate(libraries.get_copy(session, user.actor, copy_id))
return _detail(request, session, user, copy)


def _parse_tags(text: str) -> list[str]:
"""Split a comma list into tag names, dropping blanks and repeats."""
names: list[str] = []
for part in text.split(","):
name = part.strip()
if name and name not in names:
names.append(name)
return names


@ui_route(router, "POST", "/copies/{copy_id}/tags", api="copy_tags_set")
def set_copy_tags( # noqa: PLR0913
request: Request,
copy_id: PathId,
session: DbSession,
user: CurrentUser,
_csrf: None = CsrfChecked,
tags: FormText = "",
) -> Response:
"""Replace a copy's tags with the comma list from the form."""
names = _parse_tags(tags)
payload = None
errors: list[str] = []
if len(names) > MAX_TAGS:
errors = [f"Use at most {MAX_TAGS} tags."]
else:
try:
payload = TagNamesIn(names=names)
except ValidationError as error:
errors = problems(error, {"names": "the tags"})
if payload is None:
copy = CopyOut.model_validate(libraries.get_copy(session, user.actor, copy_id))
return _detail(
request,
session,
user,
copy,
tag_text=tags,
errors=errors,
status_code=422,
)
libraries.set_copy_tags(session, user.actor, copy_id, payload.names)
return RedirectResponse(f"/copies/{copy_id}", status_code=_SEE_OTHER)


@ui_route(router, "POST", "/copies/{copy_id}/shelves", api="shelf_copies_add")
def add_copy_to_shelf(
copy_id: PathId,
session: DbSession,
user: CurrentUser,
shelf_id: _ID,
_csrf: None = CsrfChecked,
) -> Response:
"""Put a copy on one of its library's shelves."""
libraries.add_to_shelf(session, user.actor, shelf_id, copy_id)
return RedirectResponse(f"/copies/{copy_id}", status_code=_SEE_OTHER)


@ui_route(router, "GET", "/copies/{copy_id}/edit", api="copies_get")
def edit_copy_form(
request: Request, copy_id: PathId, session: DbSession, user: CurrentUser
Expand Down
10 changes: 10 additions & 0 deletions src/librelibrary/ui/libraries.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
LibraryPatch,
MemberOut,
Page,
ShelfOut,
TagOut,
)
from librelibrary.services import libraries
from librelibrary.ui import ui_route
Expand Down Expand Up @@ -142,6 +144,12 @@ def library_detail(
)
)
labels = edition_labels(session, user.actor, [c.edition_id for c in copies.items])
shelves = Page[ShelfOut].model_validate(
libraries.list_shelves(session, user.actor, library_id, ALL, 0)
)
tags = Page[TagOut].model_validate(
libraries.list_tags(session, user.actor, library_id, ALL, 0)
)
return templates.TemplateResponse(
request,
"library.html",
Expand All @@ -153,6 +161,8 @@ def library_detail(
"can_manage": allowed(user.actor, Action.LIBRARY_MANAGE, resource),
"can_edit": allowed(user.actor, Action.LIBRARY_EDIT, resource),
"members": members,
"shelves": shelves,
"tags": tags,
"copies": copies,
"labels": labels,
"page": page,
Expand Down
Loading
Loading