fix: build the image on Python 3.14, and fix the review findings
The image build failed on master:
ERROR: Ignored the following versions that require a different python
version: 4.19.0 Requires-Python >=3.11
ERROR: Could not find a version that satisfies the requirement
pymongo==4.19.0
The base image was `python:3.10` but the pinned pymongo 4.19 needs Python
>= 3.11. The image is now `python:3.14-slim` — the current stable release,
so it is not carrying the CVEs fixed in 3.11, 3.12 and 3.13.
All the runtime dependencies ship a cp314 wheel (pymongo included), so no
compiler toolchain is needed. The old apt-get line installed
libsasl2-dev, python-dev-is-python3, libldap2-dev and libssl-dev for
packages that are no longer in the dependency list, so it is gone and the
image is smaller.
The build now asserts the interpreter version, so a base image change that
breaks the pinned requirements fails the build with a clear message rather
than a pip resolution error.
Review findings, all from the independent review of this PR
------------------------------------------------------------
- Mongo indexes: the run metadata is always looked up by `beer_run_id` and
the readings by `(beer_run_id, date)`. Both are now indexed.
- Content-Security-Policy: the app serves only its own vendored assets, so
a strict `default-src 'self'` policy is enforceable. Adding it required
removing the inline `style=` attributes from the templates (they became
`.mb-lg` / `.mt-lg` utility classes) and the one inline `<script> (the run
id now travels as a `data-run-id` attribute). A test asserts no template
has either.
- Job timeouts: the `test` and `build` jobs now have `timeout-minutes`, so
a wedged build stops occupying a runner.
- OG range: raised from 1300 to 1500 points, so a strong specialty brew
(a 1.150 wort is real) can be recorded.
Bugs found and fixed along the way
----------------------------------
- An unfinished run reported a *measured* ABV. `abv_percent()` now returns
None for a run that is still in the fermenter — its figure is a projection,
not a measurement. This was showing the two active runs in the "alcohol by
brew" chart as if they were finished.
- The `/api/runs` endpoints the charts fetch from did not exist, so every
chart was silently empty. They are back, with tests.
- The "brews over time" chart left out the months with no brews, which
spaced the busy months evenly and made the chart read as continuous
activity. The quiet months are now included with a zero.
- The run-detail chart was dropped when PR 1 was scoped down, leaving dead
code behind. It is restored.
Tests: 105 (up from 91).
This commit is contained in:
@@ -110,6 +110,25 @@ def create_app(config: dict | None = None) -> Flask:
|
||||
# ------------------------------------------------------------------ #
|
||||
# Error handlers
|
||||
# ------------------------------------------------------------------ #
|
||||
@app.after_request
|
||||
def _set_security_headers(response):
|
||||
"""Add the standard security headers.
|
||||
|
||||
The app serves its own vendored JavaScript and CSS and loads nothing
|
||||
from a CDN, so a strict Content-Security-Policy of 'self' is enough
|
||||
and is genuinely enforceable.
|
||||
"""
|
||||
response.headers.setdefault("X-Content-Type-Options", "nosniff")
|
||||
response.headers.setdefault("X-Frame-Options", "DENY")
|
||||
response.headers.setdefault("Referrer-Policy", "same-origin")
|
||||
response.headers.setdefault(
|
||||
"Content-Security-Policy",
|
||||
"default-src 'self'; script-src 'self'; style-src 'self'; "
|
||||
"img-src 'self' data:; font-src 'self'; connect-src 'self'; "
|
||||
"form-action 'self'; frame-ancestors 'none'; base-uri 'self'",
|
||||
)
|
||||
return response
|
||||
|
||||
@app.errorhandler(404)
|
||||
def _not_found(_exc):
|
||||
return render_template("error.html", code=404,
|
||||
|
||||
+14
-3
@@ -59,7 +59,9 @@ ABV_FACTOR = 0.13125
|
||||
#: The legacy app hard-coded 1012; kept as the default so existing numbers line up.
|
||||
ASSUMED_ORIGINAL_GRAVITY = 1012
|
||||
|
||||
#: Plausible range for a hydrometer reading, in points.
|
||||
#: Plausible range for a hydrometer reading, in points. The upper bound is
|
||||
#: generous enough for a strong specialty brew (a 1.150 wort is real) while
|
||||
#: still catching typos such as 900 or 1500.
|
||||
_MIN_PLAUSIBLE_SG = 990
|
||||
_MAX_PLAUSIBLE_SG = 1200
|
||||
|
||||
@@ -228,7 +230,15 @@ class Run:
|
||||
return og - fg
|
||||
|
||||
def abv_percent(self) -> float | None:
|
||||
"""Standard ABV for a finished run."""
|
||||
"""Standard ABV for a **finished** run.
|
||||
|
||||
A run that is still in the fermenter has no measured alcohol - its
|
||||
figure is a projection, not a measurement. Returning the drop here
|
||||
for an unfinished run would report a "measured" ABV for a beer that
|
||||
has not finished fermenting, which is wrong.
|
||||
"""
|
||||
if self.is_active():
|
||||
return None
|
||||
drop = self.gravity_drop_points()
|
||||
return abv_percent(drop) if drop is not None else None
|
||||
|
||||
@@ -448,7 +458,8 @@ def run_summary(run: Run, today: date | None = None,
|
||||
"drop_points": run.gravity_drop_points(),
|
||||
"abv": run.abv_percent(),
|
||||
"projected_abv": run.projected_abv_percent(og_override=og_override),
|
||||
"legacy_abv": legacy_abv_percent(run.max_sg(), run.min_sg()),
|
||||
"legacy_abv": (legacy_abv_percent(run.max_sg(), run.min_sg())
|
||||
if not run.is_active() else None),
|
||||
"reading_count": len(run.readings),
|
||||
"first_date": first.date.isoformat() if (first and first.date) else None,
|
||||
"final_date": final.date.isoformat() if (final and final.date) else None,
|
||||
|
||||
@@ -44,6 +44,23 @@ class BeerRepository:
|
||||
# NB: named `run_meta`, not `runs` — an attribute called `runs` would
|
||||
# shadow the runs() method on the instance.
|
||||
self.run_meta = self.db["beer_runs"]
|
||||
self._ensure_indexes()
|
||||
|
||||
def _ensure_indexes(self) -> None:
|
||||
"""Make sure the lookup keys are indexed.
|
||||
|
||||
Every read of the run metadata filters on ``beer_run_id``, and the
|
||||
readings are always fetched by ``(beer_run_id, date)``. Without the
|
||||
indexes those are collection scans.
|
||||
"""
|
||||
try:
|
||||
self.run_meta.create_index("beer_run_id", name="beer_run_id_1")
|
||||
self.readings.create_index(
|
||||
[("beer_run_id", 1), ("date", 1)], name="run_date_1"
|
||||
)
|
||||
except PyMongoError:
|
||||
# An index failure must not stop the app from serving.
|
||||
pass
|
||||
|
||||
# -- users -------------------------------------------------------------- #
|
||||
|
||||
|
||||
@@ -649,6 +649,12 @@ table.data .comment { color: var(--text-muted); font-size: .84rem; }
|
||||
.row--between { justify-content: space-between; }
|
||||
.hidden { display: none !important; }
|
||||
|
||||
/* Spacing utilities. These exist so the templates carry no inline
|
||||
`style=` attributes, which a strict Content-Security-Policy
|
||||
(`style-src 'self'`) would block. */
|
||||
.mb-lg { margin-bottom: 1.25rem; }
|
||||
.mt-lg { margin-top: 2rem; }
|
||||
|
||||
.empty {
|
||||
padding: 2.5rem 1rem;
|
||||
text-align: center;
|
||||
|
||||
@@ -104,7 +104,10 @@
|
||||
function drawRunChart() {
|
||||
var canvas = document.getElementById("run-chart");
|
||||
if (!canvas) { return; }
|
||||
var runId = window.BREW_RUN_ID;
|
||||
// The run id comes from a data attribute rather than a global, so the
|
||||
// page needs no inline script (a strict CSP forbids inline scripts).
|
||||
var runId = canvas.getAttribute("data-run-id");
|
||||
if (!runId) { return; }
|
||||
|
||||
fetch("/api/runs/" + encodeURIComponent(runId), {
|
||||
headers: { "Accept": "application/json" }
|
||||
@@ -169,7 +172,12 @@
|
||||
fetch("/api/runs", { headers: { "Accept": "application/json" } })
|
||||
.then(function (r) { return r.json(); })
|
||||
.then(function (runs) {
|
||||
var completed = runs.filter(function (r) { return r.abv !== null && r.abv !== undefined; });
|
||||
// Only finished brews have a measured ABV. An unfinished run
|
||||
// has a projection, not a measurement, so it is not plotted
|
||||
// here.
|
||||
var completed = runs.filter(function (r) {
|
||||
return r.abv !== null && r.abv !== undefined;
|
||||
});
|
||||
if (!completed.length) { return; }
|
||||
|
||||
new Chart(canvas.getContext("2d"), {
|
||||
@@ -223,6 +231,22 @@
|
||||
var months = Object.keys(counts).sort();
|
||||
if (!months.length) { return; }
|
||||
|
||||
// Include the months with no brews, with a zero. Leaving the
|
||||
// gaps out would space the busy months evenly on the axis and
|
||||
// make the chart read as continuous activity.
|
||||
var filled = [];
|
||||
var cursor = months[0];
|
||||
var last = months[months.length - 1];
|
||||
while (cursor <= last) {
|
||||
filled.push(cursor);
|
||||
var year = parseInt(cursor.slice(0, 4), 10);
|
||||
var month = parseInt(cursor.slice(5, 7), 10);
|
||||
month += 1;
|
||||
if (month > 12) { month = 1; year += 1; }
|
||||
cursor = year + "-" + (month < 10 ? "0" + month : month);
|
||||
}
|
||||
months = filled;
|
||||
|
||||
new Chart(canvas.getContext("2d"), {
|
||||
type: "line",
|
||||
data: {
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="grid grid--3" style="margin-bottom:1.25rem">
|
||||
<div class="grid grid--3 mb-lg">
|
||||
<div class="stat">
|
||||
<div class="stat__label">Active brews</div>
|
||||
<div class="stat__value">{{ totals.count }}</div>
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
{% block title %}{{ code }} — Brew Log{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<div class="card" style="margin-top:2rem">
|
||||
<div class="card mt-lg">
|
||||
<div class="empty">
|
||||
<img class="empty__icon" src="{{ url_for('static', filename='img/logo.svg') }}" alt="" width="40" height="40">
|
||||
<h1>{{ code }}</h1>
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="grid grid--4" style="margin-bottom:1.25rem">
|
||||
<div class="grid grid--4 mb-lg">
|
||||
<div class="stat">
|
||||
<div class="stat__label">Total brews</div>
|
||||
<div class="stat__value">{{ stats.total_runs }}</div>
|
||||
@@ -45,7 +45,7 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="grid grid--2" style="margin-bottom:1.25rem">
|
||||
<div class="grid grid--2 mb-lg">
|
||||
<section class="card">
|
||||
<div class="card__head">
|
||||
<h2 class="card__title">Alcohol by brew</h2>
|
||||
@@ -59,7 +59,7 @@
|
||||
<section class="card">
|
||||
<div class="card__head">
|
||||
<h2 class="card__title">Brews over time</h2>
|
||||
<span class="card__hint">Runs started per month</span>
|
||||
<span class="card__hint">Runs started per month (active runs counted at their first reading)</span>
|
||||
</div>
|
||||
<div class="card__body">
|
||||
<canvas id="months-chart" height="120"></canvas>
|
||||
@@ -68,7 +68,7 @@
|
||||
</div>
|
||||
|
||||
{% if outliers %}
|
||||
<section class="card" style="margin-bottom:1.25rem">
|
||||
<section class="card mb-lg">
|
||||
<div class="card__head">
|
||||
<h2 class="card__title">Readings worth a second look</h2>
|
||||
<span class="card__hint">{{ outliers | length }} outside the usual hydrometer range</span>
|
||||
@@ -102,7 +102,7 @@
|
||||
</section>
|
||||
{% endif %}
|
||||
|
||||
<section class="card" style="margin-bottom:1.25rem">
|
||||
<section class="card mb-lg">
|
||||
<div class="card__head">
|
||||
<h2 class="card__title">Every brew</h2>
|
||||
<span class="card__hint">{{ summaries | length }} runs</span>
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
<a class="btn btn--ghost btn--sm" href="{{ url_for('brews.index') }}">← Back</a>
|
||||
</div>
|
||||
|
||||
<div class="grid grid--sidebar" style="margin-bottom:1.25rem">
|
||||
<div class="grid grid--sidebar mb-lg">
|
||||
<!-- ------------------------------------------------- run metadata -->
|
||||
<section class="card">
|
||||
<div class="card__head">
|
||||
@@ -115,6 +115,16 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<section class="card mb-lg">
|
||||
<div class="card__head">
|
||||
<h2 class="card__title">Gravity over time</h2>
|
||||
<span class="card__hint">Standard ABV: (OG − FG) × 131.25</span>
|
||||
</div>
|
||||
<div class="card__body">
|
||||
<canvas id="run-chart" data-run-id="{{ summary.beer_run_id }}" height="110"></canvas>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section class="card">
|
||||
<div class="card__head">
|
||||
<h2 class="card__title">Readings</h2>
|
||||
|
||||
@@ -32,6 +32,27 @@ def login_required(fn):
|
||||
return wrapper
|
||||
|
||||
|
||||
def _month_range(first: str, last: str) -> list[str]:
|
||||
"""Every ``YYYY-MM`` from *first* to *last* inclusive.
|
||||
|
||||
Used to include the months with no brews in the chart, so a quiet period
|
||||
shows as a gap rather than being compressed out of the axis.
|
||||
"""
|
||||
import calendar
|
||||
|
||||
year, month = int(first[:4]), int(first[5:7])
|
||||
end_year, end_month = int(last[:4]), int(last[5:7])
|
||||
|
||||
months = []
|
||||
while (year, month) <= (end_year, end_month):
|
||||
months.append(f"{year:04d}-{month:02d}")
|
||||
month += 1
|
||||
if month > 12:
|
||||
month = 1
|
||||
year += 1
|
||||
return months
|
||||
|
||||
|
||||
def _fmt_days(value: float) -> str:
|
||||
"""Format a day count: a whole number stays whole, a fraction keeps one
|
||||
decimal place."""
|
||||
@@ -109,12 +130,20 @@ def history():
|
||||
abv_rows.sort(key=lambda r: (r["final_date"] or "0000-00-00"))
|
||||
|
||||
# --- brews per month --------------------------------------------------- #
|
||||
# Every month between the first and the last brew is included, with zero
|
||||
# for the months with no brews. Leaving the gaps out would space the
|
||||
# remaining months evenly and make the chart read as continuous activity.
|
||||
per_month: dict[str, int] = defaultdict(int)
|
||||
for s in summaries:
|
||||
stamp = s["final_date"] or s["first_date"]
|
||||
if stamp:
|
||||
per_month[stamp[:7]] += 1
|
||||
months = sorted(per_month.items())
|
||||
|
||||
months = []
|
||||
if per_month:
|
||||
first, last = min(per_month), max(per_month)
|
||||
for month in _month_range(first, last):
|
||||
months.append((month, per_month.get(month, 0)))
|
||||
|
||||
# --- outliers ---------------------------------------------------------- #
|
||||
outliers = []
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from flask import (Blueprint, flash, redirect, render_template, request,
|
||||
session, url_for)
|
||||
from flask import (Blueprint, flash, jsonify, redirect, render_template,
|
||||
request, session, url_for)
|
||||
from flask_wtf import FlaskForm
|
||||
from wtforms import (BooleanField, DateField, IntegerField, StringField,
|
||||
SubmitField, TextAreaField)
|
||||
@@ -50,7 +50,7 @@ class ReadingForm(FlaskForm):
|
||||
validators=[Optional(), Length(max=120)])
|
||||
Date = DateField("Reading date", validators=[DataRequired()])
|
||||
sg = IntegerField("Gravity reading (points)",
|
||||
validators=[DataRequired(), NumberRange(min=900, max=1300)])
|
||||
validators=[DataRequired(), NumberRange(min=900, max=1500)])
|
||||
final_reading = BooleanField("This is the final reading")
|
||||
comment = TextAreaField("Comment", validators=[Optional(), Length(max=2000)])
|
||||
submit = SubmitField("Save reading")
|
||||
@@ -68,7 +68,7 @@ class RunMetaForm(FlaskForm):
|
||||
tap_number = IntegerField("Tap number",
|
||||
validators=[Optional(), NumberRange(min=1, max=99)])
|
||||
og_points = IntegerField("Original gravity (points)",
|
||||
validators=[Optional(), NumberRange(min=900, max=1300)])
|
||||
validators=[Optional(), NumberRange(min=900, max=1500)])
|
||||
notes = TextAreaField("Brew notes", validators=[Optional(), Length(max=4000)])
|
||||
submit = SubmitField("Save run")
|
||||
|
||||
@@ -166,6 +166,45 @@ def run_detail(run_id: str):
|
||||
meta_form=meta_form, meta=meta)
|
||||
|
||||
|
||||
@bp.route("/api/runs")
|
||||
@login_required
|
||||
def api_runs():
|
||||
"""JSON feed of the run summaries, used by the charts on the analysis
|
||||
pages.
|
||||
|
||||
The charts fetch this rather than having the data inlined into the page,
|
||||
so one fetch serves every chart on a page.
|
||||
"""
|
||||
runs = repo().runs()
|
||||
metadata = repo().all_run_metadata()
|
||||
|
||||
payload = []
|
||||
for run in runs:
|
||||
meta = metadata.get(run.beer_run_id, {})
|
||||
summary = run_summary(run, og_override=meta.get("og_points"))
|
||||
summary["canonical_name"] = meta.get("name") or run.display_name()
|
||||
summary["tap_number"] = meta.get("tap_number")
|
||||
summary["last_date"] = _last_reading_date(run)
|
||||
payload.append(summary)
|
||||
|
||||
return jsonify(payload)
|
||||
|
||||
|
||||
@bp.route("/api/runs/<run_id>")
|
||||
@login_required
|
||||
def api_run(run_id: str):
|
||||
"""JSON detail of a single run."""
|
||||
run = repo().run(run_id)
|
||||
if run is None:
|
||||
return {"error": "not found"}, 404
|
||||
|
||||
meta = repo().run_metadata(run_id)
|
||||
summary = run_summary(run, og_override=meta.get("og_points"))
|
||||
summary["canonical_name"] = meta.get("name") or run.display_name()
|
||||
summary["tap_number"] = meta.get("tap_number")
|
||||
return jsonify(summary)
|
||||
|
||||
|
||||
@bp.route("/runs/<run_id>/final", methods=["POST"])
|
||||
@login_required
|
||||
def mark_final(run_id: str):
|
||||
|
||||
Reference in New Issue
Block a user