From 0701c1aaceafe4eb2ad7d3cb0723e4562a211e4d Mon Sep 17 00:00:00 2001 From: Peter Stockings Date: Sat, 3 Dec 2022 17:46:07 +1100 Subject: [PATCH] Fix calendar year view --- app.py | 10 ++-- templates/partials/page/calendar.html | 69 ++++++++------------------- utils.py | 13 ++++- 3 files changed, 40 insertions(+), 52 deletions(-) diff --git a/app.py b/app.py index 4246f62..4022bb6 100644 --- a/app.py +++ b/app.py @@ -1,11 +1,11 @@ from datetime import datetime, date, timedelta -import calendar +from dateutil.relativedelta import relativedelta import os 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 get_people_and_exercise_rep_maxes, convert_str_to_date, get_earliest_and_latest_workout_date, filter_workout_topsets, get_exercise_ids_from_workouts +from utils import 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 app = Flask(__name__) @@ -92,6 +92,10 @@ def get_calendar(person_id): 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 + if htmx: return render_template('partials/page/calendar.html', person=person, selected_date=selected_date, selected_view=selected_view, next_date=next_date, previous_date=previous_date, start_date=start_date, end_date=end_date) @@ -299,7 +303,7 @@ def my_utility_processor(): def strftime(date, format="%b %d %Y"): return date.strftime(format) - return dict(get_list_of_people_and_workout_count=get_list_of_people_and_workout_count, is_selected_page=is_selected_page, get_first_element_from_list_with_matching_attribute=get_first_element_from_list_with_matching_attribute, is_checked=is_checked, strftime=strftime, datetime=datetime, timedelta=timedelta) + return dict(get_list_of_people_and_workout_count=get_list_of_people_and_workout_count, is_selected_page=is_selected_page, get_first_element_from_list_with_matching_attribute=get_first_element_from_list_with_matching_attribute, is_checked=is_checked, strftime=strftime, datetime=datetime, timedelta=timedelta, relativedelta=relativedelta, first_and_last_visible_days_in_month=first_and_last_visible_days_in_month) if __name__ == '__main__': diff --git a/templates/partials/page/calendar.html b/templates/partials/page/calendar.html index f302426..6c66774 100644 --- a/templates/partials/page/calendar.html +++ b/templates/partials/page/calendar.html @@ -109,11 +109,16 @@
{% for i in range(12) %} - + {% set date = start_date + relativedelta(months=i) %} + {% set first_day_of_month = date.replace(day=1) %} + {% set last_day_of_month = date + relativedelta(day=31) %} + {% set (first_day, last_day) = first_and_last_visible_days_in_month(first_day_of_month, last_day_of_month) + %}
December 2018 + hx-vals='{"date": "{{ next_date }}", "view": "month"}' hx-push-url="true">{{ + strftime(first_day_of_month, '%B %Y') }}
@@ -127,52 +132,20 @@
-
-
-
-
-
-
-
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
-
31
-
-
-
-
-
+ {% for i in range((last_day-first_day).days + 1) %} + {% set day_date = first_day + timedelta(days=i) %} + {% set workout = + get_first_element_from_list_with_matching_attribute(person['Workouts'], + 'StartDate', + day_date) %} + {% set is_in_month = day_date.month == first_day_of_month.month%} +
+ {% if is_in_month %} + {{ day_date.day }} {% endif %}
+ {% endfor %}
diff --git a/utils.py b/utils.py index c38da10..a7ce87a 100644 --- a/utils.py +++ b/utils.py @@ -1,4 +1,4 @@ -from datetime import date, datetime +from datetime import datetime, date, timedelta import json @@ -147,3 +147,14 @@ def get_exercise_ids_from_workouts(workouts): def flatten_list(list_of_lists): return [item for sublist in list_of_lists for item in sublist] + + +def first_and_last_visible_days_in_month(first_day_of_month, last_day_of_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 (start_date, end_date)