Refactor start_date as date rather then string
This commit is contained in:
19
app.py
19
app.py
@@ -4,7 +4,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 get_people_and_exercise_rep_maxes
|
||||
from utils import get_people_and_exercise_rep_maxes, convert_str_to_date
|
||||
from flask_htmx import HTMX
|
||||
|
||||
app = Flask(__name__)
|
||||
@@ -36,10 +36,10 @@ def get_person_list():
|
||||
def get_person(person_id):
|
||||
person = db.get_person(person_id)
|
||||
|
||||
max_date = request.args.get(
|
||||
'max_date') or datetime.strptime(max(person['Workouts'], key=lambda x: datetime.strptime(x['StartDate'], '%b %d %Y'))['StartDate'], '%b %d %Y').strftime('%Y-%m-%d')
|
||||
min_date = request.args.get(
|
||||
'min_date') or datetime.strptime(min(person['Workouts'], key=lambda x: datetime.strptime(x['StartDate'], '%b %d %Y'))['StartDate'], '%b %d %Y').strftime('%Y-%m-%d')
|
||||
max_date = convert_str_to_date(request.args.get(
|
||||
'max_date'), '%Y-%m-%d') or max(person['Workouts'], key=lambda x: x['StartDate'])['StartDate']
|
||||
min_date = convert_str_to_date(request.args.get(
|
||||
'min_date'), '%Y-%m-%d') or min(person['Workouts'], key=lambda x: x['StartDate'])['StartDate']
|
||||
|
||||
selected_exercise_ids = [int(i)
|
||||
for i in request.args.getlist('exercise_id')] or [e['ExerciseId'] for e in person['Exercises']]
|
||||
@@ -49,8 +49,8 @@ def get_person(person_id):
|
||||
if topset['ExerciseId'] in selected_exercise_ids]
|
||||
return workout
|
||||
|
||||
person['Workouts'] = [filter_workout_topsets(workout, selected_exercise_ids) for workout in person['Workouts'] if datetime.strptime(
|
||||
workout['StartDate'], '%b %d %Y').strftime('%Y-%m-%d') <= max_date and datetime.strptime(workout['StartDate'], '%b %d %Y').strftime('%Y-%m-%d') >= min_date]
|
||||
person['Workouts'] = [filter_workout_topsets(workout, selected_exercise_ids) for workout in person['Workouts'] if
|
||||
workout['StartDate'] <= max_date and workout['StartDate'] >= min_date]
|
||||
|
||||
if selected_exercise_ids:
|
||||
filtered_exercises = filter(
|
||||
@@ -267,7 +267,10 @@ def my_utility_processor():
|
||||
return 'checked'
|
||||
return 'checked' if val in checked_vals else ''
|
||||
|
||||
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)
|
||||
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)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
2
db.py
2
db.py
@@ -193,7 +193,7 @@ class DataBase():
|
||||
'PersonId': next((t['PersonId'] for t in topsets), -1),
|
||||
'PersonName': next((t['PersonName'] for t in topsets), 'Unknown'),
|
||||
'WorkoutId': workout_id,
|
||||
'StartDate': topsets[0]['StartDate'].strftime("%Y-%m-%d"),
|
||||
'StartDate': topsets[0]['StartDate'],
|
||||
'Exercises': self.get_exercises(),
|
||||
'TopSets': [{"TopSetId": t['TopSetId'], "ExerciseId": t['ExerciseId'], "ExerciseName": t['ExerciseName'], "Weight": t['Weight'], "Repetitions": t['Repetitions']} for t in topsets if t['TopSetId'] is not None]
|
||||
}
|
||||
|
||||
@@ -106,7 +106,7 @@
|
||||
{% for w in person['Workouts'] %}
|
||||
<tr>
|
||||
<td class="p-4 whitespace-nowrap text-sm font-normal text-gray-500">
|
||||
{{ w['StartDate'] }}
|
||||
{{ strftime(w['StartDate'], "%b %d %Y") }}
|
||||
</td>
|
||||
|
||||
{% for e in exercise_list %}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
{% if is_edit|default(false, true) == false %}
|
||||
<span class="text-base font-normal text-gray-500">{{ start_date }}</span>
|
||||
<span class="text-base font-normal text-gray-500">{{ strftime(start_date, "%b %d %Y") }}</span>
|
||||
<a class="text-sm font-medium text-cyan-600 hover:bg-gray-100 rounded-lg inline-flex items-center p-2 cursor-pointer"
|
||||
hx-get="{{ url_for('get_workout_start_date_edit_form', person_id=person_id, workout_id=workout_id) }}"
|
||||
hx-target="#edit-start-date">
|
||||
|
||||
13
utils.py
13
utils.py
@@ -1,4 +1,4 @@
|
||||
from datetime import date
|
||||
from datetime import date, datetime
|
||||
import json
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ def get_workouts(topsets):
|
||||
t for t in topsets if t['WorkoutId'] == workout_id]
|
||||
workouts.append({
|
||||
'WorkoutId': workout_id,
|
||||
'StartDate': topsets_in_workout[0]['StartDate'].strftime("%b %d %Y"),
|
||||
'StartDate': topsets_in_workout[0]['StartDate'],
|
||||
'TopSets': [{"TopSetId": t['TopSetId'], "ExerciseId": t['ExerciseId'], "ExerciseName": t['ExerciseName'], "Weight": t['Weight'], "Repetitions": t['Repetitions']} for t in topsets_in_workout]
|
||||
})
|
||||
return workouts
|
||||
@@ -117,3 +117,12 @@ def get_stats_from_topsets(topsets):
|
||||
"Value": average_workouts_per_week})
|
||||
|
||||
return stats
|
||||
|
||||
|
||||
def convert_str_to_date(date_str, format='%Y-%m-%d'):
|
||||
try:
|
||||
return datetime.strptime(date_str, format).date()
|
||||
except ValueError:
|
||||
return None
|
||||
except TypeError:
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user