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
|
import jinja_partials
|
||||||
from decorators import validate_person, validate_topset, validate_workout
|
from decorators import validate_person, validate_topset, validate_workout
|
||||||
from db import DataBase
|
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
|
from flask_htmx import HTMX
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
@@ -36,10 +36,10 @@ def get_person_list():
|
|||||||
def get_person(person_id):
|
def get_person(person_id):
|
||||||
person = db.get_person(person_id)
|
person = db.get_person(person_id)
|
||||||
|
|
||||||
max_date = request.args.get(
|
max_date = convert_str_to_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')
|
'max_date'), '%Y-%m-%d') or max(person['Workouts'], key=lambda x: x['StartDate'])['StartDate']
|
||||||
min_date = request.args.get(
|
min_date = convert_str_to_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')
|
'min_date'), '%Y-%m-%d') or min(person['Workouts'], key=lambda x: x['StartDate'])['StartDate']
|
||||||
|
|
||||||
selected_exercise_ids = [int(i)
|
selected_exercise_ids = [int(i)
|
||||||
for i in request.args.getlist('exercise_id')] or [e['ExerciseId'] for e in person['Exercises']]
|
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]
|
if topset['ExerciseId'] in selected_exercise_ids]
|
||||||
return workout
|
return workout
|
||||||
|
|
||||||
person['Workouts'] = [filter_workout_topsets(workout, selected_exercise_ids) for workout in person['Workouts'] if datetime.strptime(
|
person['Workouts'] = [filter_workout_topsets(workout, selected_exercise_ids) for workout in person['Workouts'] if
|
||||||
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]
|
workout['StartDate'] <= max_date and workout['StartDate'] >= min_date]
|
||||||
|
|
||||||
if selected_exercise_ids:
|
if selected_exercise_ids:
|
||||||
filtered_exercises = filter(
|
filtered_exercises = filter(
|
||||||
@@ -267,7 +267,10 @@ def my_utility_processor():
|
|||||||
return 'checked'
|
return 'checked'
|
||||||
return 'checked' if val in checked_vals else ''
|
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__':
|
if __name__ == '__main__':
|
||||||
|
|||||||
2
db.py
2
db.py
@@ -193,7 +193,7 @@ class DataBase():
|
|||||||
'PersonId': next((t['PersonId'] for t in topsets), -1),
|
'PersonId': next((t['PersonId'] for t in topsets), -1),
|
||||||
'PersonName': next((t['PersonName'] for t in topsets), 'Unknown'),
|
'PersonName': next((t['PersonName'] for t in topsets), 'Unknown'),
|
||||||
'WorkoutId': workout_id,
|
'WorkoutId': workout_id,
|
||||||
'StartDate': topsets[0]['StartDate'].strftime("%Y-%m-%d"),
|
'StartDate': topsets[0]['StartDate'],
|
||||||
'Exercises': self.get_exercises(),
|
'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]
|
'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'] %}
|
{% for w in person['Workouts'] %}
|
||||||
<tr>
|
<tr>
|
||||||
<td class="p-4 whitespace-nowrap text-sm font-normal text-gray-500">
|
<td class="p-4 whitespace-nowrap text-sm font-normal text-gray-500">
|
||||||
{{ w['StartDate'] }}
|
{{ strftime(w['StartDate'], "%b %d %Y") }}
|
||||||
</td>
|
</td>
|
||||||
|
|
||||||
{% for e in exercise_list %}
|
{% for e in exercise_list %}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
{% if is_edit|default(false, true) == false %}
|
{% 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"
|
<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-get="{{ url_for('get_workout_start_date_edit_form', person_id=person_id, workout_id=workout_id) }}"
|
||||||
hx-target="#edit-start-date">
|
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
|
import json
|
||||||
|
|
||||||
|
|
||||||
@@ -14,7 +14,7 @@ def get_workouts(topsets):
|
|||||||
t for t in topsets if t['WorkoutId'] == workout_id]
|
t for t in topsets if t['WorkoutId'] == workout_id]
|
||||||
workouts.append({
|
workouts.append({
|
||||||
'WorkoutId': workout_id,
|
'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]
|
'TopSets': [{"TopSetId": t['TopSetId'], "ExerciseId": t['ExerciseId'], "ExerciseName": t['ExerciseName'], "Weight": t['Weight'], "Repetitions": t['Repetitions']} for t in topsets_in_workout]
|
||||||
})
|
})
|
||||||
return workouts
|
return workouts
|
||||||
@@ -117,3 +117,12 @@ def get_stats_from_topsets(topsets):
|
|||||||
"Value": average_workouts_per_week})
|
"Value": average_workouts_per_week})
|
||||||
|
|
||||||
return stats
|
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