From 3fd7a63d5efdaf2592ad038225e09e8a91acc8bd Mon Sep 17 00:00:00 2001 From: Peter Stockings Date: Fri, 31 Mar 2023 22:06:53 +1100 Subject: [PATCH] Add tags to dashboard --- app.py | 39 ++++--- db.py | 21 ++++ templates/dashboard.html | 2 +- templates/partials/page/dashboard.html | 151 +++++++++++++------------ templates/partials/page/person.html | 78 +------------ templates/partials/tags.html | 77 +++++++++++++ 6 files changed, 205 insertions(+), 163 deletions(-) create mode 100644 templates/partials/tags.html diff --git a/app.py b/app.py index ed7c644..fac05aa 100644 --- a/app.py +++ b/app.py @@ -38,6 +38,7 @@ def dashboard(): exercises = db.get_exercises() people = db.get_people() + tags = db.get_tags_for_dashboard() selected_person_ids = [int(i) for i in request.args.getlist('person_id')] @@ -59,8 +60,8 @@ def dashboard(): if htmx: return render_template('partials/page/dashboard.html', - model=people_and_exercise_rep_maxes, people=people, exercises=exercises, min_date=min_date, max_date=max_date, selected_person_ids=selected_person_ids, selected_exercise_ids=selected_exercise_ids), 200, {"HX-Trigger": "updatedPeople"} - return render_template('dashboard.html', model=people_and_exercise_rep_maxes, people=people, exercises=exercises, min_date=min_date, max_date=max_date, selected_person_ids=selected_person_ids, selected_exercise_ids=selected_exercise_ids) + model=people_and_exercise_rep_maxes, people=people, exercises=exercises, min_date=min_date, max_date=max_date, selected_person_ids=selected_person_ids, selected_exercise_ids=selected_exercise_ids, tags=tags), 200, {"HX-Trigger": "updatedPeople"} + return render_template('dashboard.html', model=people_and_exercise_rep_maxes, people=people, exercises=exercises, min_date=min_date, max_date=max_date, selected_person_ids=selected_person_ids, selected_exercise_ids=selected_exercise_ids, tags=tags) @ app.route("/person/list", methods=['GET']) @@ -314,28 +315,36 @@ 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): +@ app.route("/tag/redirect", methods=['GET']) +def goto_tag(): + person_id = request.args.get("person_id") tag_filter = request.args.get('filter') - return redirect(url_for('get_person', person_id=person_id) + tag_filter) + if person_id: + return redirect(url_for('get_person', person_id=int(person_id)) + tag_filter) + return redirect(url_for('dashboard') + tag_filter) -@ app.route("/person//tag/add", methods=['GET']) -@ validate_person -def add_tag(person_id): +@ app.route("/tag/add", methods=['GET']) +def add_tag(): + person_id = request.args.get("person_id") tag = request.args.get('tag') tag_filter = request.args.get('filter') - db.add_tag_for_person(person_id, tag, tag_filter) + if person_id: + db.add_tag_for_person(person_id, tag, tag_filter) + else: + db.add_tag_for_dashboard(tag, tag_filter) return "" -@ app.route("/person//tag//delete", methods=['GET']) -@ validate_person -def delete_tag(person_id, tag_id): +@ app.route("/tag//delete", methods=['GET']) +def delete_tag(tag_id): + person_id = request.args.get("person_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) + if person_id: + 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) + db.delete_tag_for_dashboard(tag_id) + return redirect(url_for('dashboard') + tag_filter) @ app.context_processor diff --git a/db.py b/db.py index 0fde403..400f527 100644 --- a/db.py +++ b/db.py @@ -275,3 +275,24 @@ class DataBase(): 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) + + def get_tags_for_dashboard(self): + 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 IS NULL + ORDER BY + T.name""", []) + + def add_tag_for_dashboard(self, tag_name, tag_filter): + self.execute('INSERT INTO Tag (name, filter) VALUES (%s, %s)', [ + tag_name, tag_filter], commit=True) + + def delete_tag_for_dashboard(self, tag_id): + self.execute('DELETE FROM Tag WHERE tag_id=%s', [tag_id], commit=True) diff --git a/templates/dashboard.html b/templates/dashboard.html index 5989fa2..7c80c08 100644 --- a/templates/dashboard.html +++ b/templates/dashboard.html @@ -4,6 +4,6 @@ {{ render_partial('partials/page/dashboard.html', model=model, people=people, exercises=exercises, min_date=min_date, max_date=max_date, -selected_person_ids=selected_person_ids, selected_exercise_ids=selected_exercise_ids) }} +selected_person_ids=selected_person_ids, selected_exercise_ids=selected_exercise_ids, tags=tags) }} {% endblock %} \ No newline at end of file diff --git a/templates/partials/page/dashboard.html b/templates/partials/page/dashboard.html index 01c67ff..905e907 100644 --- a/templates/partials/page/dashboard.html +++ b/templates/partials/page/dashboard.html @@ -1,85 +1,92 @@ -
-
-
-
-
- +
+
+
+
+
+
+ +
+
-
-
-
-
-
-
- +
+
+
+
+ +
+ +
+
+
+
+
+
+
+
+
+
- -
-
-
-
-
-
-
-
-
-
+ {% with person_id=None, tags=tags %} + {% include 'partials/tags.html' %} + {% endwith %}
+ +
{% for p in model['People'] %}
diff --git a/templates/partials/page/person.html b/templates/partials/page/person.html index 52d08d4..ed667e8 100644 --- a/templates/partials/page/person.html +++ b/templates/partials/page/person.html @@ -99,81 +99,9 @@
-
- - {% for t in tags %} -
- {{ - t['TagName'] }} - - - - - - - -
- {% endfor %} - -
-
- -
-
-
- + {% with person_id=person['PersonId'], tags=tags %} + {% include 'partials/tags.html' %} + {% endwith %}
diff --git a/templates/partials/tags.html b/templates/partials/tags.html new file mode 100644 index 0000000..38c4380 --- /dev/null +++ b/templates/partials/tags.html @@ -0,0 +1,77 @@ +
+ + {% for t in tags %} +
+ {{ + t['TagName'] }} + + + + + + + +
+ {% endfor %} + +
+
+ +
+
+
+ \ No newline at end of file