Skip to content

webapp

Webapp router.

This module contains the web routes for the tool inventory application. It provides endpoints for creating, reading, updating, and deleting tools, as well as updating tool quantities.

web_create_tool async

web_create_tool(name: Annotated[str, Form()], description: Annotated[str, Form()], quantity: Annotated[int, Form()]) -> RedirectResponse

Create a new tool.

Parameters:

Name Type Description Default
name Annotated[str, Form()]

The name of the tool.

required
description Annotated[str, Form()]

The description of the tool.

required
quantity Annotated[int, Form()]

The quantity of the tool.

required

Returns:

Type Description
RedirectResponse

A redirect response to the home page.

Source code in src/tool_inventory/routers/webapp.py
@router.post("/create")
async def web_create_tool(
    name: Annotated[str, Form()],
    description: Annotated[str, Form()],
    quantity: Annotated[int, Form()],
) -> RedirectResponse:
    """Create a new tool.

    Args:
        name: The name of the tool.
        description: The description of the tool.
        quantity: The quantity of the tool.

    Returns:
        A redirect response to the home page.
    """
    with Session(engine) as session:
        db = Database(session)
        db.create_tool(
            ToolCreate(
                name=name,
                description=description,
                quantity=quantity,
            ).to_model(),
        )
    return RedirectResponse("/", status_code=status.HTTP_303_SEE_OTHER)

web_create_tool_form async

web_create_tool_form(request: Request) -> HTMLResponse

Render the tool creation form.

Parameters:

Name Type Description Default
request Request

The request object.

required

Returns:

Type Description
HTMLResponse

An HTML response with the tool creation form.

Source code in src/tool_inventory/routers/webapp.py
@router.get("/create")
async def web_create_tool_form(
    request: Request,
) -> HTMLResponse:
    """Render the tool creation form.

    Args:
        request: The request object.

    Returns:
        An HTML response with the tool creation form.
    """
    return templates.TemplateResponse(
        "tool_form.html",
        {"request": request},
    )

web_delete_tool async

web_delete_tool(request: Request, tool_id: UUID) -> HTMLResponse

Delete a tool.

Parameters:

Name Type Description Default
request Request

The request object.

required
tool_id UUID

The UUID of the tool to delete.

required

Returns:

Type Description
HTMLResponse

A script to delete the tool.

Source code in src/tool_inventory/routers/webapp.py
@router.post("/delete/{tool_id}")
async def web_delete_tool(
    request: Request,
    tool_id: UUID,
) -> HTMLResponse:
    """Delete a tool.

    Args:
        request: The request object.
        tool_id: The UUID of the tool to delete.

    Returns:
        A script to delete the tool.
    """
    with Session(engine) as session:
        db = Database(session)
        db.delete_tool(tool_id)
    return templates.TemplateResponse(
        "delete_tool.html",
        {"request": request, "tool_id": tool_id},
    )

web_edit_tool async

web_edit_tool(tool_id: UUID, name: Annotated[str, Form()], description: Annotated[str, Form()], quantity: Annotated[int, Form()]) -> RedirectResponse

Edit an existing tool.

Parameters:

Name Type Description Default
tool_id UUID

The UUID of the tool to edit.

required
name Annotated[str, Form()]

The new name of the tool.

required
description Annotated[str, Form()]

The new description of the tool.

required
quantity Annotated[int, Form()]

The new quantity of the tool.

required

Returns:

Type Description
RedirectResponse

A redirect response to the home page.

Source code in src/tool_inventory/routers/webapp.py
@router.post("/edit/{tool_id}")
async def web_edit_tool(
    tool_id: UUID,
    name: Annotated[str, Form()],
    description: Annotated[str, Form()],
    quantity: Annotated[int, Form()],
) -> RedirectResponse:
    """Edit an existing tool.

    Args:
        tool_id: The UUID of the tool to edit.
        name: The new name of the tool.
        description: The new description of the tool.
        quantity: The new quantity of the tool.

    Returns:
        A redirect response to the home page.
    """
    with Session(engine) as session:
        db = Database(session)
        db.update_tool(
            ToolPatch(
                name=name,
                description=description,
                quantity=quantity,
            ).patch(db.get_tool_by_id(tool_id)),
        )
    return RedirectResponse("/", status_code=status.HTTP_303_SEE_OTHER)

web_edit_tool_form async

web_edit_tool_form(request: Request, tool_id: UUID) -> HTMLResponse

Render the tool edit form.

Parameters:

Name Type Description Default
request Request

The request object.

required
tool_id UUID

The UUID of the tool to edit.

required

Returns:

Type Description
HTMLResponse

An HTML response with the tool edit form.

Source code in src/tool_inventory/routers/webapp.py
@router.get("/edit/{tool_id}")
async def web_edit_tool_form(
    request: Request,
    tool_id: UUID,
) -> HTMLResponse:
    """Render the tool edit form.

    Args:
        request: The request object.
        tool_id: The UUID of the tool to edit.

    Returns:
        An HTML response with the tool edit form.
    """
    with Session(engine) as session:
        db = Database(session)
        return templates.TemplateResponse(
            "tool_form.html",
            {"request": request, "tool": db.get_tool_by_id(tool_id)},
        )

web_read_tools async

web_read_tools(request: Request) -> HTMLResponse

Fetch and display all tools.

Parameters:

Name Type Description Default
request Request

The request object.

required

Returns:

Type Description
HTMLResponse

An HTML response with the list of tools.

Source code in src/tool_inventory/routers/webapp.py
@router.get("/")
async def web_read_tools(
    request: Request,
) -> HTMLResponse:
    """Fetch and display all tools.

    Args:
        request: The request object.

    Returns:
        An HTML response with the list of tools.
    """
    with Session(engine) as session:
        db = Database(session)
        tools = db.get_tools()
        return templates.TemplateResponse(
            "index.html",
            {"request": request, "tools": tools},
        )

web_search_tools async

web_search_tools(request: Request, query: str) -> HTMLResponse

Search for tools.

Parameters:

Name Type Description Default
request Request

The request object.

required
query str

The search query.

required

Returns:

Type Description
HTMLResponse

An HTML response with the search results.

Source code in src/tool_inventory/routers/webapp.py
@router.get("/search")
async def web_search_tools(
    request: Request,
    query: str,
) -> HTMLResponse:
    """Search for tools.

    Args:
        request: The request object.
        query: The search query.

    Returns:
        An HTML response with the search results.
    """
    with Session(engine) as session:
        db = Database(session)
        tools = db.search_tools(query)
        return templates.TemplateResponse(
            "index.html",
            {"request": request, "query": query, "tools": tools},
        )

web_update_quantity async

web_update_quantity(request: Request, tool_id: UUID, action: Annotated[str, Form()]) -> HTMLResponse

Update the quantity of a tool.

Parameters:

Name Type Description Default
request Request

The request object.

required
tool_id UUID

The UUID of the tool to update.

required
action Annotated[str, Form()]

The action to perform (increment or decrement).

required

Returns:

Type Description
HTMLResponse

A script to update quantity.

Source code in src/tool_inventory/routers/webapp.py
@router.post("/update_quantity/{tool_id}")
async def web_update_quantity(
    request: Request,
    tool_id: UUID,
    action: Annotated[str, Form()],
) -> HTMLResponse:
    """Update the quantity of a tool.

    Args:
        request: The request object.
        tool_id: The UUID of the tool to update.
        action: The action to perform (increment or decrement).

    Returns:
        A script to update quantity.
    """
    with Session(engine) as session:
        db = Database(session)
        tool = db.get_tool_by_id(tool_id)
        db.update_tool(
            ToolPatch(
                quantity=max(
                    0,
                    tool.quantity + 1 if action == "increment" else tool.quantity - 1,
                ),
            ).patch(tool),
        )
    return templates.TemplateResponse(
        "update_quantity.html",
        {"request": request, "tool_id": tool.id, "quantity": tool.quantity},
    )