Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7a15d9f7c0 | ||
|
|
a9c4c16d4f | ||
|
|
14aa2ac5d6 | ||
|
|
f4771e33d4 | ||
|
|
cf8d72f73e | ||
|
|
2e7976e382 | ||
|
|
2a0b8fcab2 | ||
|
|
392b79e801 | ||
|
|
3ebe9a0894 | ||
|
|
8d37b2ad67 | ||
|
|
9ebd0f4498 | ||
|
|
d54df47a2c | ||
|
|
12e6984c63 | ||
|
|
fcb4dbaa62 |
No files matched your search
@@ -34,7 +34,12 @@
|
||||
"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'\\);\")"
|
||||
"Bash(sqlite3 app.db \"SELECT COUNT\\(*\\) FROM augmented_parts WHERE scope_model_id=\\(SELECT id FROM scope_models WHERE name='Viper'\\);\")",
|
||||
"Bash(sqlite3 app.db \"PRAGMA wal_checkpoint\\(FULL\\);\")",
|
||||
"Bash(curl -s -o /dev/null -w \"%{http_code}\\\\n\" http://127.0.0.1:8000/login)",
|
||||
"Bash(curl -s -o /dev/null -w \"%{http_code}\\\\n\" http://127.0.0.1:8000/admin/imported-parts)",
|
||||
"Bash(rg -n \"hx-delete|router.delete\" app/web/admin.py app/templates/admin/*.html)",
|
||||
"Bash(rg -n \"is_superuser\" tests/conftest.py tests/*.py)"
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -25,6 +25,9 @@ AUTO_CREATE_TABLES=true
|
||||
COOKIE_NAME=access_token
|
||||
# Set true when serving over HTTPS.
|
||||
COOKIE_SECURE=false
|
||||
# How long a browser login lasts before being bounced back to /login.
|
||||
# Independent of ACCESS_TOKEN_EXPIRE_MINUTES, which governs JSON API tokens.
|
||||
SESSION_TIMEOUT_HOURS=24
|
||||
|
||||
# --- LDAP (optional; direct-bind) ---
|
||||
# When enabled, the login page and /api/auth/ldap-login accept a directory
|
||||
|
||||
@@ -87,7 +87,7 @@ The business domain, independent of the auth/RBAC skeleton:
|
||||
- **`app/services/scope_import.py`** is a separate, manually-triggered bulk
|
||||
importer: reads an uploaded `.xlsx` of third-party scopes, upserting by
|
||||
`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).
|
||||
- **`app/services/scope_guide.py`** assembles the `/scopes` page: one row per
|
||||
Scope-type `AugmentedPart`, one column per other `PartType`, each cell
|
||||
|
||||
@@ -21,3 +21,4 @@
|
||||
- Models
|
||||
- Colours
|
||||
- Part Types
|
||||
- Notes
|
||||
@@ -1,4 +1,4 @@
|
||||
VERSION := $(shell git describe --tags --always)
|
||||
VERSION := $(shell git tag | tail -1)
|
||||
IMAGE_NAME := fitguide
|
||||
|
||||
sync:
|
||||
|
||||
@@ -149,8 +149,9 @@ Every setting is read from an environment variable first, then `.env`
|
||||
|----------|---------|-------|
|
||||
| `SECRET_KEY` | dev placeholder | **set a strong value in production** |
|
||||
| `DATABASE_URL` | `sqlite+aiosqlite:///./app.db` | use `postgresql+asyncpg://...` for Postgres |
|
||||
| `ACCESS_TOKEN_EXPIRE_MINUTES` | `15` | |
|
||||
| `ACCESS_TOKEN_EXPIRE_MINUTES` | `15` | JSON API bearer token lifetime |
|
||||
| `REFRESH_TOKEN_EXPIRE_DAYS` | `7` | |
|
||||
| `SESSION_TIMEOUT_HOURS` | `24` | browser login (HTMX UI cookie) lifetime |
|
||||
| `AUTO_CREATE_TABLES` | `true` | create tables on startup (dev); use Alembic in prod |
|
||||
| `COOKIE_SECURE` | `false` | set `true` behind HTTPS |
|
||||
| `FIRST_SUPERUSER_EMAIL` / `_PASSWORD` | admin@example.com / changethis | used by seed |
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
"""add jobs table
|
||||
|
||||
Revision ID: 04425ce3d4d5
|
||||
Revises: a3f7c19b2d40
|
||||
Create Date: 2026-08-05 15:21:09.527442
|
||||
"""
|
||||
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = "04425ce3d4d5"
|
||||
down_revision: Union[str, None] = "a3f7c19b2d40"
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.create_table(
|
||||
"jobs",
|
||||
sa.Column("id", sa.String(length=32), nullable=False),
|
||||
sa.Column("kind", sa.String(length=64), nullable=False),
|
||||
sa.Column("status", sa.String(length=16), nullable=False),
|
||||
sa.Column("message", sa.Text(), nullable=False),
|
||||
sa.Column("result", sa.JSON(), nullable=True),
|
||||
sa.Column(
|
||||
"created_at",
|
||||
sa.DateTime(timezone=True),
|
||||
server_default=sa.text("(CURRENT_TIMESTAMP)"),
|
||||
nullable=False,
|
||||
),
|
||||
sa.Column("finished_at", sa.DateTime(timezone=True), nullable=True),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
)
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.drop_table("jobs")
|
||||
# ### end Alembic commands ###
|
||||
@@ -31,10 +31,16 @@ def upgrade() -> None:
|
||||
nullable=False,
|
||||
),
|
||||
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(
|
||||
"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(
|
||||
["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("body", sa.Text(), nullable=False),
|
||||
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(["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:
|
||||
raise HTTPException(status_code=404, detail="Attachment point not found")
|
||||
if ap_a.augmented_part_id == ap_b.augmented_part_id:
|
||||
raise HTTPException(
|
||||
status_code=422, detail="a part cannot fit itself"
|
||||
)
|
||||
raise HTTPException(status_code=422, detail="a part cannot fit itself")
|
||||
existing = await fit_crud.get_by_attachment_points(db, ap_a, ap_b)
|
||||
if existing is not None:
|
||||
raise HTTPException(status_code=409, detail="Fit already exists")
|
||||
|
||||
+5
-1
@@ -33,9 +33,13 @@ class Settings(BaseSettings):
|
||||
database_url: str = "sqlite+aiosqlite:///./app.db"
|
||||
auto_create_tables: bool = True
|
||||
|
||||
# Web / cookies (the HTMX UI stores the access token in an httpOnly cookie)
|
||||
# Web / cookies (the HTMX UI stores the access token in an httpOnly cookie).
|
||||
# session_timeout_hours governs how long a browser login lasts before the
|
||||
# user is bounced back to /login; it's independent of the JSON API's
|
||||
# short-lived access_token_expire_minutes.
|
||||
cookie_name: str = "access_token"
|
||||
cookie_secure: bool = False
|
||||
session_timeout_hours: int = 24
|
||||
|
||||
# LDAP (direct-bind). When enabled, the login page offers an LDAP option.
|
||||
# At login the user's DN is built from ``ldap_bind_dn_template`` (with
|
||||
|
||||
@@ -35,6 +35,7 @@ if engine.sync_engine.dialect.name == "sqlite":
|
||||
cursor.execute("PRAGMA busy_timeout=5000")
|
||||
cursor.close()
|
||||
|
||||
|
||||
AsyncSessionLocal = async_sessionmaker(
|
||||
engine,
|
||||
class_=AsyncSession,
|
||||
|
||||
@@ -47,10 +47,12 @@ def _create_token(
|
||||
return token, payload
|
||||
|
||||
|
||||
def create_access_token(subject: str | int) -> str:
|
||||
def create_access_token(
|
||||
subject: str | int, expires_delta: timedelta | None = None
|
||||
) -> str:
|
||||
token, _ = _create_token(
|
||||
subject,
|
||||
timedelta(minutes=settings.access_token_expire_minutes),
|
||||
expires_delta or timedelta(minutes=settings.access_token_expire_minutes),
|
||||
ACCESS_TOKEN_TYPE,
|
||||
)
|
||||
return token
|
||||
|
||||
+1
-3
@@ -23,9 +23,7 @@ async def get_default(db: AsyncSession) -> Role | None:
|
||||
|
||||
async def set_default(db: AsyncSession, role: Role) -> Role:
|
||||
"""Make ``role`` the sole default, clearing the flag on every other role."""
|
||||
await db.execute(
|
||||
sa_update(Role).where(Role.id != role.id).values(is_default=False)
|
||||
)
|
||||
await db.execute(sa_update(Role).where(Role.id != role.id).values(is_default=False))
|
||||
role.is_default = True
|
||||
await db.commit()
|
||||
await db.refresh(role)
|
||||
|
||||
@@ -24,9 +24,7 @@ async def list_by_augmented_parts(
|
||||
if not augmented_part_ids:
|
||||
return []
|
||||
result = await db.execute(
|
||||
select(ScopeInfo).where(
|
||||
ScopeInfo.augmented_part_id.in_(augmented_part_ids)
|
||||
)
|
||||
select(ScopeInfo).where(ScopeInfo.augmented_part_id.in_(augmented_part_ids))
|
||||
)
|
||||
return list(result.scalars().all())
|
||||
|
||||
@@ -38,6 +36,7 @@ async def create(
|
||||
magnification_min: float | None = None,
|
||||
magnification_max: float | None = None,
|
||||
model_suffix: str | None = None,
|
||||
objective_lens_diameter_mm: float | None = None,
|
||||
objective_thread_pitch: str | None = None,
|
||||
) -> ScopeInfo:
|
||||
info = ScopeInfo(
|
||||
@@ -45,6 +44,7 @@ async def create(
|
||||
magnification_min=magnification_min,
|
||||
magnification_max=magnification_max,
|
||||
model_suffix=model_suffix,
|
||||
objective_lens_diameter_mm=objective_lens_diameter_mm,
|
||||
objective_thread_pitch=objective_thread_pitch,
|
||||
)
|
||||
db.add(info)
|
||||
|
||||
@@ -14,6 +14,7 @@ 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.job import Job
|
||||
from app.models.manufacturer import Manufacturer
|
||||
from app.models.part_colour import PartColour
|
||||
from app.models.part_type import PartType
|
||||
@@ -40,6 +41,7 @@ __all__ = [
|
||||
"ImportedPart",
|
||||
"ImportedPartConfig",
|
||||
"IsoThread",
|
||||
"Job",
|
||||
"Manufacturer",
|
||||
"PartColour",
|
||||
"PartType",
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
from datetime import datetime
|
||||
|
||||
from sqlalchemy import JSON, DateTime, String, Text, func
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
|
||||
from app.core.database import Base
|
||||
|
||||
|
||||
class Job(Base):
|
||||
"""Tracks a long-running admin operation (sync, import, fit recompute).
|
||||
|
||||
Persisted (rather than kept in a module-level dict) so that any worker
|
||||
process/replica handling a status-poll request can see progress recorded
|
||||
by whichever process is actually running the job.
|
||||
"""
|
||||
|
||||
__tablename__ = "jobs"
|
||||
|
||||
id: Mapped[str] = mapped_column(String(32), primary_key=True)
|
||||
kind: Mapped[str] = mapped_column(String(64), nullable=False)
|
||||
status: Mapped[str] = mapped_column(
|
||||
String(16), nullable=False, default="running"
|
||||
) # running | done | error
|
||||
message: Mapped[str] = mapped_column(Text, nullable=False, default="Working…")
|
||||
result: Mapped[dict | None] = mapped_column(JSON, nullable=True, default=None)
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True), server_default=func.now()
|
||||
)
|
||||
finished_at: Mapped[datetime | None] = mapped_column(
|
||||
DateTime(timezone=True), nullable=True, default=None
|
||||
)
|
||||
@@ -27,6 +27,11 @@ class ScopeInfo(Base):
|
||||
model_suffix: Mapped[str | None] = mapped_column(
|
||||
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
|
||||
# or notation is lost; the parsed thread lives on its own AttachmentPoint.
|
||||
objective_thread_pitch: Mapped[str | None] = mapped_column(
|
||||
|
||||
@@ -5,6 +5,7 @@ class ScopeInfoBase(BaseModel):
|
||||
magnification_min: float | None = None
|
||||
magnification_max: float | None = None
|
||||
model_suffix: str | None = None
|
||||
objective_lens_diameter_mm: float | None = None
|
||||
objective_thread_pitch: str | None = None
|
||||
|
||||
|
||||
|
||||
+12
-3
@@ -1,5 +1,7 @@
|
||||
"""Token issuance / rotation shared by the JSON API and the HTMX UI."""
|
||||
|
||||
from datetime import timedelta
|
||||
|
||||
import jwt
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
@@ -13,9 +15,16 @@ from app.crud import token as token_crud
|
||||
from app.models.user import User
|
||||
|
||||
|
||||
async def issue_token_pair(db: AsyncSession, user: User) -> tuple[str, str]:
|
||||
"""Create an access + refresh token and persist the refresh token for revocation."""
|
||||
access = create_access_token(user.id)
|
||||
async def issue_token_pair(
|
||||
db: AsyncSession, user: User, access_expires_delta: timedelta | None = None
|
||||
) -> tuple[str, str]:
|
||||
"""Create an access + refresh token and persist the refresh token for revocation.
|
||||
|
||||
``access_expires_delta`` overrides the access token's default lifetime
|
||||
(``settings.access_token_expire_minutes``) — used by the web login flow to
|
||||
issue a longer-lived session cookie without affecting API bearer tokens.
|
||||
"""
|
||||
access = create_access_token(user.id, access_expires_delta)
|
||||
refresh, payload = create_refresh_token(user.id)
|
||||
await token_crud.store(
|
||||
db,
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
"""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.scope_info import ScopeInfo
|
||||
from app.models.scope_model import ScopeModel
|
||||
from app.models.static_connection import StaticConnection
|
||||
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 other_ap in points_by_part.get(other_id, []):
|
||||
if attachment_points_match(own_ap, other_ap):
|
||||
candidate_pairs.add(
|
||||
_canonical_pair(own_ap.id, other_ap.id)
|
||||
)
|
||||
candidate_pairs.add(_canonical_pair(own_ap.id, other_ap.id))
|
||||
|
||||
result = RecomputeResult()
|
||||
|
||||
@@ -171,7 +169,9 @@ async def _reconcile(
|
||||
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.
|
||||
|
||||
Creates Possible fits for newly-matching attachment point pairs, and removes
|
||||
|
||||
+74
-52
@@ -1,11 +1,12 @@
|
||||
"""Lightweight in-process tracking for long-running admin operations.
|
||||
"""DB-backed 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.
|
||||
Jobs are persisted to the ``jobs`` table rather than kept in a module-level
|
||||
dict, so that a status-poll request landing on a *different* worker
|
||||
process/replica than the one running the job can still see its progress —
|
||||
required once the app runs with more than one uvicorn worker or replica
|
||||
(a module-level dict is process-local and would report "not found" for a job
|
||||
started elsewhere, even while that job is still running and writing to the
|
||||
shared database).
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -13,62 +14,83 @@ from __future__ import annotations
|
||||
import asyncio
|
||||
import uuid
|
||||
from collections.abc import Awaitable, Callable
|
||||
from dataclasses import dataclass, field
|
||||
from datetime import UTC, datetime
|
||||
from datetime import UTC, datetime, timedelta
|
||||
|
||||
_MAX_FINISHED_AGE_SECONDS = 3600
|
||||
from sqlalchemy import delete
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.core.database import AsyncSessionLocal
|
||||
from app.models.job import Job
|
||||
|
||||
_MAX_FINISHED_AGE = timedelta(hours=1)
|
||||
|
||||
|
||||
@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
|
||||
async def get(db: AsyncSession, job_id: str) -> Job | None:
|
||||
return await db.get(Job, job_id)
|
||||
|
||||
|
||||
_jobs: dict[str, Job] = {}
|
||||
async def start(kind: str, run: Callable[[str], Awaitable[None]]) -> Job:
|
||||
"""Create a job row, launch ``run(job_id)`` in the background, and return
|
||||
the job immediately.
|
||||
|
||||
|
||||
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.
|
||||
``run`` is responsible for calling ``mark_done``/``mark_error`` to record
|
||||
the outcome, including on 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 with AsyncSessionLocal() as db:
|
||||
await _evict_stale(db)
|
||||
job = Job(id=uuid.uuid4().hex, kind=kind, status="running", message="Working…")
|
||||
db.add(job)
|
||||
await db.commit()
|
||||
|
||||
async def _runner() -> None:
|
||||
try:
|
||||
await run(job)
|
||||
await run(job.id)
|
||||
except Exception as exc:
|
||||
job.status = "error"
|
||||
job.message = f"{kind} failed: {exc}"
|
||||
finally:
|
||||
job.finished_at = datetime.now(UTC)
|
||||
await mark_error(job.id, f"{kind} failed: {exc}")
|
||||
|
||||
asyncio.create_task(_runner())
|
||||
return job
|
||||
|
||||
|
||||
async def mark_done(job_id: str, message: str, result: dict | None = None) -> None:
|
||||
await _update(job_id, status="done", message=message, result=result)
|
||||
|
||||
|
||||
async def mark_error(job_id: str, message: str) -> None:
|
||||
await _update(job_id, status="error", message=message)
|
||||
|
||||
|
||||
async def set_message(job_id: str, message: str) -> None:
|
||||
await _update(job_id, message=message)
|
||||
|
||||
|
||||
async def _update(
|
||||
job_id: str,
|
||||
*,
|
||||
status: str | None = None,
|
||||
message: str | None = None,
|
||||
result: dict | None = None,
|
||||
) -> None:
|
||||
async with AsyncSessionLocal() as db:
|
||||
job = await db.get(Job, job_id)
|
||||
if job is None:
|
||||
return
|
||||
if status is not None:
|
||||
job.status = status
|
||||
if status in ("done", "error"):
|
||||
job.finished_at = datetime.now(UTC)
|
||||
if message is not None:
|
||||
job.message = message
|
||||
if result is not None:
|
||||
job.result = result
|
||||
await db.commit()
|
||||
|
||||
|
||||
async def _evict_stale(db: AsyncSession) -> None:
|
||||
cutoff = datetime.now(UTC) - _MAX_FINISHED_AGE
|
||||
await db.execute(
|
||||
delete(Job).where(Job.finished_at.is_not(None), Job.finished_at < cutoff)
|
||||
)
|
||||
await db.commit()
|
||||
+21
-21
@@ -45,7 +45,7 @@ async def build(
|
||||
{
|
||||
"scope": AugmentedPart,
|
||||
"info": ScopeInfo | None,
|
||||
"objective_diameter_mm": float | None,
|
||||
"objective_attachment_point": AttachmentPoint | None,
|
||||
"cells": {part_type_id: [{"part": AugmentedPart, "label": str}]},
|
||||
},
|
||||
...,
|
||||
@@ -54,7 +54,9 @@ async def build(
|
||||
"""
|
||||
part_types = await part_type_crud.list_all(db, limit=1000)
|
||||
scope_type = next((pt for pt in part_types if pt.name == SCOPE_TYPE_NAME), None)
|
||||
column_types = [pt for pt in part_types if scope_type is None or pt.id != scope_type.id]
|
||||
column_types = [
|
||||
pt for pt in part_types if scope_type is None or pt.id != scope_type.id
|
||||
]
|
||||
|
||||
if scope_type is None:
|
||||
return {"column_types": column_types, "rows": []}
|
||||
@@ -72,7 +74,9 @@ async def build(
|
||||
if not scope_ids:
|
||||
return {"column_types": column_types, "rows": []}
|
||||
|
||||
fits = await fit_crud.list_for_parts(db, list(scope_ids), exclude_label=FitLabel.NONE)
|
||||
fits = await fit_crud.list_for_parts(
|
||||
db, list(scope_ids), exclude_label=FitLabel.NONE
|
||||
)
|
||||
|
||||
# Resolve the "other" (non-scope) part on each fit in a single batched query.
|
||||
other_ids: set[int] = set()
|
||||
@@ -80,28 +84,22 @@ async def build(
|
||||
scope_id, other_id = _scope_and_other(fit, scope_ids)
|
||||
if scope_id is not None and other_id not in scope_ids:
|
||||
other_ids.add(other_id)
|
||||
other_parts = {p.id: p for p in await augmented_part_crud.get_many(db, list(other_ids))}
|
||||
other_parts = {
|
||||
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 = {
|
||||
info.augmented_part_id: info
|
||||
for info in await scope_info_crud.list_by_augmented_parts(db, list(scope_ids))
|
||||
}
|
||||
|
||||
# The lens outside diameter lives on the "Objective" attachment point's
|
||||
# StaticConnection, not on ScopeInfo (which only carries the objective
|
||||
# *thread* spec) — see app/services/scope_import.py's column-mapping doc.
|
||||
objective_diameters: dict[int, float] = {}
|
||||
objective_aps = await attachment_point_crud.list_by_augmented_parts_and_name(
|
||||
db, list(scope_ids), OBJECTIVE_ATTACHMENT_POINT_NAME
|
||||
)
|
||||
for ap in objective_aps:
|
||||
if ap.static_connection is not None:
|
||||
objective_diameters[ap.augmented_part_id] = ap.static_connection.od_mm
|
||||
elif ap.iso_thread is not None:
|
||||
objective_diameters[ap.augmented_part_id] = ap.iso_thread.diameter_mm
|
||||
elif ap.unc_thread is not None:
|
||||
objective_diameters[ap.augmented_part_id] = float(ap.unc_thread.diameter_fraction)
|
||||
|
||||
# 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.
|
||||
cell_map: dict[int, dict[int, dict[int, dict]]] = {sid: {} for sid in scope_ids}
|
||||
@@ -121,7 +119,9 @@ async def build(
|
||||
rows = []
|
||||
for scope in scopes:
|
||||
cells = {
|
||||
type_id: sorted(entries.values(), key=lambda e: e["part"].description.lower())
|
||||
type_id: sorted(
|
||||
entries.values(), key=lambda e: e["part"].description.lower()
|
||||
)
|
||||
for type_id, entries in cell_map[scope.id].items()
|
||||
}
|
||||
if fits_part_type_id is not None and fits_part_type_id not in cells:
|
||||
@@ -130,7 +130,7 @@ async def build(
|
||||
{
|
||||
"scope": scope,
|
||||
"info": infos.get(scope.id),
|
||||
"objective_diameter_mm": objective_diameters.get(scope.id),
|
||||
"objective_attachment_point": objective_aps.get(scope.id),
|
||||
"cells": cells,
|
||||
}
|
||||
)
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
The spreadsheet describes parts of PartType "Scope". Each row maps onto an
|
||||
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.
|
||||
|
||||
Column → destination
|
||||
@@ -14,9 +14,9 @@ model_suffix → ScopeInfo.model_suffix (+ description)
|
||||
sub_part_type → SubPartType (get-or-create under "Scope")
|
||||
product_number → AugmentedPart.sku (also the upsert / fit-lookup key)
|
||||
magnification_min / _max → ScopeInfo
|
||||
objective_lens_diameter_mm → ScopeInfo (descriptive only, no attachment point)
|
||||
objective_thread_pitch → ScopeInfo
|
||||
ocular_outer_diameter_mm → StaticConnection + AttachmentPoint "Ocular"
|
||||
objective_lens_outside_diameter_mm → StaticConnection + AttachmentPoint "Objective"
|
||||
objective_thread → ISO/UNC thread + AttachmentPoint "Objective"
|
||||
ocular_fit → Fit: scope's Ocular 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",
|
||||
"magnification_min",
|
||||
"magnification_max",
|
||||
"objective_lens_outside_diameter_mm",
|
||||
"objective_lens_diameter_mm",
|
||||
"model_suffix",
|
||||
"sub_part_type",
|
||||
"product_number",
|
||||
@@ -140,15 +140,11 @@ def parse_workbook(data: bytes) -> list[dict]:
|
||||
wb.close()
|
||||
|
||||
|
||||
async def _static_ap(
|
||||
db: AsyncSession, *, augmented_part_id: int, label: str, od_mm: float
|
||||
):
|
||||
async def _static_ap(db: AsyncSession, *, augmented_part_id: int, label: str, od_mm: float):
|
||||
"""Create an attachment point carrying a StaticConnection for ``od_mm``."""
|
||||
sc = await static_connection_crud.get_by_od_mm(db, od_mm)
|
||||
if sc is None:
|
||||
sc = await static_connection_crud.create(
|
||||
db, od_mm=od_mm, od_in=od_mm / MM_PER_INCH
|
||||
)
|
||||
sc = await static_connection_crud.create(db, od_mm=od_mm, od_in=od_mm / MM_PER_INCH)
|
||||
apn = await apn_crud.get_by_name(db, label)
|
||||
if apn is None:
|
||||
apn = await apn_crud.create(db, name=label)
|
||||
@@ -160,9 +156,7 @@ async def _static_ap(
|
||||
)
|
||||
|
||||
|
||||
async def _thread_ap(
|
||||
db: AsyncSession, *, augmented_part_id: int, label: str, spec: str
|
||||
):
|
||||
async def _thread_ap(db: AsyncSession, *, augmented_part_id: int, label: str, spec: str):
|
||||
"""Create an attachment point for a parsed ISO/UNC thread, or None."""
|
||||
parsed = _parse_thread_attribute(spec)
|
||||
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."""
|
||||
if scope_ap is None:
|
||||
messages.append(
|
||||
f"{scope_role}_fit '{target_sku}' skipped: scope has no "
|
||||
f"{scope_role} attachment point"
|
||||
f"{scope_role}_fit '{target_sku}' skipped: scope has no {scope_role} attachment point"
|
||||
)
|
||||
return
|
||||
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)
|
||||
if not target_aps:
|
||||
messages.append(
|
||||
f"{scope_role}_fit '{target_sku}' skipped: referenced part has no "
|
||||
"attachment points"
|
||||
f"{scope_role}_fit '{target_sku}' skipped: referenced part has no attachment points"
|
||||
)
|
||||
return
|
||||
target_ap = target_aps[0]
|
||||
existing = await fit_crud.get_by_attachment_points(db, scope_ap, target_ap)
|
||||
if existing is None:
|
||||
await fit_crud.create(
|
||||
db, ap_a=scope_ap, ap_b=target_ap, label=FitLabel.POSSIBLE
|
||||
)
|
||||
await fit_crud.create(db, ap_a=scope_ap, ap_b=target_ap, label=FitLabel.POSSIBLE)
|
||||
|
||||
|
||||
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):
|
||||
# +2: 1-based, and the header occupies row 1.
|
||||
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(
|
||||
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)
|
||||
if manufacturer is None:
|
||||
manufacturer = await manufacturer_crud.create(
|
||||
db, name=manufacturer_name, url=""
|
||||
)
|
||||
manufacturer = await manufacturer_crud.create(db, name=manufacturer_name, url="")
|
||||
|
||||
model_name = _text(row.get("model"))
|
||||
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_max=_num(row.get("magnification_max")),
|
||||
model_suffix=model_suffix,
|
||||
objective_lens_diameter_mm=_num(row.get("objective_lens_diameter_mm")),
|
||||
objective_thread_pitch=_text(row.get("objective_thread_pitch")),
|
||||
)
|
||||
|
||||
# Rebuild attachment points from scratch so re-imports stay idempotent
|
||||
# (cascades delete any fits that referenced the old points).
|
||||
for old_ap in await attachment_point_crud.list_for_augmented_part(
|
||||
db, part.id
|
||||
):
|
||||
for old_ap in await attachment_point_crud.list_for_augmented_part(db, part.id):
|
||||
await attachment_point_crud.delete(db, old_ap)
|
||||
|
||||
ocular_ap = None
|
||||
@@ -350,34 +339,22 @@ async def import_scopes(db: AsyncSession, rows: list[dict]) -> ScopeImportResult
|
||||
od_mm=ocular_od,
|
||||
)
|
||||
|
||||
objective_static_ap = None
|
||||
objective_od = _num(row.get("objective_lens_outside_diameter_mm"))
|
||||
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,
|
||||
)
|
||||
|
||||
# The objective attachment point comes from the thread alone — the
|
||||
# lens diameter is a descriptive attribute, not a connection.
|
||||
objective_thread_spec = _text(row.get("objective_thread"))
|
||||
objective_thread_ap = None
|
||||
objective_ap = None
|
||||
if objective_thread_spec:
|
||||
objective_thread_ap = await _thread_ap(
|
||||
objective_ap = await _thread_ap(
|
||||
db,
|
||||
augmented_part_id=part.id,
|
||||
label=OBJECTIVE_LABEL,
|
||||
spec=objective_thread_spec,
|
||||
)
|
||||
if objective_thread_ap is None:
|
||||
if objective_ap is None:
|
||||
result.messages.append(
|
||||
f"could not parse objective_thread "
|
||||
f"'{objective_thread_spec}'"
|
||||
f"could not parse objective_thread '{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))
|
||||
row_results.append(result)
|
||||
except Exception as exc: # noqa: BLE001 - report, don't abort the batch
|
||||
|
||||
@@ -567,6 +567,11 @@ textarea:focus {
|
||||
margin-bottom: 0.75rem;
|
||||
}
|
||||
|
||||
/* Bulk actions (e.g. delete-selected above a checkbox table) */
|
||||
.bulk-actions {
|
||||
margin-bottom: 0.75rem;
|
||||
}
|
||||
|
||||
.filter-bar form {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
|
||||
@@ -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 %}
|
||||
<a href="/admin/audit-log">{{ icon("list-checks") }} Audit Log</a>
|
||||
{% endif %}
|
||||
{% if su %}
|
||||
<a href="/admin/danger-zone">{{ icon("alert-circle") }} Danger Zone</a>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
{% from "_connection_macros.html" import connection_spec %}
|
||||
{%- macro mag(v) -%}{{ v | int if v == v | int else v | round(1) }}{%- endmacro -%}
|
||||
{%- macro magnification_range(info) -%}
|
||||
{%- if info and info.magnification_min is not none and info.magnification_max is not none -%}
|
||||
@@ -8,10 +9,10 @@
|
||||
{{ mag(info.magnification_max) }}
|
||||
{%- endif -%}
|
||||
{%- endmacro -%}
|
||||
{%- macro magnification(info, objective_diameter_mm) -%}
|
||||
{%- macro magnification(info) -%}
|
||||
{%- set range = magnification_range(info) -%}
|
||||
{%- if range -%}
|
||||
{{ range }}x{% if objective_diameter_mm is not none %}{{ mag(objective_diameter_mm) }}{% endif %}
|
||||
{{ range }}x{% if info.objective_lens_diameter_mm is not none %}{{ mag(info.objective_lens_diameter_mm) }}{% endif %}
|
||||
{%- else -%}
|
||||
<span class="muted">—</span>
|
||||
{%- endif -%}
|
||||
@@ -20,6 +21,9 @@
|
||||
<table class="table sortable">
|
||||
<thead>
|
||||
<tr>
|
||||
{% if can_write_scopes %}
|
||||
<th data-sort-off><input type="checkbox" id="scopes-select-all" title="Select all"></th>
|
||||
{% endif %}
|
||||
<th>Scope</th>
|
||||
<th>Manufacturer</th>
|
||||
<th>Model</th>
|
||||
@@ -35,7 +39,10 @@
|
||||
{% for row in rows %}
|
||||
{% set scope = row.scope %}
|
||||
{% set info = row.info %}
|
||||
<tr>
|
||||
<tr id="scope-row-{{ scope.id }}">
|
||||
{% if can_write_scopes %}
|
||||
<td><input type="checkbox" name="scope_ids" value="{{ scope.id }}"></td>
|
||||
{% endif %}
|
||||
<td>
|
||||
<strong><a href="/admin/augmented-parts/{{ scope.id }}">{{ scope.description }}</a></strong>
|
||||
{% if scope.sku %}<br><span class="muted">{{ scope.sku }}</span>{% endif %}
|
||||
@@ -43,9 +50,11 @@
|
||||
<td>{{ scope.manufacturer.name if scope.manufacturer else "—" }}</td>
|
||||
<td>{{ scope.scope_model.name if scope.scope_model else "—" }}</td>
|
||||
<td>{{ scope.sub_part_type.name if scope.sub_part_type else "—" }}</td>
|
||||
<td>{{ magnification(info, row.objective_diameter_mm) }}</td>
|
||||
<td>{{ magnification(info) }}</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 }}
|
||||
{% else %}<span class="muted">—</span>{% endif %}
|
||||
</td>
|
||||
@@ -67,7 +76,7 @@
|
||||
</tr>
|
||||
{% else %}
|
||||
<tr>
|
||||
<td colspan="{{ 6 + column_types | length }}" class="muted">
|
||||
<td colspan="{{ 6 + column_types | length + (1 if can_write_scopes else 0) }}" class="muted">
|
||||
No scopes match your search.
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
@@ -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 %}
|
||||
@@ -44,6 +44,12 @@
|
||||
{% else %}—{% endif %}
|
||||
</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>
|
||||
</dl>
|
||||
</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, attachment point, fit, and imported
|
||||
part in the database — manufacturers, 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, the OData connection configuration, and part
|
||||
taxonomy (part colours, part types, sub part types).
|
||||
</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 %}
|
||||
@@ -29,11 +29,9 @@
|
||||
<button class="btn btn-primary"
|
||||
hx-post="/admin/imported-parts/refresh"
|
||||
hx-target="#imported-parts-section"
|
||||
hx-swap="outerHTML"
|
||||
hx-indicator="#refresh-indicator">
|
||||
hx-swap="outerHTML">
|
||||
Refresh from OData
|
||||
</button>
|
||||
<span id="refresh-indicator" class="htmx-indicator muted"> Loading…</span>
|
||||
</div>
|
||||
|
||||
{% include "admin/_imported_parts_table.html" %}
|
||||
|
||||
@@ -44,6 +44,50 @@
|
||||
</form>
|
||||
</div>
|
||||
|
||||
{% if can_write_scopes %}
|
||||
<div class="bulk-actions">
|
||||
<button id="delete-selected-scopes-btn" type="button" class="btn btn-danger" disabled
|
||||
hx-post="/scopes/bulk-delete"
|
||||
hx-include="[name='scope_ids']:checked, [name='description'], [name='manufacturer_id'], [name='scope_model_id'], [name='sub_part_type_id'], [name='fits_part_type_id']"
|
||||
hx-target="#scopes-table-card"
|
||||
hx-swap="outerHTML"
|
||||
hx-confirm="Delete selected scopes?">Delete selected</button>
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
{% include "_scopes_table_card.html" %}
|
||||
</main>
|
||||
|
||||
{% if can_write_scopes %}
|
||||
<script>
|
||||
(function () {
|
||||
function updateDeleteButton() {
|
||||
var count = document.querySelectorAll('input[name="scope_ids"]:checked').length;
|
||||
var btn = document.getElementById('delete-selected-scopes-btn');
|
||||
if (!btn) return;
|
||||
btn.textContent = count > 0 ? 'Delete selected (' + count + ')' : 'Delete selected';
|
||||
btn.disabled = count === 0;
|
||||
btn.setAttribute('hx-confirm', 'Delete ' + count + ' selected scope' + (count === 1 ? '' : 's') + '?');
|
||||
}
|
||||
|
||||
document.addEventListener('change', function (e) {
|
||||
if (!e.target) return;
|
||||
|
||||
if (e.target.id === 'scopes-select-all') {
|
||||
document.querySelectorAll('input[name="scope_ids"]').forEach(function (cb) {
|
||||
cb.checked = e.target.checked;
|
||||
});
|
||||
}
|
||||
|
||||
if (e.target.id === 'scopes-select-all' || e.target.name === 'scope_ids') {
|
||||
updateDeleteButton();
|
||||
}
|
||||
});
|
||||
|
||||
// Re-run after every HTMX swap (filter results, post-delete) so the button
|
||||
// state reflects the freshly rendered checkboxes, which start unchecked.
|
||||
document.addEventListener('htmx:afterSwap', updateDeleteButton);
|
||||
}());
|
||||
</script>
|
||||
{% endif %}
|
||||
{% endblock %}
|
||||
+171
-44
@@ -43,6 +43,7 @@ from app.models.fit import FitLabel
|
||||
from app.models.unc_thread import UncSeries
|
||||
from app.models.user import User
|
||||
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 jobs
|
||||
from app.services import odata as odata_service
|
||||
@@ -131,6 +132,16 @@ def web_require(*perms: str):
|
||||
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:
|
||||
all_perms = await role_crud.list_permissions(db)
|
||||
scopes = sorted({p.name.split(":")[0] for p in all_perms})
|
||||
@@ -1355,6 +1366,19 @@ async def save_imported_part_config(
|
||||
return HTMLResponse("Configuration saved.", status_code=200)
|
||||
|
||||
|
||||
_IMPORTED_PARTS_REFRESH_TARGET_ID = "imported-parts-section"
|
||||
|
||||
|
||||
async def _imported_parts_table_ctx(
|
||||
db: AsyncSession, *, sync_result: str | None = None, sync_error: str | None = None
|
||||
) -> dict:
|
||||
return {
|
||||
**await _default_imported_parts_table_ctx(db),
|
||||
"sync_result": sync_result,
|
||||
"sync_error": sync_error,
|
||||
}
|
||||
|
||||
|
||||
@router.post("/imported-parts/refresh", response_class=HTMLResponse)
|
||||
async def refresh_imported_parts(
|
||||
request: Request,
|
||||
@@ -1362,45 +1386,94 @@ async def refresh_imported_parts(
|
||||
actor: User = Depends(web_require("imported_parts:write")),
|
||||
):
|
||||
config = await imported_part_config_crud.get(db)
|
||||
|
||||
async def _imported_table_ctx(sync_result=None, sync_error=None):
|
||||
return {
|
||||
**await _default_imported_parts_table_ctx(db),
|
||||
"sync_result": sync_result,
|
||||
"sync_error": sync_error,
|
||||
}
|
||||
|
||||
if not config or not config.odata_url:
|
||||
return templates.TemplateResponse(
|
||||
request,
|
||||
"admin/_imported_parts_table.html",
|
||||
await _imported_table_ctx(sync_error="OData URL is not configured."),
|
||||
await _imported_parts_table_ctx(
|
||||
db, sync_error="OData URL is not configured."
|
||||
),
|
||||
)
|
||||
|
||||
actor_id = actor.id
|
||||
|
||||
async def _run(job_id: str) -> None:
|
||||
async with AsyncSessionLocal() as db:
|
||||
config = await imported_part_config_crud.get(db)
|
||||
if not config or not config.odata_url:
|
||||
await jobs.mark_error(job_id, "OData URL is not configured.")
|
||||
return
|
||||
try:
|
||||
records = await odata_service.fetch(
|
||||
config.odata_url, config.username, config.password
|
||||
)
|
||||
created, updated = await imported_part_crud.upsert_bulk(db, records)
|
||||
except Exception as exc:
|
||||
await jobs.mark_error(job_id, f"Sync failed: {exc}")
|
||||
return
|
||||
actor_row = await user_crud.get(db, actor_id)
|
||||
await audit_crud.log(
|
||||
db,
|
||||
actor=actor,
|
||||
actor=actor_row,
|
||||
action="update",
|
||||
resource_type="ImportedPart",
|
||||
resource_id=None,
|
||||
description=f"OData sync: {created} created, {updated} updated ({len(records)} total records)",
|
||||
)
|
||||
return templates.TemplateResponse(
|
||||
request,
|
||||
"admin/_imported_parts_table.html",
|
||||
await _imported_table_ctx(
|
||||
sync_result=f"Sync complete: {created} created, {updated} updated."
|
||||
description=(
|
||||
f"OData sync: {created} created, {updated} updated "
|
||||
f"({len(records)} total records)"
|
||||
),
|
||||
)
|
||||
except Exception as exc:
|
||||
await jobs.mark_done(
|
||||
job_id,
|
||||
message=f"Sync complete: {created} created, {updated} updated.",
|
||||
)
|
||||
|
||||
job = await jobs.start("imported_parts_refresh", _run)
|
||||
return templates.TemplateResponse(
|
||||
request,
|
||||
"admin/_job_progress.html",
|
||||
{
|
||||
"container_id": _IMPORTED_PARTS_REFRESH_TARGET_ID,
|
||||
"poll_url": f"/admin/imported-parts/refresh/jobs/{job.id}",
|
||||
"job": job,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@router.get("/imported-parts/refresh/jobs/{job_id}", response_class=HTMLResponse)
|
||||
async def imported_parts_refresh_job_status(
|
||||
request: Request,
|
||||
job_id: str,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
_: User = Depends(web_require("imported_parts:write")),
|
||||
):
|
||||
job = await jobs.get(db, job_id)
|
||||
if job is None:
|
||||
return templates.TemplateResponse(
|
||||
request,
|
||||
"admin/_imported_parts_table.html",
|
||||
await _imported_table_ctx(sync_error=f"Sync failed: {exc}"),
|
||||
await _imported_parts_table_ctx(
|
||||
db, sync_error="Refresh job not found — it may have expired."
|
||||
),
|
||||
)
|
||||
if job.status == "running":
|
||||
return templates.TemplateResponse(
|
||||
request,
|
||||
"admin/_job_progress.html",
|
||||
{
|
||||
"container_id": _IMPORTED_PARTS_REFRESH_TARGET_ID,
|
||||
"poll_url": f"/admin/imported-parts/refresh/jobs/{job_id}",
|
||||
"job": job,
|
||||
},
|
||||
)
|
||||
return templates.TemplateResponse(
|
||||
request,
|
||||
"admin/_imported_parts_table.html",
|
||||
await _imported_parts_table_ctx(
|
||||
db,
|
||||
sync_result=job.message if job.status == "done" else None,
|
||||
sync_error=job.message if job.status == "error" else None,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
@@ -1500,13 +1573,12 @@ async def upload_scope_import(
|
||||
data = await file.read()
|
||||
actor_id = actor.id
|
||||
|
||||
async def _run(job: jobs.Job) -> None:
|
||||
async def _run(job_id: str) -> None:
|
||||
async with AsyncSessionLocal() as db:
|
||||
try:
|
||||
result = await scope_import_service.import_from_bytes(db, data)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
job.status = "error"
|
||||
job.message = f"Could not read spreadsheet: {exc}"
|
||||
await jobs.mark_error(job_id, f"Could not read spreadsheet: {exc}")
|
||||
return
|
||||
actor_row = await user_crud.get(db, actor_id)
|
||||
await audit_crud.log(
|
||||
@@ -1521,10 +1593,16 @@ async def upload_scope_import(
|
||||
f"{result.errors} errors"
|
||||
),
|
||||
)
|
||||
job.status = "done"
|
||||
job.result = result
|
||||
await jobs.mark_done(
|
||||
job_id,
|
||||
message=(
|
||||
f"Scope import complete: {result.created} created, "
|
||||
f"{result.updated} updated."
|
||||
),
|
||||
result=result.model_dump(mode="json"),
|
||||
)
|
||||
|
||||
job = jobs.start("scope_import", _run)
|
||||
job = await jobs.start("scope_import", _run)
|
||||
return templates.TemplateResponse(
|
||||
request, "admin/_job_progress.html", _scope_import_progress_ctx(job)
|
||||
)
|
||||
@@ -1534,9 +1612,10 @@ async def upload_scope_import(
|
||||
async def scope_import_job_status(
|
||||
request: Request,
|
||||
job_id: str,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
_: User = Depends(web_require("augmented_parts:write")),
|
||||
):
|
||||
job = jobs.get(job_id)
|
||||
job = await jobs.get(db, job_id)
|
||||
if job is None:
|
||||
return templates.TemplateResponse(
|
||||
request,
|
||||
@@ -1707,13 +1786,12 @@ async def sync_augmented_parts(
|
||||
):
|
||||
actor_id = actor.id
|
||||
|
||||
async def _run(job: jobs.Job) -> None:
|
||||
async def _run(job_id: str) -> None:
|
||||
async with AsyncSessionLocal() as db:
|
||||
try:
|
||||
result = await augmented_part_sync_service.sync(db)
|
||||
except Exception as exc:
|
||||
job.status = "error"
|
||||
job.message = f"Sync failed: {exc}"
|
||||
await jobs.mark_error(job_id, f"Sync failed: {exc}")
|
||||
return
|
||||
actor_row = await user_crud.get(db, actor_id)
|
||||
await audit_crud.log(
|
||||
@@ -1727,18 +1805,21 @@ async def sync_augmented_parts(
|
||||
f"{result.skipped} skipped (no type match)"
|
||||
),
|
||||
)
|
||||
job.status = "done"
|
||||
job.message = (
|
||||
f"Sync complete: {result.created} created, {result.deleted} deleted"
|
||||
await jobs.mark_done(
|
||||
job_id,
|
||||
message=(
|
||||
f"Sync complete: {result.created} created, "
|
||||
f"{result.deleted} deleted"
|
||||
+ (
|
||||
f", {result.skipped} skipped (type not recognised)"
|
||||
if result.skipped
|
||||
else ""
|
||||
)
|
||||
+ "."
|
||||
),
|
||||
)
|
||||
|
||||
job = jobs.start("augmented_part_sync", _run)
|
||||
job = await jobs.start("augmented_part_sync", _run)
|
||||
return templates.TemplateResponse(
|
||||
request,
|
||||
"admin/_job_progress.html",
|
||||
@@ -1757,7 +1838,7 @@ async def augmented_parts_sync_job_status(
|
||||
db: AsyncSession = Depends(get_db),
|
||||
_: User = Depends(web_require("augmented_parts:write")),
|
||||
):
|
||||
job = jobs.get(job_id)
|
||||
job = await jobs.get(db, job_id)
|
||||
if job is None:
|
||||
form_ctx = await _augmented_part_form_ctx(db)
|
||||
return templates.TemplateResponse(
|
||||
@@ -2472,7 +2553,7 @@ async def recompute_augmented_part_fits(
|
||||
):
|
||||
actor_id = actor.id
|
||||
|
||||
async def _run(job: jobs.Job) -> None:
|
||||
async def _run(job_id: str) -> None:
|
||||
async with AsyncSessionLocal() as db:
|
||||
result = await fit_matching_service.recompute_for_part(
|
||||
db, augmented_part_id
|
||||
@@ -2489,10 +2570,11 @@ async def recompute_augmented_part_fits(
|
||||
f"{result.created} created, {result.removed} removed"
|
||||
),
|
||||
)
|
||||
job.status = "done"
|
||||
job.message = f"{result.created} created, {result.removed} removed."
|
||||
await jobs.mark_done(
|
||||
job_id, f"{result.created} created, {result.removed} removed."
|
||||
)
|
||||
|
||||
job = jobs.start("fit_recompute_part", _run)
|
||||
job = await jobs.start("fit_recompute_part", _run)
|
||||
return templates.TemplateResponse(
|
||||
request,
|
||||
"admin/_job_progress.html",
|
||||
@@ -2516,7 +2598,7 @@ async def augmented_part_fits_recompute_job_status(
|
||||
db: AsyncSession = Depends(get_db),
|
||||
actor: User = Depends(web_require("fits:write")),
|
||||
):
|
||||
job = jobs.get(job_id)
|
||||
job = await jobs.get(db, job_id)
|
||||
if job is None:
|
||||
ctx = await _augmented_part_fits_ctx(
|
||||
db,
|
||||
@@ -2763,7 +2845,7 @@ async def recompute_all_fits(
|
||||
):
|
||||
actor_id = actor.id
|
||||
|
||||
async def _run(job: jobs.Job) -> None:
|
||||
async def _run(job_id: str) -> None:
|
||||
async with AsyncSessionLocal() as db:
|
||||
result = await fit_matching_service.recompute_all(db)
|
||||
actor_row = await user_crud.get(db, actor_id)
|
||||
@@ -2778,13 +2860,15 @@ async def recompute_all_fits(
|
||||
f"{result.removed} removed"
|
||||
),
|
||||
)
|
||||
job.status = "done"
|
||||
job.message = (
|
||||
await jobs.mark_done(
|
||||
job_id,
|
||||
message=(
|
||||
f"Recompute complete: {result.created} created, "
|
||||
f"{result.removed} removed."
|
||||
),
|
||||
)
|
||||
|
||||
job = jobs.start("fit_recompute_all", _run)
|
||||
job = await jobs.start("fit_recompute_all", _run)
|
||||
return templates.TemplateResponse(
|
||||
request,
|
||||
"admin/_job_progress_inline.html",
|
||||
@@ -2802,9 +2886,10 @@ async def recompute_all_fits(
|
||||
async def recompute_all_fits_job_status(
|
||||
request: Request,
|
||||
job_id: str,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
_: User = Depends(web_require("fits:write")),
|
||||
):
|
||||
job = jobs.get(job_id)
|
||||
job = await jobs.get(db, job_id)
|
||||
if job is None:
|
||||
return HTMLResponse("Recompute job not found — it may have expired.")
|
||||
if job.status == "running":
|
||||
@@ -3598,3 +3683,45 @@ async def audit_log_page(
|
||||
"admin/audit_log.html",
|
||||
{"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},
|
||||
)
|
||||
+9
-3
@@ -1,3 +1,5 @@
|
||||
from datetime import timedelta
|
||||
|
||||
from fastapi import APIRouter, Depends, Form, HTTPException, Query, Request, status
|
||||
from fastapi.responses import HTMLResponse, RedirectResponse, Response
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
@@ -36,7 +38,7 @@ def _set_auth_cookie(response: Response, access_token: str) -> None:
|
||||
httponly=True,
|
||||
secure=settings.cookie_secure,
|
||||
samesite="lax",
|
||||
max_age=settings.access_token_expire_minutes * 60,
|
||||
max_age=settings.session_timeout_hours * 3600,
|
||||
path="/",
|
||||
)
|
||||
|
||||
@@ -79,7 +81,9 @@ async def login_submit(
|
||||
},
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
)
|
||||
access, _ = await issue_token_pair(db, user)
|
||||
access, _ = await issue_token_pair(
|
||||
db, user, timedelta(hours=settings.session_timeout_hours)
|
||||
)
|
||||
response = RedirectResponse(
|
||||
url=_safe_next(next_url), status_code=status.HTTP_303_SEE_OTHER
|
||||
)
|
||||
@@ -110,7 +114,9 @@ async def login_ldap_submit(
|
||||
},
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
)
|
||||
access, _ = await issue_token_pair(db, user)
|
||||
access, _ = await issue_token_pair(
|
||||
db, user, timedelta(hours=settings.session_timeout_hours)
|
||||
)
|
||||
response = RedirectResponse(
|
||||
url=_safe_next(next_url), status_code=status.HTTP_303_SEE_OTHER
|
||||
)
|
||||
|
||||
+63
-4
@@ -5,11 +5,13 @@ non-Scope part type listing the accessories that fit each scope. Supports HTMX
|
||||
search/filter over scope attributes and required accessory fits.
|
||||
"""
|
||||
|
||||
from fastapi import APIRouter, Depends, Request
|
||||
from fastapi import APIRouter, Depends, Form, Request
|
||||
from fastapi.responses import HTMLResponse
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.core.database import get_db
|
||||
from app.crud import audit as audit_crud
|
||||
from app.crud import augmented_part as augmented_part_crud
|
||||
from app.crud import manufacturer as manufacturer_crud
|
||||
from app.crud import part_type as part_type_crud
|
||||
from app.crud import scope_model as scope_model_crud
|
||||
@@ -26,6 +28,10 @@ def _to_int(value: str) -> int | None:
|
||||
return int(value) if value.strip() else None
|
||||
|
||||
|
||||
def _can_write_scopes(user: User) -> bool:
|
||||
return user.is_superuser or "augmented_parts:write" in user.permissions
|
||||
|
||||
|
||||
async def _build(db: AsyncSession, filters: dict) -> dict:
|
||||
"""Run the guide query for the given raw (string) filter values."""
|
||||
return await scope_guide_service.build(
|
||||
@@ -77,7 +83,13 @@ async def scopes_page(
|
||||
return templates.TemplateResponse(
|
||||
request,
|
||||
"scopes.html",
|
||||
{"user": user, "filters": filters, **guide, **(await _filter_options(db))},
|
||||
{
|
||||
"user": user,
|
||||
"filters": filters,
|
||||
"can_write_scopes": _can_write_scopes(user),
|
||||
**guide,
|
||||
**(await _filter_options(db)),
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@@ -90,7 +102,7 @@ async def scopes_filter(
|
||||
sub_part_type_id: str = "",
|
||||
fits_part_type_id: str = "",
|
||||
db: AsyncSession = Depends(get_db),
|
||||
_: User = Depends(web_require("augmented_parts:read", "fits:read")),
|
||||
user: User = Depends(web_require("augmented_parts:read", "fits:read")),
|
||||
):
|
||||
filters = {
|
||||
"description": description,
|
||||
@@ -100,4 +112,51 @@ async def scopes_filter(
|
||||
"fits_part_type_id": fits_part_type_id,
|
||||
}
|
||||
guide = await _build(db, filters)
|
||||
return templates.TemplateResponse(request, "_scopes_table_card.html", guide)
|
||||
return templates.TemplateResponse(
|
||||
request,
|
||||
"_scopes_table_card.html",
|
||||
{**guide, "can_write_scopes": _can_write_scopes(user)},
|
||||
)
|
||||
|
||||
|
||||
@router.post("/scopes/bulk-delete", response_class=HTMLResponse)
|
||||
async def bulk_delete_scopes(
|
||||
request: Request,
|
||||
scope_ids: list[int] = Form([]),
|
||||
description: str = Form(""),
|
||||
manufacturer_id: str = Form(""),
|
||||
scope_model_id: str = Form(""),
|
||||
sub_part_type_id: str = Form(""),
|
||||
fits_part_type_id: str = Form(""),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
actor: User = Depends(web_require("augmented_parts:write")),
|
||||
):
|
||||
for augmented_part_id in scope_ids:
|
||||
ap = await augmented_part_crud.get(db, augmented_part_id)
|
||||
if ap is None:
|
||||
continue
|
||||
label = ap.sku or ap.description
|
||||
await augmented_part_crud.delete(db, ap)
|
||||
await audit_crud.log(
|
||||
db,
|
||||
actor=actor,
|
||||
action="delete",
|
||||
resource_type="AugmentedPart",
|
||||
resource_id=augmented_part_id,
|
||||
description=audit_crud.describe_delete(
|
||||
"AugmentedPart", augmented_part_id, label
|
||||
),
|
||||
)
|
||||
filters = {
|
||||
"description": description,
|
||||
"manufacturer_id": manufacturer_id,
|
||||
"scope_model_id": scope_model_id,
|
||||
"sub_part_type_id": sub_part_type_id,
|
||||
"fits_part_type_id": fits_part_type_id,
|
||||
}
|
||||
guide = await _build(db, filters)
|
||||
return templates.TemplateResponse(
|
||||
request,
|
||||
"_scopes_table_card.html",
|
||||
{**guide, "can_write_scopes": _can_write_scopes(actor)},
|
||||
)
|
||||
Binary file not shown.
@@ -2,7 +2,11 @@ import asyncio
|
||||
import re
|
||||
|
||||
import pytest
|
||||
import pytest_asyncio
|
||||
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
|
||||
|
||||
from app.core.database import Base
|
||||
from app.crud import imported_part_config as imported_part_config_crud
|
||||
from app.crud import role as role_crud
|
||||
from app.models.attachment_point import AttachmentPoint
|
||||
from app.models.attachment_point_name import AttachmentPointName
|
||||
@@ -13,6 +17,25 @@ from tests.test_scope_import import _scope_row, _workbook
|
||||
_JOB_URL_RE = re.compile(r'hx-get="([^"]+/jobs/[^"]+)"')
|
||||
|
||||
|
||||
@pytest_asyncio.fixture
|
||||
async def db_sessionmaker(tmp_path):
|
||||
"""Override conftest's in-memory+StaticPool fixture for this module.
|
||||
|
||||
These tests run a real background asyncio task (the job) concurrently
|
||||
with polling requests, both hitting the DB at once. StaticPool pins every
|
||||
session to one shared raw connection, which can't safely serve two
|
||||
callers at the same time; a real file-backed DB with its own connection
|
||||
pool (how SQLite/Postgres actually behave outside tests) can.
|
||||
"""
|
||||
db_path = tmp_path / "jobs_test.db"
|
||||
engine = create_async_engine(f"sqlite+aiosqlite:///{db_path}")
|
||||
async with engine.begin() as conn:
|
||||
await conn.run_sync(Base.metadata.create_all)
|
||||
maker = async_sessionmaker(engine, expire_on_commit=False)
|
||||
yield maker
|
||||
await engine.dispose()
|
||||
|
||||
|
||||
async def _web_login(client, email, password="secret123"):
|
||||
resp = await client.post("/login", data={"email": email, "password": password})
|
||||
assert resp.status_code == 303
|
||||
@@ -39,6 +62,7 @@ async def test_scope_import_runs_in_background(
|
||||
client, seeded, make_user, db_sessionmaker, monkeypatch
|
||||
):
|
||||
monkeypatch.setattr("app.web.admin.AsyncSessionLocal", db_sessionmaker)
|
||||
monkeypatch.setattr("app.services.jobs.AsyncSessionLocal", db_sessionmaker)
|
||||
async with db_sessionmaker() as db:
|
||||
perms = seeded["perms"]
|
||||
editor = await role_crud.create(
|
||||
@@ -69,6 +93,7 @@ async def test_augmented_parts_sync_runs_in_background(
|
||||
client, seeded, make_user, db_sessionmaker, monkeypatch
|
||||
):
|
||||
monkeypatch.setattr("app.web.admin.AsyncSessionLocal", db_sessionmaker)
|
||||
monkeypatch.setattr("app.services.jobs.AsyncSessionLocal", db_sessionmaker)
|
||||
async with db_sessionmaker() as db:
|
||||
perms = seeded["perms"]
|
||||
editor = await role_crud.create(
|
||||
@@ -93,6 +118,7 @@ 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)
|
||||
monkeypatch.setattr("app.services.jobs.AsyncSessionLocal", db_sessionmaker)
|
||||
async with db_sessionmaker() as db:
|
||||
apn = AttachmentPointName(name="Objective")
|
||||
part_t = PartType(name="Scope")
|
||||
@@ -137,6 +163,7 @@ async def test_fit_recompute_all_runs_in_background(
|
||||
client, seeded, make_user, db_sessionmaker, monkeypatch
|
||||
):
|
||||
monkeypatch.setattr("app.web.admin.AsyncSessionLocal", db_sessionmaker)
|
||||
monkeypatch.setattr("app.services.jobs.AsyncSessionLocal", db_sessionmaker)
|
||||
async with db_sessionmaker() as db:
|
||||
perms = seeded["perms"]
|
||||
editor = await role_crud.create(
|
||||
@@ -154,3 +181,64 @@ async def test_fit_recompute_all_runs_in_background(
|
||||
|
||||
final_body = await _poll_until_done(client, match.group(1))
|
||||
assert "Recompute complete" in final_body
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_imported_parts_refresh_runs_in_background(
|
||||
client, seeded, make_user, db_sessionmaker, monkeypatch
|
||||
):
|
||||
monkeypatch.setattr("app.web.admin.AsyncSessionLocal", db_sessionmaker)
|
||||
monkeypatch.setattr("app.services.jobs.AsyncSessionLocal", db_sessionmaker)
|
||||
|
||||
async def _fake_fetch(url, username, password):
|
||||
return [{"InventoryID": "INV-1", "Description": "Widget"}]
|
||||
|
||||
monkeypatch.setattr("app.web.admin.odata_service.fetch", _fake_fetch)
|
||||
|
||||
async with db_sessionmaker() as db:
|
||||
await imported_part_config_crud.upsert(
|
||||
db, odata_url="https://example.test/odata", username=None, password=None
|
||||
)
|
||||
perms = seeded["perms"]
|
||||
editor = await role_crud.create(
|
||||
db,
|
||||
name="imported-parts-editor",
|
||||
permission_ids=[perms["imported_parts:write"].id],
|
||||
)
|
||||
editor_id = editor.id
|
||||
await make_user("refresher@example.com", role_ids=[editor_id])
|
||||
await _web_login(client, "refresher@example.com")
|
||||
|
||||
resp = await client.post("/admin/imported-parts/refresh")
|
||||
assert resp.status_code == 200
|
||||
assert 'id="imported-parts-section"' 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: 1 created, 0 updated." in final_body
|
||||
assert "INV-1" in final_body
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_imported_parts_refresh_without_config_skips_job(
|
||||
client, seeded, make_user, db_sessionmaker, monkeypatch
|
||||
):
|
||||
monkeypatch.setattr("app.web.admin.AsyncSessionLocal", db_sessionmaker)
|
||||
monkeypatch.setattr("app.services.jobs.AsyncSessionLocal", db_sessionmaker)
|
||||
async with db_sessionmaker() as db:
|
||||
perms = seeded["perms"]
|
||||
editor = await role_crud.create(
|
||||
db,
|
||||
name="imported-parts-editor-noconfig",
|
||||
permission_ids=[perms["imported_parts:write"].id],
|
||||
)
|
||||
editor_id = editor.id
|
||||
await make_user("refresher2@example.com", role_ids=[editor_id])
|
||||
await _web_login(client, "refresher2@example.com")
|
||||
|
||||
resp = await client.post("/admin/imported-parts/refresh")
|
||||
assert resp.status_code == 200
|
||||
assert "OData URL is not configured." in resp.text
|
||||
# No background job started for this synchronous validation failure.
|
||||
assert _JOB_URL_RE.search(resp.text) is None
|
||||
@@ -0,0 +1,158 @@
|
||||
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,
|
||||
AttachmentPointName,
|
||||
IsoThread,
|
||||
UncThread,
|
||||
ClampConnection,
|
||||
StaticConnection,
|
||||
Manufacturer,
|
||||
):
|
||||
rows = (await db.execute(Model.__table__.select())).all()
|
||||
assert rows == [], f"{Model.__name__} should be empty after purge"
|
||||
|
||||
# Kept: users, RBAC, the OData connection config, and part taxonomy.
|
||||
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"
|
||||
|
||||
for Model in (PartColour, PartType, SubPartType):
|
||||
rows = (await db.execute(Model.__table__.select())).all()
|
||||
assert rows != [], f"{Model.__name__} should survive the purge"
|
||||
|
||||
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():
|
||||
a = _ap(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))
|
||||
a = _ap(
|
||||
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)
|
||||
|
||||
|
||||
def test_unc_thread_series_mismatch():
|
||||
a = _ap(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))
|
||||
a = _ap(
|
||||
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)
|
||||
|
||||
|
||||
def test_iso_and_unc_never_match_each_other():
|
||||
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)
|
||||
|
||||
|
||||
@@ -94,9 +114,15 @@ def test_two_bare_attachment_points_never_match():
|
||||
|
||||
|
||||
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)
|
||||
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)
|
||||
a = AugmentedPart(
|
||||
id=1, part_type_id=1, description="a", 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 parts_could_fit(a, c)
|
||||
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
|
||||
|
||||
|
||||
async def test_ldap_login_bad_credentials(
|
||||
client, seeded, monkeypatch, ldap_enabled
|
||||
):
|
||||
async def test_ldap_login_bad_credentials(client, seeded, monkeypatch, ldap_enabled):
|
||||
async def fake_authenticate(username, password):
|
||||
return None
|
||||
|
||||
|
||||
+143
-11
@@ -4,9 +4,9 @@ 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.fit import Fit, FitLabel
|
||||
from app.models.iso_thread import IsoThread
|
||||
from app.models.part_type import PartType
|
||||
from app.models.scope_info import ScopeInfo
|
||||
from app.models.static_connection import StaticConnection
|
||||
from app.models.sub_part_type import SubPartType
|
||||
from app.services import scope_guide
|
||||
|
||||
@@ -34,7 +34,9 @@ async def _seed_guide_data(db):
|
||||
db.add(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)
|
||||
fc1 = part("Flip cover 50mm", cover_t.id)
|
||||
a1 = part("ARD 50mm", ard_t.id)
|
||||
@@ -46,6 +48,7 @@ async def _seed_guide_data(db):
|
||||
augmented_part_id=s1.id,
|
||||
magnification_min=3,
|
||||
magnification_max=18,
|
||||
objective_lens_diameter_mm=50,
|
||||
objective_thread_pitch="M52x0.75",
|
||||
)
|
||||
)
|
||||
@@ -172,9 +175,7 @@ async def test_build_filters_by_required_fit_type(db_sessionmaker):
|
||||
|
||||
|
||||
async def _web_login(client, email, password="secret123"):
|
||||
resp = await client.post(
|
||||
"/login", data={"email": email, "password": password}
|
||||
)
|
||||
resp = await client.post("/login", data={"email": email, "password": password})
|
||||
assert resp.status_code == 303
|
||||
|
||||
|
||||
@@ -206,17 +207,33 @@ async def test_scopes_page_renders_rows_and_fits(
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_build_includes_objective_diameter(db_sessionmaker):
|
||||
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,
|
||||
static_connection=StaticConnection(od_mm=50, od_in=1.9685),
|
||||
iso_thread_id=iso.id,
|
||||
)
|
||||
)
|
||||
await db.commit()
|
||||
@@ -224,12 +241,15 @@ async def test_build_includes_objective_diameter(db_sessionmaker):
|
||||
guide = await scope_guide.build(db)
|
||||
|
||||
rows = {r["scope"].description: r for r in guide["rows"]}
|
||||
assert rows["Alpha 3-18x50"]["objective_diameter_mm"] == 50
|
||||
assert rows["Zeta 1-6x24"]["objective_diameter_mm"] is None
|
||||
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_diameter_in_magnification(
|
||||
async def test_scopes_page_renders_objective_attachment_point_thread(
|
||||
client, seeded, make_user, db_sessionmaker
|
||||
):
|
||||
async with db_sessionmaker() as db:
|
||||
@@ -237,14 +257,34 @@ async def test_scopes_page_renders_objective_diameter_in_magnification(
|
||||
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,
|
||||
static_connection=StaticConnection(od_mm=50, od_in=1.9685),
|
||||
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×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")
|
||||
@@ -290,3 +330,95 @@ async def test_scopes_page_redirects_when_anonymous(client):
|
||||
assert resp.status_code == 302
|
||||
# Deep link is preserved so the user lands back here after logging in.
|
||||
assert resp.headers["location"] == "/login?next=%2Fscopes"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_scopes_page_hides_checkboxes_for_read_only_user(
|
||||
client, seeded, make_user, db_sessionmaker
|
||||
):
|
||||
async with db_sessionmaker() as db:
|
||||
await _seed_guide_data(db)
|
||||
await make_user("reader6@example.com", role_ids=[seeded["roles"]["user"].id])
|
||||
await _web_login(client, "reader6@example.com")
|
||||
resp = await client.get("/scopes")
|
||||
assert resp.status_code == 200
|
||||
assert 'name="scope_ids"' not in resp.text
|
||||
assert "delete-selected-scopes-btn" not in resp.text
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_scopes_page_shows_checkboxes_for_write_user(
|
||||
client, seeded, make_user, db_sessionmaker
|
||||
):
|
||||
async with db_sessionmaker() as db:
|
||||
ids = await _seed_guide_data(db)
|
||||
await make_user("writer@example.com", is_superuser=True)
|
||||
await _web_login(client, "writer@example.com")
|
||||
resp = await client.get("/scopes")
|
||||
assert resp.status_code == 200
|
||||
assert f'name="scope_ids" value="{ids["s1"].id}"' in resp.text
|
||||
assert "delete-selected-scopes-btn" in resp.text
|
||||
assert 'hx-post="/scopes/bulk-delete"' in resp.text
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_bulk_delete_scopes_removes_selected_augmented_parts(
|
||||
client, seeded, make_user, db_sessionmaker
|
||||
):
|
||||
async with db_sessionmaker() as db:
|
||||
ids = await _seed_guide_data(db)
|
||||
await make_user("writer2@example.com", is_superuser=True)
|
||||
await _web_login(client, "writer2@example.com")
|
||||
|
||||
resp = await client.post(
|
||||
"/scopes/bulk-delete",
|
||||
data={"scope_ids": [str(ids["s1"].id), str(ids["s2"].id)]},
|
||||
)
|
||||
assert resp.status_code == 200
|
||||
|
||||
async with db_sessionmaker() as db:
|
||||
from app.crud import augmented_part as augmented_part_crud
|
||||
|
||||
assert await augmented_part_crud.get(db, ids["s1"].id) is None
|
||||
assert await augmented_part_crud.get(db, ids["s2"].id) is None
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_bulk_delete_scopes_leaves_unselected_rows(
|
||||
client, seeded, make_user, db_sessionmaker
|
||||
):
|
||||
async with db_sessionmaker() as db:
|
||||
ids = await _seed_guide_data(db)
|
||||
await make_user("writer3@example.com", is_superuser=True)
|
||||
await _web_login(client, "writer3@example.com")
|
||||
|
||||
resp = await client.post(
|
||||
"/scopes/bulk-delete", data={"scope_ids": [str(ids["s1"].id)]}
|
||||
)
|
||||
assert resp.status_code == 200
|
||||
|
||||
async with db_sessionmaker() as db:
|
||||
from app.crud import augmented_part as augmented_part_crud
|
||||
|
||||
assert await augmented_part_crud.get(db, ids["s1"].id) is None
|
||||
assert await augmented_part_crud.get(db, ids["s2"].id) is not None
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_bulk_delete_scopes_forbidden_without_write_permission(
|
||||
client, seeded, make_user, db_sessionmaker
|
||||
):
|
||||
async with db_sessionmaker() as db:
|
||||
ids = await _seed_guide_data(db)
|
||||
await make_user("reader7@example.com", role_ids=[seeded["roles"]["user"].id])
|
||||
await _web_login(client, "reader7@example.com")
|
||||
|
||||
resp = await client.post(
|
||||
"/scopes/bulk-delete", data={"scope_ids": [str(ids["s1"].id)]}
|
||||
)
|
||||
assert resp.status_code == 403
|
||||
|
||||
async with db_sessionmaker() as db:
|
||||
from app.crud import augmented_part as augmented_part_crud
|
||||
|
||||
assert await augmented_part_crud.get(db, ids["s1"].id) is not None
|
||||
+10
-10
@@ -19,7 +19,7 @@ HEADERS = [
|
||||
"model",
|
||||
"magnification_min",
|
||||
"magnification_max",
|
||||
"objective_lens_outside_diameter_mm",
|
||||
"objective_lens_diameter_mm",
|
||||
"model_suffix",
|
||||
"sub_part_type",
|
||||
"product_number",
|
||||
@@ -52,7 +52,7 @@ def _scope_row(**overrides):
|
||||
"model": "Viper",
|
||||
"magnification_min": 5,
|
||||
"magnification_max": 25,
|
||||
"objective_lens_outside_diameter_mm": 56,
|
||||
"objective_lens_diameter_mm": 56,
|
||||
"model_suffix": "PST",
|
||||
"sub_part_type": "Riflescope",
|
||||
"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_max == 25
|
||||
assert info.model_suffix == "PST"
|
||||
assert info.objective_lens_diameter_mm == 56
|
||||
assert info.objective_thread_pitch == "0.75"
|
||||
|
||||
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 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"]
|
||||
assert len(objectives) == 2 # static OD + thread
|
||||
static_obj = [a for a in objectives if a.static_connection is not None]
|
||||
thread_obj = [a for a in objectives if a.iso_thread is not None]
|
||||
assert static_obj and static_obj[0].static_connection.od_mm == 56
|
||||
assert thread_obj
|
||||
assert thread_obj[0].iso_thread.diameter_mm == 40
|
||||
assert thread_obj[0].iso_thread.pitch_mm == 0.75
|
||||
assert len(objectives) == 1
|
||||
assert objectives[0].static_connection is None
|
||||
assert objectives[0].iso_thread.diameter_mm == 40
|
||||
assert objectives[0].iso_thread.pitch_mm == 0.75
|
||||
|
||||
|
||||
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.
|
||||
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):
|
||||
|
||||
Reference in new issue
Block a user