Partial refactor of tags functionality
Still need to move tags db logic to BP and move workout tag logic to BP as well
This commit is contained in:
103
routes/tags.py
Normal file
103
routes/tags.py
Normal file
@@ -0,0 +1,103 @@
|
||||
from flask import Blueprint, request, redirect, url_for, render_template, current_app
|
||||
from urllib.parse import urlencode, parse_qs, unquote_plus
|
||||
from flask_login import current_user
|
||||
from extensions import db
|
||||
from jinja2_fragments import render_block
|
||||
|
||||
tags_bp = Blueprint('tags', __name__, url_prefix='/tag')
|
||||
|
||||
# Helper function to get tags (assuming similar logic exists in dashboard/person_overview)
|
||||
# NOTE: This is a placeholder and might need adjustment based on actual data fetching logic
|
||||
def _get_tags_data(person_id=None):
|
||||
if person_id:
|
||||
# Logic to fetch tags for a specific person
|
||||
tags_raw = db.get_tags_for_person(person_id)
|
||||
# Assuming get_tags_for_person returns list like [{'tag_id': 1, 'tag_name': 'Bulk', 'tag_filter': '?tag=Bulk'}]
|
||||
return tags_raw # Adjust based on actual return format
|
||||
else:
|
||||
tags_raw = db.get_tags_for_dashboard()
|
||||
return tags_raw
|
||||
|
||||
|
||||
@tags_bp.route("/redirect", methods=['GET'])
|
||||
def goto_tag():
|
||||
"""Redirects or loads content based on tag filter."""
|
||||
person_id = request.args.get("person_id")
|
||||
tag_filter = request.args.get("filter", "") # Default to empty string
|
||||
|
||||
if person_id:
|
||||
# Assuming person_overview handles HTMX requests to render blocks
|
||||
# Corrected endpoint name for person overview
|
||||
target_url = url_for('person_overview', person_id=person_id) + tag_filter
|
||||
# Check if it's an HTMX request targeting #container
|
||||
if request.headers.get('HX-Target') == 'container' or request.headers.get('HX-Target') == '#container':
|
||||
# Need the actual function that renders person_overview content block
|
||||
# Placeholder: Re-render the person overview block with the filter
|
||||
# This requires knowing how person_overview fetches its data based on filters
|
||||
# return render_block('person_overview.html', 'content_block', person_id=person_id, filter=tag_filter)
|
||||
# For now, let's assume a full redirect might be simpler if block rendering is complex
|
||||
return redirect(target_url)
|
||||
else:
|
||||
return redirect(target_url)
|
||||
else:
|
||||
# Assuming dashboard handles HTMX requests to render blocks
|
||||
target_url = url_for('dashboard') + tag_filter
|
||||
if request.headers.get('HX-Target') == 'container' or request.headers.get('HX-Target') == '#container':
|
||||
# Need the actual function that renders dashboard content block
|
||||
# Placeholder: Re-render the dashboard block with the filter
|
||||
# This requires knowing how dashboard fetches its data based on filters
|
||||
# return render_block('dashboard.html', 'content_block', filter=tag_filter)
|
||||
# For now, let's assume a full redirect might be simpler
|
||||
return redirect(target_url)
|
||||
else:
|
||||
return redirect(target_url)
|
||||
|
||||
|
||||
@tags_bp.route("/add", methods=['POST']) # Changed to POST
|
||||
def add_tag():
|
||||
"""Adds a tag and returns the updated tags partial."""
|
||||
person_id = request.form.get("person_id") # Get from form data
|
||||
tag_name = request.form.get('tag_name')
|
||||
current_filter_str = request.form.get('current_filter', '')
|
||||
|
||||
if not tag_name:
|
||||
# Handle error - maybe return an error message partial?
|
||||
# For now, just re-render tags without adding
|
||||
tags = _get_tags_data(person_id)
|
||||
return render_template('partials/tags.html', tags=tags, person_id=person_id)
|
||||
|
||||
|
||||
# Parse the current filter string, add the new tag, and re-encode
|
||||
parsed_params = parse_qs(current_filter_str)
|
||||
# parse_qs returns lists for values, handle potential existing 'tag' param
|
||||
parsed_params['tag'] = [tag_name] # Set/overwrite tag param with the new one
|
||||
# Re-encode, ensuring proper handling of multiple values if needed (though 'tag' is likely single)
|
||||
tag_filter_value = "?" + urlencode(parsed_params, doseq=True)
|
||||
|
||||
if person_id:
|
||||
db.add_or_update_tag_for_person(person_id, tag_name, tag_filter_value)
|
||||
else:
|
||||
db.add_or_update_tag_for_dashboard(tag_name, tag_filter_value)
|
||||
|
||||
# Fetch updated tags and render the partial
|
||||
tags = _get_tags_data(person_id)
|
||||
return render_template('partials/tags.html', tags=tags, person_id=person_id)
|
||||
|
||||
|
||||
@tags_bp.route("/<int:tag_id>/delete", methods=['DELETE']) # Changed to DELETE
|
||||
def delete_tag(tag_id):
|
||||
"""Deletes a tag and returns the updated tags partial."""
|
||||
# We might get person_id from request body/headers if needed, or assume context
|
||||
# For simplicity, let's try deleting based on tag_id only first, assuming tags are unique enough or context is handled elsewhere
|
||||
# If person_id is strictly required for deletion scope:
|
||||
person_id = request.form.get("person_id") # Or from headers/session
|
||||
|
||||
# Decide which delete function to call based on context (person page vs dashboard)
|
||||
if person_id:
|
||||
db.delete_tag_for_person(person_id=person_id, tag_id=tag_id)
|
||||
else:
|
||||
db.delete_tag_for_dashboard(tag_id=tag_id)
|
||||
|
||||
# Fetch updated tags and render the partial
|
||||
tags = _get_tags_data(person_id)
|
||||
return render_template('partials/tags.html', tags=tags, person_id=person_id)
|
||||
Reference in New Issue
Block a user