From 83d3e3136f54e87e7e8a8d3104275186b38c3c10 Mon Sep 17 00:00:00 2001 From: Peter Stockings Date: Thu, 7 Dec 2023 17:49:19 +1100 Subject: [PATCH] Change logic for calendar month view so it always renders the same number of weeks (Sunday-Saturday) regardless of length of month, & what day of the week it started. This resolves the rerender when you switch between months with varying number of weeks --- utils.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/utils.py b/utils.py index 5b95263..cb6c347 100644 --- a/utils.py +++ b/utils.py @@ -222,13 +222,12 @@ def get_date_info(input_date, selected_view): # Business logic, should move above to a separate function if selected_view == 'month': - start = dict([(6, 0), (0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6)]) - start_date = first_day_of_month - \ - timedelta(days=start[first_day_of_month.weekday()]) + # Step 1: Find the first Sunday before or on the first day of the month + days_to_subtract = (first_day_of_month.weekday() + 1) % 7 + start_date = first_day_of_month - timedelta(days=days_to_subtract) - end = dict([(6, 6), (0, 5), (1, 4), (2, 3), (3, 2), (4, 1), (5, 0)]) - end_date = last_day_of_month + \ - timedelta(days=end[last_day_of_month.weekday()]) + # Step 2: Calculate the last day to display, based on the number of weeks + end_date = start_date + timedelta(days=6 * 7 - 1) return { 'next_date': next_month,