feat: Flask routes and Jinja2/HTMX/Tailwind templates
This commit is contained in:
@@ -1,3 +1,64 @@
|
||||
from flask import Blueprint
|
||||
from flask import Blueprint, render_template
|
||||
from datetime import date, timedelta
|
||||
import sqlalchemy as sa
|
||||
from app import db
|
||||
from app.models import WorkEntry
|
||||
from app.business.time_calc import minutes_to_str, work_minutes_reference
|
||||
from app.business.travel_calc import compute_km_for_entry, compute_co2_grams
|
||||
from app.business.leave_calc import compute_leave_used, get_or_create_balance
|
||||
from app.config_loader import get_vehicles, get_journeys
|
||||
|
||||
bp = Blueprint("dashboard", __name__)
|
||||
|
||||
|
||||
@bp.route("/")
|
||||
def index():
|
||||
today = date.today()
|
||||
year = today.year
|
||||
|
||||
monday = today - timedelta(days=today.weekday())
|
||||
sunday = monday + timedelta(days=6)
|
||||
week_entries = db.session.scalars(
|
||||
sa.select(WorkEntry).where(WorkEntry.date.between(monday, sunday))
|
||||
).all()
|
||||
|
||||
week_actual = sum(e.total_minutes() for e in week_entries)
|
||||
week_ref = sum(work_minutes_reference(e.day_type) for e in week_entries)
|
||||
week_balance = week_actual - week_ref
|
||||
|
||||
month_start = today.replace(day=1)
|
||||
month_entries = db.session.scalars(
|
||||
sa.select(WorkEntry).where(WorkEntry.date.between(month_start, today))
|
||||
).all()
|
||||
|
||||
vehicles = get_vehicles()
|
||||
journeys = get_journeys()
|
||||
|
||||
month_km = {}
|
||||
month_co2 = 0.0
|
||||
for entry in month_entries:
|
||||
km = compute_km_for_entry(entry.journey_profile_id, journeys)
|
||||
for v, d in km.items():
|
||||
month_km[v] = month_km.get(v, 0) + d
|
||||
month_co2 += compute_co2_grams(km, vehicles)
|
||||
|
||||
balance = get_or_create_balance(year)
|
||||
used = compute_leave_used(year)
|
||||
|
||||
today_entry = db.session.scalar(
|
||||
sa.select(WorkEntry).where(WorkEntry.date == today)
|
||||
)
|
||||
|
||||
return render_template(
|
||||
"dashboard.html",
|
||||
today=today,
|
||||
today_entry=today_entry,
|
||||
journeys=journeys,
|
||||
week_actual_str=minutes_to_str(week_actual),
|
||||
week_balance=week_balance,
|
||||
week_balance_str=minutes_to_str(abs(week_balance)),
|
||||
month_km=month_km,
|
||||
month_co2_kg=round(month_co2 / 1000, 2),
|
||||
balance=balance,
|
||||
used=used,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user