Add MOT/insurance renewal hints, table service card, and favicon
- Renewal tracking: compute due/overdue status from the most recent mot/insurance event (renewal_months, renewal_due_soon_days settings), surfaced on the service card and dashboard. - Service-status card metrics now render as a two-column label/value table with row separators instead of wrapping auto-fill grid cells. - Add a bicycle favicon (favicon.svg) to base + login templates. Co-Authored-By: opencode <opencode@users.noreply.github.com>
This commit is contained in:
@@ -1,12 +1,68 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlite3
|
||||
from calendar import monthrange
|
||||
from dataclasses import dataclass
|
||||
from datetime import date
|
||||
|
||||
from . import db
|
||||
from .config import get_settings
|
||||
|
||||
|
||||
@dataclass
|
||||
class RenewalDue:
|
||||
"""MOT / insurance renewal snapshot for one bike.
|
||||
|
||||
renewal_date: the date the logged MOT/insurance took effect.
|
||||
due_date: when it falls due (renewal_date + renewal_months).
|
||||
days_until: due_date - today (negative when overdue).
|
||||
status:
|
||||
"no-data" -> no MOT/insurance event logged
|
||||
"ok" -> more than the lead time from due
|
||||
"due-soon" -> within the lead time of the due date
|
||||
"overdue" -> past the due date
|
||||
"""
|
||||
|
||||
renewal_date: date | None
|
||||
due_date: date | None
|
||||
days_until: int | None
|
||||
status: str
|
||||
|
||||
|
||||
def _add_months(d: date, months: int) -> date:
|
||||
"""Clamp the day to the last valid day of the target month (e.g. Feb 29)."""
|
||||
total = d.month - 1 + months
|
||||
year = d.year + total // 12
|
||||
month = total % 12 + 1
|
||||
day = min(d.day, monthrange(year, month)[1])
|
||||
return date(year, month, day)
|
||||
|
||||
|
||||
def compute_renewal_due(
|
||||
conn: sqlite3.Connection, bike: db.Bike, type: str, today: date | None = None
|
||||
) -> RenewalDue:
|
||||
"""Compute when the most recent `type` (mot/insurance) event falls due."""
|
||||
if today is None:
|
||||
today = date.today()
|
||||
settings = get_settings()
|
||||
|
||||
last = db.latest_event_of_type(conn, bike.id, type)
|
||||
if last is None:
|
||||
return RenewalDue(None, None, None, "no-data")
|
||||
|
||||
renewal_date = last.event_date
|
||||
due_date = _add_months(renewal_date, settings.renewal_months)
|
||||
days_until = (due_date - today).days
|
||||
if days_until < 0:
|
||||
status = "overdue"
|
||||
elif days_until <= settings.renewal_due_soon_days:
|
||||
status = "due-soon"
|
||||
else:
|
||||
status = "ok"
|
||||
|
||||
return RenewalDue(renewal_date, due_date, days_until, status)
|
||||
|
||||
|
||||
@dataclass
|
||||
class ServiceDue:
|
||||
"""Service-due snapshot for one bike.
|
||||
@@ -65,6 +121,8 @@ def compute_service_due(conn: sqlite3.Connection, bike: db.Bike) -> ServiceDue:
|
||||
class BikeSummary:
|
||||
bike: db.Bike
|
||||
due: ServiceDue
|
||||
mot: RenewalDue
|
||||
insurance: RenewalDue
|
||||
event_count: int
|
||||
total_cost: int
|
||||
|
||||
@@ -73,6 +131,8 @@ def summarize_bike(conn: sqlite3.Connection, bike: db.Bike) -> BikeSummary:
|
||||
return BikeSummary(
|
||||
bike=bike,
|
||||
due=compute_service_due(conn, bike),
|
||||
mot=compute_renewal_due(conn, bike, "mot"),
|
||||
insurance=compute_renewal_due(conn, bike, "insurance"),
|
||||
event_count=db.count_events(conn, bike.id),
|
||||
total_cost=db.sum_cost(conn, bike.id),
|
||||
)
|
||||
@@ -113,3 +173,22 @@ def status_text(due: ServiceDue) -> str:
|
||||
if due.status == "ok":
|
||||
return f"due in {due.miles_until_due:,} mi"
|
||||
return f"overdue by {abs(due.miles_until_due):,} mi"
|
||||
|
||||
|
||||
def renewal_class(r: RenewalDue) -> str:
|
||||
return {
|
||||
"overdue": "badge-overdue",
|
||||
"due-soon": "badge-due-soon",
|
||||
"ok": "badge-ok",
|
||||
"no-data": "badge-muted",
|
||||
}[r.status]
|
||||
|
||||
|
||||
def renewal_text(r: RenewalDue) -> str:
|
||||
if r.status == "no-data":
|
||||
return "not logged"
|
||||
if r.status == "overdue":
|
||||
return f"overdue by {abs(r.days_until)} days"
|
||||
if r.status == "due-soon":
|
||||
return f"due in {r.days_until} days"
|
||||
return f"due in {r.days_until} days"
|
||||
|
||||
Reference in New Issue
Block a user