Adding Part Types
This commit is contained in:
1 parent
b7ad517174
commit
fe6f9cca05
14 files changed
+350
-2
No files matched your search
@@ -0,0 +1,34 @@
|
|||||||
|
"""add_part_types_table
|
||||||
|
|
||||||
|
Revision ID: e23aa8dd2536
|
||||||
|
Revises: a52e7ca7a925
|
||||||
|
Create Date: 2026-06-25 10:49:56.463466
|
||||||
|
"""
|
||||||
|
from typing import Sequence, Union
|
||||||
|
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision: str = 'e23aa8dd2536'
|
||||||
|
down_revision: Union[str, None] = 'a52e7ca7a925'
|
||||||
|
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! ###
|
||||||
|
op.create_table('part_types',
|
||||||
|
sa.Column('id', sa.Integer(), nullable=False),
|
||||||
|
sa.Column('name', sa.String(), nullable=False),
|
||||||
|
sa.PrimaryKeyConstraint('id'),
|
||||||
|
sa.UniqueConstraint('name')
|
||||||
|
)
|
||||||
|
# ### end Alembic commands ###
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade() -> None:
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
op.drop_table('part_types')
|
||||||
|
# ### end Alembic commands ###
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
from fastapi import APIRouter, Depends, HTTPException, status
|
||||||
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
|
||||||
|
from app.core.database import get_db
|
||||||
|
from app.crud import part_type as part_type_crud
|
||||||
|
from app.deps import require_permission
|
||||||
|
from app.models.user import User
|
||||||
|
from app.schemas.part_type import PartTypeCreate, PartTypeRead, PartTypeUpdate
|
||||||
|
|
||||||
|
router = APIRouter()
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("", response_model=list[PartTypeRead])
|
||||||
|
async def list_part_types(
|
||||||
|
skip: int = 0,
|
||||||
|
limit: int = 100,
|
||||||
|
db: AsyncSession = Depends(get_db),
|
||||||
|
_: User = Depends(require_permission("part_types:read")),
|
||||||
|
):
|
||||||
|
return await part_type_crud.list_all(db, skip=skip, limit=limit)
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("", response_model=PartTypeRead, status_code=status.HTTP_201_CREATED)
|
||||||
|
async def create_part_type(
|
||||||
|
payload: PartTypeCreate,
|
||||||
|
db: AsyncSession = Depends(get_db),
|
||||||
|
_: User = Depends(require_permission("part_types:write")),
|
||||||
|
):
|
||||||
|
return await part_type_crud.create(db, name=payload.name)
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/{part_type_id}", response_model=PartTypeRead)
|
||||||
|
async def get_part_type(
|
||||||
|
part_type_id: int,
|
||||||
|
db: AsyncSession = Depends(get_db),
|
||||||
|
_: User = Depends(require_permission("part_types:read")),
|
||||||
|
):
|
||||||
|
pt = await part_type_crud.get(db, part_type_id)
|
||||||
|
if pt is None:
|
||||||
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Part type not found")
|
||||||
|
return pt
|
||||||
|
|
||||||
|
|
||||||
|
@router.patch("/{part_type_id}", response_model=PartTypeRead)
|
||||||
|
async def update_part_type(
|
||||||
|
part_type_id: int,
|
||||||
|
payload: PartTypeUpdate,
|
||||||
|
db: AsyncSession = Depends(get_db),
|
||||||
|
_: User = Depends(require_permission("part_types:write")),
|
||||||
|
):
|
||||||
|
pt = await part_type_crud.get(db, part_type_id)
|
||||||
|
if pt is None:
|
||||||
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Part type not found")
|
||||||
|
return await part_type_crud.update(db, pt, payload.model_dump(exclude_unset=True))
|
||||||
|
|
||||||
|
|
||||||
|
@router.delete("/{part_type_id}", status_code=status.HTTP_204_NO_CONTENT)
|
||||||
|
async def delete_part_type(
|
||||||
|
part_type_id: int,
|
||||||
|
db: AsyncSession = Depends(get_db),
|
||||||
|
_: User = Depends(require_permission("part_types:delete")),
|
||||||
|
):
|
||||||
|
pt = await part_type_crud.get(db, part_type_id)
|
||||||
|
if pt is None:
|
||||||
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Part type not found")
|
||||||
|
await part_type_crud.delete(db, pt)
|
||||||
+2
-1
@@ -1,6 +1,6 @@
|
|||||||
from fastapi import APIRouter
|
from fastapi import APIRouter
|
||||||
|
|
||||||
from app.api import auth, iso_threads, manufacturers, roles, scope_models, unc_threads, users
|
from app.api import auth, iso_threads, manufacturers, part_types, roles, scope_models, unc_threads, users
|
||||||
|
|
||||||
api_router = APIRouter()
|
api_router = APIRouter()
|
||||||
api_router.include_router(auth.router, prefix="/auth", tags=["auth"])
|
api_router.include_router(auth.router, prefix="/auth", tags=["auth"])
|
||||||
@@ -10,3 +10,4 @@ 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(unc_threads.router, prefix="/unc-threads", tags=["unc-threads"])
|
||||||
api_router.include_router(manufacturers.router, prefix="/manufacturers", tags=["manufacturers"])
|
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(scope_models.router, prefix="/scope-models", tags=["scope-models"])
|
||||||
|
api_router.include_router(part_types.router, prefix="/part-types", tags=["part-types"])
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
from sqlalchemy import select
|
||||||
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
|
||||||
|
from app.models.part_type import PartType
|
||||||
|
|
||||||
|
|
||||||
|
async def get(db: AsyncSession, part_type_id: int) -> PartType | None:
|
||||||
|
return await db.get(PartType, part_type_id)
|
||||||
|
|
||||||
|
|
||||||
|
async def get_by_name(db: AsyncSession, name: str) -> PartType | None:
|
||||||
|
result = await db.execute(select(PartType).where(PartType.name == name))
|
||||||
|
return result.scalar_one_or_none()
|
||||||
|
|
||||||
|
|
||||||
|
async def list_all(
|
||||||
|
db: AsyncSession, skip: int = 0, limit: int = 100
|
||||||
|
) -> list[PartType]:
|
||||||
|
result = await db.execute(
|
||||||
|
select(PartType).order_by(PartType.name).offset(skip).limit(limit)
|
||||||
|
)
|
||||||
|
return list(result.scalars().all())
|
||||||
|
|
||||||
|
|
||||||
|
async def create(db: AsyncSession, *, name: str) -> PartType:
|
||||||
|
pt = PartType(name=name)
|
||||||
|
db.add(pt)
|
||||||
|
await db.commit()
|
||||||
|
await db.refresh(pt)
|
||||||
|
return pt
|
||||||
|
|
||||||
|
|
||||||
|
async def update(db: AsyncSession, part_type: PartType, data: dict) -> PartType:
|
||||||
|
for field, value in data.items():
|
||||||
|
setattr(part_type, field, value)
|
||||||
|
await db.commit()
|
||||||
|
await db.refresh(part_type)
|
||||||
|
return part_type
|
||||||
|
|
||||||
|
|
||||||
|
async def delete(db: AsyncSession, part_type: PartType) -> None:
|
||||||
|
await db.delete(part_type)
|
||||||
|
await db.commit()
|
||||||
@@ -3,6 +3,7 @@
|
|||||||
from app.models.associations import role_permissions, user_roles
|
from app.models.associations import role_permissions, user_roles
|
||||||
from app.models.iso_thread import IsoThread
|
from app.models.iso_thread import IsoThread
|
||||||
from app.models.manufacturer import Manufacturer
|
from app.models.manufacturer import Manufacturer
|
||||||
|
from app.models.part_type import PartType
|
||||||
from app.models.permission import Permission
|
from app.models.permission import Permission
|
||||||
from app.models.scope_model import ScopeModel
|
from app.models.scope_model import ScopeModel
|
||||||
from app.models.refresh_token import RefreshToken
|
from app.models.refresh_token import RefreshToken
|
||||||
@@ -13,6 +14,7 @@ from app.models.user import User
|
|||||||
__all__ = [
|
__all__ = [
|
||||||
"IsoThread",
|
"IsoThread",
|
||||||
"Manufacturer",
|
"Manufacturer",
|
||||||
|
"PartType",
|
||||||
"Permission",
|
"Permission",
|
||||||
"ScopeModel",
|
"ScopeModel",
|
||||||
"RefreshToken",
|
"RefreshToken",
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
from sqlalchemy.orm import Mapped, mapped_column
|
||||||
|
|
||||||
|
from app.core.database import Base
|
||||||
|
|
||||||
|
|
||||||
|
class PartType(Base):
|
||||||
|
__tablename__ = "part_types"
|
||||||
|
|
||||||
|
id: Mapped[int] = mapped_column(primary_key=True)
|
||||||
|
name: Mapped[str] = mapped_column(nullable=False, unique=True)
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
from pydantic import BaseModel, ConfigDict
|
||||||
|
|
||||||
|
|
||||||
|
class PartTypeBase(BaseModel):
|
||||||
|
name: str
|
||||||
|
|
||||||
|
|
||||||
|
class PartTypeCreate(PartTypeBase):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class PartTypeUpdate(BaseModel):
|
||||||
|
name: str | None = None
|
||||||
|
|
||||||
|
|
||||||
|
class PartTypeRead(PartTypeBase):
|
||||||
|
model_config = ConfigDict(from_attributes=True)
|
||||||
|
|
||||||
|
id: int
|
||||||
@@ -20,6 +20,9 @@
|
|||||||
{% if user.is_superuser or "scope_models:read" in user.permissions %}
|
{% if user.is_superuser or "scope_models:read" in user.permissions %}
|
||||||
<a href="/admin/scope-models">Scope Models</a>
|
<a href="/admin/scope-models">Scope Models</a>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
{% if user.is_superuser or "part_types:read" in user.permissions %}
|
||||||
|
<a href="/admin/part-types">Part Types</a>
|
||||||
|
{% endif %}
|
||||||
<a href="/docs" target="_blank">API docs</a>
|
<a href="/docs" target="_blank">API docs</a>
|
||||||
</nav>
|
</nav>
|
||||||
<form method="post" action="/logout" class="logout">
|
<form method="post" action="/logout" class="logout">
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
<tr id="part-type-{{ pt.id }}">
|
||||||
|
<td>{{ pt.id }}</td>
|
||||||
|
<td>{{ pt.name }}</td>
|
||||||
|
<td class="row-actions">
|
||||||
|
<button class="btn btn-ghost"
|
||||||
|
hx-get="/admin/part-types/{{ pt.id }}/edit"
|
||||||
|
hx-target="#part-type-{{ pt.id }}"
|
||||||
|
hx-swap="outerHTML">Edit</button>
|
||||||
|
<button class="btn btn-danger"
|
||||||
|
hx-delete="/admin/part-types/{{ pt.id }}"
|
||||||
|
hx-target="#part-type-{{ pt.id }}"
|
||||||
|
hx-swap="outerHTML"
|
||||||
|
hx-confirm="Delete {{ pt.name }}?">Delete</button>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
<tr id="part-type-{{ pt.id }}">
|
||||||
|
<td colspan="3">
|
||||||
|
<form hx-post="/admin/part-types/{{ pt.id }}"
|
||||||
|
hx-target="#part-type-{{ pt.id }}"
|
||||||
|
hx-swap="outerHTML"
|
||||||
|
class="inline-edit">
|
||||||
|
<span class="muted">#{{ pt.id }}</span>
|
||||||
|
<label>Name <input type="text" name="name" value="{{ pt.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-types/{{ pt.id }}/row"
|
||||||
|
hx-target="#part-type-{{ pt.id }}"
|
||||||
|
hx-swap="outerHTML">Cancel</button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
<div id="part-type-list" class="card">
|
||||||
|
<table class="table">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>ID</th><th>Name</th><th></th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{% for pt in part_types %}
|
||||||
|
{% include "admin/_part_type_row.html" %}
|
||||||
|
{% else %}
|
||||||
|
<tr><td colspan="3" class="muted">No part types recorded.</td></tr>
|
||||||
|
{% endfor %}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
{% extends "base.html" %}
|
||||||
|
{% block title %}Part Types · {{ app_name }}{% endblock %}
|
||||||
|
{% block body %}
|
||||||
|
{% include "_nav.html" %}
|
||||||
|
<main class="container">
|
||||||
|
<h1>Part Types</h1>
|
||||||
|
|
||||||
|
<div class="card">
|
||||||
|
<h2>Add part type</h2>
|
||||||
|
<form hx-post="/admin/part-types" hx-target="#part-type-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_types_table.html" %}
|
||||||
|
</main>
|
||||||
|
{% endblock %}
|
||||||
@@ -7,6 +7,7 @@ from sqlalchemy.ext.asyncio import AsyncSession
|
|||||||
from app.core.database import get_db
|
from app.core.database import get_db
|
||||||
from app.crud import iso_thread as iso_thread_crud
|
from app.crud import iso_thread as iso_thread_crud
|
||||||
from app.crud import manufacturer as manufacturer_crud
|
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 role as role_crud
|
||||||
from app.crud import scope_model as scope_model_crud
|
from app.crud import scope_model as scope_model_crud
|
||||||
from app.crud import unc_thread as unc_thread_crud
|
from app.crud import unc_thread as unc_thread_crud
|
||||||
@@ -705,3 +706,94 @@ async def delete_scope_model(
|
|||||||
if sm is not None:
|
if sm is not None:
|
||||||
await scope_model_crud.delete(db, sm)
|
await scope_model_crud.delete(db, sm)
|
||||||
return Response(status_code=status.HTTP_200_OK)
|
return Response(status_code=status.HTTP_200_OK)
|
||||||
|
|
||||||
|
|
||||||
|
# --- Part Types --------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/part-types", response_class=HTMLResponse)
|
||||||
|
async def part_types_page(
|
||||||
|
request: Request,
|
||||||
|
db: AsyncSession = Depends(get_db),
|
||||||
|
user: User = Depends(web_require("part_types:read")),
|
||||||
|
):
|
||||||
|
return templates.TemplateResponse(
|
||||||
|
request,
|
||||||
|
"admin/part_types.html",
|
||||||
|
{"user": user, "part_types": await part_type_crud.list_all(db)},
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("/part-types", response_class=HTMLResponse)
|
||||||
|
async def create_part_type(
|
||||||
|
request: Request,
|
||||||
|
name: str = Form(...),
|
||||||
|
db: AsyncSession = Depends(get_db),
|
||||||
|
_: User = Depends(web_require("part_types:write")),
|
||||||
|
):
|
||||||
|
if not await part_type_crud.get_by_name(db, name):
|
||||||
|
await part_type_crud.create(db, name=name)
|
||||||
|
return templates.TemplateResponse(
|
||||||
|
request,
|
||||||
|
"admin/_part_types_table.html",
|
||||||
|
{"part_types": await part_type_crud.list_all(db)},
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/part-types/{part_type_id}/edit", response_class=HTMLResponse)
|
||||||
|
async def edit_part_type_row(
|
||||||
|
request: Request,
|
||||||
|
part_type_id: int,
|
||||||
|
db: AsyncSession = Depends(get_db),
|
||||||
|
_: User = Depends(web_require("part_types:write")),
|
||||||
|
):
|
||||||
|
pt = await part_type_crud.get(db, part_type_id)
|
||||||
|
if pt is None:
|
||||||
|
raise HTTPException(status_code=404, detail="Part type not found")
|
||||||
|
return templates.TemplateResponse(
|
||||||
|
request, "admin/_part_type_row_edit.html", {"pt": pt}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/part-types/{part_type_id}/row", response_class=HTMLResponse)
|
||||||
|
async def part_type_row(
|
||||||
|
request: Request,
|
||||||
|
part_type_id: int,
|
||||||
|
db: AsyncSession = Depends(get_db),
|
||||||
|
_: User = Depends(web_require("part_types:read")),
|
||||||
|
):
|
||||||
|
pt = await part_type_crud.get(db, part_type_id)
|
||||||
|
if pt is None:
|
||||||
|
raise HTTPException(status_code=404, detail="Part type not found")
|
||||||
|
return templates.TemplateResponse(
|
||||||
|
request, "admin/_part_type_row.html", {"pt": pt}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("/part-types/{part_type_id}", response_class=HTMLResponse)
|
||||||
|
async def update_part_type(
|
||||||
|
request: Request,
|
||||||
|
part_type_id: int,
|
||||||
|
name: str = Form(...),
|
||||||
|
db: AsyncSession = Depends(get_db),
|
||||||
|
_: User = Depends(web_require("part_types:write")),
|
||||||
|
):
|
||||||
|
pt = await part_type_crud.get(db, part_type_id)
|
||||||
|
if pt is None:
|
||||||
|
raise HTTPException(status_code=404, detail="Part type not found")
|
||||||
|
await part_type_crud.update(db, pt, {"name": name})
|
||||||
|
return templates.TemplateResponse(
|
||||||
|
request, "admin/_part_type_row.html", {"pt": pt}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@router.delete("/part-types/{part_type_id}")
|
||||||
|
async def delete_part_type(
|
||||||
|
part_type_id: int,
|
||||||
|
db: AsyncSession = Depends(get_db),
|
||||||
|
_: User = Depends(web_require("part_types:delete")),
|
||||||
|
):
|
||||||
|
pt = await part_type_crud.get(db, part_type_id)
|
||||||
|
if pt is not None:
|
||||||
|
await part_type_crud.delete(db, pt)
|
||||||
|
return Response(status_code=status.HTTP_200_OK)
|
||||||
+12
-1
@@ -11,9 +11,12 @@ import asyncio
|
|||||||
|
|
||||||
from app.core.config import settings
|
from app.core.config import settings
|
||||||
from app.core.database import AsyncSessionLocal, init_db
|
from app.core.database import AsyncSessionLocal, init_db
|
||||||
|
from app.crud import part_type as part_type_crud
|
||||||
from app.crud import role as role_crud
|
from app.crud import role as role_crud
|
||||||
from app.crud import user as user_crud
|
from app.crud import user as user_crud
|
||||||
|
|
||||||
|
PART_TYPES = ["Flip Cover", "ARD", "Polarizer", "Adapter Ring", "Scope"]
|
||||||
|
|
||||||
# Default permission catalogue: "<resource>:<action>".
|
# Default permission catalogue: "<resource>:<action>".
|
||||||
PERMISSIONS = [
|
PERMISSIONS = [
|
||||||
("users:read", "View users"),
|
("users:read", "View users"),
|
||||||
@@ -33,12 +36,15 @@ PERMISSIONS = [
|
|||||||
("scope_models:read", "View scope models"),
|
("scope_models:read", "View scope models"),
|
||||||
("scope_models:write", "Create and update scope models"),
|
("scope_models:write", "Create and update scope models"),
|
||||||
("scope_models:delete", "Delete scope models"),
|
("scope_models:delete", "Delete scope models"),
|
||||||
|
("part_types:read", "View part types"),
|
||||||
|
("part_types:write", "Create and update part types"),
|
||||||
|
("part_types:delete", "Delete part types"),
|
||||||
]
|
]
|
||||||
|
|
||||||
# Default roles -> permission names. "admin" gets everything.
|
# Default roles -> permission names. "admin" gets everything.
|
||||||
ROLES = {
|
ROLES = {
|
||||||
"admin": [name for name, _ in PERMISSIONS],
|
"admin": [name for name, _ in PERMISSIONS],
|
||||||
"user": ["users:read", "iso_threads:read", "unc_threads:read", "manufacturers:read", "scope_models:read"],
|
"user": ["users:read", "iso_threads:read", "unc_threads:read", "manufacturers:read", "scope_models:read", "part_types:read"],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -69,6 +75,11 @@ async def seed() -> None:
|
|||||||
)
|
)
|
||||||
roles[role_name] = role
|
roles[role_name] = role
|
||||||
|
|
||||||
|
# Part types
|
||||||
|
for pt_name in PART_TYPES:
|
||||||
|
if await part_type_crud.get_by_name(db, pt_name) is None:
|
||||||
|
await part_type_crud.create(db, name=pt_name)
|
||||||
|
|
||||||
# Superuser
|
# Superuser
|
||||||
existing = await user_crud.get_by_email(db, settings.first_superuser_email)
|
existing = await user_crud.get_by_email(db, settings.first_superuser_email)
|
||||||
if existing is None:
|
if existing is None:
|
||||||
|
|||||||
Reference in new issue
Block a user