Add WIP of exercise live search, just testing speed in prod site

This commit is contained in:
Peter Stockings
2024-11-03 09:45:48 +11:00
parent 817a6016e2
commit e756607dc8
5 changed files with 27 additions and 1 deletions

7
app.py
View File

@@ -438,6 +438,13 @@ def show_workout(person_id, workout_id):
view_model = db.workout.get(person_id, workout_id)
return render_template('workout.html', **view_model)
@app.route("/exercises/get")
def get_exercises():
query = request.args.get('query')
exercises = db.exercises.get(query)
return render_template('partials/exercise_dropdown.html', exercises=exercises)
@app.teardown_appcontext
def closeConnection(exception):

2
db.py
View File

@@ -7,6 +7,7 @@ from dateutil.relativedelta import relativedelta
from urllib.parse import urlparse
from flask import g
from features.calendar import Calendar
from features.exercises import Exercises
from features.stats import Stats
from features.workout import Workout
from utils import count_prs_over_time, get_all_exercises_from_topsets, get_exercise_graph_model, get_stats_from_topsets, get_topsets_for_person, get_weekly_pr_graph_model, get_workout_counts, get_workouts
@@ -17,6 +18,7 @@ class DataBase():
self.calendar = Calendar(self.execute)
self.stats = Stats(self.execute)
self.workout = Workout(self.execute)
self.exercises = Exercises(self.execute)
db_url = urlparse(os.environ['DATABASE_URL'])
# if db_url is null then throw error
if not db_url:

9
features/exercises.py Normal file
View File

@@ -0,0 +1,9 @@
class Exercises:
def __init__(self, db_connection_method):
self.execute = db_connection_method
def get(self, query):
# Add wildcards to the query
search_query = f"%{query}%"
exercises = self.execute("SELECT exercise_id, name FROM exercise WHERE LOWER(name) LIKE LOWER(%s) ORDER BY name ASC;", [search_query])
return exercises

View File

@@ -0,0 +1,5 @@
{% for e in exercises %}
<option value="{{ e.exercise_id }}">{{
e.name
}}</option>
{% endfor %}

View File

@@ -97,9 +97,12 @@
</label>
<input
class="appearance-none block w-full bg-gray-200 text-gray-700 border border-gray-200 rounded py-3 px-4 leading-tight focus:outline-none focus:bg-white focus:border-gray-500"
type="text" name="repetitions" </div>
type="text" name="query" placeholder="Search exercises..." hx-get="{{ url_for('get_exercises') }}"
hx-target="#exercise-results" hx-trigger="keyup changed delay:500ms" autocomplete="off">
<div id="exercise-results">
</div>
</div>
</div>