diff --git a/app.py b/app.py index 0abb525..54ad2c7 100644 --- a/app.py +++ b/app.py @@ -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): diff --git a/db.py b/db.py index 796f066..98a5b54 100644 --- a/db.py +++ b/db.py @@ -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: diff --git a/features/exercises.py b/features/exercises.py new file mode 100644 index 0000000..0881995 --- /dev/null +++ b/features/exercises.py @@ -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 \ No newline at end of file diff --git a/templates/partials/exercise_dropdown.html b/templates/partials/exercise_dropdown.html new file mode 100644 index 0000000..40025f4 --- /dev/null +++ b/templates/partials/exercise_dropdown.html @@ -0,0 +1,5 @@ +{% for e in exercises %} + +{% endfor %} \ No newline at end of file diff --git a/templates/workout.html b/templates/workout.html index cce5399..bb1b06e 100644 --- a/templates/workout.html +++ b/templates/workout.html @@ -97,8 +97,11 @@ + 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"> +