Minor changes
This commit is contained in:
94
app.py
94
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/<device_id>")
|
||||
# def device(device_id):
|
||||
# device = db.get_device(device_id)
|
||||
# return render_template('device.html', device=device)
|
||||
|
||||
|
||||
# @app.route("/overview/<device_id>")
|
||||
# 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__':
|
||||
|
||||
Reference in New Issue
Block a user