Show workouts on monthly calendar, not yet interactive (Will make it so you can change month and select workout to view on click)

This commit is contained in:
Peter Stockings
2023-10-13 16:56:23 +11:00
parent 8386789a2a
commit c7598f970d
3 changed files with 115 additions and 235 deletions

60
app.py
View File

@@ -11,6 +11,7 @@ import os
import sparklines
from dateutil.parser import isoparse
import humanize
from dateutil.relativedelta import relativedelta
app = Flask(__name__)
@@ -326,6 +327,62 @@ def render_users_and_workouts():
duration_sparkline = sparklines.sparklines(
[int(w['duration_minutes']) for w in workouts])[0]
# Generate a monthly calendar view of the workouts
def get_month_bounds(dt):
# First day of the month
first_day_of_month = dt.replace(day=1)
# Last day of the month
next_month = first_day_of_month + relativedelta(months=1)
last_day_of_month = next_month - timedelta(days=1)
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 start_date, end_date
def date_range(start_date, end_date):
current_date = start_date
while current_date <= end_date:
yield current_date
current_date += timedelta(days=1)
def get_workout_for_date(workouts, date):
for w in workouts:
if w['start_time_date'].date() == date:
return w
return None
current_date = datetime.now().date()
start_date, end_date = get_month_bounds(current_date)
monthly_workouts = [w for w in workouts if start_date <=
w['start_time_date'].date() <= end_date]
days_of_month = [
{
'date': single_date,
'day_of_month': single_date.day,
'is_workout': bool(workout),
'workout': workout if workout else None,
'is_current_date': single_date == current_date,
'is_current_month': single_date.month == current_date.month and single_date.year == current_date.year
}
for single_date in date_range(start_date, end_date)
for workout in [get_workout_for_date(monthly_workouts, single_date)]
]
calendar_month = {
'month_year': current_date.strftime('%B, %Y'),
'days_of_month': days_of_month
}
user_data = {
'id': user.id,
'name': user.name,
@@ -336,7 +393,8 @@ def render_users_and_workouts():
'duration_by_week': duration_by_week,
'num_days': num_days,
'workout_counts_sparkline': workout_counts_sparkline,
'duration_sparkline': duration_sparkline
'duration_sparkline': duration_sparkline,
'calendar_month': calendar_month
}
users_data.append(user_data)