Funnel FuturistFunnel Futurist
← Return to C17
0%
── OPTIONAL READING · AIOS DEEP OPERATOR CERTIFICATION ──

job_board.py

About 2 min read

Copy the source below into a file with this exact name.

"""Render the fictional repair shop's job board as one static HTML page.

This is the exercise's starting point, and it is deliberately plain: every job is printed in full, the explanations
sit on top, and nothing is grouped visually. The data and the contract are fine. The presentation is the problem
you are asked to fix.

Usage: python3 job_board.py [jobs.json] [index.html]

The contract the tests in test_job_board.py hold (keep it while you redesign):
- every job appears exactly once, as an element with id="job-<id>", data-job-id and data-status;
- each status count sits in an element with data-count="<status>";
- a job that needs the owner links to its quote in the external quote tool with data-external="quote", and the page
  never offers an in-page "Approve" control, because approval happens in that tool;
- a blocked job shows the word "Blocked" and its reason in an element with data-blocker;
- each mechanic has an element with data-mechanic and data-open-count (jobs not yet ready);
- an empty snapshot shows an element with data-empty;
- the snapshot time sits in an element with data-snapshot.
"""
import html
import json
import sys
from pathlib import Path

STATUSES = [
    ("needs_owner", "Needs your decision"),
    ("blocked", "Blocked"),
    ("waiting_parts", "Waiting on parts"),
    ("in_repair", "In repair"),
    ("ready", "Ready for pickup"),
]
LABEL = dict(STATUSES)


def esc(value):
    return html.escape(str(value), quote=True)


def job_html(job, quote_tool):
    parts = [
        f'<div class="job" id="job-{job["id"]}" data-job-id="{job["id"]}" data-status="{esc(job["status"])}">',
        f'<p><b>Job {job["id"]}: {esc(job["title"])}</b></p>',
        f'<p>Status: {esc(LABEL[job["status"]])}. Bike: {esc(job["bike"])}. Customer: {esc(job["customer"])}. '
        f'Mechanic: {esc(job["mechanic"])}. Due: {esc(job["due"])}.</p>',
        f'<p>Next step: {esc(job["next"])}</p>',
    ]
    if job["status"] == "blocked":
        parts.append(f'<p>Blocked because: <span data-blocker>{esc(job["blocker"])}</span></p>')
    if job.get("quote_url"):
        parts.append(f'<p>The owner decides in {esc(quote_tool)}: <a data-external="quote" href="{esc(job["quote_url"])}">'
                     f'Open quote in {esc(quote_tool)}</a></p>')
    parts.append(f'<p>Notes: {esc(job["notes"])}</p></div>')
    return "\n".join(parts)


def render(data):
    jobs = data["jobs"]
    counts = {key: sum(1 for j in jobs if j["status"] == key) for key, _ in STATUSES}
    out = [
        f"<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n<title>{esc(data['shop'])} job board</title>",
        "<meta charset=\"UTF-8\">",
        "<meta content=\"initial-scale=1, width=device-width\" name=\"viewport\">",
        "<style>body{font-family:serif;margin:16px}.job{border-top:1px solid #999;padding:4px 0}</style>",
        "</head><body>",
        f"<h1>{esc(data['shop'])}: job board</h1>",
        "<p>This page lists every job in the workshop. Each job shows its status, bike, customer, mechanic, due date, "
        "next step and notes. Jobs that need the owner's decision link to the quote tool, where the owner approves or "
        "declines the quote. Blocked jobs show why they are blocked. Ready jobs are waiting for the customer to "
        "collect. The mechanics section lists how many open jobs each mechanic holds.</p>",
        f"<p>Snapshot: <span data-snapshot>{esc(data['snapshot'])}</span></p>",
        "<p><a href=\"#queue\">Queue</a> | <a href=\"#mechanics\">Mechanics</a></p>",
        "<h2 id=\"queue\">Queue</h2>",
        "<p>Counts: " + "; ".join(
            f'{esc(label)}: <span data-count="{key}">{counts[key]}</span>' for key, label in STATUSES) + "</p>",
    ]
    if not jobs:
        out.append("<p data-empty>No jobs in this snapshot.</p>")
    for key, label in STATUSES:
        group = [j for j in jobs if j["status"] == key]
        if group:
            out.append(f"<h3>{esc(label)}</h3>")
            out.extend(job_html(j, data["quote_tool"]) for j in group)
    out.append("<h2 id=\"mechanics\">Mechanics</h2>")
    for name in data["mechanics"]:
        open_jobs = [j for j in jobs if j["mechanic"] == name and j["status"] != "ready"]
        out.append(f'<p data-mechanic="{esc(name)}" data-open-count="{len(open_jobs)}">{esc(name)} has '
                   f'{len(open_jobs)} open jobs: '
                   + (", ".join(f'<a href="#job-{j["id"]}">Job {j["id"]}</a>' for j in open_jobs) or "none") + ".</p>")
    out.append("</body></html>")
    return "\n".join(out)


def main(argv):
    here = Path(__file__).resolve().parent
    source = Path(argv[1]) if len(argv) > 1 else here / "jobs.json"
    target = Path(argv[2]) if len(argv) > 2 else here / "index.html"
    target.write_text(render(json.loads(source.read_text())), encoding="utf-8")
    print(target)


if __name__ == "__main__":
    main(sys.argv)