Fix calendar year view

This commit is contained in:
Peter Stockings
2022-12-03 17:46:07 +11:00
parent aed610d8b6
commit 0701c1aace
3 changed files with 40 additions and 52 deletions

10
app.py
View File

@@ -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__':

View File

@@ -109,11 +109,16 @@
<div class="flex flex-wrap bg-white overflow-hidden">
{% 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)
%}
<div class="w-auto mx-3 my-3 border-solid border-grey-light rounded border shadow-sm">
<div class="bg-grey-lighter px-2 py-2 border-solid border-grey-light border-b text-center cursor-pointer"
hx-get="{{ url_for('get_calendar', person_id=person['PersonId']) }}" hx-target="#container"
hx-vals='{"date": "{{ next_date }}", "view": "month"}' hx-push-url="true">December 2018
hx-vals='{"date": "{{ next_date }}", "view": "month"}' hx-push-url="true">{{
strftime(first_day_of_month, '%B %Y') }}
</div>
<div class="">
<div class="grid grid-cols-7 border-b font-semibold">
@@ -127,52 +132,20 @@
</div>
<div class="grid grid-cols-7 overflow-hidden flex-1 pl-2 pr-2 w-full">
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div class="py-3 px-4 hover:bg-blue hover:text-white text-center cursor-pointer">1</div>
<div class="py-3 px-4 hover:bg-blue hover:text-white text-center cursor-pointer">2</div>
<div class="py-3 px-4 hover:bg-blue hover:text-white text-center cursor-pointer">3</div>
<div class="py-3 px-4 hover:bg-blue hover:text-white text-center cursor-pointer">4</div>
<div class="py-3 px-4 hover:bg-blue hover:text-white text-center cursor-pointer">5</div>
<div
class="py-3 px-4 hover:bg-blue hover:text-white text-center cursor-pointer bg-blue text-white">
6</div>
<div
class="py-3 px-4 hover:bg-blue hover:text-white text-center cursor-pointer bg-blue text-white">
7</div>
<div class="py-3 px-4 hover:bg-blue hover:text-white text-center cursor-pointer">8</div>
<div class="py-3 px-4 hover:bg-blue hover:text-white text-center cursor-pointer">9</div>
<div class="py-3 px-4 hover:bg-blue hover:text-white text-center cursor-pointer">10</div>
<div class="py-3 px-4 hover:bg-blue hover:text-white text-center cursor-pointer">11</div>
<div class="py-3 px-4 hover:bg-blue hover:text-white text-center cursor-pointer">12</div>
<div class="py-3 px-4 hover:bg-blue hover:text-white text-center cursor-pointer">13</div>
<div class="py-3 px-4 hover:bg-blue hover:text-white text-center cursor-pointer">14</div>
<div class="py-3 px-4 hover:bg-blue hover:text-white text-center cursor-pointer">15</div>
<div class="py-3 px-4 hover:bg-blue hover:text-white text-center cursor-pointer">16</div>
<div class="py-3 px-4 hover:bg-blue hover:text-white text-center cursor-pointer">17</div>
<div class="py-3 px-4 hover:bg-blue hover:text-white text-center cursor-pointer">18</div>
<div class="py-3 px-4 hover:bg-blue hover:text-white text-center cursor-pointer">19</div>
<div class="py-3 px-4 hover:bg-blue hover:text-white text-center cursor-pointer">20</div>
<div class="py-3 px-4 hover:bg-blue hover:text-white text-center cursor-pointer">21</div>
<div class="py-3 px-4 hover:bg-blue hover:text-white text-center cursor-pointer">22</div>
<div class="py-3 px-4 hover:bg-blue hover:text-white text-center cursor-pointer">23</div>
<div class="py-3 px-4 hover:bg-blue hover:text-white text-center cursor-pointer">24</div>
<div class="py-3 px-4 hover:bg-blue hover:text-white text-center cursor-pointer">25</div>
<div class="py-3 px-4 hover:bg-blue hover:text-white text-center cursor-pointer">26</div>
<div class="py-3 px-4 hover:bg-blue hover:text-white text-center cursor-pointer">27</div>
<div class="py-3 px-4 hover:bg-blue hover:text-white text-center cursor-pointer">28</div>
<div class="py-3 px-4 hover:bg-blue hover:text-white text-center cursor-pointer">29</div>
<div class="py-3 px-4 hover:bg-blue hover:text-white text-center cursor-pointer">30</div>
<div class="py-3 px-4 hover:bg-blue hover:text-white text-center cursor-pointer">31</div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
{% 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%}
<div class="{% if day_date == datetime.today().date() and is_in_month %}rounded-md border-4 border-green-50 border{% endif %} py-3 px-4 hover:bg-blue hover:text-white text-center cursor-pointer {% if workout and is_in_month %}bg-green-100{% endif %}"
{% if workout %}
hx-get="{{ url_for('get_workout' ,person_id=person['PersonId'], workout_id=workout['WorkoutId']) }}"
hx-target="#container" hx-push-url="true" {% endif %}>
{% if is_in_month %}
{{ day_date.day }} {% endif %}</div>
{% endfor %}
</div>
</div>
</div>

View File

@@ -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)