From ee6fc48c901ccfdae9a4c06557ef47ac77ab0e7a Mon Sep 17 00:00:00 2001 From: Benjamin Sigonneau Date: Sun, 15 Jan 2023 22:43:07 +0100 Subject: [PATCH] Display results only for the current month --- views/search_results.tpl | 7 ++++++- webapp.py | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/views/search_results.tpl b/views/search_results.tpl index ca3747e..0076e96 100644 --- a/views/search_results.tpl +++ b/views/search_results.tpl @@ -8,6 +8,11 @@

Prix du m³ de fioul pour Languidic

+

+ +
{{ displayed_month }}
+ +

@@ -19,7 +24,7 @@ - % for x in results: + % for x in month_results: diff --git a/webapp.py b/webapp.py index d15ca46..7425f80 100644 --- a/webapp.py +++ b/webapp.py @@ -1,7 +1,9 @@ +import datetime as dt import locale import os -from bottle import hook, request, route, run, static_file, view +from bottle import hook, redirect, request, route, run, static_file, view +from dateutil.relativedelta import * import models from fioul import get_today_prices_from_internet @@ -12,9 +14,31 @@ from models import Price @route("/") +def home(): + today = dt.date.today() + return redirect(f"/{today.year}/{today.month}") + + +@route("//") @view("search_results", template_settings={"noescape": True}) -def results_page(): +def results_page(year, month): results = Price.select().order_by(Price.date.desc()) + start = dt.date(year, month, day=1) + prev = start + relativedelta(months=-1) + next = start + relativedelta(months=+1) + print(f"debug upper date boundary: {start + relativedelta(months=1)}") + month_results = ( + Price.select() + .where( + Price.date >= start, + Price.date < next, + ) + .order_by(Price.date.desc()) + ) + prev_page = f"/{prev.year}/{prev.month}" + next_page = f"/{next.year}/{next.month}" + displayed_month = start.strftime("%B %Y") + print(f"year: {year}, month: {month}, displayed: {displayed_month}") current_500, current_1000 = get_today_prices_from_internet() day_classes = { 0: "monday", @@ -27,6 +51,10 @@ def results_page(): } return dict( results=results, + month_results=month_results, + prev_page=prev_page, + next_page=next_page, + displayed_month=displayed_month, day_classes=day_classes, current_500=current_500, current_1000=current_1000,
{{current_1000}} {{current_500}}
{{x.date.strftime('%A')}} {{x.date.strftime('%d %b %Y')}}