Adding sub-parts
This commit is contained in:
1 parent
45bc5bf8f6
commit
c8696e67b5
14 files changed
+436
-2
No files matched your search
@@ -0,0 +1,29 @@
|
|||||||
|
"""add_sub_part_types_table
|
||||||
|
|
||||||
|
Revision ID: b60d0aa3ceda
|
||||||
|
Revises: fd5379986da4
|
||||||
|
Create Date: 2026-06-25 11:29:20.420056
|
||||||
|
"""
|
||||||
|
from typing import Sequence, Union
|
||||||
|
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision: str = 'b60d0aa3ceda'
|
||||||
|
down_revision: Union[str, None] = 'fd5379986da4'
|
||||||
|
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 ###
|
||||||
+2
-1
@@ -1,6 +1,6 @@
|
|||||||
from fastapi import APIRouter
|
from fastapi import APIRouter
|
||||||
|
|
||||||
from app.api import audit_log, auth, iso_threads, manufacturers, part_types, roles, scope_models, unc_threads, users
|
from app.api import audit_log, auth, iso_threads, manufacturers, part_types, roles, scope_models, sub_part_types, 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"])
|
||||||
@@ -11,4 +11,5 @@ api_router.include_router(unc_threads.router, prefix="/unc-threads", tags=["unc-
|
|||||||
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"])
|
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(audit_log.router, prefix="/audit-log", tags=["audit-log"])
|
api_router.include_router(audit_log.router, prefix="/audit-log", tags=["audit-log"])
|
||||||
@@ -0,0 +1,88 @@
|
|||||||
|
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 sub_part_type as sub_part_type_crud
|
||||||
|
from app.deps import require_permission
|
||||||
|
from app.models.user import User
|
||||||
|
from app.schemas.sub_part_type import SubPartTypeCreate, SubPartTypeRead, SubPartTypeUpdate
|
||||||
|
|
||||||
|
router = APIRouter()
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("", response_model=list[SubPartTypeRead])
|
||||||
|
async def list_sub_part_types(
|
||||||
|
skip: int = 0,
|
||||||
|
limit: int = 100,
|
||||||
|
db: AsyncSession = Depends(get_db),
|
||||||
|
_: User = Depends(require_permission("sub_part_types:read")),
|
||||||
|
):
|
||||||
|
return await sub_part_type_crud.list_all(db, skip=skip, limit=limit)
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("", response_model=SubPartTypeRead, status_code=status.HTTP_201_CREATED)
|
||||||
|
async def create_sub_part_type(
|
||||||
|
payload: SubPartTypeCreate,
|
||||||
|
db: AsyncSession = Depends(get_db),
|
||||||
|
actor: User = Depends(require_permission("sub_part_types:write")),
|
||||||
|
):
|
||||||
|
spt = await sub_part_type_crud.create(
|
||||||
|
db, name=payload.name, part_type_id=payload.part_type_id
|
||||||
|
)
|
||||||
|
await audit_crud.log(
|
||||||
|
db, actor=actor, action="create", resource_type="SubPartType",
|
||||||
|
resource_id=spt.id,
|
||||||
|
description=audit_crud.describe_create("SubPartType", spt.id, spt.name),
|
||||||
|
)
|
||||||
|
return spt
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/{sub_part_type_id}", response_model=SubPartTypeRead)
|
||||||
|
async def get_sub_part_type(
|
||||||
|
sub_part_type_id: int,
|
||||||
|
db: AsyncSession = Depends(get_db),
|
||||||
|
_: User = Depends(require_permission("sub_part_types:read")),
|
||||||
|
):
|
||||||
|
spt = await sub_part_type_crud.get(db, sub_part_type_id)
|
||||||
|
if spt is None:
|
||||||
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Sub part type not found")
|
||||||
|
return spt
|
||||||
|
|
||||||
|
|
||||||
|
@router.patch("/{sub_part_type_id}", response_model=SubPartTypeRead)
|
||||||
|
async def update_sub_part_type(
|
||||||
|
sub_part_type_id: int,
|
||||||
|
payload: SubPartTypeUpdate,
|
||||||
|
db: AsyncSession = Depends(get_db),
|
||||||
|
actor: User = Depends(require_permission("sub_part_types:write")),
|
||||||
|
):
|
||||||
|
spt = await sub_part_type_crud.get(db, sub_part_type_id)
|
||||||
|
if spt is None:
|
||||||
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Sub part type not found")
|
||||||
|
changes = payload.model_dump(exclude_unset=True)
|
||||||
|
spt = await sub_part_type_crud.update(db, spt, changes)
|
||||||
|
await audit_crud.log(
|
||||||
|
db, actor=actor, action="update", resource_type="SubPartType",
|
||||||
|
resource_id=spt.id,
|
||||||
|
description=audit_crud.describe_update("SubPartType", spt.id, changes),
|
||||||
|
)
|
||||||
|
return spt
|
||||||
|
|
||||||
|
|
||||||
|
@router.delete("/{sub_part_type_id}", status_code=status.HTTP_204_NO_CONTENT)
|
||||||
|
async def delete_sub_part_type(
|
||||||
|
sub_part_type_id: int,
|
||||||
|
db: AsyncSession = Depends(get_db),
|
||||||
|
actor: User = Depends(require_permission("sub_part_types:delete")),
|
||||||
|
):
|
||||||
|
spt = await sub_part_type_crud.get(db, sub_part_type_id)
|
||||||
|
if spt is None:
|
||||||
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Sub part type not found")
|
||||||
|
name = spt.name
|
||||||
|
await sub_part_type_crud.delete(db, spt)
|
||||||
|
await audit_crud.log(
|
||||||
|
db, actor=actor, action="delete", resource_type="SubPartType",
|
||||||
|
resource_id=sub_part_type_id,
|
||||||
|
description=audit_crud.describe_delete("SubPartType", sub_part_type_id, name),
|
||||||
|
)
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
from sqlalchemy import select
|
||||||
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
|
||||||
|
from app.models.sub_part_type import SubPartType
|
||||||
|
|
||||||
|
|
||||||
|
async def get(db: AsyncSession, sub_part_type_id: int) -> SubPartType | None:
|
||||||
|
return await db.get(SubPartType, sub_part_type_id)
|
||||||
|
|
||||||
|
|
||||||
|
async def get_by_name_and_part_type(
|
||||||
|
db: AsyncSession, name: str, part_type_id: int
|
||||||
|
) -> SubPartType | None:
|
||||||
|
result = await db.execute(
|
||||||
|
select(SubPartType).where(
|
||||||
|
SubPartType.name == name, SubPartType.part_type_id == part_type_id
|
||||||
|
)
|
||||||
|
)
|
||||||
|
return result.scalar_one_or_none()
|
||||||
|
|
||||||
|
|
||||||
|
async def list_all(
|
||||||
|
db: AsyncSession, skip: int = 0, limit: int = 100
|
||||||
|
) -> list[SubPartType]:
|
||||||
|
result = await db.execute(
|
||||||
|
select(SubPartType).order_by(SubPartType.name).offset(skip).limit(limit)
|
||||||
|
)
|
||||||
|
return list(result.scalars().all())
|
||||||
|
|
||||||
|
|
||||||
|
async def create(
|
||||||
|
db: AsyncSession, *, name: str, part_type_id: int
|
||||||
|
) -> SubPartType:
|
||||||
|
spt = SubPartType(name=name, part_type_id=part_type_id)
|
||||||
|
db.add(spt)
|
||||||
|
await db.commit()
|
||||||
|
await db.refresh(spt, attribute_names=["part_type"])
|
||||||
|
return spt
|
||||||
|
|
||||||
|
|
||||||
|
async def update(
|
||||||
|
db: AsyncSession, sub_part_type: SubPartType, data: dict
|
||||||
|
) -> SubPartType:
|
||||||
|
for field, value in data.items():
|
||||||
|
setattr(sub_part_type, field, value)
|
||||||
|
await db.commit()
|
||||||
|
await db.refresh(sub_part_type, attribute_names=["part_type"])
|
||||||
|
return sub_part_type
|
||||||
|
|
||||||
|
|
||||||
|
async def delete(db: AsyncSession, sub_part_type: SubPartType) -> None:
|
||||||
|
await db.delete(sub_part_type)
|
||||||
|
await db.commit()
|
||||||
@@ -7,6 +7,7 @@ from app.models.manufacturer import Manufacturer
|
|||||||
from app.models.part_type import PartType
|
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.sub_part_type import SubPartType
|
||||||
from app.models.refresh_token import RefreshToken
|
from app.models.refresh_token import RefreshToken
|
||||||
from app.models.role import Role
|
from app.models.role import Role
|
||||||
from app.models.unc_thread import UncThread
|
from app.models.unc_thread import UncThread
|
||||||
@@ -19,6 +20,7 @@ __all__ = [
|
|||||||
"PartType",
|
"PartType",
|
||||||
"Permission",
|
"Permission",
|
||||||
"ScopeModel",
|
"ScopeModel",
|
||||||
|
"SubPartType",
|
||||||
"RefreshToken",
|
"RefreshToken",
|
||||||
"Role",
|
"Role",
|
||||||
"UncThread",
|
"UncThread",
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
from sqlalchemy import ForeignKey, UniqueConstraint
|
||||||
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||||
|
|
||||||
|
from app.core.database import Base
|
||||||
|
from app.models.part_type import PartType
|
||||||
|
|
||||||
|
|
||||||
|
class SubPartType(Base):
|
||||||
|
__tablename__ = "sub_part_types"
|
||||||
|
__table_args__ = (UniqueConstraint("name", "part_type_id"),)
|
||||||
|
|
||||||
|
id: Mapped[int] = mapped_column(primary_key=True)
|
||||||
|
name: Mapped[str] = mapped_column(nullable=False)
|
||||||
|
part_type_id: Mapped[int] = mapped_column(
|
||||||
|
ForeignKey("part_types.id", ondelete="RESTRICT"), nullable=False
|
||||||
|
)
|
||||||
|
|
||||||
|
part_type: Mapped[PartType] = relationship(lazy="selectin")
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
from pydantic import BaseModel, ConfigDict
|
||||||
|
|
||||||
|
from app.schemas.part_type import PartTypeRead
|
||||||
|
|
||||||
|
|
||||||
|
class SubPartTypeBase(BaseModel):
|
||||||
|
name: str
|
||||||
|
part_type_id: int
|
||||||
|
|
||||||
|
|
||||||
|
class SubPartTypeCreate(SubPartTypeBase):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class SubPartTypeUpdate(BaseModel):
|
||||||
|
name: str | None = None
|
||||||
|
part_type_id: int | None = None
|
||||||
|
|
||||||
|
|
||||||
|
class SubPartTypeRead(SubPartTypeBase):
|
||||||
|
model_config = ConfigDict(from_attributes=True)
|
||||||
|
|
||||||
|
id: int
|
||||||
|
part_type: PartTypeRead
|
||||||
@@ -23,6 +23,9 @@
|
|||||||
{% if user.is_superuser or "part_types:read" in user.permissions %}
|
{% if user.is_superuser or "part_types:read" in user.permissions %}
|
||||||
<a href="/admin/part-types">Part Types</a>
|
<a href="/admin/part-types">Part Types</a>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
{% if user.is_superuser or "sub_part_types:read" in user.permissions %}
|
||||||
|
<a href="/admin/sub-part-types">Sub Part Types</a>
|
||||||
|
{% endif %}
|
||||||
{% if user.is_superuser or "audit_log:read" in user.permissions %}
|
{% if user.is_superuser or "audit_log:read" in user.permissions %}
|
||||||
<a href="/admin/audit-log">Audit Log</a>
|
<a href="/admin/audit-log">Audit Log</a>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
<tr id="sub-part-type-{{ spt.id }}">
|
||||||
|
<td>{{ spt.id }}</td>
|
||||||
|
<td>{{ spt.name }}</td>
|
||||||
|
<td>{{ spt.part_type.name }}</td>
|
||||||
|
<td class="row-actions">
|
||||||
|
<button class="btn btn-ghost"
|
||||||
|
hx-get="/admin/sub-part-types/{{ spt.id }}/edit"
|
||||||
|
hx-target="#sub-part-type-{{ spt.id }}"
|
||||||
|
hx-swap="outerHTML">Edit</button>
|
||||||
|
<button class="btn btn-danger"
|
||||||
|
hx-delete="/admin/sub-part-types/{{ spt.id }}"
|
||||||
|
hx-target="#sub-part-type-{{ spt.id }}"
|
||||||
|
hx-swap="outerHTML"
|
||||||
|
hx-confirm="Delete {{ spt.name }}?">Delete</button>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
<tr id="sub-part-type-{{ spt.id }}">
|
||||||
|
<td colspan="4">
|
||||||
|
<form hx-post="/admin/sub-part-types/{{ spt.id }}"
|
||||||
|
hx-target="#sub-part-type-{{ spt.id }}"
|
||||||
|
hx-swap="outerHTML"
|
||||||
|
class="inline-edit">
|
||||||
|
<span class="muted">#{{ spt.id }}</span>
|
||||||
|
<label>Name <input type="text" name="name" value="{{ spt.name }}" required></label>
|
||||||
|
<label>Parent Type
|
||||||
|
<select name="part_type_id" required>
|
||||||
|
{% for pt in part_types %}
|
||||||
|
<option value="{{ pt.id }}" {% if pt.id == spt.part_type_id %}selected{% endif %}>{{ pt.name }}</option>
|
||||||
|
{% endfor %}
|
||||||
|
</select>
|
||||||
|
</label>
|
||||||
|
<div class="row-actions">
|
||||||
|
<button type="submit" class="btn btn-primary">Save</button>
|
||||||
|
<button type="button" class="btn btn-ghost"
|
||||||
|
hx-get="/admin/sub-part-types/{{ spt.id }}/row"
|
||||||
|
hx-target="#sub-part-type-{{ spt.id }}"
|
||||||
|
hx-swap="outerHTML">Cancel</button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
<div id="sub-part-type-list" class="card">
|
||||||
|
<table class="table">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>ID</th><th>Name</th><th>Parent Type</th><th></th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{% for spt in sub_part_types %}
|
||||||
|
{% include "admin/_sub_part_type_row.html" %}
|
||||||
|
{% else %}
|
||||||
|
<tr><td colspan="4" class="muted">No sub part types recorded.</td></tr>
|
||||||
|
{% endfor %}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
{% extends "base.html" %}
|
||||||
|
{% block title %}Sub Part Types · {{ app_name }}{% endblock %}
|
||||||
|
{% block body %}
|
||||||
|
{% include "_nav.html" %}
|
||||||
|
<main class="container">
|
||||||
|
<h1>Sub Part Types</h1>
|
||||||
|
|
||||||
|
<div class="card">
|
||||||
|
<h2>Add sub part type</h2>
|
||||||
|
<form hx-post="/admin/sub-part-types" hx-target="#sub-part-type-list" hx-swap="outerHTML" class="form-grid">
|
||||||
|
<label>Name <input type="text" name="name" required></label>
|
||||||
|
<label>Parent Type
|
||||||
|
<select name="part_type_id" required>
|
||||||
|
<option value="">— select —</option>
|
||||||
|
{% for pt in part_types %}
|
||||||
|
<option value="{{ pt.id }}">{{ pt.name }}</option>
|
||||||
|
{% endfor %}
|
||||||
|
</select>
|
||||||
|
</label>
|
||||||
|
<button type="submit" class="btn btn-primary">Add</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{% include "admin/_sub_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 audit as audit_crud
|
from app.crud import audit as audit_crud
|
||||||
from app.crud import iso_thread as iso_thread_crud
|
from app.crud import iso_thread as iso_thread_crud
|
||||||
|
from app.crud import sub_part_type as sub_part_type_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 part_type as part_type_crud
|
||||||
from app.crud import role as role_crud
|
from app.crud import role as role_crud
|
||||||
@@ -890,6 +891,122 @@ async def delete_part_type(
|
|||||||
return Response(status_code=status.HTTP_200_OK)
|
return Response(status_code=status.HTTP_200_OK)
|
||||||
|
|
||||||
|
|
||||||
|
# --- Sub Part Types ----------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/sub-part-types", response_class=HTMLResponse)
|
||||||
|
async def sub_part_types_page(
|
||||||
|
request: Request,
|
||||||
|
db: AsyncSession = Depends(get_db),
|
||||||
|
user: User = Depends(web_require("sub_part_types:read")),
|
||||||
|
):
|
||||||
|
return templates.TemplateResponse(
|
||||||
|
request,
|
||||||
|
"admin/sub_part_types.html",
|
||||||
|
{
|
||||||
|
"user": user,
|
||||||
|
"sub_part_types": await sub_part_type_crud.list_all(db),
|
||||||
|
"part_types": await part_type_crud.list_all(db),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("/sub-part-types", response_class=HTMLResponse)
|
||||||
|
async def create_sub_part_type(
|
||||||
|
request: Request,
|
||||||
|
name: str = Form(...),
|
||||||
|
part_type_id: int = Form(...),
|
||||||
|
db: AsyncSession = Depends(get_db),
|
||||||
|
actor: User = Depends(web_require("sub_part_types:write")),
|
||||||
|
):
|
||||||
|
if not await sub_part_type_crud.get_by_name_and_part_type(db, name, part_type_id):
|
||||||
|
created = await sub_part_type_crud.create(db, name=name, part_type_id=part_type_id)
|
||||||
|
await audit_crud.log(
|
||||||
|
db, actor=actor, action="create", resource_type="SubPartType",
|
||||||
|
resource_id=created.id,
|
||||||
|
description=audit_crud.describe_create("SubPartType", created.id, created.name),
|
||||||
|
)
|
||||||
|
return templates.TemplateResponse(
|
||||||
|
request,
|
||||||
|
"admin/_sub_part_types_table.html",
|
||||||
|
{"sub_part_types": await sub_part_type_crud.list_all(db)},
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/sub-part-types/{sub_part_type_id}/edit", response_class=HTMLResponse)
|
||||||
|
async def edit_sub_part_type_row(
|
||||||
|
request: Request,
|
||||||
|
sub_part_type_id: int,
|
||||||
|
db: AsyncSession = Depends(get_db),
|
||||||
|
_: User = Depends(web_require("sub_part_types:write")),
|
||||||
|
):
|
||||||
|
spt = await sub_part_type_crud.get(db, sub_part_type_id)
|
||||||
|
if spt is None:
|
||||||
|
raise HTTPException(status_code=404, detail="Sub part type not found")
|
||||||
|
return templates.TemplateResponse(
|
||||||
|
request,
|
||||||
|
"admin/_sub_part_type_row_edit.html",
|
||||||
|
{"spt": spt, "part_types": await part_type_crud.list_all(db)},
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/sub-part-types/{sub_part_type_id}/row", response_class=HTMLResponse)
|
||||||
|
async def sub_part_type_row(
|
||||||
|
request: Request,
|
||||||
|
sub_part_type_id: int,
|
||||||
|
db: AsyncSession = Depends(get_db),
|
||||||
|
_: User = Depends(web_require("sub_part_types:read")),
|
||||||
|
):
|
||||||
|
spt = await sub_part_type_crud.get(db, sub_part_type_id)
|
||||||
|
if spt is None:
|
||||||
|
raise HTTPException(status_code=404, detail="Sub part type not found")
|
||||||
|
return templates.TemplateResponse(
|
||||||
|
request, "admin/_sub_part_type_row.html", {"spt": spt}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("/sub-part-types/{sub_part_type_id}", response_class=HTMLResponse)
|
||||||
|
async def update_sub_part_type(
|
||||||
|
request: Request,
|
||||||
|
sub_part_type_id: int,
|
||||||
|
name: str = Form(...),
|
||||||
|
part_type_id: int = Form(...),
|
||||||
|
db: AsyncSession = Depends(get_db),
|
||||||
|
actor: User = Depends(web_require("sub_part_types:write")),
|
||||||
|
):
|
||||||
|
spt = await sub_part_type_crud.get(db, sub_part_type_id)
|
||||||
|
if spt is None:
|
||||||
|
raise HTTPException(status_code=404, detail="Sub part type not found")
|
||||||
|
changes = {"name": name, "part_type_id": part_type_id}
|
||||||
|
await sub_part_type_crud.update(db, spt, changes)
|
||||||
|
await audit_crud.log(
|
||||||
|
db, actor=actor, action="update", resource_type="SubPartType",
|
||||||
|
resource_id=sub_part_type_id,
|
||||||
|
description=audit_crud.describe_update("SubPartType", sub_part_type_id, changes),
|
||||||
|
)
|
||||||
|
return templates.TemplateResponse(
|
||||||
|
request, "admin/_sub_part_type_row.html", {"spt": spt}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@router.delete("/sub-part-types/{sub_part_type_id}")
|
||||||
|
async def delete_sub_part_type(
|
||||||
|
sub_part_type_id: int,
|
||||||
|
db: AsyncSession = Depends(get_db),
|
||||||
|
actor: User = Depends(web_require("sub_part_types:delete")),
|
||||||
|
):
|
||||||
|
spt = await sub_part_type_crud.get(db, sub_part_type_id)
|
||||||
|
if spt is not None:
|
||||||
|
name = spt.name
|
||||||
|
await sub_part_type_crud.delete(db, spt)
|
||||||
|
await audit_crud.log(
|
||||||
|
db, actor=actor, action="delete", resource_type="SubPartType",
|
||||||
|
resource_id=sub_part_type_id,
|
||||||
|
description=audit_crud.describe_delete("SubPartType", sub_part_type_id, name),
|
||||||
|
)
|
||||||
|
return Response(status_code=status.HTTP_200_OK)
|
||||||
|
|
||||||
|
|
||||||
# --- Audit Log ---------------------------------------------------------------
|
# --- Audit Log ---------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+17
-1
@@ -13,10 +13,15 @@ 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 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 sub_part_type as sub_part_type_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"]
|
PART_TYPES = ["Flip Cover", "ARD", "Polarizer", "Adapter Ring", "Scope"]
|
||||||
|
|
||||||
|
SUB_PART_TYPES = {
|
||||||
|
"Scope": ["Riflescope", "Binoculars", "Combat Optic", "Red Dot", "Spotter"],
|
||||||
|
}
|
||||||
|
|
||||||
# Default permission catalogue: "<resource>:<action>".
|
# Default permission catalogue: "<resource>:<action>".
|
||||||
PERMISSIONS = [
|
PERMISSIONS = [
|
||||||
("users:read", "View users"),
|
("users:read", "View users"),
|
||||||
@@ -39,13 +44,16 @@ PERMISSIONS = [
|
|||||||
("part_types:read", "View part types"),
|
("part_types:read", "View part types"),
|
||||||
("part_types:write", "Create and update part types"),
|
("part_types:write", "Create and update part types"),
|
||||||
("part_types:delete", "Delete part types"),
|
("part_types:delete", "Delete part types"),
|
||||||
|
("sub_part_types:read", "View sub part types"),
|
||||||
|
("sub_part_types:write", "Create and update sub part types"),
|
||||||
|
("sub_part_types:delete", "Delete sub part types"),
|
||||||
("audit_log:read", "View audit log"),
|
("audit_log:read", "View audit log"),
|
||||||
]
|
]
|
||||||
|
|
||||||
# Default roles -> permission names. "admin" gets everything.
|
# Default roles -> permission names. "admin" gets everything.
|
||||||
ROLES = {
|
ROLES = {
|
||||||
"admin": [name for name, _ in PERMISSIONS], # includes audit_log:read
|
"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_types:read"],
|
"user": ["users:read", "iso_threads:read", "unc_threads:read", "manufacturers:read", "scope_models:read", "part_types:read", "sub_part_types:read"],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -81,6 +89,14 @@ async def seed() -> None:
|
|||||||
if await part_type_crud.get_by_name(db, pt_name) is None:
|
if await part_type_crud.get_by_name(db, pt_name) is None:
|
||||||
await part_type_crud.create(db, name=pt_name)
|
await part_type_crud.create(db, name=pt_name)
|
||||||
|
|
||||||
|
# Sub part types
|
||||||
|
for pt_name, spt_names in SUB_PART_TYPES.items():
|
||||||
|
pt = await part_type_crud.get_by_name(db, pt_name)
|
||||||
|
if pt is not None:
|
||||||
|
for spt_name in spt_names:
|
||||||
|
if await sub_part_type_crud.get_by_name_and_part_type(db, spt_name, pt.id) is None:
|
||||||
|
await sub_part_type_crud.create(db, name=spt_name, part_type_id=pt.id)
|
||||||
|
|
||||||
# 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