Migrate data and improve ui

This commit is contained in:
Peter Stockings
2022-07-23 18:59:21 +10:00
parent 2a8b72a881
commit c16922be36
7 changed files with 130 additions and 117 deletions

33
app.py
View File

@@ -1,34 +1,12 @@
from flask import Flask, abort, render_template, g, redirect, request, url_for from flask import Flask, render_template, redirect, request, url_for
from flasgger import Swagger, swag_from from flasgger import Swagger, swag_from
from functools import wraps
import sqlite3
from db import DataBase from db import DataBase
from decorators import validate_person, validate_topset, validate_workout from decorators import validate_person, validate_topset, validate_workout
template = {
"swagger": "2.0",
"info": {
"title": "WorkoutTracker API",
"description": "API for tracking topsets of workouts",
"contact": {
"responsibleOrganization": "ME",
"responsibleDeveloper": "Me",
"email": "me@me.com",
"url": "www.me.com",
},
"version": "0.0.1"
},
"schemes": [
"http",
"https"
],
"operationId": "getmyData"
}
app = Flask(__name__) app = Flask(__name__)
app.config.from_pyfile('config.py') app.config.from_pyfile('config.py')
swagger = Swagger(app, template=template) swagger = Swagger(app, template_file='swagger/base.json')
db = DataBase(app) db = DataBase(app)
@@ -44,7 +22,7 @@ def dashboard():
@ validate_person @ validate_person
def get_person(person_id): def get_person(person_id):
person = db.get_person_final(person_id) person = db.get_person_final(person_id)
return render_template('workouts.html', person=person) return render_template('person.html', person=person)
@ app.route("/person/<int:person_id>/workout", methods=['POST']) @ app.route("/person/<int:person_id>/workout", methods=['POST'])
@@ -117,10 +95,7 @@ def my_utility_processor():
return '' return ''
def get_list_of_people_and_workout_count(): def get_list_of_people_and_workout_count():
person_id = -1 person_id = request.view_args.get('person_id')
if 'person_id' in request.view_args:
person_id = request.view_args['person_id']
return db.get_people_and_workout_count(person_id) return db.get_people_and_workout_count(person_id)
def get_first_element_from_list_with_matching_attribute(list, attribute, value): def get_first_element_from_list_with_matching_attribute(list, attribute, value):

2
db.py
View File

@@ -41,6 +41,8 @@ class DataBase():
return topset return topset
def delete_workout(self, workout_id): def delete_workout(self, workout_id):
self.execute('DELETE FROM TopSet WHERE WorkoutId=?',
[workout_id], commit=True)
self.execute('DELETE FROM Workout WHERE WorkoutId=?', self.execute('DELETE FROM Workout WHERE WorkoutId=?',
[workout_id], commit=True) [workout_id], commit=True)

16
swagger/base.json Normal file
View File

@@ -0,0 +1,16 @@
{
"swagger": "2.0",
"info": {
"title": "WorkoutTracker API",
"description": "API for tracking topsets of workouts",
"contact": {
"responsibleOrganization": "ME",
"responsibleDeveloper": "Me",
"email": "me@me.com",
"url": "www.me.com"
},
"version": "0.0.1"
},
"schemes": ["http", "https"],
"operationId": "getmyData"
}

98
templates/person.html Normal file
View File

@@ -0,0 +1,98 @@
{% extends 'base.html' %}
{% block content %}
<div class="flex justify-center">
<div class="bg-white shadow rounded-lg p-4 sm:p-6 xl:p-8 md:w-full lg:w-10/12 xl:w-8/12 2xl:w-5/12">
<div class="mb-4 flex items-center justify-between">
<div>
<h3 class="text-xl font-bold text-gray-900 mb-2">{{ person['PersonName'] }}</h3>
<span class="text-base font-normal text-gray-500">List of workouts</span>
</div>
<div>
<form action="{{ url_for('create_workout', person_id=person['PersonId']) }}" method="post">
<button
class="sm:inline-flex text-white bg-cyan-600 hover:bg-cyan-700 focus:ring-4 focus:ring-cyan-200 font-medium rounded-lg text-sm px-5 py-2.5 text-center items-center mt-6">New
workout</button>
</form>
</div>
</div>
<div class="flex flex-col mt-8">
<div class="overflow-x-auto rounded-lg">
<div class="align-middle inline-block min-w-full">
<div class="shadow overflow-hidden sm:rounded-lg">
{% if person['Workouts']|length > 0 %}
<table class="min-w-full divide-y divide-gray-200">
<thead class="bg-gray-50">
<tr>
<th scope="col"
class="p-4 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
Date
</th>
{% for e in person['Exercises'] %}
<th scope="col"
class="p-4 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
{{ e['ExerciseName'] }}
</th>
{% endfor %}
<th scope="col"
class="p-4 text-left text-xs font-medium text-gray-500 uppercase tracking-wider w-8">
</th>
</tr>
</thead>
<tbody class="bg-white">
{% for w in person['Workouts'] %}
<tr>
<td class="p-4 whitespace-nowrap text-sm font-normal text-gray-500">
{{ w['StartDate'] }}
</td>
{% for e in person['Exercises'] %}
<td class="p-4 whitespace-nowrap text-sm font-semibold text-gray-900">
{% set topset_exercise =
get_first_element_from_list_with_matching_attribute(w['TopSets'], 'ExerciseId',
e['ExerciseId']) %}
{% if topset_exercise %}
{{ topset_exercise['Repetitions'] }} x {{ topset_exercise['Weight'] }}kg
{% endif %}
</td>
{% endfor %}
<td class="p-4 whitespace-nowrap text-sm font-semibold text-gray-900">
<a href="{{ url_for('get_workout' ,person_id=person['PersonId'], workout_id=w['WorkoutId']) }}"
class="text-sm font-medium text-cyan-600 hover:bg-gray-100 rounded-lg inline-flex items-center p-2">
Edit
</a>
<form
action="{{ url_for('delete_workout', person_id=person['PersonId'], workout_id=w['WorkoutId']) }}"
method="delete" class="inline">
<button
class="text-sm font-medium text-cyan-600 hover:bg-gray-100 rounded-lg inline-flex items-center p-2"
type="submit">Delete</button>
</form>
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}
{% if person['Workouts']|length == 0 %}
<div class="bg-purple-100 rounded-lg py-5 px-6 mb-4 text-base text-purple-700 mb-3"
role="alert">
No workouts found.
</div>
{% endif %}
</div>
</div>
</div>
</div>
</div>
</div>
{% endblock %}

View File

@@ -12,7 +12,7 @@
<form action="{{ url_for('delete_workout', person_id=workout['PersonId'], workout_id=workout['WorkoutId']) }}" <form action="{{ url_for('delete_workout', person_id=workout['PersonId'], workout_id=workout['WorkoutId']) }}"
method="delete"> method="delete">
<button <button
class="sm:inline-flex text-white bg-cyan-600 hover:bg-cyan-700 focus:ring-4 focus:ring-cyan-200 font-medium rounded-lg text-sm px-5 py-2.5 text-center items-center mt-6" class="sm:inline-flex text-white bg-red-200 hover:bg-red-700 focus:ring-4 focus:ring-red-200 font-medium rounded-lg text-sm px-5 py-2.5 text-center items-center mt-6"
type="submit">Delete type="submit">Delete
workout</button> workout</button>
</form> </form>
@@ -26,6 +26,8 @@
</div> </div>
<div class="flow-root"> <div class="flow-root">
{% if workout['TopSets']|length > 0 %}
<table class="items-center w-full bg-transparent border-collapse"> <table class="items-center w-full bg-transparent border-collapse">
<thead> <thead>
<tr> <tr>
@@ -62,6 +64,13 @@
{% endfor %} {% endfor %}
</tbody> </tbody>
</table> </table>
{% endif %}
{% if workout['TopSets']|length == 0 %}
<div class="bg-purple-100 rounded-lg py-5 px-6 mb-4 text-base text-purple-700 mb-3" role="alert">
No topsets found.
</div>
{% endif %}
</div> </div>
</div> </div>

View File

@@ -1,87 +0,0 @@
{% extends 'base.html' %}
{% block content %}
<div class="bg-white shadow rounded-lg p-4 sm:p-6 xl:p-8 ">
<div class="mb-4 flex items-center justify-between">
<div>
<h3 class="text-xl font-bold text-gray-900 mb-2">{{ person['PersonName'] }}</h3>
<span class="text-base font-normal text-gray-500">List of workouts</span>
</div>
</div>
<div class="flex flex-col mt-8">
<div class="overflow-x-auto rounded-lg">
<div class="align-middle inline-block min-w-full">
<div class="shadow overflow-hidden sm:rounded-lg">
<table class="min-w-full divide-y divide-gray-200">
<thead class="bg-gray-50">
<tr>
<th scope="col"
class="p-4 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
Date
</th>
{% for e in person['Exercises'] %}
<th scope="col"
class="p-4 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
{{ e['ExerciseName'] }}
</th>
{% endfor %}
<th scope="col"
class="p-4 text-left text-xs font-medium text-gray-500 uppercase tracking-wider w-8">
</th>
</tr>
</thead>
<tbody class="bg-white">
{% for w in person['Workouts'] %}
<tr>
<td class="p-4 whitespace-nowrap text-sm font-normal text-gray-500">
{{ w['StartDate'] }}
</td>
{% for e in person['Exercises'] %}
<td class="p-4 whitespace-nowrap text-sm font-semibold text-gray-900">
{% set topset_exercise =
get_first_element_from_list_with_matching_attribute(w['TopSets'], 'ExerciseId',
e['ExerciseId']) %}
{% if topset_exercise %}
{{ topset_exercise['Repetitions'] }} x {{ topset_exercise['Weight'] }}kg
{% endif %}
</td>
{% endfor %}
<td class="p-4 whitespace-nowrap text-sm font-semibold text-gray-900">
<a href="{{ url_for('get_workout' ,person_id=person['PersonId'], workout_id=w['WorkoutId']) }}"
class="text-sm font-medium text-cyan-600 hover:bg-gray-100 rounded-lg inline-flex items-center p-2">
Edit
</a>
<form
action="{{ url_for('delete_workout', person_id=person['PersonId'], workout_id=w['WorkoutId']) }}"
method="delete" class="inline">
<button
class="text-sm font-medium text-cyan-600 hover:bg-gray-100 rounded-lg inline-flex items-center p-2"
type="submit">Delete</button>
</form>
</td>
</tr>
{% endfor %}
</tbody>
</table>
<form action="{{ url_for('create_workout', person_id=person['PersonId']) }}" method="post">
<button
class="sm:inline-flex text-white bg-cyan-600 hover:bg-cyan-700 focus:ring-4 focus:ring-cyan-200 font-medium rounded-lg text-sm px-5 py-2.5 text-center items-center mt-6">New
workout</button>
</form>
</div>
</div>
</div>
</div>
</div>
{% endblock %}

Binary file not shown.