adding part colour
This commit is contained in:
1 parent
422e217180
commit
2e7002b73b
14 files changed
+379
-2
No files matched your search
@@ -0,0 +1,86 @@
|
||||
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 part_colour as part_colour_crud
|
||||
from app.deps import require_permission
|
||||
from app.models.user import User
|
||||
from app.schemas.part_colour import PartColourCreate, PartColourRead, PartColourUpdate
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get("", response_model=list[PartColourRead])
|
||||
async def list_part_colours(
|
||||
skip: int = 0,
|
||||
limit: int = 100,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
_: User = Depends(require_permission("part_colours:read")),
|
||||
):
|
||||
return await part_colour_crud.list_all(db, skip=skip, limit=limit)
|
||||
|
||||
|
||||
@router.post("", response_model=PartColourRead, status_code=status.HTTP_201_CREATED)
|
||||
async def create_part_colour(
|
||||
payload: PartColourCreate,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
actor: User = Depends(require_permission("part_colours:write")),
|
||||
):
|
||||
pc = await part_colour_crud.create(db, name=payload.name)
|
||||
await audit_crud.log(
|
||||
db, actor=actor, action="create", resource_type="PartColour",
|
||||
resource_id=pc.id,
|
||||
description=audit_crud.describe_create("PartColour", pc.id, pc.name),
|
||||
)
|
||||
return pc
|
||||
|
||||
|
||||
@router.get("/{part_colour_id}", response_model=PartColourRead)
|
||||
async def get_part_colour(
|
||||
part_colour_id: int,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
_: User = Depends(require_permission("part_colours:read")),
|
||||
):
|
||||
pc = await part_colour_crud.get(db, part_colour_id)
|
||||
if pc is None:
|
||||
raise HTTPException(status_code=404, detail="Part colour not found")
|
||||
return pc
|
||||
|
||||
|
||||
@router.patch("/{part_colour_id}", response_model=PartColourRead)
|
||||
async def update_part_colour(
|
||||
part_colour_id: int,
|
||||
payload: PartColourUpdate,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
actor: User = Depends(require_permission("part_colours:write")),
|
||||
):
|
||||
pc = await part_colour_crud.get(db, part_colour_id)
|
||||
if pc is None:
|
||||
raise HTTPException(status_code=404, detail="Part colour not found")
|
||||
changes = payload.model_dump(exclude_unset=True)
|
||||
pc = await part_colour_crud.update(db, pc, changes)
|
||||
await audit_crud.log(
|
||||
db, actor=actor, action="update", resource_type="PartColour",
|
||||
resource_id=pc.id,
|
||||
description=audit_crud.describe_update("PartColour", pc.id, changes),
|
||||
)
|
||||
return pc
|
||||
|
||||
|
||||
@router.delete("/{part_colour_id}", status_code=status.HTTP_204_NO_CONTENT)
|
||||
async def delete_part_colour(
|
||||
part_colour_id: int,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
actor: User = Depends(require_permission("part_colours:delete")),
|
||||
):
|
||||
pc = await part_colour_crud.get(db, part_colour_id)
|
||||
if pc is None:
|
||||
raise HTTPException(status_code=404, detail="Part colour not found")
|
||||
name = pc.name
|
||||
await part_colour_crud.delete(db, pc)
|
||||
await audit_crud.log(
|
||||
db, actor=actor, action="delete", resource_type="PartColour",
|
||||
resource_id=part_colour_id,
|
||||
description=audit_crud.describe_delete("PartColour", part_colour_id, name),
|
||||
)
|
||||
+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, static_connections, sub_part_types, unc_threads, users
|
||||
from app.api import audit_log, auth, 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"])
|
||||
@@ -10,6 +10,7 @@ api_router.include_router(iso_threads.router, prefix="/iso-threads", tags=["iso-
|
||||
api_router.include_router(unc_threads.router, prefix="/unc-threads", tags=["unc-threads"])
|
||||
api_router.include_router(manufacturers.router, prefix="/manufacturers", tags=["manufacturers"])
|
||||
api_router.include_router(scope_models.router, prefix="/scope-models", tags=["scope-models"])
|
||||
api_router.include_router(part_colours.router, prefix="/part-colours", tags=["part-colours"])
|
||||
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"])
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.models.part_colour import PartColour
|
||||
|
||||
|
||||
async def get(db: AsyncSession, part_colour_id: int) -> PartColour | None:
|
||||
return await db.get(PartColour, part_colour_id)
|
||||
|
||||
|
||||
async def get_by_name(db: AsyncSession, name: str) -> PartColour | None:
|
||||
result = await db.execute(
|
||||
select(PartColour).where(PartColour.name == name)
|
||||
)
|
||||
return result.scalar_one_or_none()
|
||||
|
||||
|
||||
async def list_all(
|
||||
db: AsyncSession, skip: int = 0, limit: int = 100
|
||||
) -> list[PartColour]:
|
||||
result = await db.execute(
|
||||
select(PartColour).order_by(PartColour.name).offset(skip).limit(limit)
|
||||
)
|
||||
return list(result.scalars().all())
|
||||
|
||||
|
||||
async def create(db: AsyncSession, *, name: str) -> PartColour:
|
||||
pc = PartColour(name=name)
|
||||
db.add(pc)
|
||||
await db.commit()
|
||||
await db.refresh(pc)
|
||||
return pc
|
||||
|
||||
|
||||
async def update(db: AsyncSession, pc: PartColour, data: dict) -> PartColour:
|
||||
for field, value in data.items():
|
||||
setattr(pc, field, value)
|
||||
await db.commit()
|
||||
await db.refresh(pc)
|
||||
return pc
|
||||
|
||||
|
||||
async def delete(db: AsyncSession, pc: PartColour) -> None:
|
||||
await db.delete(pc)
|
||||
await db.commit()
|
||||
@@ -6,6 +6,7 @@ from app.models.imported_part import ImportedPart
|
||||
from app.models.imported_part_config import ImportedPartConfig
|
||||
from app.models.iso_thread import IsoThread
|
||||
from app.models.manufacturer import Manufacturer
|
||||
from app.models.part_colour import PartColour
|
||||
from app.models.part_type import PartType
|
||||
from app.models.permission import Permission
|
||||
from app.models.scope_model import ScopeModel
|
||||
@@ -22,6 +23,7 @@ __all__ = [
|
||||
"ImportedPartConfig",
|
||||
"IsoThread",
|
||||
"Manufacturer",
|
||||
"PartColour",
|
||||
"PartType",
|
||||
"Permission",
|
||||
"ScopeModel",
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
from sqlalchemy import UniqueConstraint
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
|
||||
from app.core.database import Base
|
||||
|
||||
|
||||
class PartColour(Base):
|
||||
__tablename__ = "part_colours"
|
||||
__table_args__ = (UniqueConstraint("name"),)
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
name: Mapped[str] = mapped_column(nullable=False)
|
||||
@@ -0,0 +1,18 @@
|
||||
from pydantic import BaseModel, ConfigDict
|
||||
|
||||
|
||||
class PartColourBase(BaseModel):
|
||||
name: str
|
||||
|
||||
|
||||
class PartColourCreate(PartColourBase):
|
||||
pass
|
||||
|
||||
|
||||
class PartColourUpdate(BaseModel):
|
||||
name: str | None = None
|
||||
|
||||
|
||||
class PartColourRead(PartColourBase):
|
||||
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 "part_colours:read" in user.permissions %}
|
||||
<a href="/admin/part-colours">Part Colours</a>
|
||||
{% endif %}
|
||||
{% if user.is_superuser or "static_connections:read" in user.permissions %}
|
||||
<a href="/admin/static-connections">Static Connections</a>
|
||||
{% endif %}
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
<tr id="part-colour-{{ pc.id }}">
|
||||
<td>{{ pc.id }}</td>
|
||||
<td>{{ pc.name }}</td>
|
||||
<td class="row-actions">
|
||||
<button class="btn btn-ghost"
|
||||
hx-get="/admin/part-colours/{{ pc.id }}/edit"
|
||||
hx-target="#part-colour-{{ pc.id }}"
|
||||
hx-swap="outerHTML">Edit</button>
|
||||
<button class="btn btn-danger"
|
||||
hx-delete="/admin/part-colours/{{ pc.id }}"
|
||||
hx-target="#part-colour-{{ pc.id }}"
|
||||
hx-swap="outerHTML"
|
||||
hx-confirm="Delete {{ pc.name }}?">Delete</button>
|
||||
</td>
|
||||
</tr>
|
||||
@@ -0,0 +1,18 @@
|
||||
<tr id="part-colour-{{ pc.id }}">
|
||||
<td colspan="3">
|
||||
<form hx-post="/admin/part-colours/{{ pc.id }}"
|
||||
hx-target="#part-colour-{{ pc.id }}"
|
||||
hx-swap="outerHTML"
|
||||
class="inline-edit">
|
||||
<span class="muted">#{{ pc.id }}</span>
|
||||
<label>Name <input type="text" name="name" value="{{ pc.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/part-colours/{{ pc.id }}/row"
|
||||
hx-target="#part-colour-{{ pc.id }}"
|
||||
hx-swap="outerHTML">Cancel</button>
|
||||
</div>
|
||||
</form>
|
||||
</td>
|
||||
</tr>
|
||||
@@ -0,0 +1,18 @@
|
||||
<div id="part-colour-list" class="card">
|
||||
<table class="table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>ID</th>
|
||||
<th>Name</th>
|
||||
<th></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for pc in part_colours %}
|
||||
{% include "admin/_part_colour_row.html" %}
|
||||
{% else %}
|
||||
<tr><td colspan="3" class="muted">No part colours recorded.</td></tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
@@ -0,0 +1,18 @@
|
||||
{% extends "base.html" %}
|
||||
{% block title %}Part Colours · {{ app_name }}{% endblock %}
|
||||
{% block body %}
|
||||
{% include "_nav.html" %}
|
||||
<main class="container">
|
||||
<h1>Part Colours</h1>
|
||||
|
||||
<div class="card">
|
||||
<h2>Add colour</h2>
|
||||
<form hx-post="/admin/part-colours" hx-target="#part-colour-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/_part_colours_table.html" %}
|
||||
</main>
|
||||
{% endblock %}
|
||||
@@ -10,6 +10,7 @@ from app.crud import imported_part as imported_part_crud
|
||||
from app.crud import imported_part_config as imported_part_config_crud
|
||||
from app.crud import iso_thread as iso_thread_crud
|
||||
from app.crud import manufacturer as manufacturer_crud
|
||||
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
|
||||
@@ -1118,6 +1119,114 @@ async def refresh_imported_parts(
|
||||
)
|
||||
|
||||
|
||||
# --- Part Colours ------------------------------------------------------------
|
||||
|
||||
|
||||
@router.get("/part-colours", response_class=HTMLResponse)
|
||||
async def part_colours_page(
|
||||
request: Request,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
user: User = Depends(web_require("part_colours:read")),
|
||||
):
|
||||
return templates.TemplateResponse(
|
||||
request,
|
||||
"admin/part_colours.html",
|
||||
{"user": user, "part_colours": await part_colour_crud.list_all(db)},
|
||||
)
|
||||
|
||||
|
||||
@router.post("/part-colours", response_class=HTMLResponse)
|
||||
async def create_part_colour(
|
||||
request: Request,
|
||||
name: str = Form(...),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
actor: User = Depends(web_require("part_colours:write")),
|
||||
):
|
||||
if not await part_colour_crud.get_by_name(db, name):
|
||||
created = await part_colour_crud.create(db, name=name)
|
||||
await audit_crud.log(
|
||||
db, actor=actor, action="create", resource_type="PartColour",
|
||||
resource_id=created.id,
|
||||
description=audit_crud.describe_create("PartColour", created.id, created.name),
|
||||
)
|
||||
return templates.TemplateResponse(
|
||||
request,
|
||||
"admin/_part_colours_table.html",
|
||||
{"part_colours": await part_colour_crud.list_all(db)},
|
||||
)
|
||||
|
||||
|
||||
@router.get("/part-colours/{part_colour_id}/edit", response_class=HTMLResponse)
|
||||
async def edit_part_colour_row(
|
||||
request: Request,
|
||||
part_colour_id: int,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
_: User = Depends(web_require("part_colours:write")),
|
||||
):
|
||||
pc = await part_colour_crud.get(db, part_colour_id)
|
||||
if pc is None:
|
||||
raise HTTPException(status_code=404, detail="Part colour not found")
|
||||
return templates.TemplateResponse(
|
||||
request, "admin/_part_colour_row_edit.html", {"pc": pc}
|
||||
)
|
||||
|
||||
|
||||
@router.get("/part-colours/{part_colour_id}/row", response_class=HTMLResponse)
|
||||
async def part_colour_row(
|
||||
request: Request,
|
||||
part_colour_id: int,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
_: User = Depends(web_require("part_colours:read")),
|
||||
):
|
||||
pc = await part_colour_crud.get(db, part_colour_id)
|
||||
if pc is None:
|
||||
raise HTTPException(status_code=404, detail="Part colour not found")
|
||||
return templates.TemplateResponse(
|
||||
request, "admin/_part_colour_row.html", {"pc": pc}
|
||||
)
|
||||
|
||||
|
||||
@router.post("/part-colours/{part_colour_id}", response_class=HTMLResponse)
|
||||
async def update_part_colour(
|
||||
request: Request,
|
||||
part_colour_id: int,
|
||||
name: str = Form(...),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
actor: User = Depends(web_require("part_colours:write")),
|
||||
):
|
||||
pc = await part_colour_crud.get(db, part_colour_id)
|
||||
if pc is None:
|
||||
raise HTTPException(status_code=404, detail="Part colour not found")
|
||||
changes = {"name": name}
|
||||
await part_colour_crud.update(db, pc, changes)
|
||||
await audit_crud.log(
|
||||
db, actor=actor, action="update", resource_type="PartColour",
|
||||
resource_id=part_colour_id,
|
||||
description=audit_crud.describe_update("PartColour", part_colour_id, changes),
|
||||
)
|
||||
return templates.TemplateResponse(
|
||||
request, "admin/_part_colour_row.html", {"pc": pc}
|
||||
)
|
||||
|
||||
|
||||
@router.delete("/part-colours/{part_colour_id}")
|
||||
async def delete_part_colour(
|
||||
part_colour_id: int,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
actor: User = Depends(web_require("part_colours:delete")),
|
||||
):
|
||||
pc = await part_colour_crud.get(db, part_colour_id)
|
||||
if pc is not None:
|
||||
name = pc.name
|
||||
await part_colour_crud.delete(db, pc)
|
||||
await audit_crud.log(
|
||||
db, actor=actor, action="delete", resource_type="PartColour",
|
||||
resource_id=part_colour_id,
|
||||
description=audit_crud.describe_delete("PartColour", part_colour_id, name),
|
||||
)
|
||||
return Response(status_code=status.HTTP_200_OK)
|
||||
|
||||
|
||||
# --- Static Connections -------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user