# ALPHA — the ATO page generator

> **Handover (20 Nov 2026): [ATO-TEMPLATE-HANDOVER.md](ATO-TEMPLATE-HANDOVER.md)** — where the final template lives, free / paid /
> gifted / lapsed, the Edit Premium rules, design rules, the review v2 fields, what the database still needs, tests and previews.

> **DRAFT — do not use.** This folder is waiting for MT to confirm the themes.
> Do not run the generator to produce pages until MT says so.
> The locked page look is tag `locked/ato-page-final-v2-10nov` (`b18669e9`):
> `site/ato-v6.html` and `site/ato-v6/`. That look must stay pixel-identical.

## Fixed on 11 Nov 2026 (branch `preview/reviews-d1-and-generator-fixes-11nov`)

The page look was not changed. No CSS file and no drawing in `svg.py` was touched.

| # | Bug | Fix |
|---|---|---|
| 1 | Bars, "Written reviews" and "Rated 4.0+" were counted from the 3 reviews on the page; the big number came from all of them | Every card number comes from one rating summary (`rating.py`), the object the server sends. A summary that does not add up stops the build |
| 2 | `round()` put a 4.5 review in the 4 bar | One rule, halves up, in `rating.bar_of` |
| 3 | `--today` defaulted to 2026-11-09 | Default is today in South Africa |
| 4 | Rating showed at 3 reviews, ignoring the 7-day account rule | The page uses the server's `shown` flag |
| 5 | "N more reviews" counted hidden stars-only and deleted reviews | It counts written reviews only |
| 6 | Two schools with one slug overwrote each other | The build stops and names both |
| 7 | Any name containing "skyhawk", "aeronav" or "excellentia" was merged into Aeronav | Merges are an exact list (`master_convert.MERGES`); merged schools keep every certificate |
| 8 | The slug was not checked before it became a folder name | Checked in `check()`; reserved names refused; the path must stay inside the output folder |
| 9 | Phone, email and website went into links unchecked; missing ones were dead `#` links | Validated; a bad value is left out and reported; a missing one is not a link |
| 10 | Upvote counting used the clock and skipped the 20-a-day cap | The server's count (`up`) is used; example data uses the build date, so builds repeat |
| 11 | "pilot" was stripped from course names with no choice | Same default; `--keep-pilot-in-courses` keeps it (decision D4) |
| 12 | `rules.py`, README and tests described superseded rules | Updated to N-10, REV-029, REV-049 |
| 13 | Every page was `noindex` with no description, canonical link or rating markup; sign-in lost the page | `--index`, `--site`, description, rating markup only when shown, sign-in returns to the school |
| + | The viewer API address used the certificate number | `/api/ato/{slug}/view`: reviews belong to the school (REV-003) |
| + | Anonymous reviews showed a made-up day ("1 Aug 2026") | Month only ("Aug 2026", REV-038) |
| + | Private fields were blocked by a short list of names | An allow-list: any unknown field stops the build |
| + | Pages of removed schools stayed in the output | Reported in `manifest.json`; `--prune` removes them |

**Not done, and why.** The locked page shows the seven category averages under the rating card (t1289u). This
template does not draw them. `--show-categories` draws them from the summary, but with the class names found in
this folder's CSS copy, which is older than the locked page. It is off by default. The row markup must be copied
from `site/ato-v6/v6.js` at `b18669e9` before it is switched on.

**No picture in this change.** `static/ato-v6.css` in this folder is an older copy and does not style the
2-column page (`ob-ratewide`, `ob-clink`, `ob-mid` are missing from it). The real page links `/ato-v6/v6.css` from
`site/`. A 390px Amethyst picture must be taken against that file.

**In plain words:** one template makes every ATO page. You give it a school's data file (or the master file) and a colour theme; it writes the finished guest page body. The top bar and logo live in `/ato/_shared/topbar.js` (ATO-20) and are not copied into each page. Design = the approved 2-column page (REV-061 / DEC-011).

## How to run (one command)
```
cd ato-generator && pip install -r requirements.txt && python3 generator.py && python3 -m pytest -q
```
Example pages appear in `out/<school-slug>/index.html`. All 277 schools:
`python3 generator.py --master ../docs/ato-master/ato-master-10nov.json --out build`
(writes `build/<slug>/index.html` plus `_redirects`). Another theme: `python3 generator.py --theme pearl --out out-pearl`.

## What each file does
| File | What it does |
|---|---|
| `generator.py` | The main script. Reads each school file, checks it, decides the rules once (gold tick only if paid, rating only from 3 reviews, shiny stars from 4.0, guests see the top 3 reviews by counted upvotes with ties going to the newest, the 7 categories in order), then fills the approved 2-column template and writes one page per school. Also writes `out/manifest.json` (which pages, which data). |
| `rating.py` | The rating summary: checks the one the server sends, and mirrors the server's arithmetic for tests and example data (exact fractions, halves up). |
| `rules.py` | The review rules the server will enforce later. Stars first and text later: stars stay locked, it is not a new review, it uses no quota, and the review shows "Text added <date>". The 6-month replace is the only way to change stars. Replies under your own Anonymous review stay Anonymous; a named review means named replies; otherwise Trial, Pro and Founding choose per reply (N-10). |
| `svg.py` | The locked drawings: ticks (DFE blue, ATO gold), Founding compass, glass / flat / empty stars, star pyramid. Same as the v6.2 design. |
| `template/ato.html.j2` | The page shell: puts the parts in order. |
| `template/parts/hero.j2` | Base, school name (+ gold tick if paid), `.official` handle, big score + stars, "4.3 from N reviews". |
| `template/parts/bases_certificates.j2` | Each base (ICAO + name → `/ad/{CODE}`, or plain text when there is no code) with its certificate on one line: Current/Expired + date + "Updated &lt;date&gt;". |
| `template/parts/about_contact.j2` | About text + Call / Email / Website in one card. |
| `template/parts/reviews.j2` | Reviews heading, the approved rating card (score, 2×2 stats, 5-to-1 bars), guest top 3 by counted aged upvotes, then "Sign in to see the rest". |
| `template/parts/courses.j2` | Courses under About and Bases: Licences / Ratings / Other, first 3 + More. |
| `template/parts/badges.j2` | Name + badge rules: DFE wins over Founding. Anonymous shows "Anonymous" with no avatar, no badges and no licences. Gold tick for paid or gifted Premium. |
| `themes/*.css` | The 4 colour token files: Midnight, Amethyst, Pearl, Champagne. Colours only. |
| `static/` | Shared page CSS (from the v6.2 design) + a small script: number fit and sweep, replies and courses open smoothly. |
| `data/ato/*.json` | 2 example schools: one paid (2 bases, 2 certificates, one expired), one unpaid verified (1 base, 2 reviews, so no rating yet). |
| `tests/` | Automatic checks (pytest), listed below. |
| `out/` | The generated pages. Never edited by hand. |

## Later, not in the static page
The generated page is the guest view only (REV-061). The review form, votes and the rest of the reviews come after sign-in, not from placeholders copied into the static HTML.

## Tests (83, all pass)
- Gold tick only for paid schools, and it goes away when a school stops paying.
- Rating hidden under 3 reviews.
- Shiny from 4.0: 4.3 and 4.0 are shiny, 3.99 and 3.6 are flat. The 4.3 page has the sweep.
- 7 categories in the right order.
- Guests get the top 3 reviews by counted upvotes, ties go to the newest.
- Anonymous carries no name, badges or licences.
- Certificates show Current and Expired, plus "Updated <date>".
- No form or votes in the static page.
- "4.3 from N reviews" and the "Edited" tag.
- Stars then text: stars locked, no quota, text only once, replace after 6 months.
- Replies follow the review's anonymous choice.
- Courses show for guests, right after Ratings.

## Paid About and Contact (design, 20 Nov 2026; DESIGN PENDING, PREM-1)

Drawn only with `--premium-preview`, only for a paid or gifted school, and `[hidden]` until the page is opened with `?preview-premium=1`
(then `page.js` sets `html[data-premium-preview]`, which also hides the free About and, on phones, the hero contact row).
The free page is byte-for-byte unchanged: same HTML for every school, with or without the flag.

- **About** (`template/parts/premium_about.j2`): one lead line (Clash Display 500), two short paragraphs, a mono fact column and one
  wide photograph with a caption. No boxes or icons. Without a school-written `lead` the first sentence of the page's own About text leads.
- **Contact** (`template/parts/premium_contact.j2`): one split card. Phone, Email and WhatsApp rows, opening hours with an "Open now" dot
  (Johannesburg time, worked out in the browser), a drawn location tile with the aerodrome pin and ICAO code (Nippo), address, Directions,
  and one "Ask about training" button that opens the enquiry form inside the card. On a phone a slim Call / Email / Directions bar is pinned
  to the bottom and steps aside while the card is on screen.
- Anything missing is left out; nothing is invented. In the preview every missing field is filled by `SAMPLE_PROFILE`, and the
  blocks say "Sample details". A school file may carry its own `profile` (validated by `check_profile`), which is then used as is.

### Needed fields (not in the database or `/api/ato/{slug}/public` today)

| Field | Used for | Suggested home |
|---|---|---|
| `about_lead` (one sentence) and up to two `about` paragraphs | lead line and paragraphs | `schools`, or a school post module |
| `founded_year`, `instructor_count`, `students_trained` | fact column | `schools` (school-written, with an "updated" day) |
| `whatsapp` (plain number) | WhatsApp row (`wa.me`) | `schools` |
| `hours` (days, open, close, closed) | Hours and "Open now" | `schools` or a `school_hours` table |
| `address` (one line) | Contact card, Directions | `schools` |
| photo with caption and alt text | wide About photo | `school_files` already holds live photos (`schoolPhotos`); add `caption`, `alt`, a "cover" flag, and send them in `/public` |
| aerodrome `lat` and `lon` | a true map tile and exact Directions | `aerodromes` (the generator's list has only ident, name, town, province) |
| enquiry endpoint, e.g. `POST /api/ato/{slug}/enquiry` | "Ask about training" | new `school_enquiries` table, rate limit, school notification |

Already in the data: `about`, phone, email, website, bases (ICAO, name, town), courses (the form's course list) and the premium
`fleet` module (the Fleet fact adds up its counts). Until the coordinates exist the location tile is a drawn chart tile, not a map,
and Directions searches the aerodrome's name and town (or the address, once it exists).

## Optional write-review fields (20 Nov 2026, v2)
A member can add three optional facts to a review. Empty means not given and nothing is shown.
- **Courses** (`courses`, a list): one or more, up to six, picked from this school's own course list (the same list and the same
  Licences / Ratings / Other groups as the Courses card). A search box appears when the school lists more than eight.
- **When you trained**: `trained_from` and `trained_to` (`YYYY-MM`), or `trained_from` plus `still_training: true`. From is needed
  for To or Still; To is never before From; no month after this one (SAST). The older single `when` still reads as one month.
  The months use our own picker (`.ga-cal` in `shared/page.js`), never the browser's month input: a year with twelve months,
  shown in SA order (`Mar 2025`, figures in JetBrains Mono), opening in place under the row. Future months and months before
  From are greyed out. The value sits in a hidden input as `YYYY-MM`. The same picker does days (`12 Jan 2027`) with `data-cal="date"`.
- **Amount paid** (`cost`): whole rand, 0 to 9,999,999, JetBrains Mono with tabular numerals, commas as you type.
- Card: `reviews.j2` prints one quiet line, `PPL (A), Night rating · Mar 2025 – Jan 2026 · R 95,000` (or `… – present`),
  only for what was given. Anonymous reviews never show it (REV-038).
- Form: `template/parts/review_fields.j2` is an inert `<template id="ob-review-fields">`; the signed-in form clones it.
  `preview-only/preview-switch.js` does that for the member preview. Behaviour: `shared/page.js`. Styles: `shared/page.css`.
- Database: Codex's candidate (`codex/17-owner-decisions`, 0063) stores one `course_id` and one `trained_month`, and has no amount.
  It needs a `review_courses` table (review_id, course_id, up to six), `trained_from`, `trained_to`, `still_training` and
  `amount_paid_cents`. See the Notion page "Edit Premium — ATO page editing".

## Edit Premium sheet: dates and hours (20 Nov 2026)
- No browser date, time or month fields anywhere. **Next intake** uses our own day picker (`.ga-cal[data-cal="date"]`, the same
  component as the review months): `12 Jan 2027`, figures in JetBrains Mono, today or later (SAST), up to three years ahead, opening
  in place under the field. The value is a hidden `intake` input as `YYYY-MM-DD`.
- **Opening hours** are 24-hour lists in half hours from 05:00 to 23:00 (`08:00`, JetBrains Mono), or "–". Ticking Closed turns the
  day's two lists off. Field names stay `open-N`, `close-N`, `closed-N` (N = 0 for Monday to 6 for Sunday).
