def get_style():
"""
Returns the current user's preferred style path (to .css file), checking:
- URL ?css= param (for preview/testing)
- 'css' cookie
- First available style, or '/static/styles/base.css' if none found
This should be called by render_template for every page load.
"""
styles = list_styles()
style_cookie = gw.web.cookies.get("css") if gw.web.app.is_enabled('web.cookies') else None
style_query = request.query.get("css")
style_path = None
# Prefer query param (if exists and valid)
if style_query:
for src, fname in styles:
if fname == style_query:
style_path = f"/static/styles/{fname}" if src == "global" else f"/static/{src}/styles/{fname}"
break
# Otherwise, prefer cookie
if not style_path and style_cookie:
for src, fname in styles:
if fname == style_cookie:
style_path = f"/static/styles/{fname}" if src == "global" else f"/static/{src}/styles/{fname}"
break
# Otherwise, first available style
if not style_path and styles:
src, fname = styles[0]
style_path = f"/static/styles/{fname}" if src == "global" else f"/static/{src}/styles/{fname}"
# Fallback to base
return style_path or "/static/styles/base.css"