diff --git a/alembic/versions/c7882cedc9ae_add_clamp_connections.py b/alembic/versions/c7882cedc9ae_add_clamp_connections.py new file mode 100644 index 0000000..5675c60 --- /dev/null +++ b/alembic/versions/c7882cedc9ae_add_clamp_connections.py @@ -0,0 +1,29 @@ +"""add clamp_connections + +Revision ID: c7882cedc9ae +Revises: bfbce324cd5f +Create Date: 2026-07-02 13:31:57.064381 +""" +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision: str = 'c7882cedc9ae' +down_revision: Union[str, None] = 'bfbce324cd5f' +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + pass + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + pass + # ### end Alembic commands ### diff --git a/app/api/clamp_connections.py b/app/api/clamp_connections.py new file mode 100644 index 0000000..a1982f2 --- /dev/null +++ b/app/api/clamp_connections.py @@ -0,0 +1,102 @@ +from fastapi import APIRouter, Depends, HTTPException, status +from sqlalchemy.ext.asyncio import AsyncSession + +from app.core.database import get_db +from app.crud import audit as audit_crud +from app.crud import clamp_connection as clamp_connection_crud +from app.deps import require_permission +from app.models.user import User +from app.schemas.clamp_connection import ( + ClampConnectionCreate, + ClampConnectionRead, + ClampConnectionUpdate, +) + +router = APIRouter() + + +@router.get("", response_model=list[ClampConnectionRead]) +async def list_clamp_connections( + skip: int = 0, + limit: int = 100, + db: AsyncSession = Depends(get_db), + _: User = Depends(require_permission("clamp_connections:read")), +): + return await clamp_connection_crud.list_all(db, skip=skip, limit=limit) + + +@router.post("", response_model=ClampConnectionRead, status_code=status.HTTP_201_CREATED) +async def create_clamp_connection( + payload: ClampConnectionCreate, + db: AsyncSession = Depends(get_db), + actor: User = Depends(require_permission("clamp_connections:write")), +): + cc = await clamp_connection_crud.create( + db, + min_mm=payload.min_mm, + max_mm=payload.max_mm, + min_in=payload.min_in, + max_in=payload.max_in, + ) + await audit_crud.log( + db, actor=actor, action="create", resource_type="ClampConnection", + resource_id=cc.id, + description=audit_crud.describe_create( + "ClampConnection", cc.id, + f"{cc.min_mm}–{cc.max_mm} mm" if cc.min_mm is not None else f"{cc.min_in}–{cc.max_in} in", + ), + ) + return cc + + +@router.get("/{clamp_connection_id}", response_model=ClampConnectionRead) +async def get_clamp_connection( + clamp_connection_id: int, + db: AsyncSession = Depends(get_db), + _: User = Depends(require_permission("clamp_connections:read")), +): + cc = await clamp_connection_crud.get(db, clamp_connection_id) + if cc is None: + raise HTTPException(status_code=404, detail="Clamp connection not found") + return cc + + +@router.patch("/{clamp_connection_id}", response_model=ClampConnectionRead) +async def update_clamp_connection( + clamp_connection_id: int, + payload: ClampConnectionUpdate, + db: AsyncSession = Depends(get_db), + actor: User = Depends(require_permission("clamp_connections:write")), +): + cc = await clamp_connection_crud.get(db, clamp_connection_id) + if cc is None: + raise HTTPException(status_code=404, detail="Clamp connection not found") + changes = payload.model_dump(exclude_unset=True) + cc = await clamp_connection_crud.update(db, cc, changes) + await audit_crud.log( + db, actor=actor, action="update", resource_type="ClampConnection", + resource_id=cc.id, + description=audit_crud.describe_update("ClampConnection", cc.id, changes), + ) + return cc + + +@router.delete("/{clamp_connection_id}", status_code=status.HTTP_204_NO_CONTENT) +async def delete_clamp_connection( + clamp_connection_id: int, + db: AsyncSession = Depends(get_db), + actor: User = Depends(require_permission("clamp_connections:delete")), +): + cc = await clamp_connection_crud.get(db, clamp_connection_id) + if cc is None: + raise HTTPException(status_code=404, detail="Clamp connection not found") + label = ( + f"{cc.min_mm}–{cc.max_mm} mm" if cc.min_mm is not None + else f"{cc.min_in}–{cc.max_in} in" + ) + await clamp_connection_crud.delete(db, cc) + await audit_crud.log( + db, actor=actor, action="delete", resource_type="ClampConnection", + resource_id=clamp_connection_id, + description=audit_crud.describe_delete("ClampConnection", clamp_connection_id, label), + ) diff --git a/app/api/router.py b/app/api/router.py index a9745ef..8e40377 100644 --- a/app/api/router.py +++ b/app/api/router.py @@ -1,6 +1,6 @@ from fastapi import APIRouter -from app.api import audit_log, augmented_parts, auth, imported_parts, iso_threads, manufacturers, part_colours, part_types, roles, scope_models, static_connections, sub_part_types, unc_threads, users +from app.api import audit_log, augmented_parts, auth, clamp_connections, imported_parts, iso_threads, manufacturers, part_colours, part_types, roles, scope_models, static_connections, sub_part_types, unc_threads, users api_router = APIRouter() api_router.include_router(auth.router, prefix="/auth", tags=["auth"]) @@ -14,6 +14,7 @@ api_router.include_router(part_colours.router, prefix="/part-colours", tags=["pa api_router.include_router(part_types.router, prefix="/part-types", tags=["part-types"]) api_router.include_router(sub_part_types.router, prefix="/sub-part-types", tags=["sub-part-types"]) api_router.include_router(static_connections.router, prefix="/static-connections", tags=["static-connections"]) +api_router.include_router(clamp_connections.router, prefix="/clamp-connections", tags=["clamp-connections"]) api_router.include_router(imported_parts.router, prefix="/imported-parts", tags=["imported-parts"]) api_router.include_router(augmented_parts.router, prefix="/augmented-parts", tags=["augmented-parts"]) api_router.include_router(audit_log.router, prefix="/audit-log", tags=["audit-log"]) diff --git a/app/crud/clamp_connection.py b/app/crud/clamp_connection.py new file mode 100644 index 0000000..f6e3782 --- /dev/null +++ b/app/crud/clamp_connection.py @@ -0,0 +1,50 @@ +from sqlalchemy import select +from sqlalchemy.ext.asyncio import AsyncSession + +from app.models.clamp_connection import ClampConnection + + +async def get(db: AsyncSession, clamp_connection_id: int) -> ClampConnection | None: + return await db.get(ClampConnection, clamp_connection_id) + + +async def list_all( + db: AsyncSession, skip: int = 0, limit: int = 100 +) -> list[ClampConnection]: + result = await db.execute( + select(ClampConnection) + .order_by(ClampConnection.min_mm.nulls_last(), ClampConnection.min_in.nulls_last()) + .offset(skip) + .limit(limit) + ) + return list(result.scalars().all()) + + +async def create( + db: AsyncSession, + *, + min_mm: float | None, + max_mm: float | None, + min_in: float | None, + max_in: float | None, +) -> ClampConnection: + cc = ClampConnection(min_mm=min_mm, max_mm=max_mm, min_in=min_in, max_in=max_in) + db.add(cc) + await db.commit() + await db.refresh(cc) + return cc + + +async def update( + db: AsyncSession, cc: ClampConnection, data: dict +) -> ClampConnection: + for field, value in data.items(): + setattr(cc, field, value) + await db.commit() + await db.refresh(cc) + return cc + + +async def delete(db: AsyncSession, cc: ClampConnection) -> None: + await db.delete(cc) + await db.commit() diff --git a/app/models/__init__.py b/app/models/__init__.py index 707a402..a4e1f8f 100644 --- a/app/models/__init__.py +++ b/app/models/__init__.py @@ -3,6 +3,7 @@ from app.models.associations import augmented_part_imported_parts, role_permissions, user_roles from app.models.audit_log import AuditLog from app.models.augmented_part import AugmentedPart +from app.models.clamp_connection import ClampConnection from app.models.imported_part import ImportedPart from app.models.imported_part_config import ImportedPartConfig from app.models.iso_thread import IsoThread @@ -21,6 +22,7 @@ from app.models.user import User __all__ = [ "AuditLog", "AugmentedPart", + "ClampConnection", "ImportedPart", "ImportedPartConfig", "IsoThread", diff --git a/app/models/clamp_connection.py b/app/models/clamp_connection.py new file mode 100644 index 0000000..cede488 --- /dev/null +++ b/app/models/clamp_connection.py @@ -0,0 +1,14 @@ +from sqlalchemy import Float +from sqlalchemy.orm import Mapped, mapped_column + +from app.core.database import Base + + +class ClampConnection(Base): + __tablename__ = "clamp_connections" + + id: Mapped[int] = mapped_column(primary_key=True) + min_mm: Mapped[float | None] = mapped_column(Float, nullable=True, default=None) + max_mm: Mapped[float | None] = mapped_column(Float, nullable=True, default=None) + min_in: Mapped[float | None] = mapped_column(Float, nullable=True, default=None) + max_in: Mapped[float | None] = mapped_column(Float, nullable=True, default=None) diff --git a/app/schemas/clamp_connection.py b/app/schemas/clamp_connection.py new file mode 100644 index 0000000..3acac8f --- /dev/null +++ b/app/schemas/clamp_connection.py @@ -0,0 +1,21 @@ +from pydantic import BaseModel, ConfigDict + + +class ClampConnectionBase(BaseModel): + min_mm: float | None = None + max_mm: float | None = None + min_in: float | None = None + max_in: float | None = None + + +class ClampConnectionCreate(ClampConnectionBase): + pass + + +class ClampConnectionUpdate(ClampConnectionBase): + pass + + +class ClampConnectionRead(ClampConnectionBase): + model_config = ConfigDict(from_attributes=True) + id: int diff --git a/app/templates/_nav.html b/app/templates/_nav.html index 97225e0..53624a6 100644 --- a/app/templates/_nav.html +++ b/app/templates/_nav.html @@ -41,6 +41,9 @@ {% if user.is_superuser or "static_connections:read" in user.permissions %} Static Connections {% endif %} + {% if user.is_superuser or "clamp_connections:read" in user.permissions %} + Clamp Connections + {% endif %} {% if user.is_superuser or "audit_log:read" in user.permissions %} Audit Log {% endif %} diff --git a/app/templates/admin/_clamp_connection_row.html b/app/templates/admin/_clamp_connection_row.html new file mode 100644 index 0000000..53a6623 --- /dev/null +++ b/app/templates/admin/_clamp_connection_row.html @@ -0,0 +1,18 @@ + + {{ cc.id }} + {{ cc.min_mm if cc.min_mm is not none else '—' }} + {{ cc.max_mm if cc.max_mm is not none else '—' }} + {{ cc.min_in if cc.min_in is not none else '—' }} + {{ cc.max_in if cc.max_in is not none else '—' }} + + + + + diff --git a/app/templates/admin/_clamp_connection_row_edit.html b/app/templates/admin/_clamp_connection_row_edit.html new file mode 100644 index 0000000..3d8100b --- /dev/null +++ b/app/templates/admin/_clamp_connection_row_edit.html @@ -0,0 +1,29 @@ + + +
+ #{{ cc.id }} + + + + +
+ + +
+
+ + diff --git a/app/templates/admin/_clamp_connections_table.html b/app/templates/admin/_clamp_connections_table.html new file mode 100644 index 0000000..f665530 --- /dev/null +++ b/app/templates/admin/_clamp_connections_table.html @@ -0,0 +1,21 @@ +
+ + + + + + + + + + + + + {% for cc in clamp_connections %} + {% include "admin/_clamp_connection_row.html" %} + {% else %} + + {% endfor %} + +
IDMin (mm)Max (mm)Min (in)Max (in)
No clamp connections recorded.
+
diff --git a/app/templates/admin/clamp_connections.html b/app/templates/admin/clamp_connections.html new file mode 100644 index 0000000..1e56f48 --- /dev/null +++ b/app/templates/admin/clamp_connections.html @@ -0,0 +1,21 @@ +{% extends "base.html" %} +{% block title %}Clamp Connections · {{ app_name }}{% endblock %} +{% block body %} +{% include "_nav.html" %} +
+

Clamp Connections

+ +
+

Add connection

+
+ + + + + +
+
+ + {% include "admin/_clamp_connections_table.html" %} +
+{% endblock %} diff --git a/app/web/admin.py b/app/web/admin.py index 97ecaf6..31bbc3a 100644 --- a/app/web/admin.py +++ b/app/web/admin.py @@ -15,6 +15,7 @@ from app.crud import part_colour as part_colour_crud from app.crud import part_type as part_type_crud from app.crud import role as role_crud from app.crud import scope_model as scope_model_crud +from app.crud import clamp_connection as clamp_connection_crud from app.crud import static_connection as static_connection_crud from app.crud import sub_part_type as sub_part_type_crud from app.crud import unc_thread as unc_thread_crud @@ -1604,6 +1605,141 @@ async def delete_static_connection( return Response(status_code=status.HTTP_200_OK) +# --- Clamp Connections ------------------------------------------------------- + + +@router.get("/clamp-connections", response_class=HTMLResponse) +async def clamp_connections_page( + request: Request, + db: AsyncSession = Depends(get_db), + user: User = Depends(web_require("clamp_connections:read")), +): + return templates.TemplateResponse( + request, + "admin/clamp_connections.html", + {"user": user, "clamp_connections": await clamp_connection_crud.list_all(db)}, + ) + + +@router.post("/clamp-connections", response_class=HTMLResponse) +async def create_clamp_connection( + request: Request, + min_mm: str = Form(""), + max_mm: str = Form(""), + min_in: str = Form(""), + max_in: str = Form(""), + db: AsyncSession = Depends(get_db), + actor: User = Depends(web_require("clamp_connections:write")), +): + cc = await clamp_connection_crud.create( + db, + min_mm=float(min_mm) if min_mm else None, + max_mm=float(max_mm) if max_mm else None, + min_in=float(min_in) if min_in else None, + max_in=float(max_in) if max_in else None, + ) + label = ( + f"{cc.min_mm}–{cc.max_mm} mm" if cc.min_mm is not None + else f"{cc.min_in}–{cc.max_in} in" + ) + await audit_crud.log( + db, actor=actor, action="create", resource_type="ClampConnection", + resource_id=cc.id, + description=audit_crud.describe_create("ClampConnection", cc.id, label), + ) + return templates.TemplateResponse( + request, + "admin/_clamp_connections_table.html", + {"clamp_connections": await clamp_connection_crud.list_all(db)}, + ) + + +@router.get("/clamp-connections/{clamp_connection_id}/edit", response_class=HTMLResponse) +async def edit_clamp_connection_row( + request: Request, + clamp_connection_id: int, + db: AsyncSession = Depends(get_db), + _: User = Depends(web_require("clamp_connections:write")), +): + cc = await clamp_connection_crud.get(db, clamp_connection_id) + if cc is None: + raise HTTPException(status_code=404, detail="Clamp connection not found") + return templates.TemplateResponse( + request, "admin/_clamp_connection_row_edit.html", {"cc": cc} + ) + + +@router.get("/clamp-connections/{clamp_connection_id}/row", response_class=HTMLResponse) +async def clamp_connection_row( + request: Request, + clamp_connection_id: int, + db: AsyncSession = Depends(get_db), + _: User = Depends(web_require("clamp_connections:read")), +): + cc = await clamp_connection_crud.get(db, clamp_connection_id) + if cc is None: + raise HTTPException(status_code=404, detail="Clamp connection not found") + return templates.TemplateResponse( + request, "admin/_clamp_connection_row.html", {"cc": cc} + ) + + +@router.post("/clamp-connections/{clamp_connection_id}", response_class=HTMLResponse) +async def update_clamp_connection( + request: Request, + clamp_connection_id: int, + min_mm: str = Form(""), + max_mm: str = Form(""), + min_in: str = Form(""), + max_in: str = Form(""), + db: AsyncSession = Depends(get_db), + actor: User = Depends(web_require("clamp_connections:write")), +): + cc = await clamp_connection_crud.get(db, clamp_connection_id) + if cc is None: + raise HTTPException(status_code=404, detail="Clamp connection not found") + changes = { + "min_mm": float(min_mm) if min_mm else None, + "max_mm": float(max_mm) if max_mm else None, + "min_in": float(min_in) if min_in else None, + "max_in": float(max_in) if max_in else None, + } + await clamp_connection_crud.update(db, cc, changes) + await audit_crud.log( + db, actor=actor, action="update", resource_type="ClampConnection", + resource_id=clamp_connection_id, + description=audit_crud.describe_update( + "ClampConnection", clamp_connection_id, changes + ), + ) + return templates.TemplateResponse( + request, "admin/_clamp_connection_row.html", {"cc": cc} + ) + + +@router.delete("/clamp-connections/{clamp_connection_id}") +async def delete_clamp_connection( + clamp_connection_id: int, + db: AsyncSession = Depends(get_db), + actor: User = Depends(web_require("clamp_connections:delete")), +): + cc = await clamp_connection_crud.get(db, clamp_connection_id) + if cc is not None: + label = ( + f"{cc.min_mm}–{cc.max_mm} mm" if cc.min_mm is not None + else f"{cc.min_in}–{cc.max_in} in" + ) + await clamp_connection_crud.delete(db, cc) + await audit_crud.log( + db, actor=actor, action="delete", resource_type="ClampConnection", + resource_id=clamp_connection_id, + description=audit_crud.describe_delete( + "ClampConnection", clamp_connection_id, label + ), + ) + return Response(status_code=status.HTTP_200_OK) + + # --- Audit Log --------------------------------------------------------------- diff --git a/scripts/seed.py b/scripts/seed.py index 8081f37..67484a6 100644 --- a/scripts/seed.py +++ b/scripts/seed.py @@ -56,6 +56,9 @@ PERMISSIONS = [ ("static_connections:read", "View static connections"), ("static_connections:write", "Create and update static connections"), ("static_connections:delete", "Delete static connections"), + ("clamp_connections:read", "View clamp connections"), + ("clamp_connections:write", "Create and update clamp connections"), + ("clamp_connections:delete", "Delete clamp connections"), ("augmented_parts:read", "View augmented parts"), ("augmented_parts:write", "Create and update augmented parts"), ("augmented_parts:delete", "Delete augmented parts"), @@ -65,7 +68,7 @@ PERMISSIONS = [ # Default roles -> permission names. "admin" gets everything. ROLES = { "admin": [name for name, _ in PERMISSIONS], # includes audit_log:read - "user": ["users:read", "iso_threads:read", "unc_threads:read", "manufacturers:read", "scope_models:read", "part_colours:read", "part_types:read", "sub_part_types:read", "imported_parts:read", "static_connections:read", "augmented_parts:read"], + "user": ["users:read", "iso_threads:read", "unc_threads:read", "manufacturers:read", "scope_models:read", "part_colours:read", "part_types:read", "sub_part_types:read", "imported_parts:read", "static_connections:read", "clamp_connections:read", "augmented_parts:read"], }