Adding AttachmentPointNames

This commit is contained in:
Ian Roddis committed 2026-07-02 13:57:03 -03:00
1 parent ae50ec7049
commit 2767fb1c8e
14 files changed
+406 -3

No files matched your search

+92
View File
@@ -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 attachment_point_name as apn_crud
from app.crud import audit as audit_crud
from app.deps import require_permission
from app.models.user import User
from app.schemas.attachment_point_name import (
AttachmentPointNameCreate,
AttachmentPointNameRead,
AttachmentPointNameUpdate,
)
router = APIRouter()
@router.get("", response_model=list[AttachmentPointNameRead])
async def list_attachment_point_names(
skip: int = 0,
limit: int = 100,
db: AsyncSession = Depends(get_db),
_: User = Depends(require_permission("attachment_point_names:read")),
):
return await apn_crud.list_all(db, skip=skip, limit=limit)
@router.post(
"", response_model=AttachmentPointNameRead, status_code=status.HTTP_201_CREATED
)
async def create_attachment_point_name(
payload: AttachmentPointNameCreate,
db: AsyncSession = Depends(get_db),
actor: User = Depends(require_permission("attachment_point_names:write")),
):
apn = await apn_crud.create(db, name=payload.name)
await audit_crud.log(
db, actor=actor, action="create", resource_type="AttachmentPointName",
resource_id=apn.id,
description=audit_crud.describe_create("AttachmentPointName", apn.id, apn.name),
)
return apn
@router.get("/{apn_id}", response_model=AttachmentPointNameRead)
async def get_attachment_point_name(
apn_id: int,
db: AsyncSession = Depends(get_db),
_: User = Depends(require_permission("attachment_point_names:read")),
):
apn = await apn_crud.get(db, apn_id)
if apn is None:
raise HTTPException(status_code=404, detail="Attachment point name not found")
return apn
@router.patch("/{apn_id}", response_model=AttachmentPointNameRead)
async def update_attachment_point_name(
apn_id: int,
payload: AttachmentPointNameUpdate,
db: AsyncSession = Depends(get_db),
actor: User = Depends(require_permission("attachment_point_names:write")),
):
apn = await apn_crud.get(db, apn_id)
if apn is None:
raise HTTPException(status_code=404, detail="Attachment point name not found")
changes = payload.model_dump(exclude_unset=True)
apn = await apn_crud.update(db, apn, changes)
await audit_crud.log(
db, actor=actor, action="update", resource_type="AttachmentPointName",
resource_id=apn.id,
description=audit_crud.describe_update("AttachmentPointName", apn.id, changes),
)
return apn
@router.delete("/{apn_id}", status_code=status.HTTP_204_NO_CONTENT)
async def delete_attachment_point_name(
apn_id: int,
db: AsyncSession = Depends(get_db),
actor: User = Depends(require_permission("attachment_point_names:delete")),
):
apn = await apn_crud.get(db, apn_id)
if apn is None:
raise HTTPException(status_code=404, detail="Attachment point name not found")
name = apn.name
await apn_crud.delete(db, apn)
await audit_crud.log(
db, actor=actor, action="delete", resource_type="AttachmentPointName",
resource_id=apn_id,
description=audit_crud.describe_delete("AttachmentPointName", apn_id, name),
)
+2 -1
View File
@@ -1,6 +1,6 @@
from fastapi import APIRouter
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
from app.api import attachment_point_names, 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"])
@@ -15,6 +15,7 @@ api_router.include_router(part_types.router, prefix="/part-types", tags=["part-t
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(attachment_point_names.router, prefix="/attachment-point-names", tags=["attachment-point-names"])
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"])
+50
View File
@@ -0,0 +1,50 @@
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from app.models.attachment_point_name import AttachmentPointName
async def get(db: AsyncSession, apn_id: int) -> AttachmentPointName | None:
return await db.get(AttachmentPointName, apn_id)
async def get_by_name(db: AsyncSession, name: str) -> AttachmentPointName | None:
result = await db.execute(
select(AttachmentPointName).where(AttachmentPointName.name == name)
)
return result.scalar_one_or_none()
async def list_all(
db: AsyncSession, skip: int = 0, limit: int = 100
) -> list[AttachmentPointName]:
result = await db.execute(
select(AttachmentPointName)
.order_by(AttachmentPointName.name)
.offset(skip)
.limit(limit)
)
return list(result.scalars().all())
async def create(db: AsyncSession, *, name: str) -> AttachmentPointName:
apn = AttachmentPointName(name=name)
db.add(apn)
await db.commit()
await db.refresh(apn)
return apn
async def update(
db: AsyncSession, apn: AttachmentPointName, data: dict
) -> AttachmentPointName:
for field, value in data.items():
setattr(apn, field, value)
await db.commit()
await db.refresh(apn)
return apn
async def delete(db: AsyncSession, apn: AttachmentPointName) -> None:
await db.delete(apn)
await db.commit()
+2
View File
@@ -1,6 +1,7 @@
"""Import all models so they register with ``Base.metadata``."""
from app.models.associations import augmented_part_imported_parts, role_permissions, user_roles
from app.models.attachment_point_name import AttachmentPointName
from app.models.audit_log import AuditLog
from app.models.augmented_part import AugmentedPart
from app.models.clamp_connection import ClampConnection
@@ -20,6 +21,7 @@ from app.models.unc_thread import UncThread
from app.models.user import User
__all__ = [
"AttachmentPointName",
"AuditLog",
"AugmentedPart",
"ClampConnection",
+12
View File
@@ -0,0 +1,12 @@
from sqlalchemy import UniqueConstraint
from sqlalchemy.orm import Mapped, mapped_column
from app.core.database import Base
class AttachmentPointName(Base):
__tablename__ = "attachment_point_names"
__table_args__ = (UniqueConstraint("name"),)
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column(nullable=False)
+18
View File
@@ -0,0 +1,18 @@
from pydantic import BaseModel, ConfigDict
class AttachmentPointNameBase(BaseModel):
name: str
class AttachmentPointNameCreate(AttachmentPointNameBase):
pass
class AttachmentPointNameUpdate(BaseModel):
name: str | None = None
class AttachmentPointNameRead(AttachmentPointNameBase):
model_config = ConfigDict(from_attributes=True)
id: int
+8 -1
View File
@@ -2,7 +2,7 @@
{% set p = user.permissions %}
{% set can_admin = su or "users:read" in p or "roles:read" in p or "audit_log:read" in p %}
{% set can_connections = su or "clamp_connections:read" in p or "iso_threads:read" in p or "static_connections:read" in p or "unc_threads:read" in p %}
{% set can_labels = su or "manufacturers:read" in p or "part_colours:read" in p or "part_types:read" in p or "scope_models:read" in p or "sub_part_types:read" in p %}
{% set can_labels = su or "attachment_point_names:read" in p or "manufacturers:read" in p or "part_colours:read" in p or "part_types:read" in p or "scope_models:read" in p or "sub_part_types:read" in p %}
{% set can_database = su or "imported_parts:read" in p or "augmented_parts:read" in p %}
{% set can_parts = can_connections or can_labels or can_database %}
@@ -103,6 +103,10 @@
<path d="M2 3h6a4 4 0 0 1 4 4v14a3 3 0 0 0-3-3H2z"/>
<path d="M22 3h-6a4 4 0 0 0-4 4v14a3 3 0 0 0 3-3h7z"/>
</symbol>
<symbol id="icon-map-pin" viewBox="0 0 24 24">
<path d="M20 10c0 6-8 12-8 12s-8-6-8-12a8 8 0 0 1 16 0Z"/>
<circle cx="12" cy="10" r="3"/>
</symbol>
</svg>
{# ── Nav macro for DRY icon use ────────────────────────────────────────────── #}
@@ -159,6 +163,9 @@
<div class="nav-group">
<span class="nav-label">{{ icon("tag") }} Labels <i class="nav-chevron">▶</i></span>
<div class="nav-dropdown">
{% if su or "attachment_point_names:read" in p %}
<a href="/admin/attachment-point-names">{{ icon("map-pin") }} Attachment Point Names</a>
{% endif %}
{% if su or "manufacturers:read" in p %}
<a href="/admin/manufacturers">{{ icon("building-2") }} Manufacturers</a>
{% endif %}
@@ -0,0 +1,15 @@
<tr id="attachment-point-name-{{ apn.id }}">
<td>{{ apn.id }}</td>
<td>{{ apn.name }}</td>
<td class="row-actions">
<button class="btn btn-ghost"
hx-get="/admin/attachment-point-names/{{ apn.id }}/edit"
hx-target="#attachment-point-name-{{ apn.id }}"
hx-swap="outerHTML">Edit</button>
<button class="btn btn-danger"
hx-delete="/admin/attachment-point-names/{{ apn.id }}"
hx-target="#attachment-point-name-{{ apn.id }}"
hx-swap="outerHTML"
hx-confirm="Delete {{ apn.name }}?">Delete</button>
</td>
</tr>
@@ -0,0 +1,18 @@
<tr id="attachment-point-name-{{ apn.id }}">
<td colspan="3">
<form hx-post="/admin/attachment-point-names/{{ apn.id }}"
hx-target="#attachment-point-name-{{ apn.id }}"
hx-swap="outerHTML"
class="inline-edit">
<span class="muted">#{{ apn.id }}</span>
<label>Name <input type="text" name="name" value="{{ apn.name }}" 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/attachment-point-names/{{ apn.id }}/row"
hx-target="#attachment-point-name-{{ apn.id }}"
hx-swap="outerHTML">Cancel</button>
</div>
</form>
</td>
</tr>
@@ -0,0 +1,18 @@
<div id="attachment-point-name-list" class="card">
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th></th>
</tr>
</thead>
<tbody>
{% for apn in attachment_point_names %}
{% include "admin/_attachment_point_name_row.html" %}
{% else %}
<tr><td colspan="3" class="muted">No attachment point names recorded.</td></tr>
{% endfor %}
</tbody>
</table>
</div>
@@ -0,0 +1,21 @@
{% extends "base.html" %}
{% block title %}Attachment Point Names · {{ app_name }}{% endblock %}
{% block body %}
{% include "_nav.html" %}
<main class="container">
<h1>Attachment Point Names</h1>
<div class="card">
<h2>Add attachment point name</h2>
<form hx-post="/admin/attachment-point-names"
hx-target="#attachment-point-name-list"
hx-swap="outerHTML"
class="form-grid">
<label>Name <input type="text" name="name" required></label>
<button type="submit" class="btn btn-primary">Add</button>
</form>
</div>
{% include "admin/_attachment_point_names_table.html" %}
</main>
{% endblock %}
+109
View File
@@ -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 attachment_point_name as apn_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
@@ -1491,6 +1492,114 @@ async def delete_part_colour(
return Response(status_code=status.HTTP_200_OK)
# --- Attachment Point Names ---------------------------------------------------
@router.get("/attachment-point-names", response_class=HTMLResponse)
async def attachment_point_names_page(
request: Request,
db: AsyncSession = Depends(get_db),
user: User = Depends(web_require("attachment_point_names:read")),
):
return templates.TemplateResponse(
request,
"admin/attachment_point_names.html",
{"user": user, "attachment_point_names": await apn_crud.list_all(db)},
)
@router.post("/attachment-point-names", response_class=HTMLResponse)
async def create_attachment_point_name(
request: Request,
name: str = Form(...),
db: AsyncSession = Depends(get_db),
actor: User = Depends(web_require("attachment_point_names:write")),
):
if not await apn_crud.get_by_name(db, name):
created = await apn_crud.create(db, name=name)
await audit_crud.log(
db, actor=actor, action="create", resource_type="AttachmentPointName",
resource_id=created.id,
description=audit_crud.describe_create("AttachmentPointName", created.id, name),
)
return templates.TemplateResponse(
request,
"admin/_attachment_point_names_table.html",
{"attachment_point_names": await apn_crud.list_all(db)},
)
@router.get("/attachment-point-names/{apn_id}/edit", response_class=HTMLResponse)
async def edit_attachment_point_name_row(
request: Request,
apn_id: int,
db: AsyncSession = Depends(get_db),
_: User = Depends(web_require("attachment_point_names:write")),
):
apn = await apn_crud.get(db, apn_id)
if apn is None:
raise HTTPException(status_code=404, detail="Attachment point name not found")
return templates.TemplateResponse(
request, "admin/_attachment_point_name_row_edit.html", {"apn": apn}
)
@router.get("/attachment-point-names/{apn_id}/row", response_class=HTMLResponse)
async def attachment_point_name_row(
request: Request,
apn_id: int,
db: AsyncSession = Depends(get_db),
_: User = Depends(web_require("attachment_point_names:read")),
):
apn = await apn_crud.get(db, apn_id)
if apn is None:
raise HTTPException(status_code=404, detail="Attachment point name not found")
return templates.TemplateResponse(
request, "admin/_attachment_point_name_row.html", {"apn": apn}
)
@router.post("/attachment-point-names/{apn_id}", response_class=HTMLResponse)
async def update_attachment_point_name(
request: Request,
apn_id: int,
name: str = Form(...),
db: AsyncSession = Depends(get_db),
actor: User = Depends(web_require("attachment_point_names:write")),
):
apn = await apn_crud.get(db, apn_id)
if apn is None:
raise HTTPException(status_code=404, detail="Attachment point name not found")
changes = {"name": name}
await apn_crud.update(db, apn, changes)
await audit_crud.log(
db, actor=actor, action="update", resource_type="AttachmentPointName",
resource_id=apn_id,
description=audit_crud.describe_update("AttachmentPointName", apn_id, changes),
)
return templates.TemplateResponse(
request, "admin/_attachment_point_name_row.html", {"apn": apn}
)
@router.delete("/attachment-point-names/{apn_id}")
async def delete_attachment_point_name(
apn_id: int,
db: AsyncSession = Depends(get_db),
actor: User = Depends(web_require("attachment_point_names:delete")),
):
apn = await apn_crud.get(db, apn_id)
if apn is not None:
name = apn.name
await apn_crud.delete(db, apn)
await audit_crud.log(
db, actor=actor, action="delete", resource_type="AttachmentPointName",
resource_id=apn_id,
description=audit_crud.describe_delete("AttachmentPointName", apn_id, name),
)
return Response(status_code=status.HTTP_200_OK)
# --- Static Connections -------------------------------------------------------