diff --git a/app.py b/app.py index e9cd524..ed7c644 100644 --- a/app.py +++ b/app.py @@ -8,7 +8,7 @@ from db import DataBase from utils import get_people_and_exercise_rep_maxes, convert_str_to_date, get_earliest_and_latest_workout_date, filter_workout_topsets, get_exercise_ids_from_workouts, first_and_last_visible_days_in_month from flask_htmx import HTMX import minify_html -from urllib.parse import urlparse +from urllib.parse import urlparse, unquote, quote app = Flask(__name__) app.config.from_pyfile('config.py') @@ -73,6 +73,7 @@ def get_person_list(): @ validate_person def get_person(person_id): person = db.get_person(person_id) + tags = db.get_tags_for_person(person_id) (min_date, max_date) = get_earliest_and_latest_workout_date(person) @@ -100,9 +101,9 @@ def get_person(person_id): person['FilteredExercises'] = list(filtered_exercises) if htmx: return render_template('partials/page/person.html', - person=person, selected_exercise_ids=active_exercise_ids, max_date=max_date, min_date=min_date), 200, {"HX-Trigger": "updatedPeople"} + person=person, selected_exercise_ids=active_exercise_ids, max_date=max_date, min_date=min_date, tags=tags), 200, {"HX-Trigger": "updatedPeople"} - return render_template('person.html', person=person, selected_exercise_ids=active_exercise_ids, max_date=max_date, min_date=min_date), 200, {"HX-Trigger": "updatedPeople"} + return render_template('person.html', person=person, selected_exercise_ids=active_exercise_ids, max_date=max_date, min_date=min_date, tags=tags), 200, {"HX-Trigger": "updatedPeople"} @ app.route("/person//calendar") @@ -313,6 +314,30 @@ def settings(): return render_template('settings.html', people=people, exercises=exercises) +@ app.route("/person//tag/redirect", methods=['GET']) +@ validate_person +def goto_tag(person_id): + tag_filter = request.args.get('filter') + return redirect(url_for('get_person', person_id=person_id) + tag_filter) + + +@ app.route("/person//tag/add", methods=['GET']) +@ validate_person +def add_tag(person_id): + tag = request.args.get('tag') + tag_filter = request.args.get('filter') + db.add_tag_for_person(person_id, tag, tag_filter) + return "" + + +@ app.route("/person//tag//delete", methods=['GET']) +@ validate_person +def delete_tag(person_id, tag_id): + tag_filter = request.args.get("filter") + db.delete_tag_for_person(person_id=person_id, tag_id=tag_id) + return redirect(url_for('get_person', person_id=person_id) + tag_filter) + + @ app.context_processor def my_utility_processor(): @@ -347,7 +372,7 @@ def my_utility_processor(): def list_to_string(list): return [str(i) for i in list] - return dict(get_list_of_people_and_workout_count=get_list_of_people_and_workout_count, is_selected_page=is_selected_page, get_first_element_from_list_with_matching_attribute=get_first_element_from_list_with_matching_attribute, in_list=in_list, strftime=strftime, datetime=datetime, timedelta=timedelta, relativedelta=relativedelta, first_and_last_visible_days_in_month=first_and_last_visible_days_in_month, list_to_string=list_to_string) + return dict(get_list_of_people_and_workout_count=get_list_of_people_and_workout_count, is_selected_page=is_selected_page, get_first_element_from_list_with_matching_attribute=get_first_element_from_list_with_matching_attribute, in_list=in_list, strftime=strftime, datetime=datetime, timedelta=timedelta, relativedelta=relativedelta, first_and_last_visible_days_in_month=first_and_last_visible_days_in_month, list_to_string=list_to_string, quote=quote) if __name__ == '__main__': diff --git a/db.py b/db.py index 4f3a533..0fde403 100644 --- a/db.py +++ b/db.py @@ -253,3 +253,25 @@ class DataBase(): LEFT JOIN Exercise E ON T.exercise_id=E.exercise_id""") return all_topsets + + def get_tags_for_person(self, person_id): + return self.execute(""" + SELECT + T.tag_id AS "TagId", + T.person_id AS "PersonId", + T.name AS "TagName", + T.filter AS "TagFilter" + FROM + Tag T + WHERE + T.person_id = %s + ORDER BY + T.name""", [person_id]) + + def add_tag_for_person(self, person_id, tag_name, tag_filter): + self.execute('INSERT INTO Tag (person_id, name, filter) VALUES (%s, %s, %s)', [ + person_id, tag_name, tag_filter], commit=True) + + def delete_tag_for_person(self, person_id, tag_id): + self.execute('DELETE FROM Tag WHERE person_id=%s AND tag_id=%s', [ + person_id, tag_id], commit=True) diff --git a/templates/base.html b/templates/base.html index 79db55e..19a1f71 100644 --- a/templates/base.html +++ b/templates/base.html @@ -28,6 +28,8 @@ + + diff --git a/templates/partials/page/person.html b/templates/partials/page/person.html index f1bee2c..c52e6bf 100644 --- a/templates/partials/page/person.html +++ b/templates/partials/page/person.html @@ -28,9 +28,9 @@ -
+
-
+
@@ -94,7 +94,83 @@
-
+
+ + {% for t in tags %} +
+ {{ + t['TagName'] }} + + + + + + + +
+ {% endfor %} + +
+
+ +
+
+
+ + +
diff --git a/templates/person.html b/templates/person.html index 90f1e29..e1a18e1 100644 --- a/templates/person.html +++ b/templates/person.html @@ -4,6 +4,6 @@ {{ render_partial('partials/page/person.html', person=person, selected_exercise_ids=selected_exercise_ids, max_date=max_date, -min_date=min_date) }} +min_date=min_date, tags=tags) }} {% endblock %} \ No newline at end of file