Compare commits

...
15 Commits
69 changed files with 2189 additions and 257 deletions

No files matched your search

+22 -1
View File
@@ -13,7 +13,28 @@
"Bash(docker build *)", "Bash(docker build *)",
"Bash(git status *)", "Bash(git status *)",
"Bash(git -C /home/iroddis/dev/armament/fitguide status --short)", "Bash(git -C /home/iroddis/dev/armament/fitguide status --short)",
"Bash(git -C /home/iroddis/dev/armament/fitguide log --oneline -10)" "Bash(git -C /home/iroddis/dev/armament/fitguide log --oneline -10)",
"Bash(node --check /Users/roddis/dev/armament/fitguide/app/static/js/sortable-tables.js)",
"Bash(sqlite3 /Users/roddis/dev/armament/fitguide/app.db \"select count\\(*\\) from users;\")",
"Bash(sqlite3 /Users/roddis/dev/armament/fitguide/app.db \"select count\\(*\\) from part_colours;\")",
"Bash(sqlite3 /Users/roddis/dev/armament/fitguide/app.db \"select count\\(*\\) from manufacturers;\")",
"Bash(curl -s -c /tmp/fg_cookies.txt -o /dev/null -w \"login:%{http_code}\\\\n\" -d \"email=admin@armament.com&password=changethis\" http://localhost:8000/login)",
"Bash(curl -s -b /tmp/fg_cookies.txt http://localhost:8000/admin/manufacturers)",
"Bash(curl -s -o /dev/null -w \"%{http_code} %{content_type}\\\\n\" http://localhost:8000/static/js/sortable-tables.js)",
"Bash(curl -s -b /tmp/fg_cookies.txt \"http://localhost:8000/admin/augmented-parts\")",
"Bash(curl -s -b /tmp/fg_cookies.txt \"http://localhost:8000/admin/augmented-parts/filter?sort=colour&dir=desc\")",
"Bash(curl -s -b /tmp/fg_cookies.txt \"http://localhost:8000/admin/roles\")",
"Bash(curl -s http://localhost:8000/scopes)",
"Bash(curl -s -o /dev/null -w \"%{http_code}\\\\n\" http://localhost:8000/scopes)",
"Bash(curl -s -b /tmp/fg_cookies.txt http://localhost:8000/scopes)",
"Bash(lsof -ti:8000 -sTCP:LISTEN)",
"Bash(xargs -r kill)",
"Bash(rm -f /tmp/fg_cookies.txt /private/tmp/claude-501/-Users-roddis-dev-armament-fitguide/7873fd9c-2f2b-4583-877e-72fc88718f24/scratchpad/smoke_sort.py)",
"Bash(curl -s -o /dev/null -w \"%{http_code}\\\\n\" http://localhost:8000/login)",
"Bash(curl -s -b /tmp/fg_cookies.txt http://localhost:8000__TRACKED_VAR__)",
"Bash(sqlite3 app.db \"PRAGMA foreign_keys=ON; SELECT scope_model_id FROM augmented_parts WHERE id=1862;\")",
"Bash(sqlite3 app.db \"SELECT id, name FROM scope_models WHERE name='Viper';\")",
"Bash(sqlite3 app.db \"SELECT COUNT\\(*\\) FROM augmented_parts WHERE scope_model_id=\\(SELECT id FROM scope_models WHERE name='Viper'\\);\")"
] ]
} }
} }
+1 -1
View File
@@ -87,7 +87,7 @@ The business domain, independent of the auth/RBAC skeleton:
- **`app/services/scope_import.py`** is a separate, manually-triggered bulk - **`app/services/scope_import.py`** is a separate, manually-triggered bulk
importer: reads an uploaded `.xlsx` of third-party scopes, upserting by importer: reads an uploaded `.xlsx` of third-party scopes, upserting by
`product_number` (→ `AugmentedPart.sku`) and creating `ScopeModel`, `product_number` (→ `AugmentedPart.sku`) and creating `ScopeModel`,
`ScopeInfo`, up to three `AttachmentPoint`s, and up to two `Fit`s per row `ScopeInfo`, up to two `AttachmentPoint`s, and up to two `Fit`s per row
(see the module docstring for the full column mapping). (see the module docstring for the full column mapping).
- **`app/services/scope_guide.py`** assembles the `/scopes` page: one row per - **`app/services/scope_guide.py`** assembles the `/scopes` page: one row per
Scope-type `AugmentedPart`, one column per other `PartType`, each cell Scope-type `AugmentedPart`, one column per other `PartType`, each cell
+1
View File
@@ -21,3 +21,4 @@
- Models - Models
- Colours - Colours
- Part Types - Part Types
- Notes
@@ -31,10 +31,16 @@ def upgrade() -> None:
nullable=False, nullable=False,
), ),
sa.Column( sa.Column(
"created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False "created_at",
sa.DateTime(timezone=True),
server_default=sa.func.now(),
nullable=False,
), ),
sa.Column( sa.Column(
"updated_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False "updated_at",
sa.DateTime(timezone=True),
server_default=sa.func.now(),
nullable=False,
), ),
sa.ForeignKeyConstraint( sa.ForeignKeyConstraint(
["attachment_point_a_id"], ["attachment_points.id"], ondelete="CASCADE" ["attachment_point_a_id"], ["attachment_points.id"], ondelete="CASCADE"
@@ -59,7 +65,10 @@ def upgrade() -> None:
sa.Column("username", sa.String(length=255), nullable=False), sa.Column("username", sa.String(length=255), nullable=False),
sa.Column("body", sa.Text(), nullable=False), sa.Column("body", sa.Text(), nullable=False),
sa.Column( sa.Column(
"created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False "created_at",
sa.DateTime(timezone=True),
server_default=sa.func.now(),
nullable=False,
), ),
sa.ForeignKeyConstraint(["fit_id"], ["fits.id"], ondelete="CASCADE"), sa.ForeignKeyConstraint(["fit_id"], ["fits.id"], ondelete="CASCADE"),
sa.ForeignKeyConstraint(["user_id"], ["users.id"], ondelete="SET NULL"), sa.ForeignKeyConstraint(["user_id"], ["users.id"], ondelete="SET NULL"),
@@ -0,0 +1,43 @@
"""add_augmented_part_notes_table
Revision ID: 8012c00b0c04
Revises: d1e2f3a4b5c6
Create Date: 2026-07-31 09:07:06.137175
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = "8012c00b0c04"
down_revision: Union[str, None] = "d1e2f3a4b5c6"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
op.create_table(
"augmented_part_notes",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("augmented_part_id", sa.Integer(), nullable=False),
sa.Column("user_id", sa.Integer(), nullable=True),
sa.Column("username", sa.String(length=255), nullable=False),
sa.Column("body", sa.Text(), nullable=False),
sa.Column(
"created_at",
sa.DateTime(timezone=True),
server_default=sa.func.now(),
nullable=False,
),
sa.ForeignKeyConstraint(
["augmented_part_id"], ["augmented_parts.id"], ondelete="CASCADE"
),
sa.ForeignKeyConstraint(["user_id"], ["users.id"], ondelete="SET NULL"),
sa.PrimaryKeyConstraint("id"),
)
def downgrade() -> None:
op.drop_table("augmented_part_notes")
@@ -0,0 +1,28 @@
"""add_objective_lens_diameter_to_scope_infos
Revision ID: a3f7c19b2d40
Revises: 8012c00b0c04
Create Date: 2026-08-04 00:00:00.000000
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = "a3f7c19b2d40"
down_revision: Union[str, None] = "8012c00b0c04"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
op.add_column(
"scope_infos",
sa.Column("objective_lens_diameter_mm", sa.Float(), nullable=True),
)
def downgrade() -> None:
op.drop_column("scope_infos", "objective_lens_diameter_mm")
+22
View File
@@ -4,10 +4,13 @@ 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 augmented_part as augmented_part_crud from app.crud import augmented_part as augmented_part_crud
from app.crud import augmented_part_note as augmented_part_note_crud
from app.deps import require_permission from app.deps import require_permission
from app.models.user import User from app.models.user import User
from app.schemas.augmented_part import ( from app.schemas.augmented_part import (
AugmentedPartCreate, AugmentedPartCreate,
AugmentedPartNoteCreate,
AugmentedPartNoteRead,
AugmentedPartRead, AugmentedPartRead,
AugmentedPartSyncResult, AugmentedPartSyncResult,
AugmentedPartUpdate, AugmentedPartUpdate,
@@ -132,3 +135,22 @@ async def delete_augmented_part(
"AugmentedPart", augmented_part_id, label "AugmentedPart", augmented_part_id, label
), ),
) )
@router.post(
"/{augmented_part_id}/notes",
response_model=AugmentedPartNoteRead,
status_code=status.HTTP_201_CREATED,
)
async def create_augmented_part_note(
augmented_part_id: int,
payload: AugmentedPartNoteCreate,
db: AsyncSession = Depends(get_db),
actor: User = Depends(require_permission("augmented_parts:write")),
):
ap = await augmented_part_crud.get(db, augmented_part_id)
if ap is None:
raise HTTPException(status_code=404, detail="Augmented part not found")
return await augmented_part_note_crud.create(
db, augmented_part=ap, author=actor, body=payload.body
)
+1 -3
View File
@@ -67,9 +67,7 @@ async def create_fit(
if ap_a is None or ap_b is None: if ap_a is None or ap_b is None:
raise HTTPException(status_code=404, detail="Attachment point not found") raise HTTPException(status_code=404, detail="Attachment point not found")
if ap_a.augmented_part_id == ap_b.augmented_part_id: if ap_a.augmented_part_id == ap_b.augmented_part_id:
raise HTTPException( raise HTTPException(status_code=422, detail="a part cannot fit itself")
status_code=422, detail="a part cannot fit itself"
)
existing = await fit_crud.get_by_attachment_points(db, ap_a, ap_b) existing = await fit_crud.get_by_attachment_points(db, ap_a, ap_b)
if existing is not None: if existing is not None:
raise HTTPException(status_code=409, detail="Fit already exists") raise HTTPException(status_code=409, detail="Fit already exists")
+1
View File
@@ -35,6 +35,7 @@ if engine.sync_engine.dialect.name == "sqlite":
cursor.execute("PRAGMA busy_timeout=5000") cursor.execute("PRAGMA busy_timeout=5000")
cursor.close() cursor.close()
AsyncSessionLocal = async_sessionmaker( AsyncSessionLocal = async_sessionmaker(
engine, engine,
class_=AsyncSession, class_=AsyncSession,
+19
View File
@@ -2,6 +2,7 @@ from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.ext.asyncio import AsyncSession
from app.models.attachment_point import AttachmentPoint from app.models.attachment_point import AttachmentPoint
from app.models.attachment_point_name import AttachmentPointName
_RELATIONSHIPS = [ _RELATIONSHIPS = [
"attachment_point_name", "attachment_point_name",
@@ -27,6 +28,24 @@ async def list_for_augmented_part(
return list(result.scalars().all()) return list(result.scalars().all())
async def list_by_augmented_parts_and_name(
db: AsyncSession, augmented_part_ids: list[int], name: str
) -> list[AttachmentPoint]:
"""AttachmentPoints named ``name`` for the given augmented parts, fetched in
one query."""
if not augmented_part_ids:
return []
result = await db.execute(
select(AttachmentPoint)
.join(AttachmentPoint.attachment_point_name)
.where(
AttachmentPoint.augmented_part_id.in_(augmented_part_ids),
AttachmentPointName.name == name,
)
)
return list(result.scalars().all())
async def list_all( async def list_all(
db: AsyncSession, skip: int = 0, limit: int = 100 db: AsyncSession, skip: int = 0, limit: int = 100
) -> list[AttachmentPoint]: ) -> list[AttachmentPoint]:
+43 -6
View File
@@ -4,9 +4,47 @@ from sqlalchemy.ext.asyncio import AsyncSession
from app.models.associations import augmented_part_imported_parts from app.models.associations import augmented_part_imported_parts
from app.models.augmented_part import AugmentedPart from app.models.augmented_part import AugmentedPart
from app.models.imported_part import ImportedPart from app.models.imported_part import ImportedPart
from app.models.manufacturer import Manufacturer
from app.models.part_colour import PartColour
from app.models.part_type import PartType
from app.models.scope_model import ScopeModel
from app.models.sub_part_type import SubPartType
_RELATIONSHIPS = ["colour", "manufacturer", "scope_model", "part_type", "sub_part_type"] _RELATIONSHIPS = ["colour", "manufacturer", "scope_model", "part_type", "sub_part_type"]
# Sortable columns for the admin list, keyed by the query-string "sort" value.
# Columns backed by a related table's name require an outer join (the FK may be
# nullable, e.g. scope_model_id) so unrelated rows still appear, ordered last.
SORT_COLUMNS = {
"id": (AugmentedPart.id, None),
"sku": (AugmentedPart.sku, None),
"description": (AugmentedPart.description, None),
"colour": (PartColour.name, (PartColour, AugmentedPart.colour_id == PartColour.id)),
"manufacturer": (
Manufacturer.name,
(Manufacturer, AugmentedPart.manufacturer_id == Manufacturer.id),
),
"scope_model": (
ScopeModel.name,
(ScopeModel, AugmentedPart.scope_model_id == ScopeModel.id),
),
"part_type": (PartType.name, (PartType, AugmentedPart.part_type_id == PartType.id)),
"sub_part_type": (
SubPartType.name,
(SubPartType, AugmentedPart.sub_part_type_id == SubPartType.id),
),
}
def _apply_sort(q, sort: str, direction: str):
column, join = SORT_COLUMNS.get(sort, SORT_COLUMNS["description"])
if join is not None:
table, cond = join
q = q.outerjoin(table, cond)
order = column.desc() if direction == "desc" else column.asc()
# AugmentedPart.id as a tiebreaker keeps paginated ordering stable.
return q.order_by(order, AugmentedPart.id)
async def get(db: AsyncSession, augmented_part_id: int) -> AugmentedPart | None: async def get(db: AsyncSession, augmented_part_id: int) -> AugmentedPart | None:
return await db.get(AugmentedPart, augmented_part_id) return await db.get(AugmentedPart, augmented_part_id)
@@ -35,9 +73,7 @@ async def get_many(db: AsyncSession, ids: list[int]) -> list[AugmentedPart]:
"""Fetch augmented parts by id in one query (selectin relationships loaded).""" """Fetch augmented parts by id in one query (selectin relationships loaded)."""
if not ids: if not ids:
return [] return []
result = await db.execute( result = await db.execute(select(AugmentedPart).where(AugmentedPart.id.in_(ids)))
select(AugmentedPart).where(AugmentedPart.id.in_(ids))
)
return list(result.scalars().all()) return list(result.scalars().all())
@@ -49,6 +85,8 @@ async def list_filtered(
scope_model_id: int | None = None, scope_model_id: int | None = None,
part_type_id: int | None = None, part_type_id: int | None = None,
sub_part_type_id: int | None = None, sub_part_type_id: int | None = None,
sort: str = "description",
direction: str = "asc",
skip: int = 0, skip: int = 0,
limit: int = 100, limit: int = 100,
) -> list[AugmentedPart]: ) -> list[AugmentedPart]:
@@ -65,9 +103,8 @@ async def list_filtered(
q = q.where(AugmentedPart.part_type_id == part_type_id) q = q.where(AugmentedPart.part_type_id == part_type_id)
if sub_part_type_id is not None: if sub_part_type_id is not None:
q = q.where(AugmentedPart.sub_part_type_id == sub_part_type_id) q = q.where(AugmentedPart.sub_part_type_id == sub_part_type_id)
result = await db.execute( q = _apply_sort(q, sort, direction)
q.order_by(AugmentedPart.description).offset(skip).limit(limit) result = await db.execute(q.offset(skip).limit(limit))
)
return list(result.scalars().all()) return list(result.scalars().all())
+19
View File
@@ -0,0 +1,19 @@
from sqlalchemy.ext.asyncio import AsyncSession
from app.models.augmented_part import AugmentedPart, AugmentedPartNote
from app.models.user import User
async def create(
db: AsyncSession, *, augmented_part: AugmentedPart, author: User, body: str
) -> AugmentedPartNote:
note = AugmentedPartNote(
augmented_part_id=augmented_part.id,
user_id=author.id,
username=author.full_name or author.email,
body=body,
)
db.add(note)
await db.commit()
await db.refresh(note)
return note
+40 -6
View File
@@ -16,14 +16,37 @@ _FIELDS = frozenset(
} }
) )
# Sortable columns for the admin list, keyed by the query-string "sort" value.
SORT_COLUMNS = {
"InventoryID": ImportedPart.InventoryID,
"Description": ImportedPart.Description,
"TBXColour": ImportedPart.TBXColour,
"ProductMaxThreadDepthmm": ImportedPart.ProductMaxThreadDepthmm,
"ProductMaxThreadDepthin": ImportedPart.ProductMaxThreadDepthin,
"ProductODmm": ImportedPart.ProductODmm,
"ProductODin": ImportedPart.ProductODin,
"TBXThreadAttribute": ImportedPart.TBXThreadAttribute,
}
def _apply_sort(q, sort: str, direction: str):
column = SORT_COLUMNS.get(sort, ImportedPart.InventoryID)
order = column.desc() if direction == "desc" else column.asc()
return q.order_by(order, ImportedPart.InventoryID)
async def get(db: AsyncSession, inventory_id: str) -> ImportedPart | None: async def get(db: AsyncSession, inventory_id: str) -> ImportedPart | None:
return await db.get(ImportedPart, inventory_id) return await db.get(ImportedPart, inventory_id)
async def list_all(db: AsyncSession, skip: int = 0, limit: int = 500) -> list[ImportedPart]: async def list_all(
db: AsyncSession, skip: int = 0, limit: int = 500
) -> list[ImportedPart]:
result = await db.execute( result = await db.execute(
select(ImportedPart).order_by(ImportedPart.InventoryID).offset(skip).limit(limit) select(ImportedPart)
.order_by(ImportedPart.InventoryID)
.offset(skip)
.limit(limit)
) )
return list(result.scalars().all()) return list(result.scalars().all())
@@ -31,13 +54,16 @@ async def list_all(db: AsyncSession, skip: int = 0, limit: int = 500) -> list[Im
async def list_filtered( async def list_filtered(
db: AsyncSession, db: AsyncSession,
description: str = "", description: str = "",
sort: str = "InventoryID",
direction: str = "asc",
skip: int = 0, skip: int = 0,
limit: int = 500, limit: int = 500,
) -> list[ImportedPart]: ) -> list[ImportedPart]:
q = select(ImportedPart) q = select(ImportedPart)
if description.strip(): if description.strip():
q = q.where(ImportedPart.Description.ilike(f"%{description.strip()}%")) q = q.where(ImportedPart.Description.ilike(f"%{description.strip()}%"))
result = await db.execute(q.order_by(ImportedPart.InventoryID).offset(skip).limit(limit)) q = _apply_sort(q, sort, direction)
result = await db.execute(q.offset(skip).limit(limit))
return list(result.scalars().all()) return list(result.scalars().all())
@@ -78,7 +104,11 @@ async def upsert_bulk(db: AsyncSession, records: list[dict]) -> tuple[int, int]:
return 0, 0 return 0, 0
clean = [ clean = [
{k: str(v).strip() if v is not None else None for k, v in r.items() if k in _FIELDS} {
k: str(v).strip() if v is not None else None
for k, v in r.items()
if k in _FIELDS
}
for r in records for r in records
if r.get("InventoryID") if r.get("InventoryID")
] ]
@@ -88,7 +118,9 @@ async def upsert_bulk(db: AsyncSession, records: list[dict]) -> tuple[int, int]:
incoming_ids = {r["InventoryID"] for r in clean} incoming_ids = {r["InventoryID"] for r in clean}
result = await db.execute( result = await db.execute(
select(ImportedPart.InventoryID).where(ImportedPart.InventoryID.in_(incoming_ids)) select(ImportedPart.InventoryID).where(
ImportedPart.InventoryID.in_(incoming_ids)
)
) )
existing_ids = {row[0] for row in result.all()} existing_ids = {row[0] for row in result.all()}
@@ -103,7 +135,9 @@ async def upsert_bulk(db: AsyncSession, records: list[dict]) -> tuple[int, int]:
vals = {k: v for k, v in record.items() if k != "InventoryID"} vals = {k: v for k, v in record.items() if k != "InventoryID"}
if vals: if vals:
await db.execute( await db.execute(
update(ImportedPart).where(ImportedPart.InventoryID == inv_id).values(vals) update(ImportedPart)
.where(ImportedPart.InventoryID == inv_id)
.values(vals)
) )
await db.commit() await db.commit()
+1 -3
View File
@@ -23,9 +23,7 @@ async def get_default(db: AsyncSession) -> Role | None:
async def set_default(db: AsyncSession, role: Role) -> Role: async def set_default(db: AsyncSession, role: Role) -> Role:
"""Make ``role`` the sole default, clearing the flag on every other role.""" """Make ``role`` the sole default, clearing the flag on every other role."""
await db.execute( await db.execute(sa_update(Role).where(Role.id != role.id).values(is_default=False))
sa_update(Role).where(Role.id != role.id).values(is_default=False)
)
role.is_default = True role.is_default = True
await db.commit() await db.commit()
await db.refresh(role) await db.refresh(role)
+3 -3
View File
@@ -24,9 +24,7 @@ async def list_by_augmented_parts(
if not augmented_part_ids: if not augmented_part_ids:
return [] return []
result = await db.execute( result = await db.execute(
select(ScopeInfo).where( select(ScopeInfo).where(ScopeInfo.augmented_part_id.in_(augmented_part_ids))
ScopeInfo.augmented_part_id.in_(augmented_part_ids)
)
) )
return list(result.scalars().all()) return list(result.scalars().all())
@@ -38,6 +36,7 @@ async def create(
magnification_min: float | None = None, magnification_min: float | None = None,
magnification_max: float | None = None, magnification_max: float | None = None,
model_suffix: str | None = None, model_suffix: str | None = None,
objective_lens_diameter_mm: float | None = None,
objective_thread_pitch: str | None = None, objective_thread_pitch: str | None = None,
) -> ScopeInfo: ) -> ScopeInfo:
info = ScopeInfo( info = ScopeInfo(
@@ -45,6 +44,7 @@ async def create(
magnification_min=magnification_min, magnification_min=magnification_min,
magnification_max=magnification_max, magnification_max=magnification_max,
model_suffix=model_suffix, model_suffix=model_suffix,
objective_lens_diameter_mm=objective_lens_diameter_mm,
objective_thread_pitch=objective_thread_pitch, objective_thread_pitch=objective_thread_pitch,
) )
db.add(info) db.add(info)
+2 -1
View File
@@ -8,7 +8,7 @@ from app.models.associations import (
from app.models.attachment_point import AttachmentPoint from app.models.attachment_point import AttachmentPoint
from app.models.attachment_point_name import AttachmentPointName from app.models.attachment_point_name import AttachmentPointName
from app.models.audit_log import AuditLog from app.models.audit_log import AuditLog
from app.models.augmented_part import AugmentedPart from app.models.augmented_part import AugmentedPart, AugmentedPartNote
from app.models.clamp_connection import ClampConnection from app.models.clamp_connection import ClampConnection
from app.models.fit import Fit, FitLabel, FitNote from app.models.fit import Fit, FitLabel, FitNote
from app.models.imported_part import ImportedPart from app.models.imported_part import ImportedPart
@@ -32,6 +32,7 @@ __all__ = [
"AttachmentPointName", "AttachmentPointName",
"AuditLog", "AuditLog",
"AugmentedPart", "AugmentedPart",
"AugmentedPartNote",
"ClampConnection", "ClampConnection",
"Fit", "Fit",
"FitLabel", "FitLabel",
+30 -1
View File
@@ -1,4 +1,6 @@
from sqlalchemy import ForeignKey, String from datetime import datetime
from sqlalchemy import DateTime, ForeignKey, String, Text, func
from sqlalchemy.orm import Mapped, mapped_column, relationship from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.core.database import Base from app.core.database import Base
@@ -45,3 +47,30 @@ class AugmentedPart(Base):
scope_model: Mapped[ScopeModel | None] = relationship(lazy="selectin") scope_model: Mapped[ScopeModel | None] = relationship(lazy="selectin")
part_type: Mapped[PartType] = relationship(lazy="selectin") part_type: Mapped[PartType] = relationship(lazy="selectin")
sub_part_type: Mapped[SubPartType | None] = relationship(lazy="selectin") sub_part_type: Mapped[SubPartType | None] = relationship(lazy="selectin")
notes: Mapped[list["AugmentedPartNote"]] = relationship(
back_populates="augmented_part",
lazy="selectin",
order_by="AugmentedPartNote.created_at",
cascade="all, delete-orphan",
)
class AugmentedPartNote(Base):
__tablename__ = "augmented_part_notes"
id: Mapped[int] = mapped_column(primary_key=True)
augmented_part_id: Mapped[int] = mapped_column(
ForeignKey("augmented_parts.id", ondelete="CASCADE"), nullable=False
)
user_id: Mapped[int | None] = mapped_column(
ForeignKey("users.id", ondelete="SET NULL"), nullable=True
)
# Denormalized snapshot of who wrote the note (mirrors FitNote.username), so
# the note stays attributable even if the user is later deleted.
username: Mapped[str] = mapped_column(String(255), nullable=False)
body: Mapped[str] = mapped_column(Text, nullable=False)
created_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), server_default=func.now()
)
augmented_part: Mapped[AugmentedPart] = relationship(back_populates="notes")
+5
View File
@@ -27,6 +27,11 @@ class ScopeInfo(Base):
model_suffix: Mapped[str | None] = mapped_column( model_suffix: Mapped[str | None] = mapped_column(
String, nullable=True, default=None String, nullable=True, default=None
) )
# The lens diameter is a descriptive attribute (the "50" in "3-18x50"), not a
# connection dimension — it has no AttachmentPoint.
objective_lens_diameter_mm: Mapped[float | None] = mapped_column(
Float, nullable=True, default=None
)
# Kept as the raw string from the sheet (e.g. "0.75", "1/16") so no precision # Kept as the raw string from the sheet (e.g. "0.75", "1/16") so no precision
# or notation is lost; the parsed thread lives on its own AttachmentPoint. # or notation is lost; the parsed thread lives on its own AttachmentPoint.
objective_thread_pitch: Mapped[str | None] = mapped_column( objective_thread_pitch: Mapped[str | None] = mapped_column(
+26 -1
View File
@@ -1,4 +1,6 @@
from pydantic import BaseModel, ConfigDict from datetime import datetime
from pydantic import BaseModel, ConfigDict, field_validator
from app.schemas.manufacturer import ManufacturerRead from app.schemas.manufacturer import ManufacturerRead
from app.schemas.part_colour import PartColourRead from app.schemas.part_colour import PartColourRead
@@ -7,6 +9,28 @@ from app.schemas.scope_model import ScopeModelRead
from app.schemas.sub_part_type import SubPartTypeRead from app.schemas.sub_part_type import SubPartTypeRead
class AugmentedPartNoteCreate(BaseModel):
body: str
@field_validator("body")
@classmethod
def validate_body(cls, v: str) -> str:
if not v.strip():
raise ValueError("body must not be blank")
return v.strip()
class AugmentedPartNoteRead(BaseModel):
model_config = ConfigDict(from_attributes=True)
id: int
augmented_part_id: int
user_id: int | None
username: str
body: str
created_at: datetime
class AugmentedPartBase(BaseModel): class AugmentedPartBase(BaseModel):
sku: str | None = None sku: str | None = None
description: str description: str
@@ -46,3 +70,4 @@ class AugmentedPartRead(AugmentedPartBase):
scope_model: ScopeModelRead | None scope_model: ScopeModelRead | None
part_type: PartTypeRead part_type: PartTypeRead
sub_part_type: SubPartTypeRead | None sub_part_type: SubPartTypeRead | None
notes: list[AugmentedPartNoteRead] = []
+1
View File
@@ -5,6 +5,7 @@ class ScopeInfoBase(BaseModel):
magnification_min: float | None = None magnification_min: float | None = None
magnification_max: float | None = None magnification_max: float | None = None
model_suffix: str | None = None model_suffix: str | None = None
objective_lens_diameter_mm: float | None = None
objective_thread_pitch: str | None = None objective_thread_pitch: str | None = None
+57
View File
@@ -0,0 +1,57 @@
"""Irreversible bulk-delete of all domain (parts/taxonomy/fit) data.
Leaves users, roles/permissions, refresh tokens, the audit log, and the
ImportedPartConfig (OData connection settings) untouched.
"""
from sqlalchemy import delete
from sqlalchemy.ext.asyncio import AsyncSession
from app.models.associations import augmented_part_imported_parts
from app.models.attachment_point import AttachmentPoint
from app.models.attachment_point_name import AttachmentPointName
from app.models.augmented_part import AugmentedPart, AugmentedPartNote
from app.models.clamp_connection import ClampConnection
from app.models.fit import Fit, FitNote
from app.models.imported_part import ImportedPart
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.scope_info import ScopeInfo
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.unc_thread import UncThread
# Children before the RESTRICT-protected reference tables they point to.
_TARGETS = [
("Fit notes", FitNote),
("Fits", Fit),
("Augmented part notes", AugmentedPartNote),
("Scope info", ScopeInfo),
("Attachment points", AttachmentPoint),
("Augmented part / imported part links", augmented_part_imported_parts),
("Augmented parts", AugmentedPart),
("Imported parts", ImportedPart),
("Scope models", ScopeModel),
("Attachment point names", AttachmentPointName),
("ISO threads", IsoThread),
("UNC threads", UncThread),
("Clamp connections", ClampConnection),
("Static connections", StaticConnection),
("Manufacturers", Manufacturer),
]
async def purge_domain_data(db: AsyncSession) -> dict[str, int]:
"""Delete every row from every domain table, in FK-safe order.
Returns a label -> rows-deleted map for reporting/audit purposes.
"""
counts: dict[str, int] = {}
for label, target in _TARGETS:
result = await db.execute(delete(target))
counts[label] = result.rowcount or 0
await db.commit()
return counts
+4 -4
View File
@@ -142,9 +142,7 @@ async def _reconcile(
for own_ap in own_points: for own_ap in own_points:
for other_ap in points_by_part.get(other_id, []): for other_ap in points_by_part.get(other_id, []):
if attachment_points_match(own_ap, other_ap): if attachment_points_match(own_ap, other_ap):
candidate_pairs.add( candidate_pairs.add(_canonical_pair(own_ap.id, other_ap.id))
_canonical_pair(own_ap.id, other_ap.id)
)
result = RecomputeResult() result = RecomputeResult()
@@ -171,7 +169,9 @@ async def _reconcile(
return result return result
async def recompute_for_part(db: AsyncSession, augmented_part_id: int) -> RecomputeResult: async def recompute_for_part(
db: AsyncSession, augmented_part_id: int
) -> RecomputeResult:
"""Reconcile Possible fits involving this part against the current algorithm. """Reconcile Possible fits involving this part against the current algorithm.
Creates Possible fits for newly-matching attachment point pairs, and removes Creates Possible fits for newly-matching attachment point pairs, and removes
+74
View File
@@ -0,0 +1,74 @@
"""Lightweight in-process tracking for long-running admin operations.
Jobs live in a module-level dict, so this only works correctly for a single
OS process (the app's current deployment: one uvicorn worker, no multi-process
or multi-replica setup — see ``entrypoint.sh`` / ``docker-compose.yml``). If
the app is ever scaled to multiple workers/replicas, this needs to move to
shared storage (a DB table, Redis) so a poll request landing on a different
process can still see the job.
"""
from __future__ import annotations
import asyncio
import uuid
from collections.abc import Awaitable, Callable
from dataclasses import dataclass, field
from datetime import UTC, datetime
_MAX_FINISHED_AGE_SECONDS = 3600
@dataclass
class Job:
id: str
kind: str
status: str = "running" # running | done | error
message: str = "Working…"
result: object | None = None
created_at: datetime = field(default_factory=lambda: datetime.now(UTC))
finished_at: datetime | None = None
_jobs: dict[str, Job] = {}
def get(job_id: str) -> Job | None:
return _jobs.get(job_id)
def _evict_stale() -> None:
now = datetime.now(UTC)
stale = [
job_id
for job_id, job in _jobs.items()
if job.finished_at
and (now - job.finished_at).total_seconds() > _MAX_FINISHED_AGE_SECONDS
]
for job_id in stale:
del _jobs[job_id]
def start(kind: str, run: Callable[[Job], Awaitable[None]]) -> Job:
"""Create a job, launch ``run(job)`` in the background, and return it immediately.
``run`` is responsible for updating ``job.status``/``job.message``/``job.result``
to reflect the outcome, including catching its own errors. Any exception that
escapes anyway is caught here as a last resort so the job doesn't hang forever
in "running" state.
"""
_evict_stale()
job = Job(id=uuid.uuid4().hex, kind=kind)
_jobs[job.id] = job
async def _runner() -> None:
try:
await run(job)
except Exception as exc:
job.status = "error"
job.message = f"{kind} failed: {exc}"
finally:
job.finished_at = datetime.now(UTC)
asyncio.create_task(_runner())
return job
+18 -7
View File
@@ -7,6 +7,7 @@ the parts of that type that fit the row's scope.
from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.ext.asyncio import AsyncSession
from app.crud import attachment_point as attachment_point_crud
from app.crud import augmented_part as augmented_part_crud from app.crud import augmented_part as augmented_part_crud
from app.crud import fit as fit_crud from app.crud import fit as fit_crud
from app.crud import part_type as part_type_crud from app.crud import part_type as part_type_crud
@@ -14,6 +15,7 @@ from app.crud import scope_info as scope_info_crud
from app.models.fit import FitLabel from app.models.fit import FitLabel
SCOPE_TYPE_NAME = "Scope" SCOPE_TYPE_NAME = "Scope"
OBJECTIVE_ATTACHMENT_POINT_NAME = "Objective"
# "exact" outranks "possible" when the same part fits a scope via several fits. # "exact" outranks "possible" when the same part fits a scope via several fits.
_LABEL_RANK = {FitLabel.EXACT.value: 2, FitLabel.POSSIBLE.value: 1} _LABEL_RANK = {FitLabel.EXACT.value: 2, FitLabel.POSSIBLE.value: 1}
@@ -43,6 +45,7 @@ async def build(
{ {
"scope": AugmentedPart, "scope": AugmentedPart,
"info": ScopeInfo | None, "info": ScopeInfo | None,
"objective_attachment_point": AttachmentPoint | None,
"cells": {part_type_id: [{"part": AugmentedPart, "label": str}]}, "cells": {part_type_id: [{"part": AugmentedPart, "label": str}]},
}, },
..., ...,
@@ -50,9 +53,7 @@ async def build(
} }
""" """
part_types = await part_type_crud.list_all(db, limit=1000) part_types = await part_type_crud.list_all(db, limit=1000)
scope_type = next( scope_type = next((pt for pt in part_types if pt.name == SCOPE_TYPE_NAME), None)
(pt for pt in part_types if pt.name == SCOPE_TYPE_NAME), None
)
column_types = [ column_types = [
pt for pt in part_types if scope_type is None or pt.id != scope_type.id pt for pt in part_types if scope_type is None or pt.id != scope_type.id
] ]
@@ -87,6 +88,13 @@ async def build(
p.id: p for p in await augmented_part_crud.get_many(db, list(other_ids)) p.id: p for p in await augmented_part_crud.get_many(db, list(other_ids))
} }
objective_aps = {
ap.augmented_part_id: ap
for ap in await attachment_point_crud.list_by_augmented_parts_and_name(
db, list(scope_ids), OBJECTIVE_ATTACHMENT_POINT_NAME
)
}
infos = { infos = {
info.augmented_part_id: info info.augmented_part_id: info
for info in await scope_info_crud.list_by_augmented_parts(db, list(scope_ids)) for info in await scope_info_crud.list_by_augmented_parts(db, list(scope_ids))
@@ -94,9 +102,7 @@ async def build(
# cell_map[scope_id][type_id][other_part_id] -> {"part", "label"}, dedup'd so a # cell_map[scope_id][type_id][other_part_id] -> {"part", "label"}, dedup'd so a
# part appears once per cell, keeping its strongest fit label. # part appears once per cell, keeping its strongest fit label.
cell_map: dict[int, dict[int, dict[int, dict]]] = { cell_map: dict[int, dict[int, dict[int, dict]]] = {sid: {} for sid in scope_ids}
sid: {} for sid in scope_ids
}
for fit in fits: for fit in fits:
scope_id, other_id = _scope_and_other(fit, scope_ids) scope_id, other_id = _scope_and_other(fit, scope_ids)
if scope_id is None: if scope_id is None:
@@ -121,7 +127,12 @@ async def build(
if fits_part_type_id is not None and fits_part_type_id not in cells: if fits_part_type_id is not None and fits_part_type_id not in cells:
continue # scope has no fit for the required accessory type continue # scope has no fit for the required accessory type
rows.append( rows.append(
{"scope": scope, "info": infos.get(scope.id), "cells": cells} {
"scope": scope,
"info": infos.get(scope.id),
"objective_attachment_point": objective_aps.get(scope.id),
"cells": cells,
}
) )
return {"column_types": column_types, "rows": rows} return {"column_types": column_types, "rows": rows}
+21 -44
View File
@@ -2,7 +2,7 @@
The spreadsheet describes parts of PartType "Scope". Each row maps onto an The spreadsheet describes parts of PartType "Scope". Each row maps onto an
AugmentedPart (keyed on ``product_number`` → ``sku``, so re-importing upserts), AugmentedPart (keyed on ``product_number`` → ``sku``, so re-importing upserts),
a ScopeModel, a ScopeInfo record for the scope-specific fields, up to three a ScopeModel, a ScopeInfo record for the scope-specific fields, up to two
attachment points, and up to two Fits against other parts. attachment points, and up to two Fits against other parts.
Column → destination Column → destination
@@ -14,9 +14,9 @@ model_suffix → ScopeInfo.model_suffix (+ description)
sub_part_type → SubPartType (get-or-create under "Scope") sub_part_type → SubPartType (get-or-create under "Scope")
product_number → AugmentedPart.sku (also the upsert / fit-lookup key) product_number → AugmentedPart.sku (also the upsert / fit-lookup key)
magnification_min / _max → ScopeInfo magnification_min / _max → ScopeInfo
objective_lens_diameter_mm → ScopeInfo (descriptive only, no attachment point)
objective_thread_pitch → ScopeInfo objective_thread_pitch → ScopeInfo
ocular_outer_diameter_mm → StaticConnection + AttachmentPoint "Ocular" ocular_outer_diameter_mm → StaticConnection + AttachmentPoint "Ocular"
objective_lens_outside_diameter_mm → StaticConnection + AttachmentPoint "Objective"
objective_thread → ISO/UNC thread + AttachmentPoint "Objective" objective_thread → ISO/UNC thread + AttachmentPoint "Objective"
ocular_fit → Fit: scope's Ocular AP ↔ referenced part's first AP ocular_fit → Fit: scope's Ocular AP ↔ referenced part's first AP
objective_fit → Fit: scope's Objective AP ↔ referenced part's first AP objective_fit → Fit: scope's Objective AP ↔ referenced part's first AP
@@ -68,7 +68,7 @@ EXPECTED_COLUMNS = [
"model", "model",
"magnification_min", "magnification_min",
"magnification_max", "magnification_max",
"objective_lens_outside_diameter_mm", "objective_lens_diameter_mm",
"model_suffix", "model_suffix",
"sub_part_type", "sub_part_type",
"product_number", "product_number",
@@ -140,15 +140,11 @@ def parse_workbook(data: bytes) -> list[dict]:
wb.close() wb.close()
async def _static_ap( async def _static_ap(db: AsyncSession, *, augmented_part_id: int, label: str, od_mm: float):
db: AsyncSession, *, augmented_part_id: int, label: str, od_mm: float
):
"""Create an attachment point carrying a StaticConnection for ``od_mm``.""" """Create an attachment point carrying a StaticConnection for ``od_mm``."""
sc = await static_connection_crud.get_by_od_mm(db, od_mm) sc = await static_connection_crud.get_by_od_mm(db, od_mm)
if sc is None: if sc is None:
sc = await static_connection_crud.create( sc = await static_connection_crud.create(db, od_mm=od_mm, od_in=od_mm / MM_PER_INCH)
db, od_mm=od_mm, od_in=od_mm / MM_PER_INCH
)
apn = await apn_crud.get_by_name(db, label) apn = await apn_crud.get_by_name(db, label)
if apn is None: if apn is None:
apn = await apn_crud.create(db, name=label) apn = await apn_crud.create(db, name=label)
@@ -160,9 +156,7 @@ async def _static_ap(
) )
async def _thread_ap( async def _thread_ap(db: AsyncSession, *, augmented_part_id: int, label: str, spec: str):
db: AsyncSession, *, augmented_part_id: int, label: str, spec: str
):
"""Create an attachment point for a parsed ISO/UNC thread, or None.""" """Create an attachment point for a parsed ISO/UNC thread, or None."""
parsed = _parse_thread_attribute(spec) parsed = _parse_thread_attribute(spec)
if parsed is None: if parsed is None:
@@ -206,8 +200,7 @@ async def _link_fit(
"""Create a POSSIBLE fit from the scope's AP to the referenced part's first AP.""" """Create a POSSIBLE fit from the scope's AP to the referenced part's first AP."""
if scope_ap is None: if scope_ap is None:
messages.append( messages.append(
f"{scope_role}_fit '{target_sku}' skipped: scope has no " f"{scope_role}_fit '{target_sku}' skipped: scope has no {scope_role} attachment point"
f"{scope_role} attachment point"
) )
return return
target = await augmented_part_crud.get_by_sku(db, target_sku) target = await augmented_part_crud.get_by_sku(db, target_sku)
@@ -220,16 +213,13 @@ async def _link_fit(
target_aps = await attachment_point_crud.list_for_augmented_part(db, target.id) target_aps = await attachment_point_crud.list_for_augmented_part(db, target.id)
if not target_aps: if not target_aps:
messages.append( messages.append(
f"{scope_role}_fit '{target_sku}' skipped: referenced part has no " f"{scope_role}_fit '{target_sku}' skipped: referenced part has no attachment points"
"attachment points"
) )
return return
target_ap = target_aps[0] target_ap = target_aps[0]
existing = await fit_crud.get_by_attachment_points(db, scope_ap, target_ap) existing = await fit_crud.get_by_attachment_points(db, scope_ap, target_ap)
if existing is None: if existing is None:
await fit_crud.create( await fit_crud.create(db, ap_a=scope_ap, ap_b=target_ap, label=FitLabel.POSSIBLE)
db, ap_a=scope_ap, ap_b=target_ap, label=FitLabel.POSSIBLE
)
async def import_scopes(db: AsyncSession, rows: list[dict]) -> ScopeImportResult: async def import_scopes(db: AsyncSession, rows: list[dict]) -> ScopeImportResult:
@@ -250,7 +240,9 @@ async def import_scopes(db: AsyncSession, rows: list[dict]) -> ScopeImportResult
for offset, row in enumerate(rows): for offset, row in enumerate(rows):
# +2: 1-based, and the header occupies row 1. # +2: 1-based, and the header occupies row 1.
row_num = offset + 2 row_num = offset + 2
product_number = _text(row.get("product_number")) or _text(row.get("model")) model_suffix = _text(row.get("model_suffix"))
default_product_number = f"{_text(row.get('model'))} {row.get('magnification_min')}-{row.get('magnification_max')}x{row.get('objective_lens_diameter_mm')} {row.get('model_suffix')}".strip()
product_number = _text(row.get("product_number")) or default_product_number
result = ScopeImportRowResult( result = ScopeImportRowResult(
row=row_num, product_number=product_number, status="skipped", messages=[] row=row_num, product_number=product_number, status="skipped", messages=[]
) )
@@ -271,9 +263,7 @@ async def import_scopes(db: AsyncSession, rows: list[dict]) -> ScopeImportResult
manufacturer = await manufacturer_crud.get_by_name(db, manufacturer_name) manufacturer = await manufacturer_crud.get_by_name(db, manufacturer_name)
if manufacturer is None: if manufacturer is None:
manufacturer = await manufacturer_crud.create( manufacturer = await manufacturer_crud.create(db, name=manufacturer_name, url="")
db, name=manufacturer_name, url=""
)
model_name = _text(row.get("model")) model_name = _text(row.get("model"))
url = _text(row.get("url")) url = _text(row.get("url"))
@@ -330,14 +320,13 @@ async def import_scopes(db: AsyncSession, rows: list[dict]) -> ScopeImportResult
magnification_min=_num(row.get("magnification_min")), magnification_min=_num(row.get("magnification_min")),
magnification_max=_num(row.get("magnification_max")), magnification_max=_num(row.get("magnification_max")),
model_suffix=model_suffix, model_suffix=model_suffix,
objective_lens_diameter_mm=_num(row.get("objective_lens_diameter_mm")),
objective_thread_pitch=_text(row.get("objective_thread_pitch")), objective_thread_pitch=_text(row.get("objective_thread_pitch")),
) )
# Rebuild attachment points from scratch so re-imports stay idempotent # Rebuild attachment points from scratch so re-imports stay idempotent
# (cascades delete any fits that referenced the old points). # (cascades delete any fits that referenced the old points).
for old_ap in await attachment_point_crud.list_for_augmented_part( for old_ap in await attachment_point_crud.list_for_augmented_part(db, part.id):
db, part.id
):
await attachment_point_crud.delete(db, old_ap) await attachment_point_crud.delete(db, old_ap)
ocular_ap = None ocular_ap = None
@@ -350,34 +339,22 @@ async def import_scopes(db: AsyncSession, rows: list[dict]) -> ScopeImportResult
od_mm=ocular_od, od_mm=ocular_od,
) )
objective_static_ap = None # The objective attachment point comes from the thread alone — the
objective_od = _num(row.get("objective_lens_outside_diameter_mm")) # lens diameter is a descriptive attribute, not a connection.
if objective_od is not None:
objective_static_ap = await _static_ap(
db,
augmented_part_id=part.id,
label=OBJECTIVE_LABEL,
od_mm=objective_od,
)
objective_thread_spec = _text(row.get("objective_thread")) objective_thread_spec = _text(row.get("objective_thread"))
objective_thread_ap = None objective_ap = None
if objective_thread_spec: if objective_thread_spec:
objective_thread_ap = await _thread_ap( objective_ap = await _thread_ap(
db, db,
augmented_part_id=part.id, augmented_part_id=part.id,
label=OBJECTIVE_LABEL, label=OBJECTIVE_LABEL,
spec=objective_thread_spec, spec=objective_thread_spec,
) )
if objective_thread_ap is None: if objective_ap is None:
result.messages.append( result.messages.append(
f"could not parse objective_thread " f"could not parse objective_thread '{objective_thread_spec}'"
f"'{objective_thread_spec}'"
) )
# For objective_fit prefer the lens-OD point, falling back to the thread.
objective_ap = objective_static_ap or objective_thread_ap
staged.append((row, ocular_ap, objective_ap, part.id, result)) staged.append((row, ocular_ap, objective_ap, part.id, result))
row_results.append(result) row_results.append(result)
except Exception as exc: # noqa: BLE001 - report, don't abort the batch except Exception as exc: # noqa: BLE001 - report, don't abort the batch
+31
View File
@@ -247,6 +247,37 @@ h2 {
gap: 0.4rem; gap: 0.4rem;
} }
/* Sortable table headers */
table.sortable thead th:not([data-sort-off]) {
cursor: pointer;
user-select: none;
}
table.sortable thead th:not([data-sort-off]):hover {
color: var(--text);
}
table.sortable thead th[aria-sort]::after {
margin-left: 0.3rem;
}
table.sortable thead th[aria-sort="ascending"]::after {
content: "\25B2";
}
table.sortable thead th[aria-sort="descending"]::after {
content: "\25BC";
}
.sort-link {
color: inherit;
text-decoration: none;
}
.sort-link:hover {
text-decoration: underline;
}
/* Tags / definition lists */ /* Tags / definition lists */
.tag { .tag {
display: inline-block; display: inline-block;
Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB

+77
View File
@@ -0,0 +1,77 @@
(function () {
"use strict";
function getCellText(row, index) {
var cell = row.children[index];
return cell ? cell.textContent.trim() : "";
}
function isEmptyValue(text) {
return text === "" || text === "—";
}
function isPureNumber(text) {
return /^-?\d+(\.\d+)?$/.test(text);
}
function columnIsNumeric(rows, index) {
var sawValue = false;
for (var i = 0; i < rows.length; i++) {
var text = getCellText(rows[i], index);
if (isEmptyValue(text)) continue;
sawValue = true;
if (!isPureNumber(text)) return false;
}
return sawValue;
}
function compareRows(a, b, index, numeric, dir) {
var textA = getCellText(a, index);
var textB = getCellText(b, index);
var emptyA = isEmptyValue(textA);
var emptyB = isEmptyValue(textB);
if (emptyA && emptyB) return 0;
if (emptyA) return 1;
if (emptyB) return -1;
var result = numeric
? parseFloat(textA) - parseFloat(textB)
: textA.localeCompare(textB, undefined, { numeric: true, sensitivity: "base" });
return dir === "desc" ? -result : result;
}
function sortTable(table, th) {
var thead = table.tHead;
var tbody = table.tBodies[0];
if (!thead || !tbody) return;
var headerRow = thead.rows[0];
var index = Array.prototype.indexOf.call(headerRow.children, th);
if (index === -1) return;
var rows = Array.prototype.slice.call(tbody.rows);
if (rows.length < 2) return;
var nextDir = th.getAttribute("data-sort-dir") === "asc" ? "desc" : "asc";
Array.prototype.forEach.call(headerRow.children, function (cell) {
cell.removeAttribute("data-sort-dir");
cell.removeAttribute("aria-sort");
});
th.setAttribute("data-sort-dir", nextDir);
th.setAttribute("aria-sort", nextDir === "asc" ? "ascending" : "descending");
var numeric = columnIsNumeric(rows, index);
rows.sort(function (a, b) {
return compareRows(a, b, index, numeric, nextDir);
});
rows.forEach(function (row) {
tbody.appendChild(row);
});
}
document.addEventListener("click", function (event) {
var th = event.target.closest("table.sortable thead th");
if (!th || th.hasAttribute("data-sort-off") || th.textContent.trim() === "") return;
sortTable(th.closest("table"), th);
});
})();
+17
View File
@@ -0,0 +1,17 @@
{%- macro connection_spec(ap) -%}
{%- if ap.iso_thread -%}
M{{ ap.iso_thread.diameter_mm }}×{{ ap.iso_thread.pitch_mm }}
{%- elif ap.unc_thread -%}
{{ ap.unc_thread.diameter_fraction }}-{{ ap.unc_thread.tpi }} {{ ap.unc_thread.series.value }}
{%- elif ap.clamp_connection -%}
{%- if ap.clamp_connection.min_mm is not none -%}
{{ ap.clamp_connection.min_mm }}–{{ ap.clamp_connection.max_mm }} mm
{%- else -%}
{{ ap.clamp_connection.min_in }}–{{ ap.clamp_connection.max_in }} in
{%- endif -%}
{%- elif ap.static_connection -%}
{{ ap.static_connection.od_mm }} mm OD
{%- else -%}
—
{%- endif -%}
{%- endmacro -%}
+3
View File
@@ -145,6 +145,9 @@
{% if su or "audit_log:read" in p %} {% if su or "audit_log:read" in p %}
<a href="/admin/audit-log">{{ icon("list-checks") }} Audit Log</a> <a href="/admin/audit-log">{{ icon("list-checks") }} Audit Log</a>
{% endif %} {% endif %}
{% if su %}
<a href="/admin/danger-zone">{{ icon("alert-circle") }} Danger Zone</a>
{% endif %}
</div> </div>
</div> </div>
{% endif %} {% endif %}
+16 -7
View File
@@ -1,17 +1,24 @@
{% from "_connection_macros.html" import connection_spec %}
{%- macro mag(v) -%}{{ v | int if v == v | int else v | round(1) }}{%- endmacro -%} {%- macro mag(v) -%}{{ v | int if v == v | int else v | round(1) }}{%- endmacro -%}
{%- macro magnification(info) -%} {%- macro magnification_range(info) -%}
{%- if info and info.magnification_min is not none and info.magnification_max is not none -%} {%- if info and info.magnification_min is not none and info.magnification_max is not none -%}
{{ mag(info.magnification_min) }}–{{ mag(info.magnification_max) }}× {{ mag(info.magnification_min) }}-{{ mag(info.magnification_max) }}
{%- elif info and info.magnification_min is not none -%} {%- elif info and info.magnification_min is not none -%}
{{ mag(info.magnification_min) }}× {{ mag(info.magnification_min) }}
{%- elif info and info.magnification_max is not none -%} {%- elif info and info.magnification_max is not none -%}
{{ mag(info.magnification_max) }}× {{ mag(info.magnification_max) }}
{%- endif -%}
{%- endmacro -%}
{%- macro magnification(info) -%}
{%- set range = magnification_range(info) -%}
{%- if range -%}
{{ range }}x{% if info.objective_lens_diameter_mm is not none %}{{ mag(info.objective_lens_diameter_mm) }}{% endif %}
{%- else -%} {%- else -%}
<span class="muted">—</span> <span class="muted">—</span>
{%- endif -%} {%- endif -%}
{%- endmacro -%} {%- endmacro -%}
<div id="scopes-table-card" class="card" style="overflow-x:auto"> <div id="scopes-table-card" class="card" style="overflow-x:auto">
<table class="table"> <table class="table sortable">
<thead> <thead>
<tr> <tr>
<th>Scope</th> <th>Scope</th>
@@ -21,7 +28,7 @@
<th>Magnification</th> <th>Magnification</th>
<th>Objective thread</th> <th>Objective thread</th>
{% for ct in column_types %} {% for ct in column_types %}
<th>{{ ct.name }}</th> <th data-sort-off>{{ ct.name }}</th>
{% endfor %} {% endfor %}
</tr> </tr>
</thead> </thead>
@@ -39,7 +46,9 @@
<td>{{ scope.sub_part_type.name if scope.sub_part_type else "—" }}</td> <td>{{ scope.sub_part_type.name if scope.sub_part_type else "—" }}</td>
<td>{{ magnification(info) }}</td> <td>{{ magnification(info) }}</td>
<td> <td>
{% if info and info.objective_thread_pitch %} {% if row.objective_attachment_point %}
<code>{{ connection_spec(row.objective_attachment_point) }}</code>
{% elif info and info.objective_thread_pitch %}
{{ info.objective_thread_pitch }} {{ info.objective_thread_pitch }}
{% else %}<span class="muted">—</span>{% endif %} {% else %}<span class="muted">—</span>{% endif %}
</td> </td>
@@ -1,5 +1,5 @@
<div id="attachment-point-name-list" class="card"> <div id="attachment-point-name-list" class="card">
<table class="table"> <table class="table sortable">
<thead> <thead>
<tr> <tr>
<th>ID</th> <th>ID</th>
@@ -20,7 +20,6 @@
{% endif %} {% endif %}
<tr id="attachment-point-{{ ap.id }}"> <tr id="attachment-point-{{ ap.id }}">
<td>{{ ap.id }}</td> <td>{{ ap.id }}</td>
<td>{{ ap.augmented_part.description | truncate(40) }}</td>
<td>{{ ap.attachment_point_name.name }}</td> <td>{{ ap.attachment_point_name.name }}</td>
<td>{{ conn_type }}</td> <td>{{ conn_type }}</td>
<td><code>{{ conn_label }}</code></td> <td><code>{{ conn_label }}</code></td>
@@ -1,9 +1,8 @@
<div id="attachment-point-list" class="card" style="overflow-x:auto"> <div id="attachment-point-list" class="card" style="overflow-x:auto">
<table class="table"> <table class="table sortable">
<thead> <thead>
<tr> <tr>
<th>ID</th> <th>ID</th>
<th>Augmented Part</th>
<th>Point Name</th> <th>Point Name</th>
<th>Connection Type</th> <th>Connection Type</th>
<th>Connection</th> <th>Connection</th>
@@ -45,6 +45,16 @@
</select> </select>
</label> </label>
<div id="ap-{{ augmented_part_id }}-fit-other-points"></div> <div id="ap-{{ augmented_part_id }}-fit-other-points"></div>
<label>Fit type
<select name="label">
<option value="exact" selected>Exact</option>
<option value="possible">Possible</option>
<option value="none">None</option>
</select>
</label>
<label>Note (optional)
<input type="text" name="note" placeholder="Add a note…">
</label>
<button type="submit" class="btn btn-primary">Add fit</button> <button type="submit" class="btn btn-primary">Add fit</button>
</form> </form>
{% endif %} {% endif %}
@@ -0,0 +1,22 @@
<div id="ap-{{ augmented_part_id }}-notes" class="ap-inline-section">
<h3>Notes</h3>
<div class="fit-notes">
{% for note in notes %}
<div class="fit-note">
<span class="fit-note-meta">{{ note.username }} · {{ note.created_at.strftime("%Y-%m-%d %H:%M") }}</span>
<p class="fit-note-body">{{ note.body }}</p>
</div>
{% else %}
<p class="muted" style="font-size:0.85rem;margin:0 0 0.5rem">No notes recorded yet.</p>
{% endfor %}
{% if can_write_notes %}
<form hx-post="/admin/augmented-parts/{{ augmented_part_id }}/notes"
hx-target="#ap-{{ augmented_part_id }}-notes" hx-swap="outerHTML"
class="inline-edit fit-note-form">
<input type="text" name="body" placeholder="Add a note…" required>
<button type="submit" class="btn btn-ghost">Add note</button>
</form>
{% endif %}
</div>
</div>
@@ -10,6 +10,8 @@
hx-target="#augmented-parts-table-card" hx-target="#augmented-parts-table-card"
hx-trigger="input delay:300ms, change" hx-trigger="input delay:300ms, change"
hx-swap="outerHTML"> hx-swap="outerHTML">
<input type="hidden" name="sort" value="{{ sort }}">
<input type="hidden" name="dir" value="{{ dir }}">
<input type="text" name="description" placeholder="Filter by description…" value="{{ filter_description or '' }}"> <input type="text" name="description" placeholder="Filter by description…" value="{{ filter_description or '' }}">
<select name="colour_id"> <select name="colour_id">
<option value="">All colours</option> <option value="">All colours</option>
@@ -1,16 +1,17 @@
{% from "admin/_sort_macros.html" import sort_th %}
<div id="augmented-parts-table-card" class="card"> <div id="augmented-parts-table-card" class="card">
{% include "admin/_pagination.html" %} {% include "admin/_pagination.html" %}
<table class="table"> <table class="table">
<thead> <thead>
<tr> <tr>
<th>ID</th> {{ sort_th("id", "ID", sort_headers) }}
<th>SKU</th> {{ sort_th("sku", "SKU", sort_headers) }}
<th>Description</th> {{ sort_th("description", "Description", sort_headers) }}
<th>Colour</th> {{ sort_th("colour", "Colour", sort_headers) }}
<th>Manufacturer</th> {{ sort_th("manufacturer", "Manufacturer", sort_headers) }}
<th>Model</th> {{ sort_th("scope_model", "Model", sort_headers) }}
<th>Type</th> {{ sort_th("part_type", "Type", sort_headers) }}
<th>Sub Type</th> {{ sort_th("sub_part_type", "Sub Type", sort_headers) }}
<th></th> <th></th>
</tr> </tr>
</thead> </thead>
@@ -1,5 +1,5 @@
<div id="clamp-connection-list" class="card"> <div id="clamp-connection-list" class="card">
<table class="table"> <table class="table sortable">
<thead> <thead>
<tr> <tr>
<th>ID</th> <th>ID</th>
@@ -0,0 +1,14 @@
{% if total %}
<p style="color:var(--color-danger,#c00)">
Purge complete: {{ total }} row(s) deleted.
</p>
<ul class="muted">
{% for label, n in counts.items() %}
{% if n %}
<li>{{ label }}: {{ n }}</li>
{% endif %}
{% endfor %}
</ul>
{% else %}
<p class="muted">Nothing to purge — the database was already clean.</p>
{% endif %}
@@ -1,6 +1,13 @@
{% if points %}
<select name="other_attachment_point_id" required> <select name="other_attachment_point_id" required>
<option value="">— select —</option> <option value="">— select —</option>
{% for p in points %} {% for p in points %}
<option value="{{ p.id }}">{{ p.attachment_point_name.name }}</option> <option value="{{ p.id }}">{{ p.attachment_point_name.name }}</option>
{% endfor %} {% endfor %}
</select> </select>
{% else %}
<input type="hidden" name="other_attachment_point_id" value="">
<p class="muted" style="font-size:0.85rem;margin:0">
This part has no attachment points yet — one matching yours will be created automatically.
</p>
{% endif %}
@@ -11,6 +11,8 @@
hx-target="#imported-parts-table-card" hx-target="#imported-parts-table-card"
hx-trigger="input delay:300ms, change" hx-trigger="input delay:300ms, change"
hx-swap="outerHTML"> hx-swap="outerHTML">
<input type="hidden" name="sort" value="{{ sort }}">
<input type="hidden" name="dir" value="{{ dir }}">
<input type="text" name="description" placeholder="Filter by description…" value="{{ filter_description or '' }}"> <input type="text" name="description" placeholder="Filter by description…" value="{{ filter_description or '' }}">
</form> </form>
</div> </div>
@@ -1,16 +1,17 @@
{% from "admin/_sort_macros.html" import sort_th %}
<div id="imported-parts-table-card" class="card" style="overflow-x:auto"> <div id="imported-parts-table-card" class="card" style="overflow-x:auto">
{% include "admin/_pagination.html" %} {% include "admin/_pagination.html" %}
<table class="table"> <table class="table">
<thead> <thead>
<tr> <tr>
<th>InventoryID</th> {{ sort_th("InventoryID", "InventoryID", sort_headers) }}
<th>Description</th> {{ sort_th("Description", "Description", sort_headers) }}
<th>TBX Colour</th> {{ sort_th("TBXColour", "TBX Colour", sort_headers) }}
<th>Max Thread Depth (mm)</th> {{ sort_th("ProductMaxThreadDepthmm", "Max Thread Depth (mm)", sort_headers) }}
<th>Max Thread Depth (in)</th> {{ sort_th("ProductMaxThreadDepthin", "Max Thread Depth (in)", sort_headers) }}
<th>OD (mm)</th> {{ sort_th("ProductODmm", "OD (mm)", sort_headers) }}
<th>OD (in)</th> {{ sort_th("ProductODin", "OD (in)", sort_headers) }}
<th>Thread Attribute</th> {{ sort_th("TBXThreadAttribute", "Thread Attribute", sort_headers) }}
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
+7
View File
@@ -0,0 +1,7 @@
<div id="{{ container_id }}"
hx-get="{{ poll_url }}"
hx-trigger="load delay:1s"
hx-swap="outerHTML"
class="{{ extra_class or '' }}">
<p class="muted">⏳ {{ job.message }}</p>
</div>
@@ -0,0 +1,5 @@
<span id="{{ container_id }}"
hx-get="{{ poll_url }}"
hx-trigger="load delay:1s"
hx-swap="outerHTML"
class="muted">⏳ {{ job.message }}</span>
@@ -1,8 +1,8 @@
<div id="manufacturer-list" class="card"> <div id="manufacturer-list" class="card">
<table class="table"> <table class="table sortable">
<thead> <thead>
<tr> <tr>
<th>ID</th><th>Name</th><th>Homepage</th><th>Logo</th><th></th> <th>ID</th><th>Name</th><th>Homepage</th><th data-sort-off>Logo</th><th></th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
+1 -1
View File
@@ -1,5 +1,5 @@
<div id="part-colour-list" class="card"> <div id="part-colour-list" class="card">
<table class="table"> <table class="table sortable">
<thead> <thead>
<tr> <tr>
<th>ID</th> <th>ID</th>
+1 -1
View File
@@ -1,5 +1,5 @@
<div id="part-type-list" class="card"> <div id="part-type-list" class="card">
<table class="table"> <table class="table sortable">
<thead> <thead>
<tr> <tr>
<th>ID</th><th>Name</th><th></th> <th>ID</th><th>Name</th><th></th>
+2 -2
View File
@@ -1,7 +1,7 @@
<div id="role-list" class="card"> <div id="role-list" class="card">
<table class="table"> <table class="table sortable">
<thead> <thead>
<tr><th>ID</th><th>Name</th><th>Description</th><th>Permissions</th><th>Default</th><th></th></tr> <tr><th>ID</th><th>Name</th><th>Description</th><th data-sort-off>Permissions</th><th data-sort-off>Default</th><th></th></tr>
</thead> </thead>
<tbody> <tbody>
{% for r in roles %} {% for r in roles %}
@@ -1,3 +1,4 @@
<div id="{{ container_id or 'scope-import-result-body' }}">
{% if error %} {% if error %}
<div class="card"> <div class="card">
<p style="color:var(--color-danger,#c00)">{{ error }}</p> <p style="color:var(--color-danger,#c00)">{{ error }}</p>
@@ -39,3 +40,4 @@
{% endif %} {% endif %}
</div> </div>
{% endif %} {% endif %}
</div>
+1 -1
View File
@@ -1,5 +1,5 @@
<div id="scope-model-list" class="card"> <div id="scope-model-list" class="card">
<table class="table"> <table class="table sortable">
<thead> <thead>
<tr> <tr>
<th>ID</th><th>Name</th><th>Manufacturer</th><th>Product Page</th><th></th> <th>ID</th><th>Name</th><th>Manufacturer</th><th>Product Page</th><th></th>
+13
View File
@@ -0,0 +1,13 @@
{% macro sort_th(key, label, sort_headers) %}
{% if sort_headers and key in sort_headers %}
{% set h = sort_headers[key] %}
<th>
<a href="#" class="sort-link{{ ' active' if h.active else '' }}"
hx-get="{{ h.url }}"
hx-target="{{ h.target_id }}"
hx-swap="outerHTML">{{ label }}{% if h.active %}<span aria-hidden="true">{{ ' ▲' if h.dir == 'asc' else ' ▼' }}</span>{% endif %}</a>
</th>
{% else %}
<th>{{ label }}</th>
{% endif %}
{% endmacro %}
@@ -1,5 +1,5 @@
<div id="static-connection-list" class="card"> <div id="static-connection-list" class="card">
<table class="table"> <table class="table sortable">
<thead> <thead>
<tr> <tr>
<th>ID</th> <th>ID</th>
@@ -1,5 +1,5 @@
<div id="sub-part-type-list" class="card"> <div id="sub-part-type-list" class="card">
<table class="table"> <table class="table sortable">
<thead> <thead>
<tr> <tr>
<th>ID</th><th>Name</th><th>Parent Type</th><th></th> <th>ID</th><th>Name</th><th>Parent Type</th><th></th>
+2 -2
View File
@@ -1,8 +1,8 @@
<div id="user-list" class="card"> <div id="user-list" class="card">
<table class="table"> <table class="table sortable">
<thead> <thead>
<tr> <tr>
<th>ID</th><th>Email</th><th>Name</th><th>Active</th><th>Superuser</th><th>Roles</th><th></th> <th>ID</th><th>Email</th><th>Name</th><th>Active</th><th>Superuser</th><th data-sort-off>Roles</th><th></th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
+1 -1
View File
@@ -7,7 +7,7 @@
<div class="card"> <div class="card">
{% if entries %} {% if entries %}
<table class="table"> <table class="table sortable">
<thead> <thead>
<tr> <tr>
<th>Time</th> <th>Time</th>
@@ -44,6 +44,12 @@
{% else %}—{% endif %} {% else %}—{% endif %}
</dd> </dd>
<dt>Model suffix</dt><dd>{{ scope_info.model_suffix or "—" }}</dd> <dt>Model suffix</dt><dd>{{ scope_info.model_suffix or "—" }}</dd>
<dt>Objective lens diameter</dt>
<dd>
{% if scope_info.objective_lens_diameter_mm is not none %}
{{ scope_info.objective_lens_diameter_mm }} mm
{% else %}—{% endif %}
</dd>
<dt>Objective thread</dt><dd>{{ scope_info.objective_thread_pitch or "—" }}</dd> <dt>Objective thread</dt><dd>{{ scope_info.objective_thread_pitch or "—" }}</dd>
</dl> </dl>
</div> </div>
@@ -61,5 +67,9 @@
{% include "admin/_augmented_part_fits.html" %} {% include "admin/_augmented_part_fits.html" %}
</div> </div>
{% endif %} {% endif %}
<div class="card">
{% include "admin/_augmented_part_notes.html" %}
</div>
</main> </main>
{% endblock %} {% endblock %}
+32
View File
@@ -0,0 +1,32 @@
{% extends "base.html" %}
{% block title %}Danger Zone · {{ app_name }}{% endblock %}
{% block body %}
{% include "_nav.html" %}
<main class="container">
<h1>Danger Zone</h1>
<div class="card" style="border: 1px solid var(--color-danger, #c00)">
<h2>Purge database</h2>
<p>
Permanently deletes every part, taxonomy entry, attachment point, fit,
and imported part in the database — manufacturers, part colours, part
types, sub part types, scope models, ISO/UNC threads, clamp/static
connections, attachment point names, augmented parts, scope info,
attachment points, fits, fit notes, and imported parts.
</p>
<p>
<strong>Not</strong> deleted: users, roles &amp; permissions, refresh
tokens, the audit log, and the OData connection configuration.
</p>
<p class="muted">This cannot be undone. Take a database backup first if you might need this data again.</p>
<button class="btn btn-danger"
hx-post="/admin/danger-zone/purge"
hx-target="#purge-result"
hx-swap="innerHTML"
hx-confirm="This will PERMANENTLY delete all parts, taxonomy, attachment points, fits, and imported parts. Users and the OData configuration are kept. This cannot be undone. Continue?">
Purge all part/fit data
</button>
<div id="purge-result" style="margin-top:1rem"></div>
</div>
</main>
{% endblock %}
+2
View File
@@ -4,10 +4,12 @@
<meta charset="utf-8"> <meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<title>{% block title %}{{ app_name }}{% endblock %}</title> <title>{% block title %}{{ app_name }}{% endblock %}</title>
<link rel="icon" href="/static/favicon.ico" sizes="any">
<link rel="stylesheet" href="/static/css/style.css"> <link rel="stylesheet" href="/static/css/style.css">
<script src="https://unpkg.com/htmx.org@1.9.12" <script src="https://unpkg.com/htmx.org@1.9.12"
integrity="sha384-ujb1lZYygJmzgSwoxRggbCHcjc0rB2XoQrxeTUQyRjrOnlCoYta87iKBWq3EsdM2" integrity="sha384-ujb1lZYygJmzgSwoxRggbCHcjc0rB2XoQrxeTUQyRjrOnlCoYta87iKBWq3EsdM2"
crossorigin="anonymous"></script> crossorigin="anonymous"></script>
<script src="/static/js/sortable-tables.js" defer></script>
</head> </head>
<body> <body>
{% block body %}{% endblock %} {% block body %}{% endblock %}
+587 -98
View File
@@ -16,9 +16,10 @@ from fastapi import (
from fastapi.responses import HTMLResponse, RedirectResponse, Response from fastapi.responses import HTMLResponse, RedirectResponse, Response
from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.ext.asyncio import AsyncSession
from app.core.database import get_db from app.core.database import AsyncSessionLocal, get_db
from app.crud import audit as audit_crud from app.crud import audit as audit_crud
from app.crud import augmented_part as augmented_part_crud from app.crud import augmented_part as augmented_part_crud
from app.crud import augmented_part_note as augmented_part_note_crud
from app.crud import imported_part as imported_part_crud 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 imported_part_config as imported_part_config_crud
from app.crud import iso_thread as iso_thread_crud from app.crud import iso_thread as iso_thread_crud
@@ -42,7 +43,9 @@ from app.models.fit import FitLabel
from app.models.unc_thread import UncSeries from app.models.unc_thread import UncSeries
from app.models.user import User from app.models.user import User
from app.services import augmented_part_sync as augmented_part_sync_service from app.services import augmented_part_sync as augmented_part_sync_service
from app.services import danger_zone as danger_zone_service
from app.services import fit_matching as fit_matching_service from app.services import fit_matching as fit_matching_service
from app.services import jobs
from app.services import odata as odata_service from app.services import odata as odata_service
from app.services import scope_import as scope_import_service from app.services import scope_import as scope_import_service
from app.web.templates import templates from app.web.templates import templates
@@ -52,6 +55,11 @@ router = APIRouter(prefix="/admin", include_in_schema=False)
_PAGE_SIZE = 50 _PAGE_SIZE = 50
def _query_url(base_path: str, params: dict) -> str:
clean = {k: v for k, v in params.items() if v not in (None, "", 0)}
return f"{base_path}?{urllib.parse.urlencode(clean)}"
def _paginate( def _paginate(
base_path: str, base_path: str,
filter_params: dict, filter_params: dict,
@@ -65,10 +73,9 @@ def _paginate(
skip = (page - 1) * page_size skip = (page - 1) * page_size
def _url(p: int) -> str: def _url(p: int) -> str:
params = {k: v for k, v in filter_params.items() if v not in (None, "", 0)} return _query_url(
params["page"] = p base_path, {**filter_params, "page": p, "page_size": page_size}
params["page_size"] = page_size )
return f"{base_path}?{urllib.parse.urlencode(params)}"
return { return {
"page": page, "page": page,
@@ -84,6 +91,33 @@ def _paginate(
} }
def _sort_headers(
base_path: str,
filter_params: dict,
columns: list[str],
sort: str,
direction: str,
target_id: str,
) -> dict:
"""Per-column header link context for click-to-sort table headers.
``filter_params`` should NOT include "sort"/"dir"/"page" — those are added here.
"""
headers = {}
for key in columns:
next_dir = "desc" if sort == key and direction == "asc" else "asc"
headers[key] = {
"url": _query_url(
base_path,
{**filter_params, "sort": key, "dir": next_dir, "page": 1},
),
"active": sort == key,
"dir": direction if sort == key else None,
"target_id": target_id,
}
return headers
def web_require(*perms: str): def web_require(*perms: str):
"""Web dependency: allow superusers or holders of all given permissions.""" """Web dependency: allow superusers or holders of all given permissions."""
@@ -98,6 +132,16 @@ def web_require(*perms: str):
return checker return checker
async def web_require_superuser(user: User = Depends(get_current_web_user)) -> User:
"""Web dependency: superusers only, no permission-string bypass."""
if not user.is_superuser:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="You do not have access to this page.",
)
return user
async def _role_perm_ctx(db: AsyncSession) -> dict: async def _role_perm_ctx(db: AsyncSession) -> dict:
all_perms = await role_crud.list_permissions(db) all_perms = await role_crud.list_permissions(db)
scopes = sorted({p.name.split(":")[0] for p in all_perms}) scopes = sorted({p.name.split(":")[0] for p in all_perms})
@@ -1205,10 +1249,50 @@ async def delete_sub_part_type(
# --- Imported Parts ---------------------------------------------------------- # --- Imported Parts ----------------------------------------------------------
_IMPORTED_PARTS_SORT_COLUMNS = [
"InventoryID",
"Description",
"TBXColour",
"ProductMaxThreadDepthmm",
"ProductMaxThreadDepthin",
"ProductODmm",
"ProductODin",
"TBXThreadAttribute",
]
async def _default_imported_parts_table_ctx(db: AsyncSession) -> dict:
"""Table-card context (default sort, no filter, page 1) after a refresh."""
total = await imported_part_crud.count(db)
pagination = _paginate(
"/admin/imported-parts/filter",
{},
1,
_PAGE_SIZE,
total,
"#imported-parts-table-card",
)
return {
"parts": await imported_part_crud.list_filtered(db, limit=_PAGE_SIZE),
"pagination": pagination,
"sort_headers": _sort_headers(
"/admin/imported-parts/filter",
{},
_IMPORTED_PARTS_SORT_COLUMNS,
"InventoryID",
"asc",
"#imported-parts-table-card",
),
"sort": "InventoryID",
"dir": "asc",
}
@router.get("/imported-parts", response_class=HTMLResponse) @router.get("/imported-parts", response_class=HTMLResponse)
async def imported_parts_page( async def imported_parts_page(
request: Request, request: Request,
sort: str = "InventoryID",
dir: str = "asc",
db: AsyncSession = Depends(get_db), db: AsyncSession = Depends(get_db),
user: User = Depends(web_require("imported_parts:read")), user: User = Depends(web_require("imported_parts:read")),
): ):
@@ -1219,19 +1303,37 @@ async def imported_parts_page(
"has_password": bool(config and config.password), "has_password": bool(config and config.password),
} }
total = await imported_part_crud.count(db) total = await imported_part_crud.count(db)
parts = await imported_part_crud.list_filtered(db, limit=_PAGE_SIZE) parts = await imported_part_crud.list_filtered(
db, sort=sort, direction=dir, limit=_PAGE_SIZE
)
pagination = _paginate( pagination = _paginate(
"/admin/imported-parts/filter", "/admin/imported-parts/filter",
{}, {"sort": sort, "dir": dir},
1, 1,
_PAGE_SIZE, _PAGE_SIZE,
total, total,
"#imported-parts-table-card", "#imported-parts-table-card",
) )
sort_headers = _sort_headers(
"/admin/imported-parts/filter",
{},
_IMPORTED_PARTS_SORT_COLUMNS,
sort,
dir,
"#imported-parts-table-card",
)
return templates.TemplateResponse( return templates.TemplateResponse(
request, request,
"admin/imported_parts.html", "admin/imported_parts.html",
{"user": user, "config": config_view, "parts": parts, "pagination": pagination}, {
"user": user,
"config": config_view,
"parts": parts,
"pagination": pagination,
"sort_headers": sort_headers,
"sort": sort,
"dir": dir,
},
) )
@@ -1273,19 +1375,8 @@ async def refresh_imported_parts(
config = await imported_part_config_crud.get(db) config = await imported_part_config_crud.get(db)
async def _imported_table_ctx(sync_result=None, sync_error=None): async def _imported_table_ctx(sync_result=None, sync_error=None):
total = await imported_part_crud.count(db)
parts = await imported_part_crud.list_filtered(db, limit=_PAGE_SIZE)
pagination = _paginate(
"/admin/imported-parts/filter",
{},
1,
_PAGE_SIZE,
total,
"#imported-parts-table-card",
)
return { return {
"parts": parts, **await _default_imported_parts_table_ctx(db),
"pagination": pagination,
"sync_result": sync_result, "sync_result": sync_result,
"sync_error": sync_error, "sync_error": sync_error,
} }
@@ -1328,15 +1419,18 @@ async def refresh_imported_parts(
async def filter_imported_parts( async def filter_imported_parts(
request: Request, request: Request,
description: str = "", description: str = "",
sort: str = "InventoryID",
dir: str = "asc",
page: int = 1, page: int = 1,
page_size: int = _PAGE_SIZE, page_size: int = _PAGE_SIZE,
db: AsyncSession = Depends(get_db), db: AsyncSession = Depends(get_db),
_: User = Depends(web_require("imported_parts:read")), _: User = Depends(web_require("imported_parts:read")),
): ):
total = await imported_part_crud.count_filtered(db, description=description) total = await imported_part_crud.count_filtered(db, description=description)
filter_params = {"description": description}
pagination = _paginate( pagination = _paginate(
"/admin/imported-parts/filter", "/admin/imported-parts/filter",
{"description": description}, {**filter_params, "sort": sort, "dir": dir},
page, page,
page_size, page_size,
total, total,
@@ -1345,13 +1439,29 @@ async def filter_imported_parts(
parts = await imported_part_crud.list_filtered( parts = await imported_part_crud.list_filtered(
db, db,
description=description, description=description,
sort=sort,
direction=dir,
skip=pagination["skip"], skip=pagination["skip"],
limit=page_size, limit=page_size,
) )
sort_headers = _sort_headers(
"/admin/imported-parts/filter",
filter_params,
_IMPORTED_PARTS_SORT_COLUMNS,
sort,
dir,
"#imported-parts-table-card",
)
return templates.TemplateResponse( return templates.TemplateResponse(
request, request,
"admin/_imported_parts_table_card.html", "admin/_imported_parts_table_card.html",
{"parts": parts, "pagination": pagination}, {
"parts": parts,
"pagination": pagination,
"sort_headers": sort_headers,
"sort": sort,
"dir": dir,
},
) )
@@ -1371,11 +1481,21 @@ async def scope_import_page(
) )
_SCOPE_IMPORT_STATUS_ID = "scope-import-status"
def _scope_import_progress_ctx(job: jobs.Job) -> dict:
return {
"container_id": _SCOPE_IMPORT_STATUS_ID,
"poll_url": f"/admin/scope-import/jobs/{job.id}",
"job": job,
}
@router.post("/scope-import", response_class=HTMLResponse) @router.post("/scope-import", response_class=HTMLResponse)
async def upload_scope_import( async def upload_scope_import(
request: Request, request: Request,
file: UploadFile = File(...), file: UploadFile = File(...),
db: AsyncSession = Depends(get_db),
actor: User = Depends(web_require("augmented_parts:write")), actor: User = Depends(web_require("augmented_parts:write")),
): ):
filename = file.filename or "" filename = file.filename or ""
@@ -1383,20 +1503,26 @@ async def upload_scope_import(
return templates.TemplateResponse( return templates.TemplateResponse(
request, request,
"admin/_scope_import_result.html", "admin/_scope_import_result.html",
{"error": "Expected an .xlsx spreadsheet."}, {
"error": "Expected an .xlsx spreadsheet.",
"container_id": _SCOPE_IMPORT_STATUS_ID,
},
) )
data = await file.read() data = await file.read()
actor_id = actor.id
async def _run(job: jobs.Job) -> None:
async with AsyncSessionLocal() as db:
try: try:
result = await scope_import_service.import_from_bytes(db, data) result = await scope_import_service.import_from_bytes(db, data)
except Exception as exc: # noqa: BLE001 except Exception as exc: # noqa: BLE001
return templates.TemplateResponse( job.status = "error"
request, job.message = f"Could not read spreadsheet: {exc}"
"admin/_scope_import_result.html", return
{"error": f"Could not read spreadsheet: {exc}"}, actor_row = await user_crud.get(db, actor_id)
)
await audit_crud.log( await audit_crud.log(
db, db,
actor=actor, actor=actor_row,
action="update", action="update",
resource_type="AugmentedPart", resource_type="AugmentedPart",
resource_id=None, resource_id=None,
@@ -1406,10 +1532,45 @@ async def upload_scope_import(
f"{result.errors} errors" f"{result.errors} errors"
), ),
) )
job.status = "done"
job.result = result
job = jobs.start("scope_import", _run)
return templates.TemplateResponse(
request, "admin/_job_progress.html", _scope_import_progress_ctx(job)
)
@router.get("/scope-import/jobs/{job_id}", response_class=HTMLResponse)
async def scope_import_job_status(
request: Request,
job_id: str,
_: User = Depends(web_require("augmented_parts:write")),
):
job = jobs.get(job_id)
if job is None:
return templates.TemplateResponse( return templates.TemplateResponse(
request, request,
"admin/_scope_import_result.html", "admin/_scope_import_result.html",
{"result": result}, {
"error": "Import job not found — it may have expired.",
"container_id": _SCOPE_IMPORT_STATUS_ID,
},
)
if job.status == "running":
return templates.TemplateResponse(
request, "admin/_job_progress.html", _scope_import_progress_ctx(job)
)
if job.status == "error":
return templates.TemplateResponse(
request,
"admin/_scope_import_result.html",
{"error": job.message, "container_id": _SCOPE_IMPORT_STATUS_ID},
)
return templates.TemplateResponse(
request,
"admin/_scope_import_result.html",
{"result": job.result, "container_id": _SCOPE_IMPORT_STATUS_ID},
) )
@@ -1430,18 +1591,22 @@ async def _augmented_part_form_ctx(db: AsyncSession) -> dict:
} }
@router.get("/augmented-parts", response_class=HTMLResponse) _AUGMENTED_PARTS_SORT_COLUMNS = [
async def augmented_parts_page( "id",
request: Request, "sku",
db: AsyncSession = Depends(get_db), "description",
user: User = Depends(web_require("augmented_parts:read")), "colour",
): "manufacturer",
ctx = await _augmented_part_form_ctx(db) "scope_model",
"part_type",
"sub_part_type",
]
async def _default_augmented_parts_table_ctx(db: AsyncSession) -> dict:
"""Table-card context (default sort, no filters, page 1) after create/sync actions."""
total = await augmented_part_crud.count_filtered(db) total = await augmented_part_crud.count_filtered(db)
ctx["augmented_parts"] = await augmented_part_crud.list_filtered( pagination = _paginate(
db, limit=_PAGE_SIZE
)
ctx["pagination"] = _paginate(
"/admin/augmented-parts/filter", "/admin/augmented-parts/filter",
{}, {},
1, 1,
@@ -1449,6 +1614,55 @@ async def augmented_parts_page(
total, total,
"#augmented-parts-table-card", "#augmented-parts-table-card",
) )
return {
"augmented_parts": await augmented_part_crud.list_filtered(
db, limit=_PAGE_SIZE
),
"pagination": pagination,
"sort_headers": _sort_headers(
"/admin/augmented-parts/filter",
{},
_AUGMENTED_PARTS_SORT_COLUMNS,
"description",
"asc",
"#augmented-parts-table-card",
),
"sort": "description",
"dir": "asc",
}
@router.get("/augmented-parts", response_class=HTMLResponse)
async def augmented_parts_page(
request: Request,
sort: str = "description",
dir: str = "asc",
db: AsyncSession = Depends(get_db),
user: User = Depends(web_require("augmented_parts:read")),
):
ctx = await _augmented_part_form_ctx(db)
total = await augmented_part_crud.count_filtered(db)
ctx["augmented_parts"] = await augmented_part_crud.list_filtered(
db, sort=sort, direction=dir, limit=_PAGE_SIZE
)
ctx["pagination"] = _paginate(
"/admin/augmented-parts/filter",
{"sort": sort, "dir": dir},
1,
_PAGE_SIZE,
total,
"#augmented-parts-table-card",
)
ctx["sort_headers"] = _sort_headers(
"/admin/augmented-parts/filter",
{},
_AUGMENTED_PARTS_SORT_COLUMNS,
sort,
dir,
"#augmented-parts-table-card",
)
ctx["sort"] = sort
ctx["dir"] = dir
ctx["user"] = user ctx["user"] = user
return templates.TemplateResponse(request, "admin/augmented_parts.html", ctx) return templates.TemplateResponse(request, "admin/augmented_parts.html", ctx)
@@ -1487,39 +1701,35 @@ async def create_augmented_part(
), ),
) )
form_ctx = await _augmented_part_form_ctx(db) form_ctx = await _augmented_part_form_ctx(db)
total = await augmented_part_crud.count_filtered(db)
pagination = _paginate(
"/admin/augmented-parts/filter",
{},
1,
_PAGE_SIZE,
total,
"#augmented-parts-table-card",
)
return templates.TemplateResponse( return templates.TemplateResponse(
request, request,
"admin/_augmented_parts_table.html", "admin/_augmented_parts_table.html",
{ {**form_ctx, **await _default_augmented_parts_table_ctx(db)},
**form_ctx,
"augmented_parts": await augmented_part_crud.list_filtered(
db, limit=_PAGE_SIZE
),
"pagination": pagination,
},
) )
_AUGMENTED_PARTS_SYNC_TARGET_ID = "augmented-part-list"
@router.post("/augmented-parts/sync", response_class=HTMLResponse) @router.post("/augmented-parts/sync", response_class=HTMLResponse)
async def sync_augmented_parts( async def sync_augmented_parts(
request: Request, request: Request,
db: AsyncSession = Depends(get_db),
actor: User = Depends(web_require("augmented_parts:write")), actor: User = Depends(web_require("augmented_parts:write")),
): ):
actor_id = actor.id
async def _run(job: jobs.Job) -> None:
async with AsyncSessionLocal() as db:
try: try:
result = await augmented_part_sync_service.sync(db) result = await augmented_part_sync_service.sync(db)
except Exception as exc:
job.status = "error"
job.message = f"Sync failed: {exc}"
return
actor_row = await user_crud.get(db, actor_id)
await audit_crud.log( await audit_crud.log(
db, db,
actor=actor, actor=actor_row,
action="update", action="update",
resource_type="AugmentedPart", resource_type="AugmentedPart",
resource_id=None, resource_id=None,
@@ -1528,7 +1738,8 @@ async def sync_augmented_parts(
f"{result.skipped} skipped (no type match)" f"{result.skipped} skipped (no type match)"
), ),
) )
sync_result = ( job.status = "done"
job.message = (
f"Sync complete: {result.created} created, {result.deleted} deleted" f"Sync complete: {result.created} created, {result.deleted} deleted"
+ ( + (
f", {result.skipped} skipped (type not recognised)" f", {result.skipped} skipped (type not recognised)"
@@ -1537,31 +1748,58 @@ async def sync_augmented_parts(
) )
+ "." + "."
) )
sync_error = None
except Exception as exc: job = jobs.start("augmented_part_sync", _run)
sync_result = None return templates.TemplateResponse(
sync_error = f"Sync failed: {exc}" request,
form_ctx = await _augmented_part_form_ctx(db) "admin/_job_progress.html",
total = await augmented_part_crud.count_filtered(db) {
pagination = _paginate( "container_id": _AUGMENTED_PARTS_SYNC_TARGET_ID,
"/admin/augmented-parts/filter", "poll_url": f"/admin/augmented-parts/sync/jobs/{job.id}",
{}, "job": job,
1, },
_PAGE_SIZE,
total,
"#augmented-parts-table-card",
) )
@router.get("/augmented-parts/sync/jobs/{job_id}", response_class=HTMLResponse)
async def augmented_parts_sync_job_status(
request: Request,
job_id: str,
db: AsyncSession = Depends(get_db),
_: User = Depends(web_require("augmented_parts:write")),
):
job = jobs.get(job_id)
if job is None:
form_ctx = await _augmented_part_form_ctx(db)
return templates.TemplateResponse( return templates.TemplateResponse(
request, request,
"admin/_augmented_parts_table.html", "admin/_augmented_parts_table.html",
{ {
**form_ctx, **form_ctx,
"augmented_parts": await augmented_part_crud.list_filtered( **await _default_augmented_parts_table_ctx(db),
db, limit=_PAGE_SIZE "sync_result": None,
), "sync_error": "Sync job not found — it may have expired.",
"pagination": pagination, },
"sync_result": sync_result, )
"sync_error": sync_error, if job.status == "running":
return templates.TemplateResponse(
request,
"admin/_job_progress.html",
{
"container_id": _AUGMENTED_PARTS_SYNC_TARGET_ID,
"poll_url": f"/admin/augmented-parts/sync/jobs/{job_id}",
"job": job,
},
)
form_ctx = await _augmented_part_form_ctx(db)
return templates.TemplateResponse(
request,
"admin/_augmented_parts_table.html",
{
**form_ctx,
**await _default_augmented_parts_table_ctx(db),
"sync_result": job.message if job.status == "done" else None,
"sync_error": job.message if job.status == "error" else None,
}, },
) )
@@ -1575,6 +1813,8 @@ async def filter_augmented_parts(
scope_model_id: str = "", scope_model_id: str = "",
part_type_id: str = "", part_type_id: str = "",
sub_part_type_id: str = "", sub_part_type_id: str = "",
sort: str = "description",
dir: str = "asc",
page: int = 1, page: int = 1,
page_size: int = _PAGE_SIZE, page_size: int = _PAGE_SIZE,
db: AsyncSession = Depends(get_db), db: AsyncSession = Depends(get_db),
@@ -1604,7 +1844,7 @@ async def filter_augmented_parts(
} }
pagination = _paginate( pagination = _paginate(
"/admin/augmented-parts/filter", "/admin/augmented-parts/filter",
filter_params, {**filter_params, "sort": sort, "dir": dir},
page, page,
page_size, page_size,
total, total,
@@ -1618,13 +1858,29 @@ async def filter_augmented_parts(
scope_model_id=scope_model_id_int, scope_model_id=scope_model_id_int,
part_type_id=part_type_id_int, part_type_id=part_type_id_int,
sub_part_type_id=sub_part_type_id_int, sub_part_type_id=sub_part_type_id_int,
sort=sort,
direction=dir,
skip=pagination["skip"], skip=pagination["skip"],
limit=page_size, limit=page_size,
) )
sort_headers = _sort_headers(
"/admin/augmented-parts/filter",
filter_params,
_AUGMENTED_PARTS_SORT_COLUMNS,
sort,
dir,
"#augmented-parts-table-card",
)
return templates.TemplateResponse( return templates.TemplateResponse(
request, request,
"admin/_augmented_parts_table_card.html", "admin/_augmented_parts_table_card.html",
{"augmented_parts": parts, "pagination": pagination}, {
"augmented_parts": parts,
"pagination": pagination,
"sort_headers": sort_headers,
"sort": sort,
"dir": dir,
},
) )
@@ -1803,6 +2059,9 @@ async def augmented_part_detail(
), ),
"can_read_fits": can_read_fits, "can_read_fits": can_read_fits,
"can_read_attachment_points": can_read_attachment_points, "can_read_attachment_points": can_read_attachment_points,
"augmented_part_id": augmented_part_id,
"notes": ap.notes,
"can_write_notes": _can_write_augmented_part_notes(user),
} }
if can_read_attachment_points: if can_read_attachment_points:
ctx.update( ctx.update(
@@ -2210,19 +2469,29 @@ async def augmented_part_fits(
return templates.TemplateResponse(request, "admin/_augmented_part_fits.html", ctx) return templates.TemplateResponse(request, "admin/_augmented_part_fits.html", ctx)
def _augmented_part_fits_target_id(augmented_part_id: int) -> str:
return f"ap-{augmented_part_id}-fits"
@router.post( @router.post(
"/augmented-parts/{augmented_part_id}/fits/recompute", response_class=HTMLResponse "/augmented-parts/{augmented_part_id}/fits/recompute", response_class=HTMLResponse
) )
async def recompute_augmented_part_fits( async def recompute_augmented_part_fits(
request: Request, request: Request,
augmented_part_id: int, augmented_part_id: int,
db: AsyncSession = Depends(get_db),
actor: User = Depends(web_require("fits:write")), actor: User = Depends(web_require("fits:write")),
): ):
result = await fit_matching_service.recompute_for_part(db, augmented_part_id) actor_id = actor.id
async def _run(job: jobs.Job) -> None:
async with AsyncSessionLocal() as db:
result = await fit_matching_service.recompute_for_part(
db, augmented_part_id
)
actor_row = await user_crud.get(db, actor_id)
await audit_crud.log( await audit_crud.log(
db, db,
actor=actor, actor=actor_row,
action="update", action="update",
resource_type="Fit", resource_type="Fit",
resource_id=augmented_part_id, resource_id=augmented_part_id,
@@ -2231,11 +2500,63 @@ async def recompute_augmented_part_fits(
f"{result.created} created, {result.removed} removed" f"{result.created} created, {result.removed} removed"
), ),
) )
job.status = "done"
job.message = f"{result.created} created, {result.removed} removed."
job = jobs.start("fit_recompute_part", _run)
return templates.TemplateResponse(
request,
"admin/_job_progress.html",
{
"container_id": _augmented_part_fits_target_id(augmented_part_id),
"poll_url": f"/admin/augmented-parts/{augmented_part_id}/fits/recompute/jobs/{job.id}",
"job": job,
"extra_class": "ap-inline-section",
},
)
@router.get(
"/augmented-parts/{augmented_part_id}/fits/recompute/jobs/{job_id}",
response_class=HTMLResponse,
)
async def augmented_part_fits_recompute_job_status(
request: Request,
augmented_part_id: int,
job_id: str,
db: AsyncSession = Depends(get_db),
actor: User = Depends(web_require("fits:write")),
):
job = jobs.get(job_id)
if job is None:
ctx = await _augmented_part_fits_ctx( ctx = await _augmented_part_fits_ctx(
db, db,
augmented_part_id, augmented_part_id,
can_write=_can_write_fits(actor), can_write=_can_write_fits(actor),
message=f"{result.created} created, {result.removed} removed.", error="Recompute job not found — it may have expired.",
)
return templates.TemplateResponse(
request, "admin/_augmented_part_fits.html", ctx
)
if job.status == "running":
return templates.TemplateResponse(
request,
"admin/_job_progress.html",
{
"container_id": _augmented_part_fits_target_id(augmented_part_id),
"poll_url": (
f"/admin/augmented-parts/{augmented_part_id}/fits/recompute/jobs/{job_id}"
),
"job": job,
"extra_class": "ap-inline-section",
},
)
ctx = await _augmented_part_fits_ctx(
db,
augmented_part_id,
can_write=_can_write_fits(actor),
message=job.message if job.status == "done" else None,
error=job.message if job.status == "error" else None,
) )
return templates.TemplateResponse(request, "admin/_augmented_part_fits.html", ctx) return templates.TemplateResponse(request, "admin/_augmented_part_fits.html", ctx)
@@ -2268,24 +2589,64 @@ async def create_augmented_part_fit(
request: Request, request: Request,
augmented_part_id: int, augmented_part_id: int,
own_attachment_point_id: int = Form(...), own_attachment_point_id: int = Form(...),
other_augmented_part_id: str = Form(""),
other_attachment_point_id: str = Form(""), other_attachment_point_id: str = Form(""),
label: FitLabel = Form(FitLabel.EXACT),
note: str = Form(""),
db: AsyncSession = Depends(get_db), db: AsyncSession = Depends(get_db),
actor: User = Depends(web_require("fits:write")), actor: User = Depends(web_require("fits:write")),
): ):
error = None error = None
if not other_attachment_point_id: auto_created_ap = None
ap_a = await attachment_point_crud.get(db, own_attachment_point_id)
ap_b = None
if ap_a is None:
error = "Attachment point not found."
elif other_attachment_point_id:
ap_b = await attachment_point_crud.get(db, int(other_attachment_point_id))
if ap_b is None:
error = "Attachment point not found."
elif other_augmented_part_id:
other_part = await augmented_part_crud.get(db, int(other_augmented_part_id))
if other_part is None:
error = "Part not found."
elif await attachment_point_crud.list_for_augmented_part(db, other_part.id):
error = "Select the other part's attachment point." error = "Select the other part's attachment point."
else: else:
ap_a = await attachment_point_crud.get(db, own_attachment_point_id) ap_b = await attachment_point_crud.create(
ap_b = await attachment_point_crud.get(db, int(other_attachment_point_id)) db,
if ap_a is None or ap_b is None: augmented_part_id=other_part.id,
error = "Attachment point not found." attachment_point_name_id=ap_a.attachment_point_name_id,
elif ap_a.augmented_part_id == ap_b.augmented_part_id: iso_thread_id=ap_a.iso_thread_id,
unc_thread_id=ap_a.unc_thread_id,
clamp_connection_id=ap_a.clamp_connection_id,
static_connection_id=ap_a.static_connection_id,
)
auto_created_ap = ap_b
else:
error = "Select the other part's attachment point."
if error is None and ap_b is not None:
if ap_a.augmented_part_id == ap_b.augmented_part_id:
error = "A part cannot fit itself." error = "A part cannot fit itself."
elif await fit_crud.get_by_attachment_points(db, ap_a, ap_b) is not None: elif await fit_crud.get_by_attachment_points(db, ap_a, ap_b) is not None:
error = "That fit already exists." error = "That fit already exists."
else: else:
fit = await fit_crud.create(db, ap_a=ap_a, ap_b=ap_b, label=FitLabel.EXACT) if auto_created_ap is not None:
await audit_crud.log(
db,
actor=actor,
action="create",
resource_type="AttachmentPoint",
resource_id=auto_created_ap.id,
description=audit_crud.describe_create(
"AttachmentPoint",
auto_created_ap.id,
f"auto-created on AugmentedPart #{auto_created_ap.augmented_part_id} "
f"to match attachment point {ap_a.id}",
),
)
fit = await fit_crud.create(db, ap_a=ap_a, ap_b=ap_b, label=label)
await audit_crud.log( await audit_crud.log(
db, db,
actor=actor, actor=actor,
@@ -2293,9 +2654,14 @@ async def create_augmented_part_fit(
resource_type="Fit", resource_type="Fit",
resource_id=fit.id, resource_id=fit.id,
description=audit_crud.describe_create( description=audit_crud.describe_create(
"Fit", fit.id, f"attachment points {ap_a.id}↔{ap_b.id}" "Fit",
fit.id,
f"attachment points {ap_a.id}↔{ap_b.id} ({label.value})",
), ),
) )
if note.strip():
await fit_note_crud.create(db, fit=fit, author=actor, body=note.strip())
await db.refresh(fit, attribute_names=["notes"])
ctx = await _augmented_part_fits_ctx( ctx = await _augmented_part_fits_ctx(
db, augmented_part_id, can_write=_can_write_fits(actor), error=error db, augmented_part_id, can_write=_can_write_fits(actor), error=error
) )
@@ -2398,16 +2764,23 @@ async def delete_augmented_part_fit(
return templates.TemplateResponse(request, "admin/_augmented_part_fits.html", ctx) return templates.TemplateResponse(request, "admin/_augmented_part_fits.html", ctx)
_ALL_FITS_RECOMPUTE_STATUS_ID = "fits-recompute-status"
@router.post("/augmented-parts/fits/recompute", response_class=HTMLResponse) @router.post("/augmented-parts/fits/recompute", response_class=HTMLResponse)
async def recompute_all_fits( async def recompute_all_fits(
request: Request, request: Request,
db: AsyncSession = Depends(get_db),
actor: User = Depends(web_require("fits:write")), actor: User = Depends(web_require("fits:write")),
): ):
actor_id = actor.id
async def _run(job: jobs.Job) -> None:
async with AsyncSessionLocal() as db:
result = await fit_matching_service.recompute_all(db) result = await fit_matching_service.recompute_all(db)
actor_row = await user_crud.get(db, actor_id)
await audit_crud.log( await audit_crud.log(
db, db,
actor=actor, actor=actor_row,
action="update", action="update",
resource_type="Fit", resource_type="Fit",
resource_id=None, resource_id=None,
@@ -2416,8 +2789,82 @@ async def recompute_all_fits(
f"{result.removed} removed" f"{result.removed} removed"
), ),
) )
return HTMLResponse( job.status = "done"
f"Recompute complete: {result.created} created, {result.removed} removed." job.message = (
f"Recompute complete: {result.created} created, "
f"{result.removed} removed."
)
job = jobs.start("fit_recompute_all", _run)
return templates.TemplateResponse(
request,
"admin/_job_progress_inline.html",
{
"container_id": _ALL_FITS_RECOMPUTE_STATUS_ID,
"poll_url": f"/admin/augmented-parts/fits/recompute/jobs/{job.id}",
"job": job,
},
)
@router.get(
"/augmented-parts/fits/recompute/jobs/{job_id}", response_class=HTMLResponse
)
async def recompute_all_fits_job_status(
request: Request,
job_id: str,
_: User = Depends(web_require("fits:write")),
):
job = jobs.get(job_id)
if job is None:
return HTMLResponse("Recompute job not found — it may have expired.")
if job.status == "running":
return templates.TemplateResponse(
request,
"admin/_job_progress_inline.html",
{
"container_id": _ALL_FITS_RECOMPUTE_STATUS_ID,
"poll_url": f"/admin/augmented-parts/fits/recompute/jobs/{job_id}",
"job": job,
},
)
return HTMLResponse(job.message)
# --- Augmented Part Notes -------------------------------------------------
def _can_write_augmented_part_notes(user: User) -> bool:
return user.is_superuser or "augmented_parts:write" in user.permissions
@router.post(
"/augmented-parts/{augmented_part_id}/notes",
response_class=HTMLResponse,
)
async def create_augmented_part_note(
request: Request,
augmented_part_id: int,
body: str = Form(...),
db: AsyncSession = Depends(get_db),
actor: User = Depends(web_require("augmented_parts:write")),
):
ap = await augmented_part_crud.get(db, augmented_part_id)
if ap is None:
raise HTTPException(status_code=404, detail="Augmented part not found")
if body.strip():
await augmented_part_note_crud.create(
db, augmented_part=ap, author=actor, body=body.strip()
)
await db.refresh(ap, attribute_names=["notes"])
return templates.TemplateResponse(
request,
"admin/_augmented_part_notes.html",
{
"augmented_part_id": augmented_part_id,
"notes": ap.notes,
"can_write_notes": _can_write_augmented_part_notes(actor),
},
) )
@@ -3162,3 +3609,45 @@ async def audit_log_page(
"admin/audit_log.html", "admin/audit_log.html",
{"user": user, "entries": await audit_crud.list_recent(db)}, {"user": user, "entries": await audit_crud.list_recent(db)},
) )
# --- Danger Zone ---------------------------------------------------------------
@router.get("/danger-zone", response_class=HTMLResponse)
async def danger_zone_page(
request: Request,
user: User = Depends(web_require_superuser),
):
return templates.TemplateResponse(
request,
"admin/danger_zone.html",
{"user": user},
)
@router.post("/danger-zone/purge", response_class=HTMLResponse)
async def purge_database(
request: Request,
db: AsyncSession = Depends(get_db),
actor: User = Depends(web_require_superuser),
):
counts = await danger_zone_service.purge_domain_data(db)
total = sum(counts.values())
breakdown = ", ".join(f"{label} {n}" for label, n in counts.items() if n)
await audit_crud.log(
db,
actor=actor,
action="delete",
resource_type="Database",
resource_id=None,
description=(
f"Purged all domain data via Danger Zone ({total} row(s) deleted"
+ (f": {breakdown})" if breakdown else ")")
),
)
return templates.TemplateResponse(
request,
"admin/_danger_zone_result.html",
{"total": total, "counts": counts},
)
+156
View File
@@ -0,0 +1,156 @@
import asyncio
import re
import pytest
from app.crud import role as role_crud
from app.models.attachment_point import AttachmentPoint
from app.models.attachment_point_name import AttachmentPointName
from app.models.augmented_part import AugmentedPart
from app.models.part_type import PartType
from tests.test_scope_import import _scope_row, _workbook
_JOB_URL_RE = re.compile(r'hx-get="([^"]+/jobs/[^"]+)"')
async def _web_login(client, email, password="secret123"):
resp = await client.post("/login", data={"email": email, "password": password})
assert resp.status_code == 303
async def _poll_until_done(client, url, max_tries=50):
"""Follow the same htmx polling loop a browser would, until no further
``hx-get`` job-status link is present in the response."""
body = ""
for _ in range(max_tries):
resp = await client.get(url)
assert resp.status_code == 200
body = resp.text
match = _JOB_URL_RE.search(body)
if not match:
return body
url = match.group(1)
await asyncio.sleep(0.02)
raise AssertionError(f"job did not finish after {max_tries} polls: {body!r}")
@pytest.mark.asyncio
async def test_scope_import_runs_in_background(
client, seeded, make_user, db_sessionmaker, monkeypatch
):
monkeypatch.setattr("app.web.admin.AsyncSessionLocal", db_sessionmaker)
async with db_sessionmaker() as db:
perms = seeded["perms"]
editor = await role_crud.create(
db,
name="scope-import-editor",
permission_ids=[perms["augmented_parts:write"].id],
)
editor_id = editor.id
await make_user("importer@example.com", role_ids=[editor_id])
await _web_login(client, "importer@example.com")
data = _workbook([_scope_row(product_number="BG-SCOPE-1")])
resp = await client.post(
"/admin/scope-import",
files={"file": ("scopes.xlsx", data, "application/octet-stream")},
)
assert resp.status_code == 200
assert "scope-import-status" in resp.text
match = _JOB_URL_RE.search(resp.text)
assert match, resp.text
final_body = await _poll_until_done(client, match.group(1))
assert "1</strong> created" in final_body
@pytest.mark.asyncio
async def test_augmented_parts_sync_runs_in_background(
client, seeded, make_user, db_sessionmaker, monkeypatch
):
monkeypatch.setattr("app.web.admin.AsyncSessionLocal", db_sessionmaker)
async with db_sessionmaker() as db:
perms = seeded["perms"]
editor = await role_crud.create(
db, name="sync-editor", permission_ids=[perms["augmented_parts:write"].id]
)
editor_id = editor.id
await make_user("syncer@example.com", role_ids=[editor_id])
await _web_login(client, "syncer@example.com")
resp = await client.post("/admin/augmented-parts/sync")
assert resp.status_code == 200
assert 'id="augmented-part-list"' in resp.text
match = _JOB_URL_RE.search(resp.text)
assert match, resp.text
final_body = await _poll_until_done(client, match.group(1))
assert "Sync complete" in final_body
@pytest.mark.asyncio
async def test_fit_recompute_for_part_runs_in_background(
client, seeded, make_user, db_sessionmaker, monkeypatch
):
monkeypatch.setattr("app.web.admin.AsyncSessionLocal", db_sessionmaker)
async with db_sessionmaker() as db:
apn = AttachmentPointName(name="Objective")
part_t = PartType(name="Scope")
db.add_all([apn, part_t])
await db.flush()
part = AugmentedPart(
description="Test scope",
colour_id=1,
manufacturer_id=1,
part_type_id=part_t.id,
)
db.add(part)
await db.flush()
db.add(
AttachmentPoint(augmented_part_id=part.id, attachment_point_name_id=apn.id)
)
await db.commit()
part_id = part.id
perms = seeded["perms"]
editor = await role_crud.create(
db,
name="fit-recompute-editor",
permission_ids=[perms["fits:write"].id, perms["augmented_parts:read"].id],
)
editor_id = editor.id
await make_user("recomputer@example.com", role_ids=[editor_id])
await _web_login(client, "recomputer@example.com")
resp = await client.post(f"/admin/augmented-parts/{part_id}/fits/recompute")
assert resp.status_code == 200
assert f'id="ap-{part_id}-fits"' in resp.text
match = _JOB_URL_RE.search(resp.text)
assert match, resp.text
final_body = await _poll_until_done(client, match.group(1))
assert "created" in final_body and "removed" in final_body
@pytest.mark.asyncio
async def test_fit_recompute_all_runs_in_background(
client, seeded, make_user, db_sessionmaker, monkeypatch
):
monkeypatch.setattr("app.web.admin.AsyncSessionLocal", db_sessionmaker)
async with db_sessionmaker() as db:
perms = seeded["perms"]
editor = await role_crud.create(
db, name="fit-recompute-all-editor", permission_ids=[perms["fits:write"].id]
)
editor_id = editor.id
await make_user("recomputerall@example.com", role_ids=[editor_id])
await _web_login(client, "recomputerall@example.com")
resp = await client.post("/admin/augmented-parts/fits/recompute")
assert resp.status_code == 200
assert "fits-recompute-status" in resp.text
match = _JOB_URL_RE.search(resp.text)
assert match, resp.text
final_body = await _poll_until_done(client, match.group(1))
assert "Recompute complete" in final_body
+148
View File
@@ -0,0 +1,148 @@
import pytest
from app.crud import augmented_part as augmented_part_crud
from app.models.augmented_part import AugmentedPart
from app.models.imported_part import ImportedPart
from app.models.manufacturer import Manufacturer
from app.models.part_colour import PartColour
from app.models.part_type import PartType
async def _web_login(client, email, password="secret123"):
resp = await client.post("/login", data={"email": email, "password": password})
assert resp.status_code == 303
async def _seed_augmented_parts(db):
part_t = PartType(name="Scope")
db.add(part_t)
await db.flush()
parts = [
AugmentedPart(
description=desc,
sku=sku,
colour_id=1,
manufacturer_id=1,
part_type_id=part_t.id,
)
for desc, sku in [
("Alpha scope", "SKU-C"),
("Bravo scope", "SKU-A"),
("Charlie scope", "SKU-B"),
]
]
db.add_all(parts)
await db.commit()
async def _seed_imported_parts(db):
db.add_all(
[
ImportedPart(InventoryID="INV-2", Description="Charlie widget"),
ImportedPart(InventoryID="INV-1", Description="Alpha widget"),
ImportedPart(InventoryID="INV-3", Description="Bravo widget"),
]
)
await db.commit()
def _order_of(body: str, needles: list[str]) -> list[str]:
positions = {n: body.find(n) for n in needles}
return sorted(positions, key=lambda n: positions[n])
@pytest.mark.asyncio
async def test_augmented_parts_page_sorts_by_description_desc(
client, seeded, make_user, db_sessionmaker
):
async with db_sessionmaker() as db:
await _seed_augmented_parts(db)
await make_user("reader@example.com", role_ids=[seeded["roles"]["user"].id])
await _web_login(client, "reader@example.com")
resp = await client.get("/admin/augmented-parts?sort=description&dir=desc")
assert resp.status_code == 200
body = resp.text
assert _order_of(body, ["Charlie scope", "Bravo scope", "Alpha scope"]) == [
"Charlie scope",
"Bravo scope",
"Alpha scope",
]
@pytest.mark.asyncio
async def test_augmented_parts_filter_sorts_by_sku_asc(
client, seeded, make_user, db_sessionmaker
):
async with db_sessionmaker() as db:
await _seed_augmented_parts(db)
await make_user("reader2@example.com", role_ids=[seeded["roles"]["user"].id])
await _web_login(client, "reader2@example.com")
resp = await client.get("/admin/augmented-parts/filter?sort=sku&dir=asc")
assert resp.status_code == 200
body = resp.text
# SKU-A (Bravo), SKU-B (Charlie), SKU-C (Alpha)
assert _order_of(body, ["Bravo scope", "Charlie scope", "Alpha scope"]) == [
"Bravo scope",
"Charlie scope",
"Alpha scope",
]
@pytest.mark.asyncio
async def test_imported_parts_page_sorts_by_description_desc(
client, seeded, make_user, db_sessionmaker
):
async with db_sessionmaker() as db:
await _seed_imported_parts(db)
await make_user("reader3@example.com", role_ids=[seeded["roles"]["user"].id])
await _web_login(client, "reader3@example.com")
resp = await client.get("/admin/imported-parts?sort=Description&dir=desc")
assert resp.status_code == 200
body = resp.text
assert _order_of(body, ["Charlie widget", "Bravo widget", "Alpha widget"]) == [
"Charlie widget",
"Bravo widget",
"Alpha widget",
]
@pytest.mark.asyncio
async def test_list_filtered_sorts_by_joined_colour_name(db_sessionmaker):
async with db_sessionmaker() as db:
red = PartColour(name="Red")
blue = PartColour(name="Blue")
manufacturer = Manufacturer(name="Acme", url="https://acme.example")
part_t = PartType(name="Scope")
db.add_all([red, blue, manufacturer, part_t])
await db.flush()
db.add_all(
[
AugmentedPart(
description="Red one",
colour_id=red.id,
manufacturer_id=manufacturer.id,
part_type_id=part_t.id,
),
AugmentedPart(
description="Blue one",
colour_id=blue.id,
manufacturer_id=manufacturer.id,
part_type_id=part_t.id,
),
]
)
await db.commit()
parts = await augmented_part_crud.list_filtered(
db, sort="colour", direction="asc"
)
assert [p.description for p in parts] == ["Blue one", "Red one"]
parts_desc = await augmented_part_crud.list_filtered(
db, sort="colour", direction="desc"
)
assert [p.description for p in parts_desc] == ["Red one", "Blue one"]
+202 -9
View File
@@ -3,8 +3,9 @@ import pytest
from app.crud import role as role_crud from app.crud import role as role_crud
from app.models.attachment_point import AttachmentPoint from app.models.attachment_point import AttachmentPoint
from app.models.attachment_point_name import AttachmentPointName from app.models.attachment_point_name import AttachmentPointName
from app.models.augmented_part import AugmentedPart from app.models.augmented_part import AugmentedPart, AugmentedPartNote
from app.models.fit import Fit, FitLabel from app.models.fit import Fit, FitLabel, FitNote
from app.models.iso_thread import IsoThread
from app.models.part_type import PartType from app.models.part_type import PartType
from app.models.scope_info import ScopeInfo from app.models.scope_info import ScopeInfo
@@ -40,12 +41,8 @@ async def _seed_part_with_fit(db):
objective_thread_pitch="M52x0.75", objective_thread_pitch="M52x0.75",
) )
) )
ap_s = AttachmentPoint( ap_s = AttachmentPoint(augmented_part_id=scope.id, attachment_point_name_id=apn.id)
augmented_part_id=scope.id, attachment_point_name_id=apn.id ap_c = AttachmentPoint(augmented_part_id=cover.id, attachment_point_name_id=apn.id)
)
ap_c = AttachmentPoint(
augmented_part_id=cover.id, attachment_point_name_id=apn.id
)
db.add_all([ap_s, ap_c]) db.add_all([ap_s, ap_c])
await db.flush() await db.flush()
@@ -91,6 +88,7 @@ async def test_detail_shows_attributes_and_fits_readonly(
assert "Add fit" not in body assert "Add fit" not in body
assert "Mark Exact" not in body assert "Mark Exact" not in body
assert "Recompute possible fits" not in body assert "Recompute possible fits" not in body
assert "Add note" not in body
# ...and attachment-point add/edit/delete controls are hidden too # ...and attachment-point add/edit/delete controls are hidden too
assert 'name="attachment_point_name_id"' not in body assert 'name="attachment_point_name_id"' not in body
assert "Delete this attachment point?" not in body assert "Delete this attachment point?" not in body
@@ -151,6 +149,199 @@ async def test_detail_write_controls_for_fit_editor(
assert "Recompute possible fits" in body assert "Recompute possible fits" in body
@pytest.mark.asyncio
async def test_create_fit_auto_creates_matching_attachment_point(
client, seeded, make_user, db_sessionmaker
):
async with db_sessionmaker() as db:
apn = AttachmentPointName(name="Objective")
scope_t = PartType(name="Scope")
cover_t = PartType(name="Flip Cover")
db.add_all([apn, scope_t, cover_t])
await db.flush()
thread = IsoThread(diameter_mm=42.0, pitch_mm=0.75)
db.add(thread)
await db.flush()
scope = AugmentedPart(
description="Alpha 3-18x50",
sku="ALP-318",
colour_id=1,
manufacturer_id=1,
part_type_id=scope_t.id,
)
cover = AugmentedPart(
description="Flip cover 50mm",
colour_id=1,
manufacturer_id=1,
part_type_id=cover_t.id,
)
db.add_all([scope, cover])
await db.flush()
own_ap = AttachmentPoint(
augmented_part_id=scope.id,
attachment_point_name_id=apn.id,
iso_thread_id=thread.id,
)
db.add(own_ap)
await db.commit()
scope_id, cover_id, own_ap_id = scope.id, cover.id, own_ap.id
perms = seeded["perms"]
editor = await role_crud.create(
db,
name="fit-editor2",
permission_ids=[
perms["augmented_parts:read"].id,
perms["fits:read"].id,
perms["fits:write"].id,
],
)
editor_id = editor.id
await make_user("editor2@example.com", role_ids=[editor_id])
await _web_login(client, "editor2@example.com")
resp = await client.post(
f"/admin/augmented-parts/{scope_id}/fits",
data={
"own_attachment_point_id": own_ap_id,
"other_augmented_part_id": cover_id,
"other_attachment_point_id": "",
"label": "possible",
},
)
assert resp.status_code == 200
body = resp.text
assert "already exists" not in body
assert "not found" not in body
async with db_sessionmaker() as db:
cover_points = [
ap
for ap in (await db.execute(AttachmentPoint.__table__.select())).all()
if ap.augmented_part_id == cover_id
]
assert len(cover_points) == 1
assert cover_points[0].iso_thread_id == thread.id
assert cover_points[0].attachment_point_name_id == apn.id
fits = (await db.execute(Fit.__table__.select())).all()
assert len(fits) == 1
assert fits[0].label == FitLabel.POSSIBLE
@pytest.mark.asyncio
async def test_create_fit_with_note_creates_fit_note(
client, seeded, make_user, db_sessionmaker
):
async with db_sessionmaker() as db:
apn = AttachmentPointName(name="Objective")
scope_t = PartType(name="Scope")
cover_t = PartType(name="Flip Cover")
db.add_all([apn, scope_t, cover_t])
await db.flush()
scope = AugmentedPart(
description="Alpha 3-18x50",
sku="ALP-318",
colour_id=1,
manufacturer_id=1,
part_type_id=scope_t.id,
)
cover = AugmentedPart(
description="Flip cover 50mm",
colour_id=1,
manufacturer_id=1,
part_type_id=cover_t.id,
)
db.add_all([scope, cover])
await db.flush()
own_ap = AttachmentPoint(
augmented_part_id=scope.id, attachment_point_name_id=apn.id
)
other_ap = AttachmentPoint(
augmented_part_id=cover.id, attachment_point_name_id=apn.id
)
db.add_all([own_ap, other_ap])
await db.commit()
scope_id, own_ap_id, other_ap_id = scope.id, own_ap.id, other_ap.id
perms = seeded["perms"]
editor = await role_crud.create(
db,
name="fit-editor3",
permission_ids=[
perms["augmented_parts:read"].id,
perms["fits:read"].id,
perms["fits:write"].id,
],
)
editor_id = editor.id
await make_user("editor3@example.com", role_ids=[editor_id])
await _web_login(client, "editor3@example.com")
resp = await client.post(
f"/admin/augmented-parts/{scope_id}/fits",
data={
"own_attachment_point_id": own_ap_id,
"other_attachment_point_id": other_ap_id,
"label": "exact",
"note": "Confirmed fit on the bench.",
},
)
assert resp.status_code == 200
body = resp.text
assert "Confirmed fit on the bench." in body
async with db_sessionmaker() as db:
fits = (await db.execute(Fit.__table__.select())).all()
assert len(fits) == 1
notes = (await db.execute(FitNote.__table__.select())).all()
assert len(notes) == 1
assert notes[0].fit_id == fits[0].id
assert notes[0].body == "Confirmed fit on the bench."
@pytest.mark.asyncio
async def test_create_augmented_part_note(client, seeded, make_user, db_sessionmaker):
async with db_sessionmaker() as db:
ids = await _seed_part_with_fit(db)
perms = seeded["perms"]
editor = await role_crud.create(
db,
name="ap-note-editor",
permission_ids=[
perms["augmented_parts:read"].id,
perms["augmented_parts:write"].id,
],
)
editor_id = editor.id
await make_user("noteeditor@example.com", role_ids=[editor_id])
await _web_login(client, "noteeditor@example.com")
resp = await client.get(f"/admin/augmented-parts/{ids['scope_id']}")
assert resp.status_code == 200
assert "Add note" in resp.text
resp = await client.post(
f"/admin/augmented-parts/{ids['scope_id']}/notes",
data={"body": "Verified thread depth on the bench."},
)
assert resp.status_code == 200
assert "Verified thread depth on the bench." in resp.text
async with db_sessionmaker() as db:
notes = (await db.execute(AugmentedPartNote.__table__.select())).all()
assert len(notes) == 1
assert notes[0].augmented_part_id == ids["scope_id"]
assert notes[0].body == "Verified thread depth on the bench."
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_detail_404_for_missing_part(client, seeded, make_user): async def test_detail_404_for_missing_part(client, seeded, make_user):
await make_user("reader2@example.com", role_ids=[seeded["roles"]["user"].id]) await make_user("reader2@example.com", role_ids=[seeded["roles"]["user"].id])
@@ -160,7 +351,9 @@ async def test_detail_404_for_missing_part(client, seeded, make_user):
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_detail_requires_read_permission(client, seeded, make_user, db_sessionmaker): async def test_detail_requires_read_permission(
client, seeded, make_user, db_sessionmaker
):
async with db_sessionmaker() as db: async with db_sessionmaker() as db:
ids = await _seed_part_with_fit(db) ids = await _seed_part_with_fit(db)
await make_user("plain@example.com") # no roles, not superuser await make_user("plain@example.com") # no roles, not superuser
+157
View File
@@ -0,0 +1,157 @@
import pytest
from app.crud import audit as audit_crud
from app.models.attachment_point import AttachmentPoint
from app.models.attachment_point_name import AttachmentPointName
from app.models.augmented_part import AugmentedPart, AugmentedPartNote
from app.models.clamp_connection import ClampConnection
from app.models.fit import Fit, FitLabel, FitNote
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.scope_info import ScopeInfo
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.unc_thread import UncSeries, UncThread
async def _seed_domain_data(db):
manufacturer = Manufacturer(name="Acme", url="https://acme.test")
colour = PartColour(name="Black")
scope_t = PartType(name="Scope")
cover_t = PartType(name="Flip Cover")
apn = AttachmentPointName(name="Objective")
db.add_all([manufacturer, colour, scope_t, cover_t, apn])
await db.flush()
sub_t = SubPartType(name="Riflescope", part_type_id=scope_t.id)
scope_model = ScopeModel(name="Alpha", manufacturer_id=manufacturer.id)
iso = IsoThread(diameter_mm=52.0, pitch_mm=0.75)
unc = UncThread(diameter_fraction="1/4", tpi=20, series=UncSeries.UNC)
clamp = ClampConnection(min_mm=25.0, max_mm=30.0)
static = StaticConnection(od_mm=50.0, od_in=1.97)
db.add_all([sub_t, scope_model, iso, unc, clamp, static])
await db.flush()
scope = AugmentedPart(
description="Alpha 3-18x50",
sku="ALP-318",
colour_id=colour.id,
manufacturer_id=manufacturer.id,
part_type_id=scope_t.id,
scope_model_id=scope_model.id,
sub_part_type_id=sub_t.id,
)
cover = AugmentedPart(
description="Flip cover 50mm",
colour_id=colour.id,
manufacturer_id=manufacturer.id,
part_type_id=cover_t.id,
)
db.add_all([scope, cover])
await db.flush()
db.add(AugmentedPartNote(augmented_part_id=scope.id, username="tester", body="note"))
db.add(ScopeInfo(augmented_part_id=scope.id, magnification_min=3, magnification_max=18))
ap_s = AttachmentPoint(
augmented_part_id=scope.id,
attachment_point_name_id=apn.id,
iso_thread_id=iso.id,
)
ap_c = AttachmentPoint(
augmented_part_id=cover.id,
attachment_point_name_id=apn.id,
iso_thread_id=iso.id,
)
db.add_all([ap_s, ap_c])
await db.flush()
fit = Fit(
attachment_point_a_id=ap_s.id,
attachment_point_b_id=ap_c.id,
augmented_part_a_id=scope.id,
augmented_part_b_id=cover.id,
label=FitLabel.POSSIBLE,
)
db.add(fit)
await db.flush()
db.add(FitNote(fit_id=fit.id, username="tester", body="fits fine"))
imported = ImportedPart(InventoryID="INV-1", Description="Alpha 3-18x50")
db.add(imported)
config = ImportedPartConfig(id=1, odata_url="https://example.test/odata", username="svc")
db.add(config)
await db.commit()
async def _web_login(client, email, password="secret123"):
resp = await client.post("/login", data={"email": email, "password": password})
assert resp.status_code == 303
@pytest.mark.asyncio
async def test_non_superuser_forbidden(client, seeded, make_user):
# "admin" role holds every permission but is not a superuser.
await make_user("admin@example.com", role_ids=[seeded["roles"]["admin"].id])
await _web_login(client, "admin@example.com")
assert (await client.get("/admin/danger-zone")).status_code == 403
assert (await client.post("/admin/danger-zone/purge")).status_code == 403
@pytest.mark.asyncio
async def test_purge_wipes_domain_data_but_keeps_users_and_odata_config(
client, seeded, make_user, db_sessionmaker
):
async with db_sessionmaker() as db:
await _seed_domain_data(db)
await make_user("root@example.com", is_superuser=True)
await _web_login(client, "root@example.com")
assert (await client.get("/admin/danger-zone")).status_code == 200
resp = await client.post("/admin/danger-zone/purge")
assert resp.status_code == 200
assert "Purge complete" in resp.text
async with db_sessionmaker() as db:
for Model in (
AugmentedPart,
AugmentedPartNote,
ScopeInfo,
AttachmentPoint,
Fit,
FitNote,
ImportedPart,
ScopeModel,
SubPartType,
PartType,
AttachmentPointName,
IsoThread,
UncThread,
ClampConnection,
StaticConnection,
PartColour,
Manufacturer,
):
rows = (await db.execute(Model.__table__.select())).all()
assert rows == [], f"{Model.__name__} should be empty after purge"
# Kept: users, RBAC, and the OData connection config.
from app.crud import user as user_crud
assert await user_crud.get_by_email(db, "root@example.com") is not None
config = await db.get(ImportedPartConfig, 1)
assert config is not None
assert config.odata_url == "https://example.test/odata"
entries = await audit_crud.list_recent(db)
assert any("Purged all domain data" in e.description for e in entries)
+34 -8
View File
@@ -34,20 +34,40 @@ def test_same_label_never_matches_even_with_identical_threads():
def test_unc_thread_exact_match(): def test_unc_thread_exact_match():
a = _ap(1, 1, unc_thread=UncThread(diameter_fraction="1/4", tpi=20, series=UncSeries.UNC)) a = _ap(
b = _ap(2, 2, unc_thread=UncThread(diameter_fraction="1/4", tpi=20, series=UncSeries.UNC)) 1,
1,
unc_thread=UncThread(diameter_fraction="1/4", tpi=20, series=UncSeries.UNC),
)
b = _ap(
2,
2,
unc_thread=UncThread(diameter_fraction="1/4", tpi=20, series=UncSeries.UNC),
)
assert attachment_points_match(a, b) assert attachment_points_match(a, b)
def test_unc_thread_series_mismatch(): def test_unc_thread_series_mismatch():
a = _ap(1, 1, unc_thread=UncThread(diameter_fraction="1/4", tpi=20, series=UncSeries.UNC)) a = _ap(
b = _ap(2, 2, unc_thread=UncThread(diameter_fraction="1/4", tpi=20, series=UncSeries.UNF)) 1,
1,
unc_thread=UncThread(diameter_fraction="1/4", tpi=20, series=UncSeries.UNC),
)
b = _ap(
2,
2,
unc_thread=UncThread(diameter_fraction="1/4", tpi=20, series=UncSeries.UNF),
)
assert not attachment_points_match(a, b) assert not attachment_points_match(a, b)
def test_iso_and_unc_never_match_each_other(): def test_iso_and_unc_never_match_each_other():
a = _ap(1, 1, iso_thread=IsoThread(diameter_mm=8, pitch_mm=1.25)) a = _ap(1, 1, iso_thread=IsoThread(diameter_mm=8, pitch_mm=1.25))
b = _ap(2, 2, unc_thread=UncThread(diameter_fraction="1/4", tpi=20, series=UncSeries.UNC)) b = _ap(
2,
2,
unc_thread=UncThread(diameter_fraction="1/4", tpi=20, series=UncSeries.UNC),
)
assert not attachment_points_match(a, b) assert not attachment_points_match(a, b)
@@ -94,9 +114,15 @@ def test_two_bare_attachment_points_never_match():
def test_parts_could_fit_requires_different_types_and_identity(): def test_parts_could_fit_requires_different_types_and_identity():
a = AugmentedPart(id=1, part_type_id=1, description="a", colour_id=1, manufacturer_id=1) a = AugmentedPart(
b = AugmentedPart(id=2, part_type_id=1, description="b", colour_id=1, manufacturer_id=1) id=1, part_type_id=1, description="a", colour_id=1, manufacturer_id=1
c = AugmentedPart(id=3, part_type_id=2, description="c", colour_id=1, manufacturer_id=1) )
b = AugmentedPart(
id=2, part_type_id=1, description="b", colour_id=1, manufacturer_id=1
)
c = AugmentedPart(
id=3, part_type_id=2, description="c", colour_id=1, manufacturer_id=1
)
assert not parts_could_fit(a, b) assert not parts_could_fit(a, b)
assert parts_could_fit(a, c) assert parts_could_fit(a, c)
assert not parts_could_fit(a, a) assert not parts_could_fit(a, a)
+1 -3
View File
@@ -80,9 +80,7 @@ async def test_ldap_login_is_idempotent(
assert await user_crud.count(db) == 1 assert await user_crud.count(db) == 1
async def test_ldap_login_bad_credentials( async def test_ldap_login_bad_credentials(client, seeded, monkeypatch, ldap_enabled):
client, seeded, monkeypatch, ldap_enabled
):
async def fake_authenticate(username, password): async def fake_authenticate(username, password):
return None return None
+95 -5
View File
@@ -1,7 +1,10 @@
import pytest import pytest
from app.models.attachment_point import AttachmentPoint
from app.models.attachment_point_name import AttachmentPointName
from app.models.augmented_part import AugmentedPart from app.models.augmented_part import AugmentedPart
from app.models.fit import Fit, FitLabel from app.models.fit import Fit, FitLabel
from app.models.iso_thread import IsoThread
from app.models.part_type import PartType from app.models.part_type import PartType
from app.models.scope_info import ScopeInfo from app.models.scope_info import ScopeInfo
from app.models.sub_part_type import SubPartType from app.models.sub_part_type import SubPartType
@@ -31,7 +34,9 @@ async def _seed_guide_data(db):
db.add(p) db.add(p)
return p return p
s1 = part("Alpha 3-18x50", scope_t.id, manufacturer_id=10, sub_part_type_id=rifle.id) s1 = part(
"Alpha 3-18x50", scope_t.id, manufacturer_id=10, sub_part_type_id=rifle.id
)
s2 = part("Zeta 1-6x24", scope_t.id, manufacturer_id=20, sub_part_type_id=reddot.id) s2 = part("Zeta 1-6x24", scope_t.id, manufacturer_id=20, sub_part_type_id=reddot.id)
fc1 = part("Flip cover 50mm", cover_t.id) fc1 = part("Flip cover 50mm", cover_t.id)
a1 = part("ARD 50mm", ard_t.id) a1 = part("ARD 50mm", ard_t.id)
@@ -43,6 +48,7 @@ async def _seed_guide_data(db):
augmented_part_id=s1.id, augmented_part_id=s1.id,
magnification_min=3, magnification_min=3,
magnification_max=18, magnification_max=18,
objective_lens_diameter_mm=50,
objective_thread_pitch="M52x0.75", objective_thread_pitch="M52x0.75",
) )
) )
@@ -169,9 +175,7 @@ async def test_build_filters_by_required_fit_type(db_sessionmaker):
async def _web_login(client, email, password="secret123"): async def _web_login(client, email, password="secret123"):
resp = await client.post( resp = await client.post("/login", data={"email": email, "password": password})
"/login", data={"email": email, "password": password}
)
assert resp.status_code == 303 assert resp.status_code == 303
@@ -199,7 +203,93 @@ async def test_scopes_page_renders_rows_and_fits(
assert "Flip cover 50mm" in body # a matching part in a fit column assert "Flip cover 50mm" in body # a matching part in a fit column
assert "fit-badge-exact" in body # the exact fit badge assert "fit-badge-exact" in body # the exact fit badge
assert "M52x0.75" in body # scope info: objective thread assert "M52x0.75" in body # scope info: objective thread
assert "3–18×" in body # magnification range, integral values un-padded assert "3-18" in body # magnification range, integral values un-padded
@pytest.mark.asyncio
async def test_build_includes_objective_lens_diameter(db_sessionmaker):
async with db_sessionmaker() as db:
await _seed_guide_data(db)
guide = await scope_guide.build(db)
rows = {r["scope"].description: r for r in guide["rows"]}
assert rows["Alpha 3-18x50"]["info"].objective_lens_diameter_mm == 50
assert rows["Zeta 1-6x24"]["info"] is None
@pytest.mark.asyncio
async def test_build_prefers_objective_attachment_point_over_scope_info(
db_sessionmaker,
):
async with db_sessionmaker() as db:
ids = await _seed_guide_data(db)
apn = AttachmentPointName(name="Objective")
db.add(apn)
await db.flush()
iso = IsoThread(diameter_mm=44, pitch_mm=0.5)
db.add(iso)
await db.flush()
db.add(
AttachmentPoint(
augmented_part_id=ids["s1"].id,
attachment_point_name_id=apn.id,
iso_thread_id=iso.id,
)
)
await db.commit()
guide = await scope_guide.build(db)
rows = {r["scope"].description: r for r in guide["rows"]}
s1_ap = rows["Alpha 3-18x50"]["objective_attachment_point"]
assert s1_ap is not None
assert s1_ap.iso_thread.diameter_mm == 44
# No "Objective"-named attachment point for s2 -> falls back to None.
assert rows["Zeta 1-6x24"]["objective_attachment_point"] is None
@pytest.mark.asyncio
async def test_scopes_page_renders_objective_attachment_point_thread(
client, seeded, make_user, db_sessionmaker
):
async with db_sessionmaker() as db:
ids = await _seed_guide_data(db)
apn = AttachmentPointName(name="Objective")
db.add(apn)
await db.flush()
iso = IsoThread(diameter_mm=44, pitch_mm=0.5)
db.add(iso)
await db.flush()
db.add(
AttachmentPoint(
augmented_part_id=ids["s1"].id,
attachment_point_name_id=apn.id,
iso_thread_id=iso.id,
)
)
await db.commit()
await make_user("reader5@example.com", role_ids=[seeded["roles"]["user"].id])
await _web_login(client, "reader5@example.com")
resp = await client.get("/scopes")
assert resp.status_code == 200
body = resp.text
# The attachment point's thread spec wins over ScopeInfo.objective_thread_pitch.
assert "M44×0.5" in body
assert "M52x0.75" not in body
@pytest.mark.asyncio
async def test_scopes_page_renders_objective_lens_diameter_in_magnification(
client, seeded, make_user, db_sessionmaker
):
async with db_sessionmaker() as db:
await _seed_guide_data(db)
await make_user("reader4@example.com", role_ids=[seeded["roles"]["user"].id])
await _web_login(client, "reader4@example.com")
resp = await client.get("/scopes")
assert resp.status_code == 200
assert "3-18x50" in resp.text
@pytest.mark.asyncio @pytest.mark.asyncio
+10 -10
View File
@@ -19,7 +19,7 @@ HEADERS = [
"model", "model",
"magnification_min", "magnification_min",
"magnification_max", "magnification_max",
"objective_lens_outside_diameter_mm", "objective_lens_diameter_mm",
"model_suffix", "model_suffix",
"sub_part_type", "sub_part_type",
"product_number", "product_number",
@@ -52,7 +52,7 @@ def _scope_row(**overrides):
"model": "Viper", "model": "Viper",
"magnification_min": 5, "magnification_min": 5,
"magnification_max": 25, "magnification_max": 25,
"objective_lens_outside_diameter_mm": 56, "objective_lens_diameter_mm": 56,
"model_suffix": "PST", "model_suffix": "PST",
"sub_part_type": "Riflescope", "sub_part_type": "Riflescope",
"product_number": "SCOPE-A", "product_number": "SCOPE-A",
@@ -141,6 +141,7 @@ async def test_import_creates_scope_with_info_and_attachment_points(db_sessionma
assert info.magnification_min == 5 assert info.magnification_min == 5
assert info.magnification_max == 25 assert info.magnification_max == 25
assert info.model_suffix == "PST" assert info.model_suffix == "PST"
assert info.objective_lens_diameter_mm == 56
assert info.objective_thread_pitch == "0.75" assert info.objective_thread_pitch == "0.75"
aps = await attachment_point_crud.list_for_augmented_part(db, part.id) aps = await attachment_point_crud.list_for_augmented_part(db, part.id)
@@ -151,14 +152,13 @@ async def test_import_creates_scope_with_info_and_attachment_points(db_sessionma
assert len(by_label["Ocular"]) == 1 assert len(by_label["Ocular"]) == 1
assert by_label["Ocular"][0].static_connection.od_mm == 40 assert by_label["Ocular"][0].static_connection.od_mm == 40
# The lens diameter is descriptive only: the sole Objective point is the
# thread.
objectives = by_label["Objective"] objectives = by_label["Objective"]
assert len(objectives) == 2 # static OD + thread assert len(objectives) == 1
static_obj = [a for a in objectives if a.static_connection is not None] assert objectives[0].static_connection is None
thread_obj = [a for a in objectives if a.iso_thread is not None] assert objectives[0].iso_thread.diameter_mm == 40
assert static_obj and static_obj[0].static_connection.od_mm == 56 assert objectives[0].iso_thread.pitch_mm == 0.75
assert thread_obj
assert thread_obj[0].iso_thread.diameter_mm == 40
assert thread_obj[0].iso_thread.pitch_mm == 0.75
async def test_import_resolves_ocular_and_objective_fits(db_sessionmaker): async def test_import_resolves_ocular_and_objective_fits(db_sessionmaker):
@@ -200,7 +200,7 @@ async def test_import_upserts_on_product_number(db_sessionmaker):
# Attachment points were rebuilt, not duplicated. # Attachment points were rebuilt, not duplicated.
aps = await attachment_point_crud.list_for_augmented_part(db, matching[0].id) aps = await attachment_point_crud.list_for_augmented_part(db, matching[0].id)
assert len(aps) == 3 assert len(aps) == 2
async def test_row_without_product_number_is_skipped(db_sessionmaker): async def test_row_without_product_number_is_skipped(db_sessionmaker):