Fix for bug where when on the first of the month(eg today 1/5/23) and your on month view for a person then clicking previous date(Next to eg. May, 2023) would take you to March, also did a slight refactor

This commit is contained in:
Peter Stockings
2023-05-01 21:20:00 +10:00
parent bdf1680b19
commit 6474741f1e
2 changed files with 71 additions and 21 deletions

28
app.py
View File

@@ -5,7 +5,7 @@ from flask import Flask, render_template, redirect, request, url_for
import jinja_partials
from decorators import validate_person, validate_topset, validate_workout
from db import DataBase
from utils import flatten, get_people_and_exercise_rep_maxes, convert_str_to_date, get_earliest_and_latest_workout_date, filter_workout_topsets, get_exercise_ids_from_workouts, first_and_last_visible_days_in_month
from utils import flatten, get_date_info, get_people_and_exercise_rep_maxes, convert_str_to_date, get_earliest_and_latest_workout_date, filter_workout_topsets, get_exercise_ids_from_workouts, first_and_last_visible_days_in_month
from flask_htmx import HTMX
import minify_html
from urllib.parse import urlparse, unquote, quote
@@ -150,27 +150,13 @@ def get_calendar(person_id):
if selected_view == 'all':
return redirect(url_for('get_person', person_id=person_id))
next_date = selected_date + (timedelta(
365/12) if selected_view == 'month' else timedelta(365))
previous_date = selected_date + (timedelta(
-365/12) if selected_view == 'month' else timedelta(-365))
# selected_view = month | year | all
date_info = get_date_info(selected_date, selected_view)
first_date_of_view = selected_date.replace(
day=1) if selected_view == 'month' else selected_date.replace(month=1, day=1)
last_date_of_view = first_date_of_view + \
(timedelta(365/12) if selected_view == 'month' else timedelta(365))
start = dict([(6, 0), (0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6)])
start_date = first_date_of_view - \
timedelta(days=start[first_date_of_view.weekday()])
end = dict([(6, 6), (0, 5), (1, 4), (2, 3), (3, 2), (4, 1), (5, 0)])
end_date = last_date_of_view + \
timedelta(days=end[last_date_of_view.weekday()])
if selected_view == 'year':
start_date = first_date_of_view
end_date = last_date_of_view
next_date = date_info['next_date']
previous_date = date_info['previous_date']
start_date = date_info['start_date']
end_date = date_info['end_date']
if htmx:
return render_template('partials/page/calendar.html',

View File

@@ -183,3 +183,67 @@ def flatten(lst):
else:
result.append(item)
return result
def get_date_info(input_date, selected_view):
if selected_view not in ['month', 'year']:
raise ValueError(
'selected_view must be either "month" or "year"')
# First day of the month
first_day_of_month = input_date.replace(day=1)
# Last day of the month
if input_date.month == 12:
last_day_of_month = input_date.replace(
year=input_date.year+1, month=1, day=1) - timedelta(days=1)
else:
last_day_of_month = input_date.replace(
month=input_date.month+1, day=1) - timedelta(days=1)
# First and last day of the year
first_day_of_year = input_date.replace(month=1, day=1)
last_day_of_year = input_date.replace(
year=input_date.year+1, month=1, day=1) - timedelta(days=1)
# Next/previous week
next_week = input_date + timedelta(weeks=1)
prev_week = input_date - timedelta(weeks=1)
# Next/previous month
year, month = divmod(input_date.year * 12 + input_date.month, 12)
next_month = date(year, month + 1, 1)
prev_month_last_day = first_day_of_month - timedelta(days=1)
prev_month = prev_month_last_day.replace(day=1)
# Next/previous year
next_year = input_date.replace(year=input_date.year+1)
prev_year = input_date.replace(year=input_date.year-1)
# 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()])
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()])
return {
'next_date': next_month,
'previous_date': prev_month,
'first_date_of_view': first_day_of_month,
'last_date_of_view': last_day_of_month,
'start_date': start_date,
'end_date': end_date,
}
elif selected_view == 'year':
return {
'next_date': next_year,
'previous_date': prev_year,
'first_date_of_view': first_day_of_year,
'last_date_of_view': last_day_of_year,
'start_date': first_day_of_year,
'end_date': last_day_of_year,
}