feat: Refactor notes functionality into blueprint

- Moved notes-related routes (viewing/editing workout notes) from `app.py` into a new blueprint at `routes/notes.py`.
- Integrated notes-specific database logic (fetching and updating notes) directly into `routes/notes.py` helper functions, removing the corresponding methods from `db.py` for better encapsulation.
- Registered the new `notes_bp` blueprint in `app.py`.
- Removed the original notes route definitions from `app.py`.
- Updated `url_for` calls in `templates/partials/workout_note.html` to reference the new blueprint endpoints (e.g., `notes.get_person_notes`).
- Updated `templates/changelog/changelog.html` to document this refactoring in its own entry.
This commit is contained in:
Peter Stockings
2025-03-31 22:08:47 +11:00
parent 6095e76f10
commit 78436b230b
6 changed files with 132 additions and 87 deletions

55
db.py
View File

@@ -345,9 +345,7 @@ class DataBase():
def delete_tag_for_dashboard(self, tag_id):
self.execute('DELETE FROM Tag WHERE tag_id=%s', [tag_id], commit=True)
def update_workout_note_for_person(self, person_id, workout_id, note):
self.execute('UPDATE workout SET note=%s WHERE person_id=%s AND workout_id=%s', [
note, person_id, workout_id], commit=True)
# Note update logic moved to routes/notes.py
def add_tag_for_workout(self, workout_id, tags_id):
# If tags_id is not empty, delete tags that are not in the new selection
@@ -537,58 +535,9 @@ class DataBase():
degree)
return exercise_progress
def get_workout_notes_for_person(self, person_id):
sql_query = """
SELECT
p.name AS person_name,
w.workout_id,
to_char(w.start_date, 'Mon DD YYYY') AS formatted_start_date,
w.note,
t.filter AS tag_filter,
t.name AS tag_name
FROM person p
LEFT JOIN workout w ON p.person_id = w.person_id AND w.note IS NOT NULL AND w.note <> ''
LEFT JOIN workout_tag wt ON w.workout_id = wt.workout_id
LEFT JOIN tag t ON wt.tag_id = t.tag_id
WHERE p.person_id = %s
ORDER BY w.start_date DESC, w.workout_id, t.name;
"""
# Execute the SQL query
raw_data = self.execute(sql_query, [person_id])
# Note fetching logic moved to routes/notes.py
if not raw_data:
return None, []
# Extract person name from the first row (all rows have the same person name)
person_name = raw_data[0]['person_name']
# Process the workout notes
workout_notes = {}
for row in raw_data:
workout_id = row['workout_id']
if workout_id and row['note']:
# Initialize the workout entry if it doesn't exist
if workout_id not in workout_notes:
workout_notes[workout_id] = {
'workout_id': workout_id,
'formatted_start_date': row['formatted_start_date'],
'note': row['note'],
'tags': []
}
# Add tags if present
if row['tag_name']:
workout_notes[workout_id]['tags'].append({
'tag_filter': row['tag_filter'],
'tag_name': row['tag_name'],
'person_id': person_id
})
# Convert to a list for the final output
workout_notes_list = list(workout_notes.values())
return person_name, workout_notes_list
def get_exercise_earliest_and_latest_dates(self, person_id, exercise_id):
sql_query = """
SELECT