diff --git a/app.py b/app.py index 10a9f88..5829298 100644 --- a/app.py +++ b/app.py @@ -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) diff --git a/templates/partials/calendar.html b/templates/partials/calendar.html index 0c19abc..431d9c4 100644 --- a/templates/partials/calendar.html +++ b/templates/partials/calendar.html @@ -28,238 +28,60 @@ -
|
-
-
- Mo - |
-
-
-
- Tu - |
-
-
-
- We - |
-
-
-
- Th - |
-
-
-
- Fr - |
-
-
-
- Sa - |
-
-
-
- Su - |
-
|---|---|---|---|---|---|---|
|
-
-
- 29 - |
-
-
-
- 30 - |
-
-
-
- 31 - |
-
-
-
- 1 - |
-
-
-
- 2 - |
-
-
-
- 3 - |
-
-
-
- 4 - |
-
|
-
-
- 5 - |
-
-
-
- 6 - |
-
-
- 7
-
- |
-
-
-
-
- 8
-
- |
-
-
-
- 9 - |
-
-
-
- 10 - |
-
-
-
- 11 - |
-
|
-
-
- 12 - |
-
-
-
- 13 - |
-
-
-
- 14 - |
-
-
-
- 15 - |
-
-
-
- 16 - |
-
-
-
- 17 - |
-
-
-
- 18 - |
-
|
-
-
- 19 - |
-
-
-
- 20 - |
-
-
-
- 21 - |
-
-
-
- 22 - |
-
-
-
- 23 - |
-
-
-
- 24 - |
-
-
-
- 25 - |
-
|
-
-
- 26 - |
-
-
-
- 27 - |
-
-
-
- 28 - |
-
-
-
- 29 - |
-
-
-
- 30 - |
-
-
-
- 1 - |
-
-
-
- 2 - |
-
Su
+Mo
+Tu
+We
+Th
+Fr
+Sa
++ {% if d.is_current_date %} +