From 11d350082d752fb21d4f919170758f4902e2be79 Mon Sep 17 00:00:00 2001 From: Peter Stockings Date: Fri, 10 Mar 2023 21:54:46 +1100 Subject: [PATCH] Minor changes --- app.py | 94 +++---------------- .../{attemptv2.html => new_workout.html} | 2 +- templates/users_and_workouts.html | 81 ++++++++++++++++ templates/workouts_list.html | 46 +++++++++ 4 files changed, 141 insertions(+), 82 deletions(-) rename templates/{attemptv2.html => new_workout.html} (96%) create mode 100644 templates/users_and_workouts.html create mode 100644 templates/workouts_list.html diff --git a/app.py b/app.py index a93e537..34f244e 100644 --- a/app.py +++ b/app.py @@ -150,17 +150,7 @@ def workout(user_id, workout_id): # ax.set_title( # 'Cadence Readings for Workout {}'.format(workout_id)) - # Hide X and Y axes label marks - # ax.xaxis.set_tick_params(labelbottom=False) - # ax.yaxis.set_tick_params(labelleft=False) - # Hide X and Y axes tick marks - # ax.set_xticks([]) - # ax.set_yticks([]) - # use formatters to specify major and minor ticks - # format date as hh:mm ax.xaxis.set_major_formatter(mdates.DateFormatter("%H:%M")) - # ax.xaxis.set_major_formatter(mdates.DateFormatter("%Y-%m")) - # ax.xaxis.set_minor_formatter(mdates.DateFormatter("%Y-%m")) # set the y-axis limits to start at 0 ax.set_ylim(bottom=0) @@ -179,10 +169,11 @@ def workout(user_id, workout_id): return jsonify({'message': 'No cadence readings for workout {}.'.format(workout_id)}), 404 elif request.method == 'DELETE': # Delete the workout and its associated cadence readings - db.session.delete(workout) CadenceReading.query.filter_by(workout_id=workout_id).delete() + db.session.delete(workout) db.session.commit() - return jsonify({'message': 'Workout {} deleted successfully.'.format(workout_id)}), 200 + workouts_data = get_workouts_for_user(user_id) + return render_template('workouts_list.html', workouts=workouts_data) else: return jsonify({'message': 'Workout {} not found for user {}.'.format(workout_id, user_id)}), 404 @@ -195,7 +186,8 @@ def workouts_for_user(user_id): def get_workouts_for_user(user_id): workouts_data = [] - workouts = Workout.query.filter_by(user_id=user_id).all() + workouts = Workout.query.filter_by(user_id=user_id).order_by( + Workout.created_at.desc()).all() for workout in workouts: cadence_readings = CadenceReading.query.filter_by( workout_id=workout.id).all() @@ -205,86 +197,26 @@ def get_workouts_for_user(user_id): end_time = max( reading.created_at for reading in cadence_readings) duration = end_time - start_time - if duration >= timedelta(hours=1): - duration_str = str(duration) - else: - duration_str = str(duration).split('.')[0] average_rpm = sum( reading.rpm for reading in cadence_readings) / len(cadence_readings) workouts_data.append({ 'id': workout.id, 'user_id': user_id, 'start_time': start_time.strftime('%Y-%m-%d %H:%M:%S'), - 'duration': duration_str, + 'duration': format_duration(duration), 'average_rpm': int(average_rpm) }) return workouts_data -if __name__ == '__main__': - app.run(debug=True) +def format_duration(duration): + hours, remainder = divmod(duration.seconds, 3600) + minutes, _ = divmod(remainder, 60) - -# @app.after_request -# def response_minify(response): -# """ -# minify html response to decrease site traffic -# """ -# if response.content_type == u'text/html; charset=utf-8': -# response.set_data( -# minify_html.minify(response.get_data( -# as_text=True), minify_js=True, remove_processing_instructions=True) -# ) - -# return response -# return response - - -# @ app.route("/") -# def home(): -# return render_template('attemptv2.html') - - -# @ app.route("/devices") -# def devices(): -# devices = db.get_devices() -# return render_template('devices.html', devices=devices) - - -# @ app.route("/device/") -# def device(device_id): -# device = db.get_device(device_id) -# return render_template('device.html', device=device) - - -# @app.route("/overview/") -# def overview(device_id): -# cadences = db.get_all_cadences(device_id) -# last_cadence = cadences[-1]['rpm'] if cadences else 0 -# if cadences: -# first = cadences[0]['logged_at'] -# last = cadences[-1]['logged_at'] -# duration = str(timedelta(seconds=(last-first).seconds)) - -# last_cadence = cadences[-1]['rpm'] - -# power = round(decimal.Decimal(0.0011)*last_cadence ** 3 + decimal.Decimal( -# 0.0026) * last_cadence ** 2 + decimal.Decimal(0.5642)*last_cadence) - -# graph_data = generate_sparkline_graph( -# [c['rpm'] for c in cadences[-100:]]) - -# return render_template('overview.html', last_cadence=last_cadence, power=power, duration=duration, cadences=cadences[-15:], graph_data=graph_data) -# return render_template('overview.html', last_cadence=0, power=0, duration=duration, cadences=[], graph_data='') - - -# @ app.route("/cadence", methods=['POST']) -# def cadence(): -# data = request.get_json() -# print('' + datetime.now().replace(microsecond=0).isoformat() + -# ' ' + json.dumps(data)) -# db.insert_cadence(data['rpm'], data['id']) -# return 'ok' + if duration >= timedelta(hours=1): + return f"{hours}h {minutes}m" + else: + return f"{minutes}m" if __name__ == '__main__': diff --git a/templates/attemptv2.html b/templates/new_workout.html similarity index 96% rename from templates/attemptv2.html rename to templates/new_workout.html index 55fb0b7..af4c9ff 100644 --- a/templates/attemptv2.html +++ b/templates/new_workout.html @@ -107,7 +107,7 @@ toggleButton.textContent = 'Start'; disconnect(); - fetch("/user/1/workouts", { + fetch("{{ url_for('workouts', user_id=user.id) }}", { method: "POST", headers: { Accept: "application/json", diff --git a/templates/users_and_workouts.html b/templates/users_and_workouts.html new file mode 100644 index 0000000..12be797 --- /dev/null +++ b/templates/users_and_workouts.html @@ -0,0 +1,81 @@ + + + + + + + Cardio Tracker + + + + + + +
+
+
+

Users

+
+ + + + + + + + + {% for u in users %} + + + + + {% endfor %} + +
NameWorkouts
{{ u.name }} +
+
{{ u.workouts_count }}
+
+ + + + +
+
+
+ +
+
+
+ + + +
+
+
+ + + \ No newline at end of file diff --git a/templates/workouts_list.html b/templates/workouts_list.html new file mode 100644 index 0000000..75f173d --- /dev/null +++ b/templates/workouts_list.html @@ -0,0 +1,46 @@ +{% for w in workouts %} +
+

+ +

+
+ No image + + +
+
+{% endfor %} + + \ No newline at end of file