Compare commits

..
61 Commits
Author SHA1 Message Date
Ian Roddis 16ba09f073 Fixing serialization for manufacturers in schema 2026-09-04 22:57:48 -03:00
Ian Roddis 53fe03fb2d Fixing serialization for manufacturers 2026-09-04 22:46:30 -03:00
Ian Roddis c414708b32 Fixing migrations for postgres 2026-08-18 21:28:39 -03:00
Ian Roddis 443a128ad7 Adding migrations to entrypoint 2026-08-18 21:12:20 -03:00
Ian Roddis 167ef3a409 Adding optic fits data 2026-08-18 16:57:56 -03:00
Ian Roddis b4dcb68bea Adding Manual connection point to allow undeclared fits 2026-08-18 16:57:28 -03:00
Ian Roddis 7e246be39c Adding autovivify aps 2026-08-18 16:33:23 -03:00
Ian Roddis 34d538f9a0 Adjusting default product name 2026-08-18 16:19:03 -03:00
Ian Roddis 9a9ebab7c8 Making dashboard a bit more useful 2026-08-18 14:07:18 -03:00
Ian Roddis 3253d8e858 Allowing unauthenticated users to see scopes 2026-08-18 13:44:55 -03:00
Ian Roddis 2ce4f91e4f Relayout of Augmented Part details page 2026-08-18 13:12:38 -03:00
Ian Roddis 6806caf8d7 Rearranging some information layout in augmented parts, adding colourization to scope table. 2026-08-18 13:01:37 -03:00
Ian Roddis 387ae49efc Updating TODO 2026-08-17 22:20:00 -03:00
Ian Roddis b7c4b05150 Apply ruff fixes 2026-08-17 22:19:28 -03:00
Ian Roddis 413e9e8381 Adding archived flag to augmented parts, fixing migrations so that creation from scratch is successful 2026-08-17 22:16:53 -03:00
Ian Roddis 740dfc97c7 Running black on all files 2026-08-17 21:33:48 -03:00
Ian Roddis 3237824e83 Fixing unit test issues 2026-08-17 21:30:52 -03:00
Ian Roddis 082ad16963 Fixing test case for attachment label matching 2026-08-17 21:18:26 -03:00
Ian Roddis 85cb58ef65 Adding support for clamp inference 2026-08-17 21:16:59 -03:00
Ian Roddis ab5315b862 Disabling attachment point label for purposes of finding fits 2026-08-17 21:08:37 -03:00
Ian Roddis 4d6fda6f74 Adding TODO 2026-08-17 16:56:19 -03:00
Ian Roddis a35d064e32 Renaming Occular to Ocular 2026-08-17 16:55:57 -03:00
Ian Roddis ce2bdc43a8 Making text search apply to both description and sku in imported parts and augmented parts 2026-08-17 16:05:01 -03:00
Ian Roddis 292830ae0f Removing objective_thread_pitch from scopes_info 2026-08-17 15:48:50 -03:00
Ian Roddis 067ddffbe5 Augmented Parts detail: description changed to sku for title 2026-08-17 15:09:52 -03:00
Ian Roddis bdb6c7ebfc Tightening up scope page and adding mouseover hints 2026-08-17 15:06:44 -03:00
Ian Roddis 2318a6daeb Making scope filter by product id 2026-08-11 13:11:15 -03:00
Ian Roddis 6d7c5c5604 Adding pagination to scopes page 2026-08-11 11:33:56 -03:00
Ian Roddis 7a15d9f7c0 Adding a bulk delete option to the scopes page 2026-08-10 12:15:09 -03:00
Ian Roddis a9c4c16d4f Extending session login time 2026-08-10 12:09:51 -03:00
Ian Roddis 14aa2ac5d6 Adding delete option for scopes 2026-08-10 12:04:56 -03:00
Ian Roddis f4771e33d4 Fixing tag detection 2026-08-05 15:27:17 -04:00
Ian Roddis cf8d72f73e Making OData Refresh a background process 2026-08-05 16:26:52 -03:00
Ian Roddis 2e7976e382 Rejigging async jobs 2026-08-05 16:21:31 -03:00
Ian Roddis 2a0b8fcab2 Fixing errors and danger zone 2026-08-05 14:19:25 -03:00
Ian Roddis 392b79e801 Fixing presentation of objective thread 2026-08-05 14:15:10 -03:00
Ian Roddis 3ebe9a0894 Fixing imports 2026-08-05 13:59:57 -03:00
Ian Roddis 8d37b2ad67 Fixing column names, fixing model number 2026-08-05 13:35:09 -03:00
Ian Roddis 9ebd0f4498 Fixing automatic product_number assignment 2026-08-04 16:55:05 -03:00
Ian Roddis d54df47a2c Making objective_lens a thing 2026-08-04 16:48:12 -03:00
Ian Roddis 12e6984c63 Making objective_lens a thing 2026-08-04 16:48:00 -03:00
Ian Roddis fcb4dbaa62 Adding notes 2026-08-04 09:39:19 -03:00
Ian Roddis a617b935d3 Fixing formatting of magnification 2026-08-04 09:02:38 -03:00
Ian Roddis d108cb7149 Making long running operations asynchronous 2026-07-31 10:40:54 -03:00
Ian Roddis 373a2c6acf Adding favicon 2026-07-31 10:09:41 -03:00
Ian Roddis 2fb662080b Adding pervasive column sorting 2026-07-31 09:52:09 -03:00
Ian Roddis 34679d53e8 Adding AugmentedItem notes 2026-07-31 09:12:41 -03:00
Ian Roddis 30a13644ce Adding fit note at manual fit creation 2026-07-31 09:02:24 -03:00
Ian Roddis 23d07505bd Adding ability to autovivify an appropriate attachment point when manually entering a fit 2026-07-31 08:59:34 -03:00
Ian Roddis 92ca6b2a9f Adding fit type to create fit form 2026-07-31 08:56:39 -03:00
Ian Roddis b2d01f1ab0 Allowing attachment points to be pulled from descriptions on sync 2026-07-28 22:17:47 -03:00
Ian Roddis e24b8e5ea6 Updating CLAUDE.md 2026-07-28 22:08:13 -03:00
Ian Roddis aef60d77be Adding ability to edit attachment points on augmented item details 2026-07-28 22:04:40 -03:00
Ian Roddis 25fccd617f Redirecting deep links after login 2026-07-27 15:43:24 -03:00
Ian Roddis a7fb902611 Adding dataset 2026-07-27 15:19:48 -03:00
Ian Roddis 1b52ad04bc Adding part details page 2026-07-27 15:11:58 -03:00
Ian Roddis 2587ccbceb Adding filters and controls for scopes 2026-07-27 14:59:03 -03:00
Ian Roddis 3febc15a8f Adding top-level Scopes table 2026-07-27 14:53:23 -03:00
Ian Roddis bc8b67d585 Adding longer database wait time 2026-07-23 11:24:04 -03:00
Ian Roddis 16174fe4cb Adjusting regex for ISO and UNC threads to better support floats 2026-07-23 10:54:10 -03:00
Ian Roddis b5529e0dc5 Exposing data path to support sqlite volume maps 2026-07-20 11:00:00 -04:00
133 changed files with 6349 additions and 689 deletions

No files matched your search

+30 -1
View File
@@ -10,7 +10,36 @@
"Bash(python *)", "Bash(python *)",
"Bash(DATABASE_URL=\"sqlite+aiosqlite:////tmp/claude-1000/-home-iroddis-dev-armament-fitguide/41e5e14d-88fe-45e7-ba3a-be5a2cbde2cd/scratchpad/mig_test.db\" uv run alembic upgrade head)", "Bash(DATABASE_URL=\"sqlite+aiosqlite:////tmp/claude-1000/-home-iroddis-dev-armament-fitguide/41e5e14d-88fe-45e7-ba3a-be5a2cbde2cd/scratchpad/mig_test.db\" uv run alembic upgrade head)",
"Bash(sqlite3 /tmp/claude-1000/-home-iroddis-dev-armament-fitguide/41e5e14d-88fe-45e7-ba3a-be5a2cbde2cd/scratchpad/mig_test.db '.schema users')", "Bash(sqlite3 /tmp/claude-1000/-home-iroddis-dev-armament-fitguide/41e5e14d-88fe-45e7-ba3a-be5a2cbde2cd/scratchpad/mig_test.db '.schema users')",
"Bash(docker build *)" "Bash(docker build *)",
"Bash(git status *)",
"Bash(git -C /home/iroddis/dev/armament/fitguide status --short)",
"Bash(git -C /home/iroddis/dev/armament/fitguide log --oneline -10)",
"Bash(node --check /Users/roddis/dev/armament/fitguide/app/static/js/sortable-tables.js)",
"Bash(sqlite3 /Users/roddis/dev/armament/fitguide/app.db \"select count\\(*\\) from users;\")",
"Bash(sqlite3 /Users/roddis/dev/armament/fitguide/app.db \"select count\\(*\\) from part_colours;\")",
"Bash(sqlite3 /Users/roddis/dev/armament/fitguide/app.db \"select count\\(*\\) from manufacturers;\")",
"Bash(curl -s -c /tmp/fg_cookies.txt -o /dev/null -w \"login:%{http_code}\\\\n\" -d \"email=admin@armament.com&password=changethis\" http://localhost:8000/login)",
"Bash(curl -s -b /tmp/fg_cookies.txt http://localhost:8000/admin/manufacturers)",
"Bash(curl -s -o /dev/null -w \"%{http_code} %{content_type}\\\\n\" http://localhost:8000/static/js/sortable-tables.js)",
"Bash(curl -s -b /tmp/fg_cookies.txt \"http://localhost:8000/admin/augmented-parts\")",
"Bash(curl -s -b /tmp/fg_cookies.txt \"http://localhost:8000/admin/augmented-parts/filter?sort=colour&dir=desc\")",
"Bash(curl -s -b /tmp/fg_cookies.txt \"http://localhost:8000/admin/roles\")",
"Bash(curl -s http://localhost:8000/scopes)",
"Bash(curl -s -o /dev/null -w \"%{http_code}\\\\n\" http://localhost:8000/scopes)",
"Bash(curl -s -b /tmp/fg_cookies.txt http://localhost:8000/scopes)",
"Bash(lsof -ti:8000 -sTCP:LISTEN)",
"Bash(xargs -r kill)",
"Bash(rm -f /tmp/fg_cookies.txt /private/tmp/claude-501/-Users-roddis-dev-armament-fitguide/7873fd9c-2f2b-4583-877e-72fc88718f24/scratchpad/smoke_sort.py)",
"Bash(curl -s -o /dev/null -w \"%{http_code}\\\\n\" http://localhost:8000/login)",
"Bash(curl -s -b /tmp/fg_cookies.txt http://localhost:8000__TRACKED_VAR__)",
"Bash(sqlite3 app.db \"PRAGMA foreign_keys=ON; SELECT scope_model_id FROM augmented_parts WHERE id=1862;\")",
"Bash(sqlite3 app.db \"SELECT id, name FROM scope_models WHERE name='Viper';\")",
"Bash(sqlite3 app.db \"SELECT COUNT\\(*\\) FROM augmented_parts WHERE scope_model_id=\\(SELECT id FROM scope_models WHERE name='Viper'\\);\")",
"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)"
] ]
} }
} }
+3
View File
@@ -25,6 +25,9 @@ AUTO_CREATE_TABLES=true
COOKIE_NAME=access_token COOKIE_NAME=access_token
# Set true when serving over HTTPS. # Set true when serving over HTTPS.
COOKIE_SECURE=false 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) --- # --- LDAP (optional; direct-bind) ---
# When enabled, the login page and /api/auth/ldap-login accept a directory # When enabled, the login page and /api/auth/ldap-login accept a directory
+2
View File
@@ -10,6 +10,8 @@ venv/
# Databases # Databases
*.db *.db
*.db-shm
*.db-wal
*.sqlite3 *.sqlite3
# Test / tooling # Test / tooling
+94 -13
View File
@@ -1,26 +1,41 @@
# CLAUDE.md # CLAUDE.md
Guidance for working in this repo. Read alongside `README.md` (user-facing setup). This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
Guidance for working in this repo. Read alongside `README.md` (user-facing setup
for the original FastAPI skeleton this app was built from).
## What this is ## What this is
A FastAPI skeleton serving **both** a JSON API (`/api/*`) and server-rendered **Fitguide**: a parts-compatibility guide for Armament scopes and accessories,
HTML via **Jinja2 + HTMX** (`/`, `/admin/*`). Async SQLAlchemy 2.0 ORM, JWT auth built on a FastAPI skeleton that serves **both** a JSON API (`/api/*`) and
with refresh-token revocation, and RBAC (users → roles → permissions). server-rendered HTML via **Jinja2 + HTMX** (`/`, `/scopes`, `/admin/*`). Async
SQLAlchemy 2.0 ORM, JWT auth with refresh-token revocation (plus optional LDAP
direct-bind login), and RBAC (users → roles → permissions). The domain layer on
top of the skeleton tracks parts, their attachment points, and which parts fit
together — see "Domain model" below.
## Commands ## Commands
```bash ```bash
uv sync --extra dev # install (incl. dev tools) uv sync --extra dev # install (incl. dev tools)
uv run alembic upgrade head # apply migrations
uv run python -m scripts.seed # seed perms/roles/superuser (idempotent) uv run python -m scripts.seed # seed perms/roles/superuser (idempotent)
uv run uvicorn app.main:app --reload # dev server on :8000 uv run uvicorn app.main:app --reload # dev server on :8000
uv run pytest # tests (in-memory SQLite) uv run pytest # tests (in-memory SQLite)
uv run pytest tests/test_foo.py::test_bar # single test
uv run ruff check . [--fix] # lint uv run ruff check . [--fix] # lint
uv run alembic upgrade head # apply migrations uv run black . # format (ruff does not format here)
uv run alembic revision --autogenerate -m "msg" # new migration after model changes uv run alembic revision --autogenerate -m "msg" # new migration after model changes
docker compose up --build # app + Postgres, auto-seeded docker compose up --build # app + Postgres, auto-seeded
``` ```
`Makefile` wraps the common ones (`make sync|seed|dev|test|fmt|migrate`).
`make clean` deletes the local `app.db`, re-migrates, and reseeds — the quick
way to get a clean dev database. `make seed` runs `alembic upgrade head` before
seeding, i.e. day-to-day dev relies on migrations, not just
`AUTO_CREATE_TABLES` (see "Database").
## Architecture / layering ## Architecture / layering
Request → router (`app/api/` JSON or `app/web/` HTML) → `app/crud/` (data access) Request → router (`app/api/` JSON or `app/web/` HTML) → `app/crud/` (data access)
@@ -28,40 +43,106 @@ Request → router (`app/api/` JSON or `app/web/` HTML) → `app/crud/` (data ac
token logic in `app/services/auth.py`. Keep route handlers thin; put DB logic in token logic in `app/services/auth.py`. Keep route handlers thin; put DB logic in
`crud/`, not in routers. `crud/`, not in routers.
`app/api/` is one router module per resource, all mounted under `/api` by
`app/api/router.py`. `app/web/` is organized differently: `pages.py` (dashboard/
login), `scopes.py` (the public-facing `/scopes` fit guide), and **`admin.py`**
— a single ~3000-line router covering every admin screen (users, roles, part
taxonomy tables, imported parts, augmented parts + detail + nested attachment
points/fits, audit log, etc.), organized into `# --- Section ---` banner
comments per resource rather than split into files. When adding an admin screen,
follow the existing banner-section convention there rather than starting a new
web router module.
## Domain model
The business domain, independent of the auth/RBAC skeleton:
- **`AugmentedPart`** (`augmented_parts`) is the generic "part" entity — a
scope, flip cover, ARD, polarizer, adapter ring, etc. — distinguished by
`PartType`/`SubPartType`, with `PartColour`, `Manufacturer`, and an optional
`ScopeModel`. "Scope" is just one `PartType` value (see `SCOPE_TYPE_NAME` in
`app/services/scope_guide.py`), not a separate table.
- **`ScopeInfo`** is a 1:1 extension of `AugmentedPart` (magnification,
objective thread, model suffix) populated only for scope-type parts.
- **`AttachmentPoint`** belongs to one `AugmentedPart`, is named via
`AttachmentPointName` (e.g. "Ocular", "Objective"), and connects via
*exactly one* of `IsoThread` / `UncThread` / `ClampConnection` /
`StaticConnection` — that "exactly one" invariant is enforced in the app
layer (CRUD/API validation), not by a DB constraint.
- **`Fit`** (`app/models/fit.py`) pairs two `AttachmentPoint`s (canonical
ordering enforced in `app/crud/fit.py` so a pair only ever has one row),
labeled `possible` / `exact` / `none`, with threaded `FitNote`s.
`app/services/fit_matching.py` computes `possible` fits automatically
(matching ISO/UNC thread specs exactly, or a static connection's dimension
falling inside a clamp connection's range); `exact`/`none` are user-asserted
and never touched by the algorithm.
- **`ImportedPart`** (`imported_parts`, PK is the string `InventoryID` — not an
int id) holds raw rows from an external inventory feed, fetched via
`app/services/odata.py` (OData v4, paginated). `app/services/
augmented_part_sync.py` reconciles these into `AugmentedPart` +
`AttachmentPoint` rows: infers `PartType` by substring match on the
description, parses `TBXThreadAttribute` against ISO/UNC regexes (falling
back to an OD-derived `StaticConnection`), and prunes `AugmentedPart`s whose
backing `ImportedPart`s have disappeared.
- **`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 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
listing parts of that type with a recorded `Fit`.
## Conventions that matter ## Conventions that matter
- **Everything async.** Engine, sessions, CRUD, routes, tests. Use `await`; never - **Everything async.** Engine, sessions, CRUD, routes, tests. Use `await`; never
add a sync DB call. `get_db` (in `app/core/database.py`) yields the request session. add a sync DB call. `get_db` (in `app/core/database.py`) yields the request session.
- **Relationships use `lazy="selectin"`** (`User.roles`, `Role.permissions`) so - **Relationships use `lazy="selectin"`** (`User.roles`, `Role.permissions`,
attributes are safe to read after the session closes — required under async. `AttachmentPoint.iso_thread`, etc.) so attributes are safe to read after the
Preserve this when adding relationships that templates/schemas read. session closes — required under async. Preserve this when adding relationships
that templates/schemas read.
- **Config** is `app/core/config.py` (`pydantic-settings`). Add new settings there; - **Config** is `app/core/config.py` (`pydantic-settings`). Add new settings there;
they're auto-read from env vars (uppercased field name) or `.env`. Env wins. they're auto-read from env vars (uppercased field name) or `.env`. Env wins.
- **RBAC**: guard API routes with `Depends(require_permission("resource:action"))`; - **RBAC**: guard API routes with `Depends(require_permission("resource:action"))`;
guard web routes with `web_require(...)` in `app/web/admin.py`. Superusers bypass guard web routes with `web_require(...)` in `app/web/admin.py`. Superusers bypass
all checks (`User.is_superuser` / `User.has_permission`). Add new permission all checks (`User.is_superuser` / `User.has_permission`). Add new permission
strings to `scripts/seed.py`'s `PERMISSIONS`/`ROLES`. strings to `scripts/seed.py`'s `PERMISSIONS`/`ROLES`.
- **HTMX UI-level write gating**: on top of route-level RBAC, pages that mix
read-only and read-write viewers (e.g. `augmented_part_detail.html`'s Fits and
Attachment Points sections) compute a `can_write_<thing>` bool from the
current user's permissions and wrap add/edit/delete controls in
`{% if can_write_<thing> %}` in the template, so read-only users see data but
not controls they'd get a 403 on. The route-level `web_require(...)` check
remains the real security boundary; the template flag is only for UX.
- **Auth, two transports**: API uses `Authorization: Bearer`; the HTMX UI uses an - **Auth, two transports**: API uses `Authorization: Bearer`; the HTMX UI uses an
httpOnly cookie (`settings.cookie_name`). `app/deps.py` reads both. Web auth httpOnly cookie (`settings.cookie_name`). `app/deps.py` reads both. Web auth
failures raise `NotAuthenticated`, handled in `main.py` as a redirect to `/login`. failures raise `NotAuthenticated`, handled in `main.py` as a redirect to `/login`
(preserving the original path as `?next=`).
- **Refresh tokens** are stored server-side (hash + `jti` + `revoked`) in the - **Refresh tokens** are stored server-side (hash + `jti` + `revoked`) in the
`refresh_tokens` table. `services/auth.rotate_refresh_token` revokes-then-reissues. `refresh_tokens` table. `services/auth.rotate_refresh_token` revokes-then-reissues.
Note: JWT payload `exp` is a tz-aware `datetime` (PyJWT only epoch-encodes inside Note: JWT payload `exp` is a tz-aware `datetime` (PyJWT only epoch-encodes inside
the token) — pass it straight to the model, don't `fromtimestamp` it. the token) — pass it straight to the model, don't `fromtimestamp` it.
- **LDAP login** (`app/services/ldap.py`) is optional (`settings.ldap_enabled`),
direct-bind, and just-in-time provisions a local `User` on first successful
login. `ldap3` is sync, so its calls run via `run_in_threadpool`.
## Database ## Database
One `DATABASE_URL` drives SQLite (`sqlite+aiosqlite://`, the default) or Postgres One `DATABASE_URL` drives SQLite (`sqlite+aiosqlite://`, the default) or Postgres
(`postgresql+asyncpg://`). `AUTO_CREATE_TABLES=true` calls `Base.metadata.create_all` (`postgresql+asyncpg://`). `AUTO_CREATE_TABLES=true` calls `Base.metadata.create_all`
on startup for dev convenience; production should use Alembic instead. `init_db()` on startup for dev convenience, but the day-to-day `make seed`/`make clean` flow
imports `app.models` inside the function to avoid a circular import. runs `alembic upgrade head` first — treat Alembic migrations as the source of
truth and keep them in sync with model changes. `init_db()` imports `app.models`
inside the function to avoid a circular import.
## Tests ## Tests
`tests/conftest.py` builds an in-memory SQLite engine with `StaticPool`, creates `tests/conftest.py` builds an in-memory SQLite engine with `StaticPool`, creates
tables, and overrides `get_db`. Fixtures: `client` (httpx ASGI), `seeded` tables, and overrides `get_db`. Fixtures: `client` (httpx ASGI), `seeded`
(perms+roles), `make_user`. `asyncio_mode = "auto"` — write `async def test_*` (perms+roles from `scripts.seed.PERMISSIONS`/`ROLES`), `make_user`, and
without decorators. `db_sessionmaker` (used directly in tests to seed domain rows — models, fits,
attachment points — beyond what `seeded`/`make_user` cover). `asyncio_mode =
"auto"` — write `async def test_*` without decorators.
## When extending ## When extending
+2
View File
@@ -20,6 +20,8 @@ RUN uv sync --no-dev
COPY . . COPY . .
RUN chmod +x entrypoint.sh RUN chmod +x entrypoint.sh
# If sqlite3 is used
VOLUME /data
EXPOSE 8000 EXPOSE 8000
# Seeds perms/roles/superuser (idempotent, no-op if already present), then runs the API server. # Seeds perms/roles/superuser (idempotent, no-op if already present), then runs the API server.
+1
View File
@@ -21,3 +21,4 @@
- Models - Models
- Colours - Colours
- Part Types - Part Types
- Notes
+4 -2
View File
@@ -1,4 +1,4 @@
VERSION := $(shell git describe --tags --always) VERSION := $(shell git tag | tail -1)
IMAGE_NAME := fitguide IMAGE_NAME := fitguide
sync: sync:
@@ -8,7 +8,7 @@ seed:
uv run alembic upgrade head uv run alembic upgrade head
uv run python -m scripts.seed uv run python -m scripts.seed
dev: dev: migrate
uv run uvicorn app.main:app --reload uv run uvicorn app.main:app --reload
open http://localhost:8000 open http://localhost:8000
@@ -30,6 +30,8 @@ clean:
docker: docker:
mkdir -p artifacts mkdir -p artifacts
docker build -t $(IMAGE_NAME):$(VERSION) . docker build -t $(IMAGE_NAME):$(VERSION) .
docker-push: docker
docker tag $(IMAGE_NAME):$(VERSION) $(IMAGE_NAME):latest docker tag $(IMAGE_NAME):$(VERSION) $(IMAGE_NAME):latest
docker tag $(IMAGE_NAME):$(VERSION) git.kinesin.ca/kinesin/$(IMAGE_NAME):$(VERSION) docker tag $(IMAGE_NAME):$(VERSION) git.kinesin.ca/kinesin/$(IMAGE_NAME):$(VERSION)
docker tag $(IMAGE_NAME):$(VERSION) git.kinesin.ca/kinesin/$(IMAGE_NAME):latest docker tag $(IMAGE_NAME):$(VERSION) git.kinesin.ca/kinesin/$(IMAGE_NAME):latest
+2 -1
View File
@@ -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** | | `SECRET_KEY` | dev placeholder | **set a strong value in production** |
| `DATABASE_URL` | `sqlite+aiosqlite:///./app.db` | use `postgresql+asyncpg://...` for Postgres | | `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` | | | `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 | | `AUTO_CREATE_TABLES` | `true` | create tables on startup (dev); use Alembic in prod |
| `COOKIE_SECURE` | `false` | set `true` behind HTTPS | | `COOKIE_SECURE` | `false` | set `true` behind HTTPS |
| `FIRST_SUPERUSER_EMAIL` / `_PASSWORD` | admin@example.com / changethis | used by seed | | `FIRST_SUPERUSER_EMAIL` / `_PASSWORD` | admin@example.com / changethis | used by seed |
@@ -0,0 +1,45 @@
"""add jobs table
Revision ID: 04425ce3d4d5
Revises: a3f7c19b2d40
Create Date: 2026-08-05 15:21:09.527442
"""
from collections.abc import Sequence
import sqlalchemy as sa
from alembic import op
# revision identifiers, used by Alembic.
revision: str = "04425ce3d4d5"
down_revision: str | None = "a3f7c19b2d40"
branch_labels: str | Sequence[str] | None = None
depends_on: 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 ###
@@ -5,25 +5,28 @@ Revises: 5b6a7817b408
Create Date: 2026-07-02 12:22:24.106795 Create Date: 2026-07-02 12:22:24.106795
""" """
from typing import Sequence, Union from collections.abc import Sequence
import sqlalchemy as sa
from alembic import op from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic. # revision identifiers, used by Alembic.
revision: str = "0c3d1a5cff1f" revision: str = "0c3d1a5cff1f"
down_revision: Union[str, None] = "5b6a7817b408" down_revision: str | None = "5b6a7817b408"
branch_labels: Union[str, Sequence[str], None] = None branch_labels: str | Sequence[str] | None = None
depends_on: Union[str, Sequence[str], None] = None depends_on: str | Sequence[str] | None = None
def upgrade() -> None: def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ### op.create_table(
pass "part_colours",
# ### end Alembic commands ### sa.Column("id", sa.Integer(), nullable=False),
sa.Column("name", sa.String(), nullable=False),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("name"),
)
def downgrade() -> None: def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ### op.drop_table("part_colours")
pass
# ### end Alembic commands ###
@@ -5,16 +5,17 @@ Revises: adf4a5b9493e
Create Date: 2026-07-14 15:00:00.000000 Create Date: 2026-07-14 15:00:00.000000
""" """
from typing import Sequence, Union from collections.abc import Sequence
import sqlalchemy as sa
from alembic import op from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic. # revision identifiers, used by Alembic.
revision: str = "1b99be8a9b1e" revision: str = "1b99be8a9b1e"
down_revision: Union[str, None] = "adf4a5b9493e" down_revision: str | None = "adf4a5b9493e"
branch_labels: Union[str, Sequence[str], None] = None branch_labels: str | Sequence[str] | None = None
depends_on: Union[str, Sequence[str], None] = None depends_on: str | Sequence[str] | None = None
def upgrade() -> None: def upgrade() -> None:
@@ -31,10 +32,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 +66,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,30 @@
"""drop_objective_thread_pitch_from_scope_infos
Revision ID: 235d823a56ee
Revises: 04425ce3d4d5
Create Date: 2026-08-17 15:18:26.763831
"""
from collections.abc import Sequence
import sqlalchemy as sa
from alembic import op
# revision identifiers, used by Alembic.
revision: str = "235d823a56ee"
down_revision: str | None = "04425ce3d4d5"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None
def upgrade() -> None:
with op.batch_alter_table("scope_infos", schema=None) as batch_op:
batch_op.drop_column("objective_thread_pitch")
def downgrade() -> None:
with op.batch_alter_table("scope_infos", schema=None) as batch_op:
batch_op.add_column(
sa.Column("objective_thread_pitch", sa.String(), nullable=True)
)
@@ -5,25 +5,29 @@ Revises: 7be5ef599d3f
Create Date: 2026-07-02 11:57:13.095417 Create Date: 2026-07-02 11:57:13.095417
""" """
from typing import Sequence, Union from collections.abc import Sequence
import sqlalchemy as sa
from alembic import op from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic. # revision identifiers, used by Alembic.
revision: str = "5b6a7817b408" revision: str = "5b6a7817b408"
down_revision: Union[str, None] = "7be5ef599d3f" down_revision: str | None = "7be5ef599d3f"
branch_labels: Union[str, Sequence[str], None] = None branch_labels: str | Sequence[str] | None = None
depends_on: Union[str, Sequence[str], None] = None depends_on: str | Sequence[str] | None = None
def upgrade() -> None: def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ### op.create_table(
pass "static_connections",
# ### end Alembic commands ### sa.Column("id", sa.Integer(), nullable=False),
sa.Column("od_mm", sa.Float(), nullable=False),
sa.Column("od_in", sa.Float(), nullable=False),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("od_mm"),
)
def downgrade() -> None: def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ### op.drop_table("static_connections")
pass
# ### end Alembic commands ###
@@ -6,16 +6,15 @@ Create Date: 2026-06-24 21:50:05.340235
""" """
from collections.abc import Sequence from collections.abc import Sequence
from typing import Union
import sqlalchemy as sa import sqlalchemy as sa
from alembic import op from alembic import op
revision: str = "627d4460a94f" revision: str = "627d4460a94f"
down_revision: Union[str, None] = "0001_initial" down_revision: str | None = "0001_initial"
branch_labels: Union[str, Sequence[str], None] = None branch_labels: str | Sequence[str] | None = None
depends_on: Union[str, Sequence[str], None] = None depends_on: str | Sequence[str] | None = None
def upgrade() -> None: def upgrade() -> None:
@@ -5,25 +5,28 @@ Revises: c7882cedc9ae
Create Date: 2026-07-02 13:54:58.335394 Create Date: 2026-07-02 13:54:58.335394
""" """
from typing import Sequence, Union from collections.abc import Sequence
import sqlalchemy as sa
from alembic import op from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic. # revision identifiers, used by Alembic.
revision: str = "70e48bda8829" revision: str = "70e48bda8829"
down_revision: Union[str, None] = "c7882cedc9ae" down_revision: str | None = "c7882cedc9ae"
branch_labels: Union[str, Sequence[str], None] = None branch_labels: str | Sequence[str] | None = None
depends_on: Union[str, Sequence[str], None] = None depends_on: str | Sequence[str] | None = None
def upgrade() -> None: def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ### op.create_table(
pass "attachment_point_names",
# ### end Alembic commands ### sa.Column("id", sa.Integer(), nullable=False),
sa.Column("name", sa.String(), nullable=False),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("name"),
)
def downgrade() -> None: def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ### op.drop_table("attachment_point_names")
pass
# ### end Alembic commands ###
@@ -5,25 +5,42 @@ Revises: b60d0aa3ceda
Create Date: 2026-06-25 15:29:20.685050 Create Date: 2026-06-25 15:29:20.685050
""" """
from typing import Sequence, Union from collections.abc import Sequence
import sqlalchemy as sa
from alembic import op from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic. # revision identifiers, used by Alembic.
revision: str = "7be5ef599d3f" revision: str = "7be5ef599d3f"
down_revision: Union[str, None] = "b60d0aa3ceda" down_revision: str | None = "b60d0aa3ceda"
branch_labels: Union[str, Sequence[str], None] = None branch_labels: str | Sequence[str] | None = None
depends_on: Union[str, Sequence[str], None] = None depends_on: str | Sequence[str] | None = None
def upgrade() -> None: def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ### op.create_table(
pass "imported_parts",
# ### end Alembic commands ### sa.Column("InventoryID", sa.String(), nullable=False),
sa.Column("Description", sa.String(), nullable=True),
sa.Column("TBXColour", sa.String(), nullable=True),
sa.Column("ProductMaxThreadDepthmm", sa.String(), nullable=True),
sa.Column("ProductMaxThreadDepthin", sa.String(), nullable=True),
sa.Column("ProductODmm", sa.String(), nullable=True),
sa.Column("ProductODin", sa.String(), nullable=True),
sa.Column("TBXThreadAttribute", sa.String(), nullable=True),
sa.PrimaryKeyConstraint("InventoryID"),
)
op.create_table(
"imported_part_config",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("odata_url", sa.String(), nullable=True),
sa.Column("username", sa.String(), nullable=True),
sa.Column("password", sa.String(), nullable=True),
sa.PrimaryKeyConstraint("id"),
)
def downgrade() -> None: def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ### op.drop_table("imported_part_config")
pass op.drop_table("imported_parts")
# ### end Alembic commands ###
@@ -0,0 +1,44 @@
"""add_augmented_part_notes_table
Revision ID: 8012c00b0c04
Revises: d1e2f3a4b5c6
Create Date: 2026-07-31 09:07:06.137175
"""
from collections.abc import Sequence
import sqlalchemy as sa
from alembic import op
# revision identifiers, used by Alembic.
revision: str = "8012c00b0c04"
down_revision: str | None = "d1e2f3a4b5c6"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None
def upgrade() -> None:
op.create_table(
"augmented_part_notes",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("augmented_part_id", sa.Integer(), nullable=False),
sa.Column("user_id", sa.Integer(), nullable=True),
sa.Column("username", sa.String(length=255), nullable=False),
sa.Column("body", sa.Text(), nullable=False),
sa.Column(
"created_at",
sa.DateTime(timezone=True),
server_default=sa.func.now(),
nullable=False,
),
sa.ForeignKeyConstraint(
["augmented_part_id"], ["augmented_parts.id"], ondelete="CASCADE"
),
sa.ForeignKeyConstraint(["user_id"], ["users.id"], ondelete="SET NULL"),
sa.PrimaryKeyConstraint("id"),
)
def downgrade() -> None:
op.drop_table("augmented_part_notes")
@@ -5,25 +5,48 @@ Revises: 0c3d1a5cff1f
Create Date: 2026-07-02 12:37:59.457033 Create Date: 2026-07-02 12:37:59.457033
""" """
from typing import Sequence, Union from collections.abc import Sequence
import sqlalchemy as sa
from alembic import op from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic. # revision identifiers, used by Alembic.
revision: str = "8bc411b5814e" revision: str = "8bc411b5814e"
down_revision: Union[str, None] = "0c3d1a5cff1f" down_revision: str | None = "0c3d1a5cff1f"
branch_labels: Union[str, Sequence[str], None] = None branch_labels: str | Sequence[str] | None = None
depends_on: Union[str, Sequence[str], None] = None depends_on: str | Sequence[str] | None = None
def upgrade() -> None: def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ### op.create_table(
pass "augmented_parts",
# ### end Alembic commands ### sa.Column("id", sa.Integer(), nullable=False),
sa.Column("sku", sa.String(), nullable=True),
sa.Column("description", sa.String(), nullable=False),
sa.Column("colour_id", sa.Integer(), nullable=False),
sa.Column("manufacturer_id", sa.Integer(), nullable=False),
sa.Column("scope_model_id", sa.Integer(), nullable=True),
sa.Column("part_type_id", sa.Integer(), nullable=False),
sa.Column("sub_part_type_id", sa.Integer(), nullable=True),
sa.ForeignKeyConstraint(
["colour_id"], ["part_colours.id"], ondelete="RESTRICT"
),
sa.ForeignKeyConstraint(
["manufacturer_id"], ["manufacturers.id"], ondelete="RESTRICT"
),
sa.ForeignKeyConstraint(
["scope_model_id"], ["scope_models.id"], ondelete="SET NULL"
),
sa.ForeignKeyConstraint(
["part_type_id"], ["part_types.id"], ondelete="RESTRICT"
),
sa.ForeignKeyConstraint(
["sub_part_type_id"], ["sub_part_types.id"], ondelete="SET NULL"
),
sa.PrimaryKeyConstraint("id"),
)
def downgrade() -> None: def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ### op.drop_table("augmented_parts")
pass
# ### end Alembic commands ###
@@ -0,0 +1,32 @@
"""add_archived_to_augmented_parts
Revision ID: 9a1c2f6e0b3d
Revises: 235d823a56ee
Create Date: 2026-08-17 21:30:00.000000
"""
from collections.abc import Sequence
import sqlalchemy as sa
from alembic import op
# revision identifiers, used by Alembic.
revision: str = "9a1c2f6e0b3d"
down_revision: str | None = "235d823a56ee"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None
def upgrade() -> None:
with op.batch_alter_table("augmented_parts", schema=None) as batch_op:
batch_op.add_column(
sa.Column(
"archived", sa.Boolean(), nullable=False, server_default=sa.false()
)
)
def downgrade() -> None:
with op.batch_alter_table("augmented_parts", schema=None) as batch_op:
batch_op.drop_column("archived")
@@ -5,16 +5,17 @@ Revises: 627d4460a94f
Create Date: 2026-06-25 10:32:11.398529 Create Date: 2026-06-25 10:32:11.398529
""" """
from typing import Sequence, Union from collections.abc import Sequence
import sqlalchemy as sa
from alembic import op from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic. # revision identifiers, used by Alembic.
revision: str = "a192abbf4091" revision: str = "a192abbf4091"
down_revision: Union[str, None] = "627d4460a94f" down_revision: str | None = "627d4460a94f"
branch_labels: Union[str, Sequence[str], None] = None branch_labels: str | Sequence[str] | None = None
depends_on: Union[str, Sequence[str], None] = None depends_on: str | Sequence[str] | None = None
def upgrade() -> None: def upgrade() -> None:
@@ -27,12 +28,19 @@ def upgrade() -> None:
sa.Column("logo_url", sa.String(), nullable=True), sa.Column("logo_url", sa.String(), nullable=True),
sa.PrimaryKeyConstraint("id"), sa.PrimaryKeyConstraint("id"),
) )
# batch_alter_table's alter_column doesn't emit CREATE TYPE for the new
# Enum on its own (unlike create_table, which ties enum creation to the
# table-create DDL event) - create it explicitly first, or Postgres fails
# with `type "uncseries" does not exist`. checkfirst / no-op on SQLite.
uncseries = sa.Enum("UNC", "UNF", "UNEF", "UN", "UNS", name="uncseries")
uncseries.create(op.get_bind(), checkfirst=True)
with op.batch_alter_table("unc_threads", schema=None) as batch_op: with op.batch_alter_table("unc_threads", schema=None) as batch_op:
batch_op.alter_column( batch_op.alter_column(
"series", "series",
existing_type=sa.VARCHAR(length=8), existing_type=sa.VARCHAR(length=8),
type_=sa.Enum("UNC", "UNF", "UNEF", "UN", "UNS", name="uncseries"), type_=uncseries,
existing_nullable=False, existing_nullable=False,
postgresql_using="series::uncseries",
) )
# ### end Alembic commands ### # ### end Alembic commands ###
@@ -46,7 +54,9 @@ def downgrade() -> None:
existing_type=sa.Enum("UNC", "UNF", "UNEF", "UN", "UNS", name="uncseries"), existing_type=sa.Enum("UNC", "UNF", "UNEF", "UN", "UNS", name="uncseries"),
type_=sa.VARCHAR(length=8), type_=sa.VARCHAR(length=8),
existing_nullable=False, existing_nullable=False,
postgresql_using="series::varchar(8)",
) )
sa.Enum(name="uncseries").drop(op.get_bind(), checkfirst=True)
op.drop_table("manufacturers") op.drop_table("manufacturers")
# ### end Alembic commands ### # ### end Alembic commands ###
@@ -0,0 +1,29 @@
"""add_objective_lens_diameter_to_scope_infos
Revision ID: a3f7c19b2d40
Revises: 8012c00b0c04
Create Date: 2026-08-04 00:00:00.000000
"""
from collections.abc import Sequence
import sqlalchemy as sa
from alembic import op
# revision identifiers, used by Alembic.
revision: str = "a3f7c19b2d40"
down_revision: str | None = "8012c00b0c04"
branch_labels: str | Sequence[str] | None = None
depends_on: 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")
@@ -5,16 +5,17 @@ Revises: a192abbf4091
Create Date: 2026-06-25 10:35:40.227253 Create Date: 2026-06-25 10:35:40.227253
""" """
from typing import Sequence, Union from collections.abc import Sequence
import sqlalchemy as sa
from alembic import op from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic. # revision identifiers, used by Alembic.
revision: str = "a52e7ca7a925" revision: str = "a52e7ca7a925"
down_revision: Union[str, None] = "a192abbf4091" down_revision: str | None = "a192abbf4091"
branch_labels: Union[str, Sequence[str], None] = None branch_labels: str | Sequence[str] | None = None
depends_on: Union[str, Sequence[str], None] = None depends_on: str | Sequence[str] | None = None
def upgrade() -> None: def upgrade() -> None:
@@ -5,25 +5,52 @@ Revises: 70e48bda8829
Create Date: 2026-07-02 14:03:23.561253 Create Date: 2026-07-02 14:03:23.561253
""" """
from typing import Sequence, Union from collections.abc import Sequence
import sqlalchemy as sa
from alembic import op from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic. # revision identifiers, used by Alembic.
revision: str = "adf4a5b9493e" revision: str = "adf4a5b9493e"
down_revision: Union[str, None] = "70e48bda8829" down_revision: str | None = "70e48bda8829"
branch_labels: Union[str, Sequence[str], None] = None branch_labels: str | Sequence[str] | None = None
depends_on: Union[str, Sequence[str], None] = None depends_on: str | Sequence[str] | None = None
def upgrade() -> None: def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ### op.create_table(
pass "attachment_points",
# ### end Alembic commands ### sa.Column("id", sa.Integer(), nullable=False),
sa.Column("augmented_part_id", sa.Integer(), nullable=False),
sa.Column("attachment_point_name_id", sa.Integer(), nullable=False),
sa.Column("iso_thread_id", sa.Integer(), nullable=True),
sa.Column("unc_thread_id", sa.Integer(), nullable=True),
sa.Column("clamp_connection_id", sa.Integer(), nullable=True),
sa.Column("static_connection_id", sa.Integer(), nullable=True),
sa.ForeignKeyConstraint(
["augmented_part_id"], ["augmented_parts.id"], ondelete="CASCADE"
),
sa.ForeignKeyConstraint(
["attachment_point_name_id"],
["attachment_point_names.id"],
ondelete="RESTRICT",
),
sa.ForeignKeyConstraint(
["iso_thread_id"], ["iso_threads.id"], ondelete="RESTRICT"
),
sa.ForeignKeyConstraint(
["unc_thread_id"], ["unc_threads.id"], ondelete="RESTRICT"
),
sa.ForeignKeyConstraint(
["clamp_connection_id"], ["clamp_connections.id"], ondelete="RESTRICT"
),
sa.ForeignKeyConstraint(
["static_connection_id"], ["static_connections.id"], ondelete="RESTRICT"
),
sa.PrimaryKeyConstraint("id"),
)
def downgrade() -> None: def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ### op.drop_table("attachment_points")
pass
# ### end Alembic commands ###
@@ -5,25 +5,32 @@ Revises: fd5379986da4
Create Date: 2026-06-25 11:29:20.420056 Create Date: 2026-06-25 11:29:20.420056
""" """
from typing import Sequence, Union from collections.abc import Sequence
import sqlalchemy as sa
from alembic import op from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic. # revision identifiers, used by Alembic.
revision: str = "b60d0aa3ceda" revision: str = "b60d0aa3ceda"
down_revision: Union[str, None] = "fd5379986da4" down_revision: str | None = "fd5379986da4"
branch_labels: Union[str, Sequence[str], None] = None branch_labels: str | Sequence[str] | None = None
depends_on: Union[str, Sequence[str], None] = None depends_on: str | Sequence[str] | None = None
def upgrade() -> None: def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ### op.create_table(
pass "sub_part_types",
# ### end Alembic commands ### sa.Column("id", sa.Integer(), nullable=False),
sa.Column("name", sa.String(), nullable=False),
sa.Column("part_type_id", sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(
["part_type_id"], ["part_types.id"], ondelete="RESTRICT"
),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("name", "part_type_id"),
)
def downgrade() -> None: def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ### op.drop_table("sub_part_types")
pass
# ### end Alembic commands ###
@@ -5,25 +5,33 @@ Revises: 8bc411b5814e
Create Date: 2026-07-02 12:46:27.493942 Create Date: 2026-07-02 12:46:27.493942
""" """
from typing import Sequence, Union from collections.abc import Sequence
import sqlalchemy as sa
from alembic import op from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic. # revision identifiers, used by Alembic.
revision: str = "bfbce324cd5f" revision: str = "bfbce324cd5f"
down_revision: Union[str, None] = "8bc411b5814e" down_revision: str | None = "8bc411b5814e"
branch_labels: Union[str, Sequence[str], None] = None branch_labels: str | Sequence[str] | None = None
depends_on: Union[str, Sequence[str], None] = None depends_on: str | Sequence[str] | None = None
def upgrade() -> None: def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ### op.create_table(
pass "augmented_part_imported_parts",
# ### end Alembic commands ### sa.Column("augmented_part_id", sa.Integer(), nullable=False),
sa.Column("imported_part_id", sa.String(), nullable=False),
sa.ForeignKeyConstraint(
["augmented_part_id"], ["augmented_parts.id"], ondelete="CASCADE"
),
sa.ForeignKeyConstraint(
["imported_part_id"], ["imported_parts.InventoryID"], ondelete="CASCADE"
),
sa.PrimaryKeyConstraint("augmented_part_id", "imported_part_id"),
)
def downgrade() -> None: def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ### op.drop_table("augmented_part_imported_parts")
pass
# ### end Alembic commands ###
@@ -5,25 +5,30 @@ Revises: bfbce324cd5f
Create Date: 2026-07-02 13:31:57.064381 Create Date: 2026-07-02 13:31:57.064381
""" """
from typing import Sequence, Union from collections.abc import Sequence
import sqlalchemy as sa
from alembic import op from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic. # revision identifiers, used by Alembic.
revision: str = "c7882cedc9ae" revision: str = "c7882cedc9ae"
down_revision: Union[str, None] = "bfbce324cd5f" down_revision: str | None = "bfbce324cd5f"
branch_labels: Union[str, Sequence[str], None] = None branch_labels: str | Sequence[str] | None = None
depends_on: Union[str, Sequence[str], None] = None depends_on: str | Sequence[str] | None = None
def upgrade() -> None: def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ### op.create_table(
pass "clamp_connections",
# ### end Alembic commands ### sa.Column("id", sa.Integer(), nullable=False),
sa.Column("min_mm", sa.Float(), nullable=True),
sa.Column("max_mm", sa.Float(), nullable=True),
sa.Column("min_in", sa.Float(), nullable=True),
sa.Column("max_in", sa.Float(), nullable=True),
sa.PrimaryKeyConstraint("id"),
)
def downgrade() -> None: def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ### op.drop_table("clamp_connections")
pass
# ### end Alembic commands ###
@@ -5,16 +5,17 @@ Revises: c9f1a2b3d4e5
Create Date: 2026-07-16 20:00:00.000000 Create Date: 2026-07-16 20:00:00.000000
""" """
from typing import Sequence, Union from collections.abc import Sequence
import sqlalchemy as sa
from alembic import op from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic. # revision identifiers, used by Alembic.
revision: str = "d1e2f3a4b5c6" revision: str = "d1e2f3a4b5c6"
down_revision: Union[str, None] = "c9f1a2b3d4e5" down_revision: str | None = "c9f1a2b3d4e5"
branch_labels: Union[str, Sequence[str], None] = None branch_labels: str | Sequence[str] | None = None
depends_on: Union[str, Sequence[str], None] = None depends_on: str | Sequence[str] | None = None
def upgrade() -> None: def upgrade() -> None:
@@ -5,16 +5,17 @@ Revises: a52e7ca7a925
Create Date: 2026-06-25 10:49:56.463466 Create Date: 2026-06-25 10:49:56.463466
""" """
from typing import Sequence, Union from collections.abc import Sequence
import sqlalchemy as sa
from alembic import op from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic. # revision identifiers, used by Alembic.
revision: str = "e23aa8dd2536" revision: str = "e23aa8dd2536"
down_revision: Union[str, None] = "a52e7ca7a925" down_revision: str | None = "a52e7ca7a925"
branch_labels: Union[str, Sequence[str], None] = None branch_labels: str | Sequence[str] | None = None
depends_on: Union[str, Sequence[str], None] = None depends_on: str | Sequence[str] | None = None
def upgrade() -> None: def upgrade() -> None:
@@ -5,25 +5,43 @@ Revises: e23aa8dd2536
Create Date: 2026-06-25 11:06:13.028067 Create Date: 2026-06-25 11:06:13.028067
""" """
from typing import Sequence, Union from collections.abc import Sequence
import sqlalchemy as sa
from alembic import op from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic. # revision identifiers, used by Alembic.
revision: str = "fd5379986da4" revision: str = "fd5379986da4"
down_revision: Union[str, None] = "e23aa8dd2536" down_revision: str | None = "e23aa8dd2536"
branch_labels: Union[str, Sequence[str], None] = None branch_labels: str | Sequence[str] | None = None
depends_on: Union[str, Sequence[str], None] = None depends_on: str | Sequence[str] | None = None
def upgrade() -> None: def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ### op.create_table(
pass "audit_logs",
# ### end Alembic commands ### sa.Column("id", sa.Integer(), nullable=False),
sa.Column(
"created_at",
sa.DateTime(timezone=True),
server_default=sa.func.now(),
nullable=False,
),
sa.Column("user_id", sa.Integer(), nullable=True),
sa.Column("user_email", sa.String(length=255), nullable=False),
sa.Column("action", sa.String(length=16), nullable=False),
sa.Column("resource_type", sa.String(length=64), nullable=False),
sa.Column("resource_id", sa.Integer(), nullable=True),
sa.Column("description", sa.String(), nullable=False),
sa.ForeignKeyConstraint(["user_id"], ["users.id"], ondelete="SET NULL"),
sa.PrimaryKeyConstraint("id"),
)
op.create_index("ix_audit_logs_created_at", "audit_logs", ["created_at"])
op.create_index("ix_audit_logs_user_id", "audit_logs", ["user_id"])
def downgrade() -> None: def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ### op.drop_index("ix_audit_logs_user_id", table_name="audit_logs")
pass op.drop_index("ix_audit_logs_created_at", table_name="audit_logs")
# ### end Alembic commands ### op.drop_table("audit_logs")
+7 -3
View File
@@ -27,7 +27,7 @@ def _connection_label(ap) -> str:
return f"{cc.min_in}–{cc.max_in} in" return f"{cc.min_in}–{cc.max_in} in"
if ap.static_connection: if ap.static_connection:
return f"{ap.static_connection.od_mm} mm OD" return f"{ap.static_connection.od_mm} mm OD"
return "—" return "Manual"
@router.get("", response_model=list[AttachmentPointRead]) @router.get("", response_model=list[AttachmentPointRead])
@@ -54,10 +54,14 @@ async def create_attachment_point(
payload.clamp_connection_id, payload.clamp_connection_id,
payload.static_connection_id, payload.static_connection_id,
] ]
if sum(v is not None for v in conn_ids) != 1: if sum(v is not None for v in conn_ids) > 1:
raise HTTPException( raise HTTPException(
status_code=400, status_code=400,
detail="Exactly one connection (iso_thread, unc_thread, clamp_connection, or static_connection) must be provided.", detail=(
"At most one connection (iso_thread, unc_thread, clamp_connection, "
"or static_connection) may be provided. Leave all unset for a "
"manual attachment point."
),
) )
ap = await attachment_point_crud.create(db, **payload.model_dump()) ap = await attachment_point_crud.create(db, **payload.model_dump())
await audit_crud.log( await audit_crud.log(
+27 -2
View File
@@ -4,10 +4,13 @@ from sqlalchemy.ext.asyncio import AsyncSession
from app.core.database import get_db from app.core.database import get_db
from app.crud import audit as audit_crud from app.crud import audit as audit_crud
from app.crud import augmented_part as augmented_part_crud from app.crud import augmented_part as augmented_part_crud
from app.crud import augmented_part_note as augmented_part_note_crud
from app.deps import require_permission from app.deps import require_permission
from app.models.user import User from app.models.user import User
from app.schemas.augmented_part import ( from app.schemas.augmented_part import (
AugmentedPartCreate, AugmentedPartCreate,
AugmentedPartNoteCreate,
AugmentedPartNoteRead,
AugmentedPartRead, AugmentedPartRead,
AugmentedPartSyncResult, AugmentedPartSyncResult,
AugmentedPartUpdate, AugmentedPartUpdate,
@@ -21,10 +24,13 @@ router = APIRouter()
async def list_augmented_parts( async def list_augmented_parts(
skip: int = 0, skip: int = 0,
limit: int = 100, limit: int = 100,
include_archived: bool = False,
db: AsyncSession = Depends(get_db), db: AsyncSession = Depends(get_db),
_: User = Depends(require_permission("augmented_parts:read")), _: User = Depends(require_permission("augmented_parts:read")),
): ):
return await augmented_part_crud.list_all(db, skip=skip, limit=limit) return await augmented_part_crud.list_all(
db, skip=skip, limit=limit, include_archived=include_archived
)
@router.post("", response_model=AugmentedPartRead, status_code=status.HTTP_201_CREATED) @router.post("", response_model=AugmentedPartRead, status_code=status.HTTP_201_CREATED)
@@ -69,7 +75,7 @@ async def sync_augmented_parts(
resource_type="AugmentedPart", resource_type="AugmentedPart",
resource_id=None, resource_id=None,
description=( description=(
f"Sync: {result.created} created, {result.deleted} deleted, " f"Sync: {result.created} created, {result.archived} archived, "
f"{result.skipped} skipped (no type match)" f"{result.skipped} skipped (no type match)"
), ),
) )
@@ -132,3 +138,22 @@ async def delete_augmented_part(
"AugmentedPart", augmented_part_id, label "AugmentedPart", augmented_part_id, label
), ),
) )
@router.post(
"/{augmented_part_id}/notes",
response_model=AugmentedPartNoteRead,
status_code=status.HTTP_201_CREATED,
)
async def create_augmented_part_note(
augmented_part_id: int,
payload: AugmentedPartNoteCreate,
db: AsyncSession = Depends(get_db),
actor: User = Depends(require_permission("augmented_parts:write")),
):
ap = await augmented_part_crud.get(db, augmented_part_id)
if ap is None:
raise HTTPException(status_code=404, detail="Augmented part not found")
return await augmented_part_note_crud.create(
db, augmented_part=ap, author=actor, body=payload.body
)
+1 -3
View File
@@ -67,9 +67,7 @@ async def create_fit(
if ap_a is None or ap_b is None: if ap_a is None or ap_b is None:
raise HTTPException(status_code=404, detail="Attachment point not found") raise HTTPException(status_code=404, detail="Attachment point not found")
if ap_a.augmented_part_id == ap_b.augmented_part_id: if ap_a.augmented_part_id == ap_b.augmented_part_id:
raise HTTPException( raise HTTPException(status_code=422, detail="a part cannot fit itself")
status_code=422, detail="a part cannot fit itself"
)
existing = await fit_crud.get_by_attachment_points(db, ap_a, ap_b) existing = await fit_crud.get_by_attachment_points(db, ap_a, ap_b)
if existing is not None: if existing is not None:
raise HTTPException(status_code=409, detail="Fit already exists") raise HTTPException(status_code=409, detail="Fit already exists")
+16 -1
View File
@@ -13,6 +13,7 @@ from app.schemas.imported_part import (
ImportedPartRead, ImportedPartRead,
SyncResult, SyncResult,
) )
from app.services import augmented_part_sync
from app.services import odata as odata_service from app.services import odata as odata_service
router = APIRouter() router = APIRouter()
@@ -128,7 +129,9 @@ async def delete_imported_part(
raise HTTPException( raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND, detail="Imported part not found" status_code=status.HTTP_404_NOT_FOUND, detail="Imported part not found"
) )
await imported_part_crud.delete(db, part) archived = await augmented_part_sync.delete_imported_part_and_archive_orphans(
db, part
)
await audit_crud.log( await audit_crud.log(
db, db,
actor=actor, actor=actor,
@@ -137,3 +140,15 @@ async def delete_imported_part(
resource_id=None, resource_id=None,
description=audit_crud.describe_delete("ImportedPart", 0, inventory_id), description=audit_crud.describe_delete("ImportedPart", 0, inventory_id),
) )
if archived:
await audit_crud.log(
db,
actor=actor,
action="update",
resource_type="AugmentedPart",
resource_id=None,
description=(
f"Archived {archived} augmented part(s) left with no remaining "
f"imported parts after deleting {inventory_id!r}"
),
)
+5 -1
View File
@@ -33,9 +33,13 @@ class Settings(BaseSettings):
database_url: str = "sqlite+aiosqlite:///./app.db" database_url: str = "sqlite+aiosqlite:///./app.db"
auto_create_tables: bool = True 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_name: str = "access_token"
cookie_secure: bool = False cookie_secure: bool = False
session_timeout_hours: int = 24
# LDAP (direct-bind). When enabled, the login page offers an LDAP option. # 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 # At login the user's DN is built from ``ldap_bind_dn_template`` (with
+15
View File
@@ -5,6 +5,7 @@ Works with SQLite (aiosqlite) or Postgres (asyncpg) depending on DATABASE_URL.
from collections.abc import AsyncGenerator from collections.abc import AsyncGenerator
from sqlalchemy import event
from sqlalchemy.ext.asyncio import ( from sqlalchemy.ext.asyncio import (
AsyncSession, AsyncSession,
async_sessionmaker, async_sessionmaker,
@@ -21,6 +22,20 @@ class Base(DeclarativeBase):
engine = create_async_engine(settings.database_url, echo=settings.debug, future=True) engine = create_async_engine(settings.database_url, echo=settings.debug, future=True)
if engine.sync_engine.dialect.name == "sqlite":
@event.listens_for(engine.sync_engine, "connect")
def _set_sqlite_pragma(dbapi_connection, connection_record) -> None:
"""WAL mode lets readers/writers avoid blocking each other; busy_timeout
makes a connection wait for a lock instead of immediately raising
"database is locked" (the pysqlite/aiosqlite default is 0ms).
"""
cursor = dbapi_connection.cursor()
cursor.execute("PRAGMA journal_mode=WAL")
cursor.execute("PRAGMA busy_timeout=5000")
cursor.close()
AsyncSessionLocal = async_sessionmaker( AsyncSessionLocal = async_sessionmaker(
engine, engine,
class_=AsyncSession, class_=AsyncSession,
+4 -2
View File
@@ -47,10 +47,12 @@ def _create_token(
return token, payload 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( token, _ = _create_token(
subject, subject,
timedelta(minutes=settings.access_token_expire_minutes), expires_delta or timedelta(minutes=settings.access_token_expire_minutes),
ACCESS_TOKEN_TYPE, ACCESS_TOKEN_TYPE,
) )
return token return token
+19
View File
@@ -2,6 +2,7 @@ from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.ext.asyncio import AsyncSession
from app.models.attachment_point import AttachmentPoint from app.models.attachment_point import AttachmentPoint
from app.models.attachment_point_name import AttachmentPointName
_RELATIONSHIPS = [ _RELATIONSHIPS = [
"attachment_point_name", "attachment_point_name",
@@ -27,6 +28,24 @@ async def list_for_augmented_part(
return list(result.scalars().all()) return list(result.scalars().all())
async def list_by_augmented_parts_and_name(
db: AsyncSession, augmented_part_ids: list[int], name: str
) -> list[AttachmentPoint]:
"""AttachmentPoints named ``name`` for the given augmented parts, fetched in
one query."""
if not augmented_part_ids:
return []
result = await db.execute(
select(AttachmentPoint)
.join(AttachmentPoint.attachment_point_name)
.where(
AttachmentPoint.augmented_part_id.in_(augmented_part_ids),
AttachmentPointName.name == name,
)
)
return list(result.scalars().all())
async def list_all( async def list_all(
db: AsyncSession, skip: int = 0, limit: int = 100 db: AsyncSession, skip: int = 0, limit: int = 100
) -> list[AttachmentPoint]: ) -> list[AttachmentPoint]:
+82 -13
View File
@@ -1,12 +1,50 @@
from sqlalchemy import distinct, select from sqlalchemy import distinct, or_, select
from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.ext.asyncio import AsyncSession
from app.models.associations import augmented_part_imported_parts from app.models.associations import augmented_part_imported_parts
from app.models.augmented_part import AugmentedPart from app.models.augmented_part import AugmentedPart
from app.models.imported_part import ImportedPart from app.models.imported_part import ImportedPart
from app.models.manufacturer import Manufacturer
from app.models.part_colour import PartColour
from app.models.part_type import PartType
from app.models.scope_model import ScopeModel
from app.models.sub_part_type import SubPartType
_RELATIONSHIPS = ["colour", "manufacturer", "scope_model", "part_type", "sub_part_type"] _RELATIONSHIPS = ["colour", "manufacturer", "scope_model", "part_type", "sub_part_type"]
# Sortable columns for the admin list, keyed by the query-string "sort" value.
# Columns backed by a related table's name require an outer join (the FK may be
# nullable, e.g. scope_model_id) so unrelated rows still appear, ordered last.
SORT_COLUMNS = {
"id": (AugmentedPart.id, None),
"sku": (AugmentedPart.sku, None),
"description": (AugmentedPart.description, None),
"colour": (PartColour.name, (PartColour, AugmentedPart.colour_id == PartColour.id)),
"manufacturer": (
Manufacturer.name,
(Manufacturer, AugmentedPart.manufacturer_id == Manufacturer.id),
),
"scope_model": (
ScopeModel.name,
(ScopeModel, AugmentedPart.scope_model_id == ScopeModel.id),
),
"part_type": (PartType.name, (PartType, AugmentedPart.part_type_id == PartType.id)),
"sub_part_type": (
SubPartType.name,
(SubPartType, AugmentedPart.sub_part_type_id == SubPartType.id),
),
}
def _apply_sort(q, sort: str, direction: str):
column, join = SORT_COLUMNS.get(sort, SORT_COLUMNS["description"])
if join is not None:
table, cond = join
q = q.outerjoin(table, cond)
order = column.desc() if direction == "desc" else column.asc()
# AugmentedPart.id as a tiebreaker keeps paginated ordering stable.
return q.order_by(order, AugmentedPart.id)
async def get(db: AsyncSession, augmented_part_id: int) -> AugmentedPart | None: async def get(db: AsyncSession, augmented_part_id: int) -> AugmentedPart | None:
return await db.get(AugmentedPart, augmented_part_id) return await db.get(AugmentedPart, augmented_part_id)
@@ -20,14 +58,23 @@ async def get_by_sku(db: AsyncSession, sku: str) -> AugmentedPart | None:
async def list_all( async def list_all(
db: AsyncSession, skip: int = 0, limit: int = 100 db: AsyncSession,
skip: int = 0,
limit: int = 100,
include_archived: bool = False,
) -> list[AugmentedPart]: ) -> list[AugmentedPart]:
result = await db.execute( q = select(AugmentedPart).order_by(AugmentedPart.description)
select(AugmentedPart) if not include_archived:
.order_by(AugmentedPart.description) q = q.where(AugmentedPart.archived.is_(False))
.offset(skip) result = await db.execute(q.offset(skip).limit(limit))
.limit(limit) return list(result.scalars().all())
)
async def get_many(db: AsyncSession, ids: list[int]) -> list[AugmentedPart]:
"""Fetch augmented parts by id in one query (selectin relationships loaded)."""
if not ids:
return []
result = await db.execute(select(AugmentedPart).where(AugmentedPart.id.in_(ids)))
return list(result.scalars().all()) return list(result.scalars().all())
@@ -39,12 +86,18 @@ async def list_filtered(
scope_model_id: int | None = None, scope_model_id: int | None = None,
part_type_id: int | None = None, part_type_id: int | None = None,
sub_part_type_id: int | None = None, sub_part_type_id: int | None = None,
include_archived: bool = False,
sort: str = "description",
direction: str = "asc",
skip: int = 0, skip: int = 0,
limit: int = 100, limit: int = 100,
) -> list[AugmentedPart]: ) -> list[AugmentedPart]:
q = select(AugmentedPart) q = select(AugmentedPart)
if description.strip(): if description.strip():
q = q.where(AugmentedPart.description.ilike(f"%{description.strip()}%")) term = f"%{description.strip()}%"
q = q.where(
or_(AugmentedPart.description.ilike(term), AugmentedPart.sku.ilike(term))
)
if colour_id is not None: if colour_id is not None:
q = q.where(AugmentedPart.colour_id == colour_id) q = q.where(AugmentedPart.colour_id == colour_id)
if manufacturer_id is not None: if manufacturer_id is not None:
@@ -55,9 +108,10 @@ async def list_filtered(
q = q.where(AugmentedPart.part_type_id == part_type_id) q = q.where(AugmentedPart.part_type_id == part_type_id)
if sub_part_type_id is not None: if sub_part_type_id is not None:
q = q.where(AugmentedPart.sub_part_type_id == sub_part_type_id) q = q.where(AugmentedPart.sub_part_type_id == sub_part_type_id)
result = await db.execute( if not include_archived:
q.order_by(AugmentedPart.description).offset(skip).limit(limit) q = q.where(AugmentedPart.archived.is_(False))
) q = _apply_sort(q, sort, direction)
result = await db.execute(q.offset(skip).limit(limit))
return list(result.scalars().all()) return list(result.scalars().all())
@@ -69,12 +123,16 @@ async def count_filtered(
scope_model_id: int | None = None, scope_model_id: int | None = None,
part_type_id: int | None = None, part_type_id: int | None = None,
sub_part_type_id: int | None = None, sub_part_type_id: int | None = None,
include_archived: bool = False,
) -> int: ) -> int:
from sqlalchemy import func from sqlalchemy import func
q = select(func.count()).select_from(AugmentedPart) q = select(func.count()).select_from(AugmentedPart)
if description.strip(): if description.strip():
q = q.where(AugmentedPart.description.ilike(f"%{description.strip()}%")) term = f"%{description.strip()}%"
q = q.where(
or_(AugmentedPart.description.ilike(term), AugmentedPart.sku.ilike(term))
)
if colour_id is not None: if colour_id is not None:
q = q.where(AugmentedPart.colour_id == colour_id) q = q.where(AugmentedPart.colour_id == colour_id)
if manufacturer_id is not None: if manufacturer_id is not None:
@@ -85,6 +143,8 @@ async def count_filtered(
q = q.where(AugmentedPart.part_type_id == part_type_id) q = q.where(AugmentedPart.part_type_id == part_type_id)
if sub_part_type_id is not None: if sub_part_type_id is not None:
q = q.where(AugmentedPart.sub_part_type_id == sub_part_type_id) q = q.where(AugmentedPart.sub_part_type_id == sub_part_type_id)
if not include_archived:
q = q.where(AugmentedPart.archived.is_(False))
result = await db.execute(q) result = await db.execute(q)
return result.scalar_one() return result.scalar_one()
@@ -198,6 +258,15 @@ async def get_unlinked_imported_parts(db: AsyncSession) -> list[ImportedPart]:
return list(result.scalars().all()) return list(result.scalars().all())
async def set_archived(
db: AsyncSession, ap: AugmentedPart, archived: bool
) -> AugmentedPart:
ap.archived = archived
await db.commit()
await db.refresh(ap, attribute_names=_RELATIONSHIPS)
return ap
async def delete(db: AsyncSession, ap: AugmentedPart) -> None: async def delete(db: AsyncSession, ap: AugmentedPart) -> None:
await db.delete(ap) await db.delete(ap)
await db.commit() await db.commit()
+19
View File
@@ -0,0 +1,19 @@
from sqlalchemy.ext.asyncio import AsyncSession
from app.models.augmented_part import AugmentedPart, AugmentedPartNote
from app.models.user import User
async def create(
db: AsyncSession, *, augmented_part: AugmentedPart, author: User, body: str
) -> AugmentedPartNote:
note = AugmentedPartNote(
augmented_part_id=augmented_part.id,
user_id=author.id,
username=author.full_name or author.email,
body=body,
)
db.add(note)
await db.commit()
await db.refresh(note)
return note
+23
View File
@@ -52,6 +52,29 @@ async def list_for_part(db: AsyncSession, augmented_part_id: int) -> list[Fit]:
return list(result.scalars().all()) return list(result.scalars().all())
async def list_for_parts(
db: AsyncSession,
part_ids: list[int],
exclude_label: FitLabel | None = None,
) -> list[Fit]:
"""All fits touching any of the given augmented parts, in one query.
Pass ``exclude_label`` to drop, e.g., confirmed non-fits (``FitLabel.NONE``).
"""
if not part_ids:
return []
q = select(Fit).where(
or_(
Fit.augmented_part_a_id.in_(part_ids),
Fit.augmented_part_b_id.in_(part_ids),
)
)
if exclude_label is not None:
q = q.where(Fit.label != exclude_label)
result = await db.execute(q.order_by(Fit.id))
return list(result.scalars().all())
async def list_all( async def list_all(
db: AsyncSession, db: AsyncSession,
skip: int = 0, skip: int = 0,
+55 -14
View File
@@ -1,4 +1,4 @@
from sqlalchemy import distinct, insert, select, update from sqlalchemy import distinct, insert, or_, select, update
from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.ext.asyncio import AsyncSession
from app.models.imported_part import ImportedPart from app.models.imported_part import ImportedPart
@@ -16,14 +16,37 @@ _FIELDS = frozenset(
} }
) )
# Sortable columns for the admin list, keyed by the query-string "sort" value.
SORT_COLUMNS = {
"InventoryID": ImportedPart.InventoryID,
"Description": ImportedPart.Description,
"TBXColour": ImportedPart.TBXColour,
"ProductMaxThreadDepthmm": ImportedPart.ProductMaxThreadDepthmm,
"ProductMaxThreadDepthin": ImportedPart.ProductMaxThreadDepthin,
"ProductODmm": ImportedPart.ProductODmm,
"ProductODin": ImportedPart.ProductODin,
"TBXThreadAttribute": ImportedPart.TBXThreadAttribute,
}
def _apply_sort(q, sort: str, direction: str):
column = SORT_COLUMNS.get(sort, ImportedPart.InventoryID)
order = column.desc() if direction == "desc" else column.asc()
return q.order_by(order, ImportedPart.InventoryID)
async def get(db: AsyncSession, inventory_id: str) -> ImportedPart | None: async def get(db: AsyncSession, inventory_id: str) -> ImportedPart | None:
return await db.get(ImportedPart, inventory_id) return await db.get(ImportedPart, inventory_id)
async def list_all(db: AsyncSession, skip: int = 0, limit: int = 500) -> list[ImportedPart]: async def list_all(
db: AsyncSession, skip: int = 0, limit: int = 500
) -> list[ImportedPart]:
result = await db.execute( result = await db.execute(
select(ImportedPart).order_by(ImportedPart.InventoryID).offset(skip).limit(limit) select(ImportedPart)
.order_by(ImportedPart.InventoryID)
.offset(skip)
.limit(limit)
) )
return list(result.scalars().all()) return list(result.scalars().all())
@@ -31,13 +54,22 @@ async def list_all(db: AsyncSession, skip: int = 0, limit: int = 500) -> list[Im
async def list_filtered( async def list_filtered(
db: AsyncSession, db: AsyncSession,
description: str = "", description: str = "",
sort: str = "InventoryID",
direction: str = "asc",
skip: int = 0, skip: int = 0,
limit: int = 500, limit: int = 500,
) -> list[ImportedPart]: ) -> list[ImportedPart]:
q = select(ImportedPart) q = select(ImportedPart)
if description.strip(): if description.strip():
q = q.where(ImportedPart.Description.ilike(f"%{description.strip()}%")) term = f"%{description.strip()}%"
result = await db.execute(q.order_by(ImportedPart.InventoryID).offset(skip).limit(limit)) q = q.where(
or_(
ImportedPart.Description.ilike(term),
ImportedPart.InventoryID.ilike(term),
)
)
q = _apply_sort(q, sort, direction)
result = await db.execute(q.offset(skip).limit(limit))
return list(result.scalars().all()) return list(result.scalars().all())
@@ -62,23 +94,28 @@ async def count_filtered(db: AsyncSession, description: str = "") -> int:
q = select(func.count()).select_from(ImportedPart) q = select(func.count()).select_from(ImportedPart)
if description.strip(): if description.strip():
q = q.where(ImportedPart.Description.ilike(f"%{description.strip()}%")) term = f"%{description.strip()}%"
q = q.where(
or_(
ImportedPart.Description.ilike(term),
ImportedPart.InventoryID.ilike(term),
)
)
result = await db.execute(q) result = await db.execute(q)
return result.scalar_one() return result.scalar_one()
async def delete(db: AsyncSession, part: ImportedPart) -> None:
await db.delete(part)
await db.commit()
async def upsert_bulk(db: AsyncSession, records: list[dict]) -> tuple[int, int]: async def upsert_bulk(db: AsyncSession, records: list[dict]) -> tuple[int, int]:
"""Insert-or-update records keyed on InventoryID. Returns (created, updated).""" """Insert-or-update records keyed on InventoryID. Returns (created, updated)."""
if not records: if not records:
return 0, 0 return 0, 0
clean = [ clean = [
{k: str(v).strip() if v is not None else None for k, v in r.items() if k in _FIELDS} {
k: str(v).strip() if v is not None else None
for k, v in r.items()
if k in _FIELDS
}
for r in records for r in records
if r.get("InventoryID") if r.get("InventoryID")
] ]
@@ -88,7 +125,9 @@ async def upsert_bulk(db: AsyncSession, records: list[dict]) -> tuple[int, int]:
incoming_ids = {r["InventoryID"] for r in clean} incoming_ids = {r["InventoryID"] for r in clean}
result = await db.execute( result = await db.execute(
select(ImportedPart.InventoryID).where(ImportedPart.InventoryID.in_(incoming_ids)) select(ImportedPart.InventoryID).where(
ImportedPart.InventoryID.in_(incoming_ids)
)
) )
existing_ids = {row[0] for row in result.all()} existing_ids = {row[0] for row in result.all()}
@@ -103,7 +142,9 @@ async def upsert_bulk(db: AsyncSession, records: list[dict]) -> tuple[int, int]:
vals = {k: v for k, v in record.items() if k != "InventoryID"} vals = {k: v for k, v in record.items() if k != "InventoryID"}
if vals: if vals:
await db.execute( await db.execute(
update(ImportedPart).where(ImportedPart.InventoryID == inv_id).values(vals) update(ImportedPart)
.where(ImportedPart.InventoryID == inv_id)
.values(vals)
) )
await db.commit() await db.commit()
+1 -3
View File
@@ -23,9 +23,7 @@ async def get_default(db: AsyncSession) -> Role | None:
async def set_default(db: AsyncSession, role: Role) -> Role: async def set_default(db: AsyncSession, role: Role) -> Role:
"""Make ``role`` the sole default, clearing the flag on every other role.""" """Make ``role`` the sole default, clearing the flag on every other role."""
await db.execute( await db.execute(sa_update(Role).where(Role.id != role.id).values(is_default=False))
sa_update(Role).where(Role.id != role.id).values(is_default=False)
)
role.is_default = True role.is_default = True
await db.commit() await db.commit()
await db.refresh(role) await db.refresh(role)
+14 -2
View File
@@ -17,6 +17,18 @@ async def get_by_augmented_part(
return result.scalar_one_or_none() return result.scalar_one_or_none()
async def list_by_augmented_parts(
db: AsyncSession, augmented_part_ids: list[int]
) -> list[ScopeInfo]:
"""ScopeInfo rows for the given augmented parts, fetched in one query."""
if not augmented_part_ids:
return []
result = await db.execute(
select(ScopeInfo).where(ScopeInfo.augmented_part_id.in_(augmented_part_ids))
)
return list(result.scalars().all())
async def create( async def create(
db: AsyncSession, db: AsyncSession,
*, *,
@@ -24,14 +36,14 @@ 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_thread_pitch: str | None = None, objective_lens_diameter_mm: float | None = None,
) -> ScopeInfo: ) -> ScopeInfo:
info = ScopeInfo( info = ScopeInfo(
augmented_part_id=augmented_part_id, augmented_part_id=augmented_part_id,
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_thread_pitch=objective_thread_pitch, objective_lens_diameter_mm=objective_lens_diameter_mm,
) )
db.add(info) db.add(info)
await db.commit() await db.commit()
+12 -1
View File
@@ -1,5 +1,6 @@
from contextlib import asynccontextmanager from contextlib import asynccontextmanager
from pathlib import Path from pathlib import Path
from urllib.parse import quote
from fastapi import FastAPI, Request from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware from fastapi.middleware.cors import CORSMiddleware
@@ -12,6 +13,7 @@ from app.core.database import init_db
from app.deps import NotAuthenticated from app.deps import NotAuthenticated
from app.web.admin import router as admin_router from app.web.admin import router as admin_router
from app.web.pages import router as pages_router from app.web.pages import router as pages_router
from app.web.scopes import router as scopes_router
STATIC_DIR = Path(__file__).resolve().parent / "static" STATIC_DIR = Path(__file__).resolve().parent / "static"
@@ -39,11 +41,20 @@ def create_app() -> FastAPI:
app.include_router(api_router, prefix="/api") app.include_router(api_router, prefix="/api")
app.include_router(pages_router) app.include_router(pages_router)
app.include_router(scopes_router)
app.include_router(admin_router) app.include_router(admin_router)
@app.exception_handler(NotAuthenticated) @app.exception_handler(NotAuthenticated)
async def _redirect_to_login(request: Request, exc: NotAuthenticated): async def _redirect_to_login(request: Request, exc: NotAuthenticated):
return RedirectResponse(url="/login", status_code=302) # Preserve the originally requested URL so we can return there after
# login. Only meaningful for GET navigations (the ones users deep-link).
login_url = "/login"
if request.method == "GET" and request.url.path != "/login":
target = request.url.path
if request.url.query:
target += "?" + request.url.query
login_url = f"/login?next={quote(target, safe='')}"
return RedirectResponse(url=login_url, status_code=302)
@app.get("/healthz", tags=["health"]) @app.get("/healthz", tags=["health"])
async def healthz(): async def healthz():
+6 -3
View File
@@ -8,22 +8,23 @@ from app.models.associations import (
from app.models.attachment_point import AttachmentPoint from app.models.attachment_point import AttachmentPoint
from app.models.attachment_point_name import AttachmentPointName from app.models.attachment_point_name import AttachmentPointName
from app.models.audit_log import AuditLog from app.models.audit_log import AuditLog
from app.models.augmented_part import AugmentedPart from app.models.augmented_part import AugmentedPart, AugmentedPartNote
from app.models.clamp_connection import ClampConnection from app.models.clamp_connection import ClampConnection
from app.models.fit import Fit, FitLabel, FitNote from app.models.fit import Fit, FitLabel, FitNote
from app.models.imported_part import ImportedPart from app.models.imported_part import ImportedPart
from app.models.imported_part_config import ImportedPartConfig from app.models.imported_part_config import ImportedPartConfig
from app.models.iso_thread import IsoThread from app.models.iso_thread import IsoThread
from app.models.job import Job
from app.models.manufacturer import Manufacturer from app.models.manufacturer import Manufacturer
from app.models.part_colour import PartColour from app.models.part_colour import PartColour
from app.models.part_type import PartType from app.models.part_type import PartType
from app.models.permission import Permission from app.models.permission import Permission
from app.models.refresh_token import RefreshToken
from app.models.role import Role
from app.models.scope_info import ScopeInfo from app.models.scope_info import ScopeInfo
from app.models.scope_model import ScopeModel from app.models.scope_model import ScopeModel
from app.models.static_connection import StaticConnection from app.models.static_connection import StaticConnection
from app.models.sub_part_type import SubPartType from app.models.sub_part_type import SubPartType
from app.models.refresh_token import RefreshToken
from app.models.role import Role
from app.models.unc_thread import UncThread from app.models.unc_thread import UncThread
from app.models.user import User from app.models.user import User
@@ -32,6 +33,7 @@ __all__ = [
"AttachmentPointName", "AttachmentPointName",
"AuditLog", "AuditLog",
"AugmentedPart", "AugmentedPart",
"AugmentedPartNote",
"ClampConnection", "ClampConnection",
"Fit", "Fit",
"FitLabel", "FitLabel",
@@ -39,6 +41,7 @@ __all__ = [
"ImportedPart", "ImportedPart",
"ImportedPartConfig", "ImportedPartConfig",
"IsoThread", "IsoThread",
"Job",
"Manufacturer", "Manufacturer",
"PartColour", "PartColour",
"PartType", "PartType",
+4 -1
View File
@@ -21,7 +21,10 @@ class AttachmentPoint(Base):
ForeignKey("attachment_point_names.id", ondelete="RESTRICT"), nullable=False ForeignKey("attachment_point_names.id", ondelete="RESTRICT"), nullable=False
) )
# Exactly one of the four below must be non-null (enforced in application layer) # At most one of the four below is non-null (enforced in application layer).
# All four null means a "Manual" attachment point: no connection details,
# used to let a user assert a Fit by hand without the fit-matching algorithm
# (app.services.fit_matching) ever auto-matching it.
iso_thread_id: Mapped[int | None] = mapped_column( iso_thread_id: Mapped[int | None] = mapped_column(
ForeignKey("iso_threads.id", ondelete="RESTRICT"), nullable=True, default=None ForeignKey("iso_threads.id", ondelete="RESTRICT"), nullable=True, default=None
) )
+34 -1
View File
@@ -1,4 +1,6 @@
from sqlalchemy import ForeignKey, String from datetime import datetime
from sqlalchemy import Boolean, DateTime, ForeignKey, String, Text, func
from sqlalchemy.orm import Mapped, mapped_column, relationship from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.core.database import Base from app.core.database import Base
@@ -34,6 +36,10 @@ class AugmentedPart(Base):
nullable=True, nullable=True,
default=None, default=None,
) )
# Set instead of deleting the row when the last backing ImportedPart is
# removed, so fit/attachment-point history is preserved. Hidden from list
# views by default.
archived: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
# linked imported parts — not eagerly loaded; used by sync service only # linked imported parts — not eagerly loaded; used by sync service only
imported_parts: Mapped[list[ImportedPart]] = relationship( imported_parts: Mapped[list[ImportedPart]] = relationship(
@@ -45,3 +51,30 @@ class AugmentedPart(Base):
scope_model: Mapped[ScopeModel | None] = relationship(lazy="selectin") scope_model: Mapped[ScopeModel | None] = relationship(lazy="selectin")
part_type: Mapped[PartType] = relationship(lazy="selectin") part_type: Mapped[PartType] = relationship(lazy="selectin")
sub_part_type: Mapped[SubPartType | None] = relationship(lazy="selectin") sub_part_type: Mapped[SubPartType | None] = relationship(lazy="selectin")
notes: Mapped[list["AugmentedPartNote"]] = relationship(
back_populates="augmented_part",
lazy="selectin",
order_by="AugmentedPartNote.created_at",
cascade="all, delete-orphan",
)
class AugmentedPartNote(Base):
__tablename__ = "augmented_part_notes"
id: Mapped[int] = mapped_column(primary_key=True)
augmented_part_id: Mapped[int] = mapped_column(
ForeignKey("augmented_parts.id", ondelete="CASCADE"), nullable=False
)
user_id: Mapped[int | None] = mapped_column(
ForeignKey("users.id", ondelete="SET NULL"), nullable=True
)
# Denormalized snapshot of who wrote the note (mirrors FitNote.username), so
# the note stays attributable even if the user is later deleted.
username: Mapped[str] = mapped_column(String(255), nullable=False)
body: Mapped[str] = mapped_column(Text, nullable=False)
created_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), server_default=func.now()
)
augmented_part: Mapped[AugmentedPart] = relationship(back_populates="notes")
+1 -1
View File
@@ -7,7 +7,7 @@ from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.core.database import Base from app.core.database import Base
class FitLabel(str, enum.Enum): class FitLabel(enum.StrEnum):
POSSIBLE = "possible" POSSIBLE = "possible"
EXACT = "exact" EXACT = "exact"
NONE = "none" NONE = "none"
+31
View File
@@ -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
)
+1 -1
View File
@@ -8,5 +8,5 @@ class Manufacturer(Base):
id: Mapped[int] = mapped_column(primary_key=True) id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column(nullable=False) name: Mapped[str] = mapped_column(nullable=False)
url: Mapped[str] = mapped_column(nullable=False) url: Mapped[str] = mapped_column(nullable=True)
logo_url: Mapped[str | None] = mapped_column(nullable=True, default=None) logo_url: Mapped[str | None] = mapped_column(nullable=True, default=None)
+4 -4
View File
@@ -27,8 +27,8 @@ 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
) )
# Kept as the raw string from the sheet (e.g. "0.75", "1/16") so no precision # The lens diameter is a descriptive attribute (the "50" in "3-18x50"), not a
# or notation is lost; the parsed thread lives on its own AttachmentPoint. # connection dimension — it has no AttachmentPoint.
objective_thread_pitch: Mapped[str | None] = mapped_column( objective_lens_diameter_mm: Mapped[float | None] = mapped_column(
String, nullable=True, default=None Float, nullable=True, default=None
) )
+1 -1
View File
@@ -6,7 +6,7 @@ from sqlalchemy.orm import Mapped, mapped_column
from app.core.database import Base from app.core.database import Base
class UncSeries(str, enum.Enum): class UncSeries(enum.StrEnum):
UNC = "UNC" UNC = "UNC"
UNF = "UNF" UNF = "UNF"
UNEF = "UNEF" UNEF = "UNEF"
+29 -2
View File
@@ -1,4 +1,6 @@
from pydantic import BaseModel, ConfigDict from datetime import datetime
from pydantic import BaseModel, ConfigDict, field_validator
from app.schemas.manufacturer import ManufacturerRead from app.schemas.manufacturer import ManufacturerRead
from app.schemas.part_colour import PartColourRead from app.schemas.part_colour import PartColourRead
@@ -7,6 +9,28 @@ from app.schemas.scope_model import ScopeModelRead
from app.schemas.sub_part_type import SubPartTypeRead from app.schemas.sub_part_type import SubPartTypeRead
class AugmentedPartNoteCreate(BaseModel):
body: str
@field_validator("body")
@classmethod
def validate_body(cls, v: str) -> str:
if not v.strip():
raise ValueError("body must not be blank")
return v.strip()
class AugmentedPartNoteRead(BaseModel):
model_config = ConfigDict(from_attributes=True)
id: int
augmented_part_id: int
user_id: int | None
username: str
body: str
created_at: datetime
class AugmentedPartBase(BaseModel): class AugmentedPartBase(BaseModel):
sku: str | None = None sku: str | None = None
description: str description: str
@@ -29,11 +53,12 @@ class AugmentedPartUpdate(BaseModel):
scope_model_id: int | None = None scope_model_id: int | None = None
part_type_id: int | None = None part_type_id: int | None = None
sub_part_type_id: int | None = None sub_part_type_id: int | None = None
archived: bool | None = None
class AugmentedPartSyncResult(BaseModel): class AugmentedPartSyncResult(BaseModel):
created: int created: int
deleted: int archived: int
skipped: int skipped: int
@@ -41,8 +66,10 @@ class AugmentedPartRead(AugmentedPartBase):
model_config = ConfigDict(from_attributes=True) model_config = ConfigDict(from_attributes=True)
id: int id: int
archived: bool
colour: PartColourRead colour: PartColourRead
manufacturer: ManufacturerRead manufacturer: ManufacturerRead
scope_model: ScopeModelRead | None scope_model: ScopeModelRead | None
part_type: PartTypeRead part_type: PartTypeRead
sub_part_type: SubPartTypeRead | None sub_part_type: SubPartTypeRead | None
notes: list[AugmentedPartNoteRead] = []
+1 -1
View File
@@ -3,7 +3,7 @@ from pydantic import BaseModel, ConfigDict, HttpUrl
class ManufacturerBase(BaseModel): class ManufacturerBase(BaseModel):
name: str name: str
url: HttpUrl url: HttpUrl | None = None
logo_url: HttpUrl | None = None logo_url: HttpUrl | None = None
+1 -1
View File
@@ -5,7 +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_thread_pitch: str | None = None objective_lens_diameter_mm: float | None = None
class ScopeInfoRead(ScopeInfoBase): class ScopeInfoRead(ScopeInfoBase):
+154 -52
View File
@@ -2,11 +2,12 @@
Algorithm Algorithm
--------- ---------
Phase 1 – prune stale entries Phase 1 – archive stale entries
• Find association rows whose imported_part_id no longer exists. • Find association rows whose imported_part_id no longer exists.
• Delete those rows. • Delete those rows.
• Delete any AugmentedPart that ends up with zero remaining associations • Archive (rather than delete) any AugmentedPart that ends up with zero
(i.e. it was created purely from now-deleted ImportedParts). remaining associations (i.e. it was created purely from now-deleted
ImportedParts), preserving its fit/attachment-point history.
Phase 2 – create entries for new ImportedParts Phase 2 – create entries for new ImportedParts
• Any ImportedPart not yet linked to an AugmentedPart gets one created. • Any ImportedPart not yet linked to an AugmentedPart gets one created.
@@ -17,7 +18,7 @@ Phase 2 – create entries for new ImportedParts
• Type ← first PartType whose name is a substring of the description • Type ← first PartType whose name is a substring of the description
(longest name wins; skipped if no match) (longest name wins; skipped if no match)
• SubType ← null • SubType ← null
• Attachment point ← always named "Occular"; one of: • Attachment point ← always named "Ocular"; one of:
- ISO/UNC thread parsed from TBXThreadAttribute (using the - ISO/UNC thread parsed from TBXThreadAttribute (using the
matching ProductMaxThreadDepth mm/in if populated), or matching ProductMaxThreadDepth mm/in if populated), or
- StaticConnection from ProductODmm/ProductODin (whichever - StaticConnection from ProductODmm/ProductODin (whichever
@@ -33,6 +34,7 @@ from sqlalchemy.ext.asyncio import AsyncSession
from app.crud import attachment_point as attachment_point_crud from app.crud import attachment_point as attachment_point_crud
from app.crud import attachment_point_name as attachment_point_name_crud from app.crud import attachment_point_name as attachment_point_name_crud
from app.crud import clamp_connection as clamp_connection_crud
from app.crud import iso_thread as iso_thread_crud from app.crud import iso_thread as iso_thread_crud
from app.crud import manufacturer as manufacturer_crud from app.crud import manufacturer as manufacturer_crud
from app.crud import part_colour as part_colour_crud from app.crud import part_colour as part_colour_crud
@@ -46,14 +48,16 @@ from app.models.part_type import PartType
from app.models.unc_thread import UncSeries from app.models.unc_thread import UncSeries
from app.schemas.augmented_part import AugmentedPartSyncResult from app.schemas.augmented_part import AugmentedPartSyncResult
ATTACHMENT_POINT_NAME = "Occular" ATTACHMENT_POINT_NAME = "Ocular"
MM_PER_INCH = 25.4 MM_PER_INCH = 25.4
# e.g. # e.g.
# M8-1.25 - 8mm OD, 1.25 mm pitch # M8-1.25 - 8mm OD, 1.25 mm pitch
# M8 x 1.25 # M8 x 1.25
# M50x.75 - No leading digit on floats
# Float pattern: \d*\.?\d+
_ISO_THREAD_RE = re.compile( _ISO_THREAD_RE = re.compile(
r"^M\s*(?P<diameter>\d+(?:\.\d+)?)\s*[xX-]\s*(?P<pitch>\d+(?:\.\d+)?)(?P<extra>.*)$" r"M\s*(?P<diameter>\d*\.?\d+)\s*[xX-]\s*P?(?P<pitch>\d*\.?\d+)(?P<extra>.*)"
) )
# e.g. # e.g.
@@ -63,8 +67,13 @@ _ISO_THREAD_RE = re.compile(
# #
# Leading digit / period, one or more digits, a dash, then # Leading digit / period, one or more digits, a dash, then
_UNC_THREAD_RE = re.compile( _UNC_THREAD_RE = re.compile(
r"^(?P<diameter>\d+(?:\.\d+)?(?:/\d+)?)\s*-\s*(?P<tpi>\d+)" r"(?P<diameter>\d*\.?\d+)\s*-\s*(?P<tpi>\d*\.?\d+)"
r"\s*(?P<series>UNEF|UNC|UNF|UNS|UN)?$", r"\s*(?P<series>UNEF|UNC|UNF|UNS|UN)?",
re.IGNORECASE,
)
_CLAMP_RANGE_RE = re.compile(
r"\((?P<min_diameter>\d*\.?\d+)\s*-\s*(?P<max_diameter>\d*\.?\d+)\s*mm\)",
re.IGNORECASE, re.IGNORECASE,
) )
@@ -99,6 +108,13 @@ def _parse_thread_attribute(value: str) -> tuple[str, dict] | None:
"series": series, "series": series,
} }
if m := _CLAMP_RANGE_RE.match(value):
# Source data often omits the series letters; UNS ("unified special") is the
# correct designation when the diameter/TPI pairing isn't a standard pitch.
return "clamp", {
"min_diametere": float(m.group("min_diameter")),
"max_diameter": float(m.group("max_diameter")),
}
return None return None
@@ -109,7 +125,9 @@ async def _get_or_create_attachment_point_name(db: AsyncSession, name: str):
return apn return apn
async def _add_attachment_point(db: AsyncSession, ap: AugmentedPart, ip: ImportedPart) -> None: async def _add_attachment_point(
db: AsyncSession, ap: AugmentedPart, ip: ImportedPart
) -> None:
"""Infer and create an attachment point for a newly-synced AugmentedPart. """Infer and create an attachment point for a newly-synced AugmentedPart.
Preference order: TBXThreadAttribute (ISO/UNC thread) then ProductODmm/in Preference order: TBXThreadAttribute (ISO/UNC thread) then ProductODmm/in
@@ -117,7 +135,11 @@ async def _add_attachment_point(db: AsyncSession, ap: AugmentedPart, ip: Importe
attribute is present but unparseable. attribute is present but unparseable.
""" """
thread_value = (ip.TBXThreadAttribute or "").strip() thread_value = (ip.TBXThreadAttribute or "").strip()
parsed = _parse_thread_attribute(thread_value) if thread_value else None parsed = None
if thread_value:
parsed = _parse_thread_attribute(thread_value)
else:
parsed = _parse_thread_attribute(ip.Description or "")
if parsed is not None: if parsed is not None:
kind, fields = parsed kind, fields = parsed
@@ -125,7 +147,9 @@ async def _add_attachment_point(db: AsyncSession, ap: AugmentedPart, ip: Importe
if kind == "iso": if kind == "iso":
depth_mm = _parse_float(ip.ProductMaxThreadDepthmm) depth_mm = _parse_float(ip.ProductMaxThreadDepthmm)
thread = await iso_thread_crud.get_by_values(db, depth_mm=depth_mm, **fields) thread = await iso_thread_crud.get_by_values(
db, depth_mm=depth_mm, **fields
)
if thread is None: if thread is None:
thread = await iso_thread_crud.create(db, depth_mm=depth_mm, **fields) thread = await iso_thread_crud.create(db, depth_mm=depth_mm, **fields)
await attachment_point_crud.create( await attachment_point_crud.create(
@@ -134,9 +158,12 @@ async def _add_attachment_point(db: AsyncSession, ap: AugmentedPart, ip: Importe
attachment_point_name_id=apn.id, attachment_point_name_id=apn.id,
iso_thread_id=thread.id, iso_thread_id=thread.id,
) )
return
elif kind == "unc": elif kind == "unc":
depth_in = _parse_float(ip.ProductMaxThreadDepthin) depth_in = _parse_float(ip.ProductMaxThreadDepthin)
thread = await unc_thread_crud.get_by_values(db, depth_in=depth_in, **fields) thread = await unc_thread_crud.get_by_values(
db, depth_in=depth_in, **fields
)
if thread is None: if thread is None:
thread = await unc_thread_crud.create(db, depth_in=depth_in, **fields) thread = await unc_thread_crud.create(db, depth_in=depth_in, **fields)
await attachment_point_crud.create( await attachment_point_crud.create(
@@ -145,9 +172,21 @@ async def _add_attachment_point(db: AsyncSession, ap: AugmentedPart, ip: Importe
attachment_point_name_id=apn.id, attachment_point_name_id=apn.id,
unc_thread_id=thread.id, unc_thread_id=thread.id,
) )
else:
pass
return return
elif kind == "clamp":
clamp = await clamp_connection_crud.get_by_values(db, **fields)
if clamp is None:
clamp = await clamp_connection_crud.create(db, **fields)
await attachment_point_crud.create(
db,
augmented_part_id=ap.id,
attachment_point_name_id=apn.id,
clamp_connection_id=clamp.id,
)
return
else:
# Unable to determine
pass
od_mm = _parse_float(ip.ProductODmm) od_mm = _parse_float(ip.ProductODmm)
od_in = _parse_float(ip.ProductODin) od_in = _parse_float(ip.ProductODin)
@@ -230,39 +269,15 @@ def _infer_part_type(description: str, part_types: list[PartType]) -> PartType |
return None return None
async def sync(db: AsyncSession) -> AugmentedPartSyncResult: async def _archive_orphans(db: AsyncSession, candidate_ap_ids: set[int]) -> int:
# ------------------------------------------------------------------ phase 1 """Archive any of `candidate_ap_ids` left with zero linked ImportedParts.
# IDs referenced in existing associations Idempotent: already-archived parts and parts that still have a link are
result = await db.execute(select(augmented_part_imported_parts.c.imported_part_id).distinct()) left untouched. Does not commit — the caller commits as part of its own
all_linked_ids: set[str] = {row[0] for row in result.all()} transaction.
"""
# IDs that still exist in imported_parts archived = 0
result = await db.execute(select(ImportedPart.InventoryID)) for ap_id in candidate_ap_ids:
current_ids: set[str] = {row[0] for row in result.all()}
orphaned_import_ids = all_linked_ids - current_ids
deleted = 0
if orphaned_import_ids:
# Which AugmentedParts are affected?
result = await db.execute(
select(augmented_part_imported_parts.c.augmented_part_id).where(
augmented_part_imported_parts.c.imported_part_id.in_(orphaned_import_ids)
)
)
orphaned_ap_ids: set[int] = {row[0] for row in result.all()}
# Remove the stale association rows
await db.execute(
augmented_part_imported_parts.delete().where(
augmented_part_imported_parts.c.imported_part_id.in_(orphaned_import_ids)
)
)
await db.flush()
# Delete AugmentedParts that now have no remaining associations
for ap_id in orphaned_ap_ids:
result = await db.execute( result = await db.execute(
select(func.count()) select(func.count())
.select_from(augmented_part_imported_parts) .select_from(augmented_part_imported_parts)
@@ -270,16 +285,101 @@ async def sync(db: AsyncSession) -> AugmentedPartSyncResult:
) )
if result.scalar() == 0: if result.scalar() == 0:
ap = await db.get(AugmentedPart, ap_id) ap = await db.get(AugmentedPart, ap_id)
if ap is not None: if ap is not None and not ap.archived:
await db.delete(ap) ap.archived = True
deleted += 1 archived += 1
return archived
async def _detach_and_archive_orphans(
db: AsyncSession, imported_part_ids: set[str]
) -> int:
"""Detach `imported_part_ids` from any AugmentedPart association and archive
AugmentedParts left with zero remaining ImportedPart links as a result.
Deletes the association rows directly rather than relying on the
ImportedPart row's ON DELETE CASCADE, since SQLite (this app's dev/test
database) doesn't enforce FK actions by default. Safe to call whether or
not the ImportedPart row(s) still exist. Does not commit — the caller
commits as part of its own transaction.
"""
if not imported_part_ids:
return 0
result = await db.execute(
select(augmented_part_imported_parts.c.augmented_part_id).where(
augmented_part_imported_parts.c.imported_part_id.in_(imported_part_ids)
)
)
candidate_ap_ids: set[int] = {row[0] for row in result.all()}
await db.execute(
augmented_part_imported_parts.delete().where(
augmented_part_imported_parts.c.imported_part_id.in_(imported_part_ids)
)
)
await db.flush()
return await _archive_orphans(db, candidate_ap_ids)
async def delete_imported_part_and_archive_orphans(
db: AsyncSession, imported_part: ImportedPart
) -> int:
"""Delete `imported_part` and archive any AugmentedPart left with no
remaining ImportedPart links as a result.
Captures affected AugmentedPart ids before deleting so this is correct
regardless of whether the DB enforces the association table's ON DELETE
CASCADE. Returns the number of AugmentedParts newly archived.
"""
inventory_id = imported_part.InventoryID
result = await db.execute(
select(augmented_part_imported_parts.c.augmented_part_id).where(
augmented_part_imported_parts.c.imported_part_id == inventory_id
)
)
candidate_ap_ids: set[int] = {row[0] for row in result.all()}
await db.delete(imported_part)
await db.execute(
augmented_part_imported_parts.delete().where(
augmented_part_imported_parts.c.imported_part_id == inventory_id
)
)
await db.flush()
archived = await _archive_orphans(db, candidate_ap_ids)
await db.commit()
return archived
async def sync(db: AsyncSession) -> AugmentedPartSyncResult:
# ------------------------------------------------------------------ phase 1
# IDs referenced in existing associations
result = await db.execute(
select(augmented_part_imported_parts.c.imported_part_id).distinct()
)
all_linked_ids: set[str] = {row[0] for row in result.all()}
# IDs that still exist in imported_parts
result = await db.execute(select(ImportedPart.InventoryID))
current_ids: set[str] = {row[0] for row in result.all()}
orphaned_import_ids = all_linked_ids - current_ids
archived = 0
if orphaned_import_ids:
archived = await _detach_and_archive_orphans(db, orphaned_import_ids)
await db.commit() await db.commit()
# ------------------------------------------------------------------ phase 2 # ------------------------------------------------------------------ phase 2
# Re-query linked IDs after phase 1 cleanup # Re-query linked IDs after phase 1 cleanup
result = await db.execute(select(augmented_part_imported_parts.c.imported_part_id).distinct()) result = await db.execute(
select(augmented_part_imported_parts.c.imported_part_id).distinct()
)
now_linked_ids: set[str] = {row[0] for row in result.all()} now_linked_ids: set[str] = {row[0] for row in result.all()}
unlinked_ids = current_ids - now_linked_ids unlinked_ids = current_ids - now_linked_ids
@@ -287,7 +387,9 @@ async def sync(db: AsyncSession) -> AugmentedPartSyncResult:
skipped = 0 skipped = 0
if not unlinked_ids: if not unlinked_ids:
return AugmentedPartSyncResult(created=created, deleted=deleted, skipped=skipped) return AugmentedPartSyncResult(
created=created, archived=archived, skipped=skipped
)
result = await db.execute( result = await db.execute(
select(ImportedPart).where(ImportedPart.InventoryID.in_(unlinked_ids)) select(ImportedPart).where(ImportedPart.InventoryID.in_(unlinked_ids))
@@ -337,4 +439,4 @@ async def sync(db: AsyncSession) -> AugmentedPartSyncResult:
await db.commit() await db.commit()
return AugmentedPartSyncResult(created=created, deleted=deleted, skipped=skipped) return AugmentedPartSyncResult(created=created, archived=archived, skipped=skipped)
+12 -3
View File
@@ -1,5 +1,7 @@
"""Token issuance / rotation shared by the JSON API and the HTMX UI.""" """Token issuance / rotation shared by the JSON API and the HTMX UI."""
from datetime import timedelta
import jwt import jwt
from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.ext.asyncio import AsyncSession
@@ -13,9 +15,16 @@ from app.crud import token as token_crud
from app.models.user import User from app.models.user import User
async def issue_token_pair(db: AsyncSession, user: User) -> tuple[str, str]: async def issue_token_pair(
"""Create an access + refresh token and persist the refresh token for revocation.""" db: AsyncSession, user: User, access_expires_delta: timedelta | None = None
access = create_access_token(user.id) ) -> 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) refresh, payload = create_refresh_token(user.id)
await token_crud.store( await token_crud.store(
db, db,
+54
View File
@@ -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
+13 -7
View File
@@ -76,9 +76,15 @@ def _static_clamp_match(ap_a: AttachmentPoint, ap_b: AttachmentPoint) -> bool:
def attachment_points_match(ap_a: AttachmentPoint, ap_b: AttachmentPoint) -> bool: def attachment_points_match(ap_a: AttachmentPoint, ap_b: AttachmentPoint) -> bool:
"""True if these two attachment points satisfy the fit-matching algorithm.""" """True if these two attachment points satisfy the fit-matching algorithm.
if ap_a.attachment_point_name_id == ap_b.attachment_point_name_id:
return False A "Manual" attachment point (all four connections null) always returns
False here — it never participates in algorithmic matching, only in
fits a user asserts by hand.
"""
# Disabled for now, the attachment_point_names should not be considered for the purposes of matching
# if ap_a.attachment_point_name_id == ap_b.attachment_point_name_id:
# return False
return ( return (
_iso_match(ap_a, ap_b) _iso_match(ap_a, ap_b)
or _unc_match(ap_a, ap_b) or _unc_match(ap_a, ap_b)
@@ -142,9 +148,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 +175,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
+96
View File
@@ -0,0 +1,96 @@
"""DB-backed tracking for long-running admin operations.
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
import asyncio
import uuid
from collections.abc import Awaitable, Callable
from datetime import UTC, datetime, timedelta
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)
async def get(db: AsyncSession, job_id: str) -> Job | None:
return await db.get(Job, job_id)
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.
``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.
"""
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.id)
except Exception as exc:
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()
+1 -2
View File
@@ -7,7 +7,6 @@ successful profile is provisioned locally on first sign-in.
""" """
import logging import logging
from dataclasses import dataclass from dataclasses import dataclass
from fastapi.concurrency import run_in_threadpool from fastapi.concurrency import run_in_threadpool
@@ -43,7 +42,7 @@ def _bind_and_fetch(username: str, password: str) -> LdapIdentity | None:
) )
try: try:
conn = ldap3.Connection(server, user=user_dn, password=password, auto_bind=True) conn = ldap3.Connection(server, user=user_dn, password=password, auto_bind=True)
except LDAPException as e: except LDAPException:
logging.exception("Failed to log into LDAP for user %s", user_dn) logging.exception("Failed to log into LDAP for user %s", user_dn)
# Wrong credentials, unreachable server, or a refused bind. # Wrong credentials, unreachable server, or a refused bind.
return None return None
+160
View File
@@ -0,0 +1,160 @@
"""Assemble the scope-centric fit guide.
Builds the data for the top-level ``/scopes`` page: one row per augmented part
of part type "Scope", one column per non-Scope part type, where each cell lists
the parts of that type that fit the row's scope.
"""
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 fit as fit_crud
from app.crud import part_type as part_type_crud
from app.crud import scope_info as scope_info_crud
from app.models.fit import FitLabel
SCOPE_TYPE_NAME = "Scope"
OBJECTIVE_ATTACHMENT_POINT_NAME = "Objective"
# "exact" outranks "possible" when the same part fits a scope via several fits.
_LABEL_RANK = {FitLabel.EXACT.value: 2, FitLabel.POSSIBLE.value: 1}
async def build(
db: AsyncSession,
*,
description: str = "",
manufacturer_id: int | None = None,
scope_model_id: int | None = None,
sub_part_type_id: int | None = None,
fits_part_type_id: int | None = None,
only_exact: bool = False,
) -> dict:
"""Return the context for ``scopes.html``.
The scope rows are narrowed by the scope-attribute filters (``description``
matches either the scope's description or its SKU/product ID; the ``*_id``
filters match exactly). ``fits_part_type_id`` keeps only scopes that have
at least one (non-``none``)
fit for that part type. Callers that paginate should slice ``rows``
themselves — this always returns the full filtered set, since
``fits_part_type_id`` is a post-filter that can't be pushed into the DB query.
``only_exact`` drops ``possible`` fits entirely, keeping only user-confirmed
``exact`` ones — used for anonymous visitors, who shouldn't see unconfirmed
fit candidates.
Shape::
{
"column_types": [PartType, ...], # non-Scope, ordered by name
"rows": [
{
"scope": AugmentedPart,
"info": ScopeInfo | None,
"objective_attachment_point": AttachmentPoint | None,
"cells": {part_type_id: [{"part": AugmentedPart, "label": str}]},
},
...,
],
}
"""
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
]
if scope_type is None:
return {"column_types": column_types, "rows": []}
scopes = await augmented_part_crud.list_filtered(
db,
description=description,
manufacturer_id=manufacturer_id,
scope_model_id=scope_model_id,
sub_part_type_id=sub_part_type_id,
part_type_id=scope_type.id,
limit=1000,
)
scope_ids = {s.id for s in scopes}
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
)
if only_exact:
fits = [f for f in fits if f.label == FitLabel.EXACT]
# Resolve the "other" (non-scope) part on each fit in a single batched query.
other_ids: set[int] = set()
for fit in fits:
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))
}
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))
}
# 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}
for fit in fits:
scope_id, other_id = _scope_and_other(fit, scope_ids)
if scope_id is None:
continue
other = other_parts.get(other_id)
if other is None or other.part_type_id == scope_type.id:
continue # scope-to-scope fits have no column
if other.archived:
continue # archived parts don't show up as compatible accessories
by_type = cell_map[scope_id].setdefault(other.part_type_id, {})
existing = by_type.get(other.id)
label = fit.label.value
if existing is None or _LABEL_RANK[label] > _LABEL_RANK[existing["label"]]:
by_type[other.id] = {"part": other, "label": label}
rows = []
for scope in scopes:
cells = {
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:
continue # scope has no fit for the required accessory type
rows.append(
{
"scope": scope,
"info": infos.get(scope.id),
"objective_attachment_point": objective_aps.get(scope.id),
"cells": cells,
}
)
return {"column_types": column_types, "rows": rows}
def _scope_and_other(fit, scope_ids: set[int]) -> tuple[int | None, int]:
"""Return (scope_part_id, other_part_id) for a fit, or (None, _) if neither
side is a scope. When both sides are scopes, side A is treated as the scope."""
if fit.augmented_part_a_id in scope_ids:
return fit.augmented_part_a_id, fit.augmented_part_b_id
if fit.augmented_part_b_id in scope_ids:
return fit.augmented_part_b_id, fit.augmented_part_a_id
return None, fit.augmented_part_b_id
+130 -58
View File
@@ -2,7 +2,7 @@
The spreadsheet describes parts of PartType "Scope". Each row maps onto an The spreadsheet describes parts of PartType "Scope". Each row maps onto an
AugmentedPart (keyed on ``product_number`` → ``sku``, so re-importing upserts), AugmentedPart (keyed on ``product_number`` → ``sku``, so re-importing upserts),
a ScopeModel, a ScopeInfo record for the scope-specific fields, up to three a ScopeModel, a ScopeInfo record for the scope-specific fields, up to two
attachment points, and up to two Fits against other parts. attachment points, and up to two Fits against other parts.
Column → destination Column → destination
@@ -14,9 +14,8 @@ 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_thread_pitch → ScopeInfo objective_lens_diameter_mm → ScopeInfo (descriptive only, no attachment point)
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
@@ -26,8 +25,15 @@ and colour defaults to an "Unknown" PartColour, since the sheet carries neither.
Fits are created with label POSSIBLE. The referenced part is looked up by Fits are created with label POSSIBLE. The referenced part is looked up by
``product_number`` and linked to its first attachment point (chosen answer: ``product_number`` and linked to its first attachment point (chosen answer:
"sole/first AP"). Both directions are resolved in a second pass so a fit between "sole/first AP"). If the referenced part has no attachment points at all, one
two scopes in the same spreadsheet resolves regardless of row order. is created for it that mirrors the scope's attachment point's connection, so
the fit has something valid to attach to. If the scope itself has no Ocular/
Objective attachment point (no ocular_outer_diameter_mm/objective_thread, or
an unparseable one), a Manual attachment point — no connection details, never
picked up by the fit-matching algorithm — is created on the scope under the
appropriate label so the fit still has somewhere to attach. Both directions
are resolved in a second pass so a fit between two scopes in the same
spreadsheet resolves regardless of row order.
Bad rows (missing key, unknown fit target, unparseable thread) are skipped and Bad rows (missing key, unknown fit target, unparseable thread) are skipped and
reported per-row; good rows are committed as they go. reported per-row; good rows are committed as they go.
@@ -68,14 +74,13 @@ 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",
"url", "url",
"ocular_outer_diameter_mm", "ocular_outer_diameter_mm",
"ocular_fit", "ocular_fit",
"objective_thread_pitch",
"objective_thread", "objective_thread",
"objective_fit", "objective_fit",
] ]
@@ -140,15 +145,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 +161,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:
@@ -194,6 +193,54 @@ async def _thread_ap(
) )
async def _mirrored_ap(db: AsyncSession, *, augmented_part_id: int, scope_ap):
"""Create an attachment point mirroring ``scope_ap``'s connection.
Used when the referenced part has no attachment point that already fits
the scope, so the explicit ocular_fit/objective_fit link has something
valid to attach to.
"""
return await attachment_point_crud.create(
db,
augmented_part_id=augmented_part_id,
attachment_point_name_id=scope_ap.attachment_point_name_id,
iso_thread_id=scope_ap.iso_thread_id,
unc_thread_id=scope_ap.unc_thread_id,
clamp_connection_id=scope_ap.clamp_connection_id,
static_connection_id=scope_ap.static_connection_id,
)
async def _ensure_manual_scope_ap(
db: AsyncSession,
*,
scope_ap,
augmented_part_id: int,
label: str,
messages: list[str],
):
"""Return ``scope_ap``, creating a Manual attachment point named ``label``
on the scope if it's None.
Used so an ocular_fit/objective_fit reference still has something to link
from even when the scope has no ocular_outer_diameter_mm/objective_thread
of its own.
"""
if scope_ap is not None:
return scope_ap
apn = await apn_crud.get_by_name(db, label)
if apn is None:
apn = await apn_crud.create(db, name=label)
messages.append(
f"scope has no {label.lower()} attachment point; created a manual one to record the fit"
)
return await attachment_point_crud.create(
db,
augmented_part_id=augmented_part_id,
attachment_point_name_id=apn.id,
)
async def _link_fit( async def _link_fit(
db: AsyncSession, db: AsyncSession,
*, *,
@@ -203,13 +250,13 @@ async def _link_fit(
scope_part_id: int, scope_part_id: int,
messages: list[str], messages: list[str],
) -> None: ) -> None:
"""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:
messages.append( ``scope_ap`` must already exist — callers ensure this via
f"{scope_role}_fit '{target_sku}' skipped: scope has no " ``_ensure_manual_scope_ap`` before calling. If the referenced part has no
f"{scope_role} attachment point" attachment points at all, one is created mirroring the scope's attachment
) point's connection.
return """
target = await augmented_part_crud.get_by_sku(db, target_sku) target = await augmented_part_crud.get_by_sku(db, target_sku)
if target is None: if target is None:
messages.append(f"{scope_role}_fit references unknown product '{target_sku}'") messages.append(f"{scope_role}_fit references unknown product '{target_sku}'")
@@ -218,18 +265,17 @@ async def _link_fit(
messages.append(f"{scope_role}_fit '{target_sku}' skipped: self-reference") messages.append(f"{scope_role}_fit '{target_sku}' skipped: self-reference")
return return
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 target_aps:
messages.append(
f"{scope_role}_fit '{target_sku}' skipped: referenced part has no "
"attachment points"
)
return
target_ap = target_aps[0] target_ap = target_aps[0]
else:
target_ap = await _mirrored_ap(db, augmented_part_id=target.id, scope_ap=scope_ap)
messages.append(
f"{scope_role}_fit '{target_sku}': referenced part had no attachment points, "
"created one matching the scope's attachment point"
)
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 +296,23 @@ 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"))
model = _text(row.get("model"))
magnification_min = _num(row.get("magnification_min"))
magnification_max = _num(row.get("magnification_max"))
objective_lens_diameter_mm = _num(row.get("objective_lens_diameter_mm")) or _num(
row.get("objective_lens")
)
# Construct the model number
default_product_number = model
if magnification_min:
default_product_number += f" {magnification_min}-{magnification_max}"
if objective_lens_diameter_mm:
default_product_number += f"x{objective_lens_diameter_mm}"
if model_suffix:
default_product_number += f" {model_suffix}"
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 +333,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 +390,12 @@ 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_thread_pitch=_text(row.get("objective_thread_pitch")), objective_lens_diameter_mm=_num(row.get("objective_lens_diameter_mm")),
) )
# 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 +408,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
@@ -391,6 +437,19 @@ async def import_scopes(db: AsyncSession, rows: list[dict]) -> ScopeImportResult
try: try:
ocular_fit = _text(row.get("ocular_fit")) ocular_fit = _text(row.get("ocular_fit"))
if ocular_fit: if ocular_fit:
ocular_ap = await _ensure_manual_scope_ap(
db,
scope_ap=ocular_ap,
augmented_part_id=part_id,
label=OCULAR_LABEL,
messages=result.messages,
)
fits = ocular_fit
if "/" in fits:
fits = [x.strip() for x in fits.split("/")]
else:
fits = [fits]
for ocular_fit in fits:
await _link_fit( await _link_fit(
db, db,
scope_ap=ocular_ap, scope_ap=ocular_ap,
@@ -401,6 +460,19 @@ async def import_scopes(db: AsyncSession, rows: list[dict]) -> ScopeImportResult
) )
objective_fit = _text(row.get("objective_fit")) objective_fit = _text(row.get("objective_fit"))
if objective_fit: if objective_fit:
objective_ap = await _ensure_manual_scope_ap(
db,
scope_ap=objective_ap,
augmented_part_id=part_id,
label=OBJECTIVE_LABEL,
messages=result.messages,
)
fits = objective_fit
if "/" in fits:
fits = [x.strip() for x in fits.split("/")]
else:
fits = [fits]
for objective_fit in fits:
await _link_fit( await _link_fit(
db, db,
scope_ap=objective_ap, scope_ap=objective_ap,
+153 -33
View File
@@ -247,6 +247,37 @@ h2 {
gap: 0.4rem; gap: 0.4rem;
} }
/* Sortable table headers */
table.sortable thead th:not([data-sort-off]) {
cursor: pointer;
user-select: none;
}
table.sortable thead th:not([data-sort-off]):hover {
color: var(--text);
}
table.sortable thead th[aria-sort]::after {
margin-left: 0.3rem;
}
table.sortable thead th[aria-sort="ascending"]::after {
content: "\25B2";
}
table.sortable thead th[aria-sort="descending"]::after {
content: "\25BC";
}
.sort-link {
color: inherit;
text-decoration: none;
}
.sort-link:hover {
text-decoration: underline;
}
/* Tags / definition lists */ /* Tags / definition lists */
.tag { .tag {
display: inline-block; display: inline-block;
@@ -455,50 +486,125 @@ textarea:focus {
margin-bottom: 0.5rem; margin-bottom: 0.5rem;
} }
.ap-item-name { font-weight: 500; flex: 0 0 auto; } .ap-item-name {
.ap-item-type { color: var(--muted); flex: 0 0 auto; font-size: 0.8rem; } font-weight: 500;
.ap-item-conn { flex: 1; } flex: 0 0 auto;
.ap-item .row-actions { margin-left: auto; }
.fit-item {
padding: 0.5rem 0;
border-bottom: 1px solid var(--border);
font-size: 0.9rem;
} }
.fit-item:last-of-type { border-bottom: none; } .ap-item-type {
color: var(--muted);
flex: 0 0 auto;
font-size: 0.8rem;
}
.fit-item-main { .ap-item-conn {
flex: 1;
}
.ap-item .row-actions {
margin-left: auto;
}
.fit-item-arrow {
color: var(--muted);
text-align: center;
}
.fit-badge {
text-transform: uppercase;
}
/* Fits table on the augmented part detail page */
.fit-table select,
.fit-table input[type="text"] {
min-width: 8rem;
}
.fit-table .fit-add-row {
background: var(--surface-2);
}
.fit-table .fit-add-row td {
vertical-align: top;
}
.fit-notes-cell {
min-width: 14rem;
}
.fit-badge-possible {
background: var(--surface-2);
color: var(--muted);
}
.fit-badge-exact {
background: #16a34a26;
color: #16a34a;
}
.fit-badge-none {
background: #dc262626;
color: #dc2626;
}
.fit-badge-possible a {
background: var(--surface-2);
color: var(--muted);
}
.fit-badge-exact a {
background: #16a34a26;
color: #16a34a;
}
.fit-badge-none a {
background: #dc262626;
color: #dc2626;
}
.fit-notes {
margin-top: 0.4rem;
padding-left: 0.25rem;
}
/* Scope guide: stacked matching-part entries within a fit column cell. */
.fit-cell-item {
display: flex; display: flex;
align-items: center; align-items: center;
gap: 0.5rem; gap: 0.4rem;
flex-wrap: wrap; font-size: 0.85rem;
white-space: nowrap;
}
.fit-cell-item+.fit-cell-item {
margin-top: 0.3rem;
}
.fit-note {
font-size: 0.85rem;
margin-bottom: 0.3rem; margin-bottom: 0.3rem;
} }
.fit-item-arrow { color: var(--muted); } .fit-note-meta {
color: var(--muted);
.fit-badge { font-size: 0.75rem;
font-size: 0.7rem;
text-transform: uppercase;
letter-spacing: 0.03em;
padding: 0.1rem 0.5rem;
border-radius: 999px;
font-weight: 600;
} }
.fit-badge-possible { background: var(--surface-2); color: var(--muted); } .fit-note-body {
.fit-badge-exact { background: #16a34a26; color: #16a34a; } margin: 0.1rem 0 0;
.fit-badge-none { background: #dc262626; color: #dc2626; } }
.fit-notes { margin-top: 0.4rem; padding-left: 0.25rem; } .fit-note-form {
margin-top: 0.3rem;
}
.fit-note { font-size: 0.85rem; margin-bottom: 0.3rem; } .fit-note-form input[type="text"] {
.fit-note-meta { color: var(--muted); font-size: 0.75rem; } flex: 1;
.fit-note-body { margin: 0.1rem 0 0; } min-width: 12rem;
}
.fit-note-form { margin-top: 0.3rem; }
.fit-note-form input[type="text"] { flex: 1; min-width: 12rem; }
/* Pagination */ /* Pagination */
.pagination { .pagination {
@@ -526,6 +632,18 @@ textarea:focus {
margin-bottom: 0.75rem; margin-bottom: 0.75rem;
} }
/* Bulk actions (e.g. delete-selected above a checkbox table) */
.bulk-actions {
margin-bottom: 0.75rem;
}
.dashboard-sync-actions {
display: flex;
flex-wrap: wrap;
gap: 0.5rem;
margin-top: 0.75rem;
}
.filter-bar form { .filter-bar form {
display: flex; display: flex;
flex-wrap: wrap; flex-wrap: wrap;
@@ -624,7 +742,9 @@ textarea:focus {
gap: 0.4rem; gap: 0.4rem;
} }
.local-login-panel summary::-webkit-details-marker { display: none; } .local-login-panel summary::-webkit-details-marker {
display: none;
}
.local-login-panel summary::before { .local-login-panel summary::before {
content: '›'; content: '›';
Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB

+77
View File
@@ -0,0 +1,77 @@
(function () {
"use strict";
function getCellText(row, index) {
var cell = row.children[index];
return cell ? cell.textContent.trim() : "";
}
function isEmptyValue(text) {
return text === "" || text === "—";
}
function isPureNumber(text) {
return /^-?\d+(\.\d+)?$/.test(text);
}
function columnIsNumeric(rows, index) {
var sawValue = false;
for (var i = 0; i < rows.length; i++) {
var text = getCellText(rows[i], index);
if (isEmptyValue(text)) continue;
sawValue = true;
if (!isPureNumber(text)) return false;
}
return sawValue;
}
function compareRows(a, b, index, numeric, dir) {
var textA = getCellText(a, index);
var textB = getCellText(b, index);
var emptyA = isEmptyValue(textA);
var emptyB = isEmptyValue(textB);
if (emptyA && emptyB) return 0;
if (emptyA) return 1;
if (emptyB) return -1;
var result = numeric
? parseFloat(textA) - parseFloat(textB)
: textA.localeCompare(textB, undefined, { numeric: true, sensitivity: "base" });
return dir === "desc" ? -result : result;
}
function sortTable(table, th) {
var thead = table.tHead;
var tbody = table.tBodies[0];
if (!thead || !tbody) return;
var headerRow = thead.rows[0];
var index = Array.prototype.indexOf.call(headerRow.children, th);
if (index === -1) return;
var rows = Array.prototype.slice.call(tbody.rows);
if (rows.length < 2) return;
var nextDir = th.getAttribute("data-sort-dir") === "asc" ? "desc" : "asc";
Array.prototype.forEach.call(headerRow.children, function (cell) {
cell.removeAttribute("data-sort-dir");
cell.removeAttribute("aria-sort");
});
th.setAttribute("data-sort-dir", nextDir);
th.setAttribute("aria-sort", nextDir === "asc" ? "ascending" : "descending");
var numeric = columnIsNumeric(rows, index);
rows.sort(function (a, b) {
return compareRows(a, b, index, numeric, nextDir);
});
rows.forEach(function (row) {
tbody.appendChild(row);
});
}
document.addEventListener("click", function (event) {
var th = event.target.closest("table.sortable thead th");
if (!th || th.hasAttribute("data-sort-off") || th.textContent.trim() === "") return;
sortTable(th.closest("table"), th);
});
})();
+17
View File
@@ -0,0 +1,17 @@
{%- macro connection_spec(ap) -%}
{%- if ap.iso_thread -%}
M{{ ap.iso_thread.diameter_mm }}×{{ ap.iso_thread.pitch_mm }}
{%- elif ap.unc_thread -%}
{{ ap.unc_thread.diameter_fraction }}-{{ ap.unc_thread.tpi }} {{ ap.unc_thread.series.value }}
{%- elif ap.clamp_connection -%}
{%- if ap.clamp_connection.min_mm is not none -%}
{{ ap.clamp_connection.min_mm }}–{{ ap.clamp_connection.max_mm }} mm
{%- else -%}
{{ ap.clamp_connection.min_in }}–{{ ap.clamp_connection.max_in }} in
{%- endif -%}
{%- elif ap.static_connection -%}
{{ ap.static_connection.od_mm }} mm OD
{%- else -%}
—
{%- endif -%}
{%- endmacro -%}
+32
View File
@@ -0,0 +1,32 @@
<div id="dashboard-sync-card" class="card">
<h2>Data sync</h2>
<dl class="kv">
<dt>Scopes</dt>
<dd>{{ scope_count }}</dd>
<dt>Imported parts skipped</dt>
<dd>{% if can_sync_augmented_parts %}<a href="/admin/augmented-parts/skipped">{{ skipped_count }}</a>{% else %}{{ skipped_count }}{% endif %}</dd>
</dl>
{% if status_message %}<p class="muted">✅ {{ status_message }}</p>{% endif %}
{% if status_error %}<p class="muted">⚠️ {{ status_error }}</p>{% endif %}
{% if can_refresh_odata or can_sync_augmented_parts or can_recompute_fits %}
<div class="dashboard-sync-actions">
{% if can_refresh_odata %}
<form hx-post="/dashboard/refresh-odata" hx-target="#dashboard-sync-card" hx-swap="outerHTML">
<button type="submit" class="btn btn-primary">Refresh from OData</button>
</form>
{% endif %}
{% if can_sync_augmented_parts %}
<form hx-post="/dashboard/sync-augmented-parts" hx-target="#dashboard-sync-card" hx-swap="outerHTML">
<button type="submit" class="btn btn-primary">Sync augmented parts</button>
</form>
{% endif %}
{% if can_recompute_fits %}
<form hx-post="/dashboard/recompute-fits" hx-target="#dashboard-sync-card" hx-swap="outerHTML">
<button type="submit" class="btn btn-primary">Recompute possible fits</button>
</form>
{% endif %}
</div>
{% endif %}
</div>
+15 -2
View File
@@ -1,10 +1,12 @@
{% set su = user.is_superuser %} {% set su = user.is_superuser if user else False %}
{% set p = user.permissions %} {% set p = user.permissions if user else [] %}
{% set can_admin = su or "users:read" in p or "roles:read" in p or "audit_log:read" in p %} {% set can_admin = su or "users:read" in p or "roles:read" in p or "audit_log:read" in p %}
{% set can_connections = su or "clamp_connections:read" in p or "iso_threads:read" in p or "static_connections:read" in p or "unc_threads:read" in p %} {% set can_connections = su or "clamp_connections:read" in p or "iso_threads:read" in p or "static_connections:read" in p or "unc_threads:read" in p %}
{% set can_labels = su or "attachment_point_names:read" in p or "manufacturers:read" in p or "part_colours:read" in p or "part_types:read" in p or "scope_models:read" in p or "sub_part_types:read" in p %} {% set can_labels = su or "attachment_point_names:read" in p or "manufacturers:read" in p or "part_colours:read" in p or "part_types:read" in p or "scope_models:read" in p or "sub_part_types:read" in p %}
{% set can_database = su or "imported_parts:read" in p or "augmented_parts:read" in p or "attachment_points:read" in p %} {% set can_database = su or "imported_parts:read" in p or "augmented_parts:read" in p or "attachment_points:read" in p %}
{% set can_parts = can_connections or can_labels or can_database %} {% set can_parts = can_connections or can_labels or can_database %}
{# /scopes is public, so anonymous visitors see the link too; logged-in users still need the read perms. #}
{% set can_scopes = (not user) or su or ("augmented_parts:read" in p and "fits:read" in p) %}
{# ── SVG icon sprite ───────────────────────────────────────────────────────── #} {# ── SVG icon sprite ───────────────────────────────────────────────────────── #}
<svg xmlns="http://www.w3.org/2000/svg" style="display:none"> <svg xmlns="http://www.w3.org/2000/svg" style="display:none">
@@ -127,6 +129,10 @@
<nav> <nav>
<a href="/">{{ icon("home") }} Dashboard</a> <a href="/">{{ icon("home") }} Dashboard</a>
{% if can_scopes %}
<a href="/scopes">{{ icon("crosshair") }} Scopes</a>
{% endif %}
{% if can_admin %} {% if can_admin %}
<div class="nav-group"> <div class="nav-group">
<span class="nav-label">{{ icon("shield") }} Admin ▾</span> <span class="nav-label">{{ icon("shield") }} Admin ▾</span>
@@ -140,6 +146,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 %}
@@ -224,8 +233,12 @@
<a href="/docs" target="_blank">{{ icon("book-open") }} API docs</a> <a href="/docs" target="_blank">{{ icon("book-open") }} API docs</a>
</nav> </nav>
{% if user %}
<form method="post" action="/logout" class="logout"> <form method="post" action="/logout" class="logout">
<span class="muted">{{ user.email }}</span> <span class="muted">{{ user.email }}</span>
<button type="submit" class="btn btn-ghost">Sign out</button> <button type="submit" class="btn btn-ghost">Sign out</button>
</form> </form>
{% else %}
<a href="/login" class="btn btn-ghost">Sign in</a>
{% endif %}
</header> </header>
+82
View File
@@ -0,0 +1,82 @@
{% 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 -%}
{{ mag(info.magnification_min) }}-{{ mag(info.magnification_max) }}
{%- elif info and info.magnification_min is not none -%}
{{ mag(info.magnification_min) }}
{%- elif info and info.magnification_max is not none -%}
{{ 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 -%}
<span class="muted">—</span>
{%- endif -%}
{%- endmacro -%}
<div id="scopes-table-card" class="card" style="overflow-x:auto">
{% include "admin/_pagination.html" %}
<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>Type</th>
<th>Magnification</th>
<th>Objective thread</th>
{% for ct in column_types %}
<th data-sort-off>{{ ct.name }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for row in rows %}
{% set scope = row.scope %}
{% set info = row.info %}
<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 %}
</td>
<td>{{ scope.sub_part_type.name if scope.sub_part_type else "—" }}</td>
<td>{{ magnification(info) }}</td>
<td>
{% if row.objective_attachment_point %}
{{ connection_spec(row.objective_attachment_point) }}
{% else %}<span class="muted">—</span>{% endif %}
</td>
{% for ct in column_types %}
{% set entries = row.cells.get(ct.id) %}
<td>
{% if entries %}
{% for e in entries %}
<div class="fit-cell-item">
<span class="fit-badge fit-badge-{{ e.label }}">
<a href="/admin/augmented-parts/{{ e.part.id }}" title="{{ e.part.description | truncate(48) }}">{{ e.part.sku }}</a>
</span>
</div>
{% endfor %}
{% else %}
<span class="muted">—</span>
{% endif %}
</td>
{% endfor %}
</tr>
{% else %}
<tr>
<td colspan="{{ 6 + column_types | length + (1 if can_write_scopes else 0) }}" class="muted">
No scopes match your search.
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
@@ -48,4 +48,6 @@
{% endfor %} {% endfor %}
</select> </select>
</label> </label>
{% elif connection_type == "manual" %}
<p>No connection details — fits for this attachment point must be asserted manually.</p>
{% endif %} {% endif %}
@@ -1,5 +1,5 @@
<div id="attachment-point-name-list" class="card"> <div id="attachment-point-name-list" class="card">
<table class="table"> <table class="table sortable">
<thead> <thead>
<tr> <tr>
<th>ID</th> <th>ID</th>
@@ -15,12 +15,11 @@
{% set conn_type = "Static Connection" %} {% set conn_type = "Static Connection" %}
{% set conn_label = ap.static_connection.od_mm ~ " mm OD" %} {% set conn_label = ap.static_connection.od_mm ~ " mm OD" %}
{% else %} {% else %}
{% set conn_type = "—" %} {% set conn_type = "Manual" %}
{% set conn_label = "—" %} {% set conn_label = "—" %}
{% endif %} {% endif %}
<tr id="attachment-point-{{ ap.id }}"> <tr id="attachment-point-{{ ap.id }}">
<td>{{ ap.id }}</td> <td>{{ ap.id }}</td>
<td>{{ ap.augmented_part.description | truncate(40) }}</td>
<td>{{ ap.attachment_point_name.name }}</td> <td>{{ ap.attachment_point_name.name }}</td>
<td>{{ conn_type }}</td> <td>{{ conn_type }}</td>
<td><code>{{ conn_label }}</code></td> <td><code>{{ conn_label }}</code></td>
@@ -2,7 +2,7 @@
{% elif ap.unc_thread_id %}{% set current_type = "unc_thread" %} {% elif ap.unc_thread_id %}{% set current_type = "unc_thread" %}
{% elif ap.clamp_connection_id %}{% set current_type = "clamp_connection" %} {% elif ap.clamp_connection_id %}{% set current_type = "clamp_connection" %}
{% elif ap.static_connection_id %}{% set current_type = "static_connection" %} {% elif ap.static_connection_id %}{% set current_type = "static_connection" %}
{% else %}{% set current_type = "" %}{% endif %} {% else %}{% set current_type = "manual" %}{% endif %}
<tr id="attachment-point-{{ ap.id }}"> <tr id="attachment-point-{{ ap.id }}">
<td colspan="6"> <td colspan="6">
<form hx-post="/admin/attachment-points/{{ ap.id }}" <form hx-post="/admin/attachment-points/{{ ap.id }}"
@@ -41,6 +41,7 @@
<option value="unc_thread" {% if current_type == "unc_thread" %}selected{% endif %}>UNC Thread</option> <option value="unc_thread" {% if current_type == "unc_thread" %}selected{% endif %}>UNC Thread</option>
<option value="clamp_connection" {% if current_type == "clamp_connection" %}selected{% endif %}>Clamp Connection</option> <option value="clamp_connection" {% if current_type == "clamp_connection" %}selected{% endif %}>Clamp Connection</option>
<option value="static_connection" {% if current_type == "static_connection" %}selected{% endif %}>Static Connection</option> <option value="static_connection" {% if current_type == "static_connection" %}selected{% endif %}>Static Connection</option>
<option value="manual" {% if current_type == "manual" %}selected{% endif %}>Manual</option>
</select> </select>
</label> </label>
{# Render the current connection select pre-filled #} {# Render the current connection select pre-filled #}
@@ -1,9 +1,8 @@
<div id="attachment-point-list" class="card" style="overflow-x:auto"> <div id="attachment-point-list" class="card" style="overflow-x:auto">
<table class="table"> <table class="table sortable">
<thead> <thead>
<tr> <tr>
<th>ID</th> <th>ID</th>
<th>Augmented Part</th>
<th>Point Name</th> <th>Point Name</th>
<th>Connection Type</th> <th>Connection Type</th>
<th>Connection</th> <th>Connection</th>
@@ -15,13 +15,14 @@
{% set conn_type = "Static" %} {% set conn_type = "Static" %}
{% set conn_label = apoint.static_connection.od_mm ~ " mm OD" %} {% set conn_label = apoint.static_connection.od_mm ~ " mm OD" %}
{% else %} {% else %}
{% set conn_type = "—" %} {% set conn_type = "Manual" %}
{% set conn_label = "—" %} {% set conn_label = "—" %}
{% endif %} {% endif %}
<div id="augmented-part-ap-{{ apoint.id }}" class="ap-item"> <div id="augmented-part-ap-{{ apoint.id }}" class="ap-item">
<span class="ap-item-name">{{ apoint.attachment_point_name.name }}</span> <span class="ap-item-name">{{ apoint.attachment_point_name.name }}</span>
<span class="ap-item-type">{{ conn_type }}</span> <span class="ap-item-type">{{ conn_type }}</span>
<code class="ap-item-conn">{{ conn_label }}</code> <code class="ap-item-conn">{{ conn_label }}</code>
{% if can_write_attachment_points %}
<div class="row-actions"> <div class="row-actions">
<button class="btn btn-ghost" <button class="btn btn-ghost"
hx-get="/admin/augmented-parts/{{ apoint.augmented_part_id }}/attachment-points/{{ apoint.id }}/edit" hx-get="/admin/augmented-parts/{{ apoint.augmented_part_id }}/attachment-points/{{ apoint.id }}/edit"
@@ -33,4 +34,5 @@
hx-swap="outerHTML" hx-swap="outerHTML"
hx-confirm="Delete this attachment point?">Delete</button> hx-confirm="Delete this attachment point?">Delete</button>
</div> </div>
{% endif %}
</div> </div>
@@ -2,7 +2,7 @@
{% elif apoint.unc_thread_id %}{% set current_type = "unc_thread" %} {% elif apoint.unc_thread_id %}{% set current_type = "unc_thread" %}
{% elif apoint.clamp_connection_id %}{% set current_type = "clamp_connection" %} {% elif apoint.clamp_connection_id %}{% set current_type = "clamp_connection" %}
{% elif apoint.static_connection_id %}{% set current_type = "static_connection" %} {% elif apoint.static_connection_id %}{% set current_type = "static_connection" %}
{% else %}{% set current_type = "" %}{% endif %} {% else %}{% set current_type = "manual" %}{% endif %}
{# _attachment_point_connection_select.html expects `ap` for pre-selection #} {# _attachment_point_connection_select.html expects `ap` for pre-selection #}
{% set ap = apoint %} {% set ap = apoint %}
{% set connection_type = current_type %} {% set connection_type = current_type %}
@@ -29,6 +29,7 @@
<option value="unc_thread" {{ "selected" if current_type == "unc_thread" }}>UNC Thread</option> <option value="unc_thread" {{ "selected" if current_type == "unc_thread" }}>UNC Thread</option>
<option value="clamp_connection" {{ "selected" if current_type == "clamp_connection" }}>Clamp Connection</option> <option value="clamp_connection" {{ "selected" if current_type == "clamp_connection" }}>Clamp Connection</option>
<option value="static_connection" {{ "selected" if current_type == "static_connection" }}>Static Connection</option> <option value="static_connection" {{ "selected" if current_type == "static_connection" }}>Static Connection</option>
<option value="manual" {{ "selected" if current_type == "manual" }}>Manual</option>
</select> </select>
</label> </label>
<div id="edit-ap-{{ apoint.id }}-connection-options"> <div id="edit-ap-{{ apoint.id }}-connection-options">
@@ -7,6 +7,7 @@
<p class="muted" style="font-size:0.85rem;margin:0 0 0.5rem">No attachment points yet.</p> <p class="muted" style="font-size:0.85rem;margin:0 0 0.5rem">No attachment points yet.</p>
{% endfor %} {% endfor %}
{% if can_write_attachment_points %}
<form hx-post="/admin/augmented-parts/{{ augmented_part_id }}/attachment-points" <form hx-post="/admin/augmented-parts/{{ augmented_part_id }}/attachment-points"
hx-target="#ap-{{ augmented_part_id }}-attachment-points" hx-target="#ap-{{ augmented_part_id }}-attachment-points"
hx-swap="outerHTML" hx-swap="outerHTML"
@@ -30,9 +31,11 @@
<option value="unc_thread">UNC Thread</option> <option value="unc_thread">UNC Thread</option>
<option value="clamp_connection">Clamp Connection</option> <option value="clamp_connection">Clamp Connection</option>
<option value="static_connection">Static Connection</option> <option value="static_connection">Static Connection</option>
<option value="manual">Manual</option>
</select> </select>
</label> </label>
<div id="ap-{{ augmented_part_id }}-add-connection-options"></div> <div id="ap-{{ augmented_part_id }}-add-connection-options"></div>
<button type="submit" class="btn btn-primary">Add</button> <button type="submit" class="btn btn-primary">Add</button>
</form> </form>
{% endif %}
</div> </div>
@@ -8,55 +8,64 @@
{%- elif ap.static_connection -%} {%- elif ap.static_connection -%}
Static {{ ap.static_connection.od_mm }} mm OD Static {{ ap.static_connection.od_mm }} mm OD
{%- else -%} {%- else -%}
— Manual
{%- endif -%} {%- endif -%}
{%- endmacro %} {%- endmacro %}
<div id="fit-{{ fit.id }}" class="fit-item"> <tr id="fit-{{ fit.id }}" class="fit-row">
<div class="fit-item-main"> <td><span class="fit-badge fit-badge-{{ fit.label.value }}">{{ fit.label.value | capitalize }}</span></td>
<span class="fit-badge fit-badge-{{ fit.label.value }}">{{ fit.label.value | capitalize }}</span> <td>
<span class="fit-item-side"><strong>{{ own_ap.attachment_point_name.name }}</strong> ({{ conn_desc(own_ap) }})</span> <strong>{{ own_ap.attachment_point_name.name }}</strong>
<span class="fit-item-arrow">↔</span> <div class="muted" style="font-size:0.8rem">{{ conn_desc(own_ap) }}</div>
<span class="fit-item-side">{{ other_part.description if other_part else "Unknown part" }} — <strong>{{ other_ap.attachment_point_name.name }}</strong> ({{ conn_desc(other_ap) }})</span> </td>
</div> <td class="fit-item-arrow">↔</td>
<div class="row-actions"> <td>
{% if fit.label.value != "exact" %} <a href="/admin/augmented-parts/{{ other_part.id }}" title="{{ other_part.description }}">{{ other_part.sku or other_part.description }}</a>
<button class="btn btn-ghost" <div class="muted" style="font-size:0.8rem">{{ other_part.part_type.name }}</div>
hx-post="/admin/augmented-parts/{{ augmented_part_id }}/fits/{{ fit.id }}/label" </td>
hx-vals='{"label": "exact"}' <td class="fit-notes-cell">
hx-target="#fit-{{ fit.id }}" hx-swap="outerHTML">Mark Exact</button>
{% endif %}
{% if fit.label.value != "possible" %}
<button class="btn btn-ghost"
hx-post="/admin/augmented-parts/{{ augmented_part_id }}/fits/{{ fit.id }}/label"
hx-vals='{"label": "possible"}'
hx-target="#fit-{{ fit.id }}" hx-swap="outerHTML">Mark Possible</button>
{% endif %}
{% if fit.label.value != "none" %}
<button class="btn btn-ghost"
hx-post="/admin/augmented-parts/{{ augmented_part_id }}/fits/{{ fit.id }}/label"
hx-vals='{"label": "none"}'
hx-target="#fit-{{ fit.id }}" hx-swap="outerHTML">Mark Impossible</button>
{% endif %}
<button class="btn btn-danger"
hx-delete="/admin/augmented-parts/{{ augmented_part_id }}/fits/{{ fit.id }}"
hx-target="#ap-{{ augmented_part_id }}-fits"
hx-swap="outerHTML"
hx-confirm="Delete this fit?">Delete</button>
</div>
<div class="fit-notes">
{% for note in fit.notes %} {% for note in fit.notes %}
<div class="fit-note"> <div class="fit-note">
<span class="fit-note-meta">{{ note.username }} · {{ note.created_at.strftime("%Y-%m-%d %H:%M") }}</span> <span class="fit-note-meta">{{ note.username }} · {{ note.created_at.strftime("%Y-%m-%d %H:%M") }}</span>
<p class="fit-note-body">{{ note.body }}</p> <p class="fit-note-body">{{ note.body }}</p>
</div> </div>
{% endfor %} {% endfor %}
{% if can_write %}
<form hx-post="/admin/augmented-parts/{{ augmented_part_id }}/fits/{{ fit.id }}/notes" <form hx-post="/admin/augmented-parts/{{ augmented_part_id }}/fits/{{ fit.id }}/notes"
hx-target="#fit-{{ fit.id }}" hx-swap="outerHTML" hx-target="#fit-{{ fit.id }}" hx-swap="outerHTML"
class="inline-edit fit-note-form"> class="inline-edit fit-note-form">
<input type="text" name="body" placeholder="Add a note…" required> <input type="text" name="body" placeholder="Add a note…" required>
<button type="submit" class="btn btn-ghost">Add note</button> <button type="submit" class="btn btn-ghost btn-sm">Add note</button>
</form> </form>
{% endif %}
</td>
{% if can_write %}
<td>
<div class="row-actions">
{% if fit.label.value != "exact" %}
<button class="btn btn-ghost btn-sm"
hx-post="/admin/augmented-parts/{{ augmented_part_id }}/fits/{{ fit.id }}/label"
hx-vals='{"label": "exact"}'
hx-target="#fit-{{ fit.id }}" hx-swap="outerHTML">Mark Exact</button>
{% endif %}
{% if fit.label.value != "possible" %}
<button class="btn btn-ghost btn-sm"
hx-post="/admin/augmented-parts/{{ augmented_part_id }}/fits/{{ fit.id }}/label"
hx-vals='{"label": "possible"}'
hx-target="#fit-{{ fit.id }}" hx-swap="outerHTML">Mark Possible</button>
{% endif %}
{% if fit.label.value != "none" %}
<button class="btn btn-ghost btn-sm"
hx-post="/admin/augmented-parts/{{ augmented_part_id }}/fits/{{ fit.id }}/label"
hx-vals='{"label": "none"}'
hx-target="#fit-{{ fit.id }}" hx-swap="outerHTML">Mark Impossible</button>
{% endif %}
<button class="btn btn-danger btn-sm"
hx-delete="/admin/augmented-parts/{{ augmented_part_id }}/fits/{{ fit.id }}"
hx-target="#ap-{{ augmented_part_id }}-fits"
hx-swap="outerHTML"
hx-confirm="Delete this fit?">Delete</button>
</div> </div>
</div> </td>
{% endif %}
</tr>
+63 -25
View File
@@ -4,35 +4,46 @@
{% if message %}<p class="alert alert-success">{{ message }}</p>{% endif %} {% if message %}<p class="alert alert-success">{{ message }}</p>{% endif %}
{% if error %}<p class="alert alert-error">{{ error }}</p>{% endif %} {% if error %}<p class="alert alert-error">{{ error }}</p>{% endif %}
{% for view in fit_views %} {% set add_form_id = "ap-" ~ augmented_part_id ~ "-fit-add-form" %}
{% with fit=view.fit, own_ap=view.own_ap, other_ap=view.other_ap, other_part=view.other_part %} {% if can_write %}
{% include "admin/_augmented_part_fit_row.html" %} <form id="{{ add_form_id }}"
{% endwith %} hx-post="/admin/augmented-parts/{{ augmented_part_id }}/fits"
{% else %}
<p class="muted" style="font-size:0.85rem;margin:0 0 0.5rem">No fits recorded yet.</p>
{% endfor %}
<div class="row-actions" style="margin:0.5rem 0">
<form hx-post="/admin/augmented-parts/{{ augmented_part_id }}/fits/recompute"
hx-target="#ap-{{ augmented_part_id }}-fits" hx-swap="outerHTML">
<button type="submit" class="btn btn-ghost">Recompute possible fits</button>
</form>
</div>
<form hx-post="/admin/augmented-parts/{{ augmented_part_id }}/fits"
hx-target="#ap-{{ augmented_part_id }}-fits" hx-target="#ap-{{ augmented_part_id }}-fits"
hx-swap="outerHTML" hx-swap="outerHTML"></form>
class="inline-edit" style="margin-top:0.5rem"> {% endif %}
<label>This part's point
<select name="own_attachment_point_id" required> <table class="table fit-table">
<thead>
<tr>
<th>Type</th>
<th>This part's point</th>
<th></th>
<th>Fits with part</th>
<th>Notes</th>
{% if can_write %}<th></th>{% endif %}
</tr>
</thead>
<tbody>
{% if can_write %}
<tr class="fit-add-row">
<td>
<select form="{{ add_form_id }}" name="label">
<option value="exact" selected>Exact</option>
<option value="possible">Possible</option>
<option value="none">None</option>
</select>
</td>
<td>
<select form="{{ add_form_id }}" name="own_attachment_point_id" required>
<option value="">— select —</option> <option value="">— select —</option>
{% for p in own_points %} {% for p in own_points %}
<option value="{{ p.id }}">{{ p.attachment_point_name.name }}</option> <option value="{{ p.id }}">{{ p.attachment_point_name.name }}</option>
{% endfor %} {% endfor %}
</select> </select>
</label> </td>
<label>Fits with part <td class="fit-item-arrow">↔</td>
<select name="other_augmented_part_id" required <td>
<select form="{{ add_form_id }}" name="other_augmented_part_id" required
hx-get="/admin/augmented-parts/{{ augmented_part_id }}/fits/other-points" hx-get="/admin/augmented-parts/{{ augmented_part_id }}/fits/other-points"
hx-target="#ap-{{ augmented_part_id }}-fit-other-points" hx-target="#ap-{{ augmented_part_id }}-fit-other-points"
hx-trigger="change" hx-trigger="change"
@@ -42,8 +53,35 @@
<option value="{{ p.id }}">{{ p.description | truncate(40) }}</option> <option value="{{ p.id }}">{{ p.description | truncate(40) }}</option>
{% endfor %} {% endfor %}
</select> </select>
</label>
<div id="ap-{{ augmented_part_id }}-fit-other-points"></div> <div id="ap-{{ augmented_part_id }}-fit-other-points"></div>
<button type="submit" class="btn btn-primary">Add fit</button> </td>
<td>
<input form="{{ add_form_id }}" type="text" name="note" placeholder="Add a note…">
</td>
<td>
<button type="submit" form="{{ add_form_id }}" class="btn btn-primary btn-sm">Add fit</button>
</td>
</tr>
{% endif %}
{% for view in fit_views %}
{% with fit=view.fit, own_ap=view.own_ap, other_ap=view.other_ap, other_part=view.other_part %}
{% include "admin/_augmented_part_fit_row.html" %}
{% endwith %}
{% else %}
<tr>
<td colspan="{{ 6 if can_write else 5 }}" class="muted" style="font-size:0.85rem">No fits recorded yet.</td>
</tr>
{% endfor %}
</tbody>
</table>
{% if can_write %}
<div class="row-actions" style="margin:0.5rem 0">
<form hx-post="/admin/augmented-parts/{{ augmented_part_id }}/fits/recompute"
hx-target="#ap-{{ augmented_part_id }}-fits" hx-swap="outerHTML">
<button type="submit" class="btn btn-ghost">Recompute possible fits</button>
</form> </form>
</div> </div>
{% endif %}
</div>
@@ -0,0 +1,22 @@
<div id="ap-{{ augmented_part_id }}-notes" class="ap-inline-section">
<h3>Notes</h3>
<div class="fit-notes">
{% for note in notes %}
<div class="fit-note">
<span class="fit-note-meta">{{ note.username }} · {{ note.created_at.strftime("%Y-%m-%d %H:%M") }}</span>
<p class="fit-note-body">{{ note.body }}</p>
</div>
{% else %}
<p class="muted" style="font-size:0.85rem;margin:0 0 0.5rem">No notes recorded yet.</p>
{% endfor %}
{% if can_write_notes %}
<form hx-post="/admin/augmented-parts/{{ augmented_part_id }}/notes"
hx-target="#ap-{{ augmented_part_id }}-notes" hx-swap="outerHTML"
class="inline-edit fit-note-form">
<input type="text" name="body" placeholder="Add a note…" required>
<button type="submit" class="btn btn-ghost">Add note</button>
</form>
{% endif %}
</div>
</div>
+14 -1
View File
@@ -1,17 +1,30 @@
<tr id="augmented-part-{{ ap.id }}"> <tr id="augmented-part-{{ ap.id }}">
<td>{{ ap.id }}</td> <td>{{ ap.id }}</td>
<td>{{ ap.sku or '—' }}</td> <td>{{ ap.sku or '—' }}</td>
<td>{{ ap.description | truncate(60) }}</td> <td><a href="/admin/augmented-parts/{{ ap.id }}">{{ ap.description | truncate(60) }}</a></td>
<td>{{ ap.colour.name }}</td> <td>{{ ap.colour.name }}</td>
<td>{{ ap.manufacturer.name }}</td> <td>{{ ap.manufacturer.name }}</td>
<td>{{ ap.scope_model.name if ap.scope_model else '—' }}</td> <td>{{ ap.scope_model.name if ap.scope_model else '—' }}</td>
<td>{{ ap.part_type.name }}</td> <td>{{ ap.part_type.name }}</td>
<td>{{ ap.sub_part_type.name if ap.sub_part_type else '—' }}</td> <td>{{ ap.sub_part_type.name if ap.sub_part_type else '—' }}</td>
<td>{% if ap.archived %}<span class="tag">Archived</span>{% else %}—{% endif %}</td>
<td class="row-actions"> <td class="row-actions">
<button class="btn btn-ghost" <button class="btn btn-ghost"
hx-get="/admin/augmented-parts/{{ ap.id }}/edit" hx-get="/admin/augmented-parts/{{ ap.id }}/edit"
hx-target="#augmented-part-{{ ap.id }}" hx-target="#augmented-part-{{ ap.id }}"
hx-swap="outerHTML">Edit</button> hx-swap="outerHTML">Edit</button>
{% if ap.archived %}
<button class="btn btn-ghost"
hx-post="/admin/augmented-parts/{{ ap.id }}/unarchive"
hx-target="#augmented-part-{{ ap.id }}"
hx-swap="outerHTML">Unarchive</button>
{% else %}
<button class="btn btn-ghost"
hx-post="/admin/augmented-parts/{{ ap.id }}/archive"
hx-target="#augmented-part-{{ ap.id }}"
hx-swap="outerHTML"
hx-confirm="Archive {{ ap.sku or ap.description }}?">Archive</button>
{% endif %}
<button class="btn btn-danger" <button class="btn btn-danger"
hx-delete="/admin/augmented-parts/{{ ap.id }}" hx-delete="/admin/augmented-parts/{{ ap.id }}"
hx-target="#augmented-part-{{ ap.id }}" hx-target="#augmented-part-{{ ap.id }}"
@@ -1,5 +1,5 @@
<tr id="augmented-part-{{ ap.id }}"> <tr id="augmented-part-{{ ap.id }}">
<td colspan="9"> <td colspan="10">
<form id="augmented-part-edit-form-{{ ap.id }}" <form id="augmented-part-edit-form-{{ ap.id }}"
hx-post="/admin/augmented-parts/{{ ap.id }}" hx-post="/admin/augmented-parts/{{ ap.id }}"
hx-target="#augmented-part-{{ ap.id }}" hx-target="#augmented-part-{{ ap.id }}"
@@ -10,7 +10,9 @@
hx-target="#augmented-parts-table-card" hx-target="#augmented-parts-table-card"
hx-trigger="input delay:300ms, change" hx-trigger="input delay:300ms, change"
hx-swap="outerHTML"> hx-swap="outerHTML">
<input type="text" name="description" placeholder="Filter by description…" value="{{ filter_description or '' }}"> <input type="hidden" name="sort" value="{{ sort }}">
<input type="hidden" name="dir" value="{{ dir }}">
<input type="text" name="description" placeholder="Filter by description or SKU…" value="{{ filter_description or '' }}">
<select name="colour_id"> <select name="colour_id">
<option value="">All colours</option> <option value="">All colours</option>
{% for pc in part_colours %} {% for pc in part_colours %}
@@ -41,6 +43,7 @@
<option value="{{ spt.id }}" {{ "selected" if filter_sub_part_type_id and filter_sub_part_type_id|int == spt.id }}>{{ spt.name }}</option> <option value="{{ spt.id }}" {{ "selected" if filter_sub_part_type_id and filter_sub_part_type_id|int == spt.id }}>{{ spt.name }}</option>
{% endfor %} {% endfor %}
</select> </select>
<label><input type="checkbox" name="include_archived" {{ "checked" if include_archived }}> Show archived</label>
</form> </form>
</div> </div>
@@ -1,16 +1,18 @@
{% from "admin/_sort_macros.html" import sort_th %}
<div id="augmented-parts-table-card" class="card"> <div id="augmented-parts-table-card" class="card">
{% include "admin/_pagination.html" %} {% include "admin/_pagination.html" %}
<table class="table"> <table class="table">
<thead> <thead>
<tr> <tr>
<th>ID</th> {{ sort_th("id", "ID", sort_headers) }}
<th>SKU</th> {{ sort_th("sku", "SKU", sort_headers) }}
<th>Description</th> {{ sort_th("description", "Description", sort_headers) }}
<th>Colour</th> {{ sort_th("colour", "Colour", sort_headers) }}
<th>Manufacturer</th> {{ sort_th("manufacturer", "Manufacturer", sort_headers) }}
<th>Model</th> {{ sort_th("scope_model", "Model", sort_headers) }}
<th>Type</th> {{ sort_th("part_type", "Type", sort_headers) }}
<th>Sub Type</th> {{ sort_th("sub_part_type", "Sub Type", sort_headers) }}
<th>Archived</th>
<th></th> <th></th>
</tr> </tr>
</thead> </thead>
@@ -18,7 +20,7 @@
{% for ap in augmented_parts %} {% for ap in augmented_parts %}
{% include "admin/_augmented_part_row.html" %} {% include "admin/_augmented_part_row.html" %}
{% else %} {% else %}
<tr><td colspan="9" class="muted">No augmented parts recorded.</td></tr> <tr><td colspan="10" class="muted">No augmented parts recorded.</td></tr>
{% endfor %} {% endfor %}
</tbody> </tbody>
</table> </table>
@@ -1,5 +1,5 @@
<div id="clamp-connection-list" class="card"> <div id="clamp-connection-list" class="card">
<table class="table"> <table class="table sortable">
<thead> <thead>
<tr> <tr>
<th>ID</th> <th>ID</th>
@@ -0,0 +1,14 @@
{% if total %}
<p style="color:var(--color-danger,#c00)">
Purge complete: {{ total }} row(s) deleted.
</p>
<ul class="muted">
{% for label, n in counts.items() %}
{% if n %}
<li>{{ label }}: {{ n }}</li>
{% endif %}
{% endfor %}
</ul>
{% else %}
<p class="muted">Nothing to purge — the database was already clean.</p>
{% endif %}
@@ -1,6 +1,13 @@
<select name="other_attachment_point_id" required> {% if points %}
<select name="other_attachment_point_id" form="{{ form_id }}" required>
<option value="">— select —</option> <option value="">— select —</option>
{% for p in points %} {% for p in points %}
<option value="{{ p.id }}">{{ p.attachment_point_name.name }}</option> <option value="{{ p.id }}">{{ p.attachment_point_name.name }}</option>
{% endfor %} {% endfor %}
</select> </select>
{% else %}
<input type="hidden" name="other_attachment_point_id" value="" form="{{ form_id }}">
<p class="muted" style="font-size:0.85rem;margin:0">
This part has no attachment points yet — one matching yours will be created automatically.
</p>
{% endif %}
@@ -11,7 +11,9 @@
hx-target="#imported-parts-table-card" hx-target="#imported-parts-table-card"
hx-trigger="input delay:300ms, change" hx-trigger="input delay:300ms, change"
hx-swap="outerHTML"> hx-swap="outerHTML">
<input type="text" name="description" placeholder="Filter by description…" value="{{ filter_description or '' }}"> <input type="hidden" name="sort" value="{{ sort }}">
<input type="hidden" name="dir" value="{{ dir }}">
<input type="text" name="description" placeholder="Filter by description or SKU…" value="{{ filter_description or '' }}">
</form> </form>
</div> </div>
@@ -1,16 +1,17 @@
{% from "admin/_sort_macros.html" import sort_th %}
<div id="imported-parts-table-card" class="card" style="overflow-x:auto"> <div id="imported-parts-table-card" class="card" style="overflow-x:auto">
{% include "admin/_pagination.html" %} {% include "admin/_pagination.html" %}
<table class="table"> <table class="table">
<thead> <thead>
<tr> <tr>
<th>InventoryID</th> {{ sort_th("InventoryID", "InventoryID", sort_headers) }}
<th>Description</th> {{ sort_th("Description", "Description", sort_headers) }}
<th>TBX Colour</th> {{ sort_th("TBXColour", "TBX Colour", sort_headers) }}
<th>Max Thread Depth (mm)</th> {{ sort_th("ProductMaxThreadDepthmm", "Max Thread Depth (mm)", sort_headers) }}
<th>Max Thread Depth (in)</th> {{ sort_th("ProductMaxThreadDepthin", "Max Thread Depth (in)", sort_headers) }}
<th>OD (mm)</th> {{ sort_th("ProductODmm", "OD (mm)", sort_headers) }}
<th>OD (in)</th> {{ sort_th("ProductODin", "OD (in)", sort_headers) }}
<th>Thread Attribute</th> {{ sort_th("TBXThreadAttribute", "Thread Attribute", sort_headers) }}
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
+7
View File
@@ -0,0 +1,7 @@
<div id="{{ container_id }}"
hx-get="{{ poll_url }}"
hx-trigger="load delay:1s"
hx-swap="outerHTML"
class="{{ extra_class or '' }}">
<p class="muted">⏳ {{ job.message }}</p>
</div>
@@ -0,0 +1,5 @@
<span id="{{ container_id }}"
hx-get="{{ poll_url }}"
hx-trigger="load delay:1s"
hx-swap="outerHTML"
class="muted">⏳ {{ job.message }}</span>
@@ -1,8 +1,8 @@
<div id="manufacturer-list" class="card"> <div id="manufacturer-list" class="card">
<table class="table"> <table class="table sortable">
<thead> <thead>
<tr> <tr>
<th>ID</th><th>Name</th><th>Homepage</th><th>Logo</th><th></th> <th>ID</th><th>Name</th><th>Homepage</th><th data-sort-off>Logo</th><th></th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
+1 -1
View File
@@ -1,5 +1,5 @@
<div id="part-colour-list" class="card"> <div id="part-colour-list" class="card">
<table class="table"> <table class="table sortable">
<thead> <thead>
<tr> <tr>
<th>ID</th> <th>ID</th>
+1 -1
View File
@@ -1,5 +1,5 @@
<div id="part-type-list" class="card"> <div id="part-type-list" class="card">
<table class="table"> <table class="table sortable">
<thead> <thead>
<tr> <tr>
<th>ID</th><th>Name</th><th></th> <th>ID</th><th>Name</th><th></th>
+2 -2
View File
@@ -1,7 +1,7 @@
<div id="role-list" class="card"> <div id="role-list" class="card">
<table class="table"> <table class="table sortable">
<thead> <thead>
<tr><th>ID</th><th>Name</th><th>Description</th><th>Permissions</th><th>Default</th><th></th></tr> <tr><th>ID</th><th>Name</th><th>Description</th><th data-sort-off>Permissions</th><th data-sort-off>Default</th><th></th></tr>
</thead> </thead>
<tbody> <tbody>
{% for r in roles %} {% for r in roles %}
@@ -1,3 +1,4 @@
<div id="{{ container_id or 'scope-import-result-body' }}">
{% if error %} {% if error %}
<div class="card"> <div class="card">
<p style="color:var(--color-danger,#c00)">{{ error }}</p> <p style="color:var(--color-danger,#c00)">{{ error }}</p>
@@ -39,3 +40,4 @@
{% endif %} {% endif %}
</div> </div>
{% endif %} {% endif %}
</div>
+1 -1
View File
@@ -1,5 +1,5 @@
<div id="scope-model-list" class="card"> <div id="scope-model-list" class="card">
<table class="table"> <table class="table sortable">
<thead> <thead>
<tr> <tr>
<th>ID</th><th>Name</th><th>Manufacturer</th><th>Product Page</th><th></th> <th>ID</th><th>Name</th><th>Manufacturer</th><th>Product Page</th><th></th>
+13
View File
@@ -0,0 +1,13 @@
{% macro sort_th(key, label, sort_headers) %}
{% if sort_headers and key in sort_headers %}
{% set h = sort_headers[key] %}
<th>
<a href="#" class="sort-link{{ ' active' if h.active else '' }}"
hx-get="{{ h.url }}"
hx-target="{{ h.target_id }}"
hx-swap="outerHTML">{{ label }}{% if h.active %}<span aria-hidden="true">{{ ' ▲' if h.dir == 'asc' else ' ▼' }}</span>{% endif %}</a>
</th>
{% else %}
<th>{{ label }}</th>
{% endif %}
{% endmacro %}
@@ -1,5 +1,5 @@
<div id="static-connection-list" class="card"> <div id="static-connection-list" class="card">
<table class="table"> <table class="table sortable">
<thead> <thead>
<tr> <tr>
<th>ID</th> <th>ID</th>
Loaded 100 of 133 files, more files were not shown because too many files have changed in this diff. Show more