Add activity logs table

This commit is contained in:
Peter Stockings
2026-01-31 14:47:59 +11:00
parent 62080b97a4
commit d7c9f71d22
10 changed files with 193 additions and 6 deletions

View File

@@ -105,11 +105,12 @@ def signup():
form = SignupForm()
if form.validate_on_submit():
hashed_password = generate_password_hash(form.password.data)
create_person(
new_person_id = create_person(
name=form.name.data,
email=form.email.data,
password_hash=hashed_password
)
db.activityRequest.log(new_person_id, 'SIGNUP', 'person', new_person_id, f"User signed up: {form.email.data}")
flash("Account created successfully. Please log in.", "success")
return redirect(url_for('auth.login'))
return render_template('auth/signup.html', form=form)
@@ -122,11 +123,11 @@ def login():
person = get_person_by_email(form.email.data)
if person and check_password_hash(person.password_hash, form.password.data):
login_user(person)
record_login_attempt(form.email.data, True, person.id)
db.activityRequest.log(person.id, 'LOGIN_SUCCESS', 'person', person.id, f"User logged in: {form.email.data}")
flash("Logged in successfully.", "success")
return redirect(url_for('calendar.get_calendar', person_id=person.id))
else:
record_login_attempt(form.email.data, False, person.id if person else None)
db.activityRequest.log(person.id if person else None, 'LOGIN_FAILURE', 'person', person.id if person else None, f"Failed login attempt for: {form.email.data}")
flash("Invalid email or password.", "danger")
return render_template('auth/login.html', form=form)
@@ -134,6 +135,8 @@ def login():
@auth.route('/logout')
@login_required
def logout():
person_id = current_user.id if current_user.is_authenticated else None
logout_user()
db.activityRequest.log(person_id, 'LOGOUT', 'person', person_id, "User logged out")
flash('You have been logged out.', 'success')
return redirect(url_for('auth.login'))

View File

@@ -1,6 +1,7 @@
from flask import Blueprint, render_template, request, current_app
from jinja2_fragments import render_block
from flask_htmx import HTMX
from flask_login import current_user
from extensions import db # Still need db for execute method
from decorators import validate_person, validate_workout
@@ -91,6 +92,7 @@ def update_workout_note(person_id, workout_id):
"""Updates a specific workout note."""
note = request.form.get('note')
_update_workout_note_for_person(person_id, workout_id, note) # Use local helper
db.activityRequest.log(current_user.id, 'UPDATE_NOTE', 'workout', workout_id, f"Updated note for workout {workout_id}")
return render_template('partials/workout_note.html', person_id=person_id, workout_id=workout_id, note=note)
@notes_bp.route("/person/<int:person_id>/workout/<int:workout_id>/note", methods=['GET'])

View File

@@ -1,7 +1,7 @@
from flask import Blueprint, render_template, redirect, url_for, request, current_app
from jinja2_fragments import render_block
from flask_htmx import HTMX
from flask_login import login_required
from flask_login import login_required, current_user
from extensions import db
from decorators import validate_workout, validate_topset, require_ownership, validate_person
from utils import convert_str_to_date
@@ -140,6 +140,7 @@ def _get_workout_view_model(person_id, workout_id):
@require_ownership
def create_workout(person_id):
new_workout_id = db.create_workout(person_id)
db.activityRequest.log(current_user.id, 'CREATE_WORKOUT', 'workout', new_workout_id, f"Created workout for person_id: {person_id}")
# Use the local helper function to get the view model
view_model = _get_workout_view_model(person_id, new_workout_id)
if "error" in view_model: # Handle case where workout creation might fail or is empty
@@ -153,6 +154,7 @@ def create_workout(person_id):
@require_ownership
def delete_workout(person_id, workout_id):
db.delete_workout(workout_id)
db.activityRequest.log(current_user.id, 'DELETE_WORKOUT', 'workout', workout_id, f"Deleted workout: {workout_id}")
return redirect(url_for('calendar.get_calendar', person_id=person_id))
@workout_bp.route("/person/<int:person_id>/workout/<int:workout_id>/start_date_edit_form", methods=['GET'])
@@ -172,6 +174,7 @@ def get_workout_start_date_edit_form(person_id, workout_id):
def update_workout_start_date(person_id, workout_id):
new_start_date_str = request.form.get('start-date')
db.update_workout_start_date(workout_id, new_start_date_str)
db.activityRequest.log(current_user.id, 'UPDATE_WORKOUT_START_DATE', 'workout', workout_id, f"Updated start date to {new_start_date_str}")
# Convert string back to date for rendering the partial
new_start_date = convert_str_to_date(new_start_date_str, '%Y-%m-%d')
return render_template('partials/start_date.html', person_id=person_id, workout_id=workout_id, start_date=new_start_date)
@@ -225,6 +228,7 @@ def create_topset(person_id, workout_id):
weight = request.form.get("weight")
new_topset_id = db.create_topset(workout_id, exercise_id, repetitions, weight)
exercise = db.get_exercise(exercise_id)
db.activityRequest.log(current_user.id, 'ADD_SET', 'topset', new_topset_id, f"Added set: {repetitions} x {weight}kg {exercise['name']} in workout {workout_id}")
return render_template('partials/topset.html', person_id=person_id, workout_id=workout_id, topset_id=new_topset_id, exercise_id=exercise_id, exercise_name=exercise.get('name'), repetitions=repetitions, weight=weight), 200, {"HX-Trigger": "topsetAdded"}
@workout_bp.route("/person/<int:person_id>/workout/<int:workout_id>/topset/<int:topset_id>", methods=['PUT'])
@@ -237,6 +241,7 @@ def update_topset(person_id, workout_id, topset_id):
weight = request.form.get("weight")
db.update_topset(exercise_id, repetitions, weight, topset_id)
exercise = db.get_exercise(exercise_id)
db.activityRequest.log(current_user.id, 'UPDATE_SET', 'topset', topset_id, f"Updated set {topset_id}: {repetitions} x {weight}kg {exercise['name']}")
return render_template('partials/topset.html', person_id=person_id, workout_id=workout_id, topset_id=topset_id, exercise_id=exercise_id, exercise_name=exercise.get('name'), repetitions=repetitions, weight=weight)
@workout_bp.route("/person/<int:person_id>/workout/<int:workout_id>/topset/<int:topset_id>/delete", methods=['DELETE'])
@@ -244,7 +249,9 @@ def update_topset(person_id, workout_id, topset_id):
@validate_topset
@require_ownership
def delete_topset(person_id, workout_id, topset_id):
topset = db.get_topset(topset_id)
db.delete_topset(topset_id)
db.activityRequest.log(current_user.id, 'DELETE_SET', 'topset', topset_id, f"Deleted set {topset_id}: {topset['exercise_name']} in workout {workout_id}")
return ""
@workout_bp.route("/person/<int:person_id>/workout/<int:workout_id>/exercise/most_recent_topset_for_exercise", methods=['GET'])