adding static connection
This commit is contained in:
1 parent
eba7896893
commit
422e217180
14 files changed
+405
-2
No files matched your search
+2
-1
@@ -1,6 +1,6 @@
|
||||
from fastapi import APIRouter
|
||||
|
||||
from app.api import audit_log, auth, imported_parts, iso_threads, manufacturers, part_types, roles, scope_models, sub_part_types, unc_threads, users
|
||||
from app.api import audit_log, auth, imported_parts, iso_threads, manufacturers, 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"])
|
||||
@@ -12,5 +12,6 @@ api_router.include_router(manufacturers.router, prefix="/manufacturers", tags=["
|
||||
api_router.include_router(scope_models.router, prefix="/scope-models", tags=["scope-models"])
|
||||
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(imported_parts.router, prefix="/imported-parts", tags=["imported-parts"])
|
||||
api_router.include_router(audit_log.router, prefix="/audit-log", tags=["audit-log"])
|
||||
@@ -0,0 +1,92 @@
|
||||
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 static_connection as static_connection_crud
|
||||
from app.deps import require_permission
|
||||
from app.models.user import User
|
||||
from app.schemas.static_connection import (
|
||||
StaticConnectionCreate,
|
||||
StaticConnectionRead,
|
||||
StaticConnectionUpdate,
|
||||
)
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get("", response_model=list[StaticConnectionRead])
|
||||
async def list_static_connections(
|
||||
skip: int = 0,
|
||||
limit: int = 100,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
_: User = Depends(require_permission("static_connections:read")),
|
||||
):
|
||||
return await static_connection_crud.list_all(db, skip=skip, limit=limit)
|
||||
|
||||
|
||||
@router.post("", response_model=StaticConnectionRead, status_code=status.HTTP_201_CREATED)
|
||||
async def create_static_connection(
|
||||
payload: StaticConnectionCreate,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
actor: User = Depends(require_permission("static_connections:write")),
|
||||
):
|
||||
sc = await static_connection_crud.create(db, od_mm=payload.od_mm, od_in=payload.od_in)
|
||||
await audit_crud.log(
|
||||
db, actor=actor, action="create", resource_type="StaticConnection",
|
||||
resource_id=sc.id,
|
||||
description=audit_crud.describe_create("StaticConnection", sc.id, f"{sc.od_mm} mm"),
|
||||
)
|
||||
return sc
|
||||
|
||||
|
||||
@router.get("/{static_connection_id}", response_model=StaticConnectionRead)
|
||||
async def get_static_connection(
|
||||
static_connection_id: int,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
_: User = Depends(require_permission("static_connections:read")),
|
||||
):
|
||||
sc = await static_connection_crud.get(db, static_connection_id)
|
||||
if sc is None:
|
||||
raise HTTPException(status_code=404, detail="Static connection not found")
|
||||
return sc
|
||||
|
||||
|
||||
@router.patch("/{static_connection_id}", response_model=StaticConnectionRead)
|
||||
async def update_static_connection(
|
||||
static_connection_id: int,
|
||||
payload: StaticConnectionUpdate,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
actor: User = Depends(require_permission("static_connections:write")),
|
||||
):
|
||||
sc = await static_connection_crud.get(db, static_connection_id)
|
||||
if sc is None:
|
||||
raise HTTPException(status_code=404, detail="Static connection not found")
|
||||
changes = payload.model_dump(exclude_unset=True)
|
||||
sc = await static_connection_crud.update(db, sc, changes)
|
||||
await audit_crud.log(
|
||||
db, actor=actor, action="update", resource_type="StaticConnection",
|
||||
resource_id=sc.id,
|
||||
description=audit_crud.describe_update("StaticConnection", sc.id, changes),
|
||||
)
|
||||
return sc
|
||||
|
||||
|
||||
@router.delete("/{static_connection_id}", status_code=status.HTTP_204_NO_CONTENT)
|
||||
async def delete_static_connection(
|
||||
static_connection_id: int,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
actor: User = Depends(require_permission("static_connections:delete")),
|
||||
):
|
||||
sc = await static_connection_crud.get(db, static_connection_id)
|
||||
if sc is None:
|
||||
raise HTTPException(status_code=404, detail="Static connection not found")
|
||||
od_mm = sc.od_mm
|
||||
await static_connection_crud.delete(db, sc)
|
||||
await audit_crud.log(
|
||||
db, actor=actor, action="delete", resource_type="StaticConnection",
|
||||
resource_id=static_connection_id,
|
||||
description=audit_crud.describe_delete(
|
||||
"StaticConnection", static_connection_id, f"{od_mm} mm"
|
||||
),
|
||||
)
|
||||
@@ -0,0 +1,52 @@
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.models.static_connection import StaticConnection
|
||||
|
||||
|
||||
async def get(db: AsyncSession, static_connection_id: int) -> StaticConnection | None:
|
||||
return await db.get(StaticConnection, static_connection_id)
|
||||
|
||||
|
||||
async def get_by_od_mm(db: AsyncSession, od_mm: float) -> StaticConnection | None:
|
||||
result = await db.execute(
|
||||
select(StaticConnection).where(StaticConnection.od_mm == od_mm)
|
||||
)
|
||||
return result.scalar_one_or_none()
|
||||
|
||||
|
||||
async def list_all(
|
||||
db: AsyncSession, skip: int = 0, limit: int = 100
|
||||
) -> list[StaticConnection]:
|
||||
result = await db.execute(
|
||||
select(StaticConnection).order_by(StaticConnection.od_mm).offset(skip).limit(limit)
|
||||
)
|
||||
return list(result.scalars().all())
|
||||
|
||||
|
||||
async def create(
|
||||
db: AsyncSession,
|
||||
*,
|
||||
od_mm: float,
|
||||
od_in: float,
|
||||
) -> StaticConnection:
|
||||
sc = StaticConnection(od_mm=od_mm, od_in=od_in)
|
||||
db.add(sc)
|
||||
await db.commit()
|
||||
await db.refresh(sc)
|
||||
return sc
|
||||
|
||||
|
||||
async def update(
|
||||
db: AsyncSession, sc: StaticConnection, data: dict
|
||||
) -> StaticConnection:
|
||||
for field, value in data.items():
|
||||
setattr(sc, field, value)
|
||||
await db.commit()
|
||||
await db.refresh(sc)
|
||||
return sc
|
||||
|
||||
|
||||
async def delete(db: AsyncSession, sc: StaticConnection) -> None:
|
||||
await db.delete(sc)
|
||||
await db.commit()
|
||||
@@ -9,6 +9,7 @@ from app.models.manufacturer import Manufacturer
|
||||
from app.models.part_type import PartType
|
||||
from app.models.permission import Permission
|
||||
from app.models.scope_model import ScopeModel
|
||||
from app.models.static_connection import StaticConnection
|
||||
from app.models.sub_part_type import SubPartType
|
||||
from app.models.refresh_token import RefreshToken
|
||||
from app.models.role import Role
|
||||
@@ -24,6 +25,7 @@ __all__ = [
|
||||
"PartType",
|
||||
"Permission",
|
||||
"ScopeModel",
|
||||
"StaticConnection",
|
||||
"SubPartType",
|
||||
"RefreshToken",
|
||||
"Role",
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
from sqlalchemy import Float, UniqueConstraint
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
|
||||
from app.core.database import Base
|
||||
|
||||
|
||||
class StaticConnection(Base):
|
||||
__tablename__ = "static_connections"
|
||||
__table_args__ = (UniqueConstraint("od_mm"),)
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
od_mm: Mapped[float] = mapped_column(Float, nullable=False)
|
||||
od_in: Mapped[float] = mapped_column(Float, nullable=False)
|
||||
@@ -0,0 +1,20 @@
|
||||
from pydantic import BaseModel, ConfigDict
|
||||
|
||||
|
||||
class StaticConnectionBase(BaseModel):
|
||||
od_mm: float
|
||||
od_in: float
|
||||
|
||||
|
||||
class StaticConnectionCreate(StaticConnectionBase):
|
||||
pass
|
||||
|
||||
|
||||
class StaticConnectionUpdate(BaseModel):
|
||||
od_mm: float | None = None
|
||||
od_in: float | None = None
|
||||
|
||||
|
||||
class StaticConnectionRead(StaticConnectionBase):
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
id: int
|
||||
@@ -29,6 +29,9 @@
|
||||
{% if user.is_superuser or "imported_parts:read" in user.permissions %}
|
||||
<a href="/admin/imported-parts">Imported Parts</a>
|
||||
{% endif %}
|
||||
{% if user.is_superuser or "static_connections:read" in user.permissions %}
|
||||
<a href="/admin/static-connections">Static Connections</a>
|
||||
{% endif %}
|
||||
{% if user.is_superuser or "audit_log:read" in user.permissions %}
|
||||
<a href="/admin/audit-log">Audit Log</a>
|
||||
{% endif %}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
<tr id="static-connection-{{ sc.id }}">
|
||||
<td>{{ sc.id }}</td>
|
||||
<td>{{ sc.od_mm }}</td>
|
||||
<td>{{ sc.od_in }}</td>
|
||||
<td class="row-actions">
|
||||
<button class="btn btn-ghost"
|
||||
hx-get="/admin/static-connections/{{ sc.id }}/edit"
|
||||
hx-target="#static-connection-{{ sc.id }}"
|
||||
hx-swap="outerHTML">Edit</button>
|
||||
<button class="btn btn-danger"
|
||||
hx-delete="/admin/static-connections/{{ sc.id }}"
|
||||
hx-target="#static-connection-{{ sc.id }}"
|
||||
hx-swap="outerHTML"
|
||||
hx-confirm="Delete {{ sc.od_mm }} mm?">Delete</button>
|
||||
</td>
|
||||
</tr>
|
||||
@@ -0,0 +1,19 @@
|
||||
<tr id="static-connection-{{ sc.id }}">
|
||||
<td colspan="4">
|
||||
<form hx-post="/admin/static-connections/{{ sc.id }}"
|
||||
hx-target="#static-connection-{{ sc.id }}"
|
||||
hx-swap="outerHTML"
|
||||
class="inline-edit">
|
||||
<span class="muted">#{{ sc.id }}</span>
|
||||
<label>OD (mm) <input type="number" name="od_mm" step="0.001" min="0" value="{{ sc.od_mm }}" required></label>
|
||||
<label>OD (in) <input type="number" name="od_in" step="0.0001" min="0" value="{{ sc.od_in }}" required></label>
|
||||
<div class="row-actions">
|
||||
<button type="submit" class="btn btn-primary">Save</button>
|
||||
<button type="button" class="btn btn-ghost"
|
||||
hx-get="/admin/static-connections/{{ sc.id }}/row"
|
||||
hx-target="#static-connection-{{ sc.id }}"
|
||||
hx-swap="outerHTML">Cancel</button>
|
||||
</div>
|
||||
</form>
|
||||
</td>
|
||||
</tr>
|
||||
@@ -0,0 +1,19 @@
|
||||
<div id="static-connection-list" class="card">
|
||||
<table class="table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>ID</th>
|
||||
<th>OD (mm)</th>
|
||||
<th>OD (in)</th>
|
||||
<th></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for sc in static_connections %}
|
||||
{% include "admin/_static_connection_row.html" %}
|
||||
{% else %}
|
||||
<tr><td colspan="4" class="muted">No static connections recorded.</td></tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
@@ -0,0 +1,19 @@
|
||||
{% extends "base.html" %}
|
||||
{% block title %}Static Connections · {{ app_name }}{% endblock %}
|
||||
{% block body %}
|
||||
{% include "_nav.html" %}
|
||||
<main class="container">
|
||||
<h1>Static Connections</h1>
|
||||
|
||||
<div class="card">
|
||||
<h2>Add connection</h2>
|
||||
<form hx-post="/admin/static-connections" hx-target="#static-connection-list" hx-swap="outerHTML" class="form-grid">
|
||||
<label>OD (mm) <input type="number" name="od_mm" step="0.001" min="0" placeholder="e.g. 30.0" required></label>
|
||||
<label>OD (in) <input type="number" name="od_in" step="0.0001" min="0" placeholder="e.g. 1.1811" required></label>
|
||||
<button type="submit" class="btn btn-primary">Add</button>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
{% include "admin/_static_connections_table.html" %}
|
||||
</main>
|
||||
{% endblock %}
|
||||
@@ -13,6 +13,7 @@ from app.crud import manufacturer as manufacturer_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 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
|
||||
from app.crud import user as user_crud
|
||||
@@ -1117,6 +1118,120 @@ async def refresh_imported_parts(
|
||||
)
|
||||
|
||||
|
||||
# --- Static Connections -------------------------------------------------------
|
||||
|
||||
|
||||
@router.get("/static-connections", response_class=HTMLResponse)
|
||||
async def static_connections_page(
|
||||
request: Request,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
user: User = Depends(web_require("static_connections:read")),
|
||||
):
|
||||
return templates.TemplateResponse(
|
||||
request,
|
||||
"admin/static_connections.html",
|
||||
{"user": user, "static_connections": await static_connection_crud.list_all(db)},
|
||||
)
|
||||
|
||||
|
||||
@router.post("/static-connections", response_class=HTMLResponse)
|
||||
async def create_static_connection(
|
||||
request: Request,
|
||||
od_mm: float = Form(...),
|
||||
od_in: float = Form(...),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
actor: User = Depends(web_require("static_connections:write")),
|
||||
):
|
||||
if not await static_connection_crud.get_by_od_mm(db, od_mm):
|
||||
created = await static_connection_crud.create(db, od_mm=od_mm, od_in=od_in)
|
||||
await audit_crud.log(
|
||||
db, actor=actor, action="create", resource_type="StaticConnection",
|
||||
resource_id=created.id,
|
||||
description=audit_crud.describe_create(
|
||||
"StaticConnection", created.id, f"{created.od_mm} mm"
|
||||
),
|
||||
)
|
||||
return templates.TemplateResponse(
|
||||
request,
|
||||
"admin/_static_connections_table.html",
|
||||
{"static_connections": await static_connection_crud.list_all(db)},
|
||||
)
|
||||
|
||||
|
||||
@router.get("/static-connections/{static_connection_id}/edit", response_class=HTMLResponse)
|
||||
async def edit_static_connection_row(
|
||||
request: Request,
|
||||
static_connection_id: int,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
_: User = Depends(web_require("static_connections:write")),
|
||||
):
|
||||
sc = await static_connection_crud.get(db, static_connection_id)
|
||||
if sc is None:
|
||||
raise HTTPException(status_code=404, detail="Static connection not found")
|
||||
return templates.TemplateResponse(
|
||||
request, "admin/_static_connection_row_edit.html", {"sc": sc}
|
||||
)
|
||||
|
||||
|
||||
@router.get("/static-connections/{static_connection_id}/row", response_class=HTMLResponse)
|
||||
async def static_connection_row(
|
||||
request: Request,
|
||||
static_connection_id: int,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
_: User = Depends(web_require("static_connections:read")),
|
||||
):
|
||||
sc = await static_connection_crud.get(db, static_connection_id)
|
||||
if sc is None:
|
||||
raise HTTPException(status_code=404, detail="Static connection not found")
|
||||
return templates.TemplateResponse(
|
||||
request, "admin/_static_connection_row.html", {"sc": sc}
|
||||
)
|
||||
|
||||
|
||||
@router.post("/static-connections/{static_connection_id}", response_class=HTMLResponse)
|
||||
async def update_static_connection(
|
||||
request: Request,
|
||||
static_connection_id: int,
|
||||
od_mm: float = Form(...),
|
||||
od_in: float = Form(...),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
actor: User = Depends(web_require("static_connections:write")),
|
||||
):
|
||||
sc = await static_connection_crud.get(db, static_connection_id)
|
||||
if sc is None:
|
||||
raise HTTPException(status_code=404, detail="Static connection not found")
|
||||
changes = {"od_mm": od_mm, "od_in": od_in}
|
||||
await static_connection_crud.update(db, sc, changes)
|
||||
await audit_crud.log(
|
||||
db, actor=actor, action="update", resource_type="StaticConnection",
|
||||
resource_id=static_connection_id,
|
||||
description=audit_crud.describe_update("StaticConnection", static_connection_id, changes),
|
||||
)
|
||||
return templates.TemplateResponse(
|
||||
request, "admin/_static_connection_row.html", {"sc": sc}
|
||||
)
|
||||
|
||||
|
||||
@router.delete("/static-connections/{static_connection_id}")
|
||||
async def delete_static_connection(
|
||||
static_connection_id: int,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
actor: User = Depends(web_require("static_connections:delete")),
|
||||
):
|
||||
sc = await static_connection_crud.get(db, static_connection_id)
|
||||
if sc is not None:
|
||||
od_mm = sc.od_mm
|
||||
await static_connection_crud.delete(db, sc)
|
||||
await audit_crud.log(
|
||||
db, actor=actor, action="delete", resource_type="StaticConnection",
|
||||
resource_id=static_connection_id,
|
||||
description=audit_crud.describe_delete(
|
||||
"StaticConnection", static_connection_id, f"{od_mm} mm"
|
||||
),
|
||||
)
|
||||
return Response(status_code=status.HTTP_200_OK)
|
||||
|
||||
|
||||
# --- Audit Log ---------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user