Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
392b79e801 | ||
|
|
3ebe9a0894 | ||
|
|
8d37b2ad67 | ||
|
|
9ebd0f4498 | ||
|
|
d54df47a2c | ||
|
|
12e6984c63 | ||
|
|
fcb4dbaa62 | ||
|
|
a617b935d3 | ||
|
|
d108cb7149 | ||
|
|
373a2c6acf |
No files matched your search
@@ -30,7 +30,11 @@
|
|||||||
"Bash(lsof -ti:8000 -sTCP:LISTEN)",
|
"Bash(lsof -ti:8000 -sTCP:LISTEN)",
|
||||||
"Bash(xargs -r kill)",
|
"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(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 -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'\\);\")"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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
|
||||||
|
|||||||
@@ -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,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")
|
||||||
+1
-3
@@ -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")
|
||||||
|
|||||||
@@ -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,
|
||||||
|
|||||||
@@ -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]:
|
||||||
|
|||||||
+1
-3
@@ -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)
|
||||||
|
|||||||
@@ -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)
|
||||||
|
|||||||
@@ -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(
|
||||||
|
|||||||
@@ -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
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -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
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -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}
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
Binary file not shown.
|
After Width: | Height: | Size: 27 KiB |
@@ -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 -%}
|
||||||
@@ -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 %}
|
||||||
|
|||||||
@@ -1,11 +1,18 @@
|
|||||||
|
{% 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 -%}
|
||||||
@@ -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>
|
||||||
|
|||||||
@@ -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 %}
|
||||||
@@ -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,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>
|
||||||
@@ -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>
|
||||||
|
|||||||
@@ -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 & 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 %}
|
||||||
@@ -4,6 +4,7 @@
|
|||||||
<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"
|
||||||
|
|||||||
+289
-27
@@ -16,7 +16,7 @@ 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 augmented_part_note as augmented_part_note_crud
|
||||||
@@ -43,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
|
||||||
@@ -130,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})
|
||||||
@@ -1469,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 ""
|
||||||
@@ -1481,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,
|
||||||
@@ -1504,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},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -1645,17 +1708,28 @@ async def create_augmented_part(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
_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,
|
||||||
@@ -1664,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)"
|
||||||
@@ -1673,10 +1748,28 @@ 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,
|
||||||
|
"admin/_job_progress.html",
|
||||||
|
{
|
||||||
|
"container_id": _AUGMENTED_PARTS_SYNC_TARGET_ID,
|
||||||
|
"poll_url": f"/admin/augmented-parts/sync/jobs/{job.id}",
|
||||||
|
"job": job,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@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)
|
form_ctx = await _augmented_part_form_ctx(db)
|
||||||
return templates.TemplateResponse(
|
return templates.TemplateResponse(
|
||||||
request,
|
request,
|
||||||
@@ -1684,8 +1777,29 @@ async def sync_augmented_parts(
|
|||||||
{
|
{
|
||||||
**form_ctx,
|
**form_ctx,
|
||||||
**await _default_augmented_parts_table_ctx(db),
|
**await _default_augmented_parts_table_ctx(db),
|
||||||
"sync_result": sync_result,
|
"sync_result": None,
|
||||||
"sync_error": sync_error,
|
"sync_error": "Sync job not found — it may have expired.",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
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,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -2355,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,
|
||||||
@@ -2376,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)
|
||||||
|
|
||||||
@@ -2588,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,
|
||||||
@@ -2606,10 +2789,47 @@ 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 -------------------------------------------------
|
# --- Augmented Part Notes -------------------------------------------------
|
||||||
|
|
||||||
@@ -3389,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},
|
||||||
|
)
|
||||||
Binary file not shown.
@@ -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
|
||||||
@@ -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,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)
|
||||||
@@ -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
|
||||||
|
|
||||||
|
|||||||
@@ -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
@@ -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):
|
||||||
|
|||||||
Reference in new issue
Block a user