Support for cookies. added change username button

This commit is contained in:
Eduardo Figueroa 2025-09-12 16:26:40 -07:00
parent 7ba32dc98b
commit f74cca961b
4 changed files with 69 additions and 7 deletions

30
app.py
View file

@ -1,4 +1,4 @@
from flask import Flask, render_template, jsonify, request, session, redirect, url_for, send_file
from flask import Flask, render_template, jsonify, request, session, redirect, url_for, send_file, make_response
from datetime import datetime, timedelta
import os
import sys
@ -85,7 +85,16 @@ def inject_apis():
def index():
"""Home page with username input"""
print("DEBUG: Index route accessed", flush=True)
return render_template('index.html')
# Get the last used username from cookie
last_username = request.cookies.get('last_username', '')
print(f"DEBUG: Last username from cookie: '{last_username}'", flush=True)
# Auto-redirect to dashboard if username cookie exists
if last_username and last_username.strip():
print(f"DEBUG: Auto-redirecting to dashboard for user: '{last_username}'", flush=True)
return redirect(url_for('dashboard_current', username=last_username.strip()))
return render_template('index.html', last_username=last_username)
@app.route('/dashboard')
def dashboard_form():
@ -153,6 +162,13 @@ def set_debug_time():
return_url = request.form.get('return_url', url_for('index'))
return redirect(return_url)
@app.route('/change-username')
def change_username():
"""Clear username cookie and redirect to index"""
response = make_response(redirect(url_for('index')))
response.set_cookie('last_username', '', expires=0) # Clear the cookie
return response
@app.route('/<username>')
def dashboard_current(username):
"""Dashboard for current NFL week"""
@ -161,7 +177,10 @@ def dashboard_current(username):
nfl_state = sleeper_api.get_nfl_state()
current_week = nfl_state.get('week', 1)
print(f"DEBUG: Current week: {current_week}", flush=True)
return dashboard(username, current_week)
response = make_response(dashboard(username, current_week))
# Set cookie to remember this username (expires in 30 days)
response.set_cookie('last_username', username, max_age=30*24*60*60)
return response
except Exception as e:
print(f"ERROR: dashboard_current exception - {str(e)}", flush=True)
print(f"ERROR: Full traceback: {traceback.format_exc()}", flush=True)
@ -172,7 +191,10 @@ def dashboard_current(username):
def dashboard_week(username, week):
"""Dashboard for specific week"""
print(f"DEBUG: Dashboard week - username: '{username}', week: {week}", flush=True)
return dashboard(username, week)
response = make_response(dashboard(username, week))
# Set cookie to remember this username (expires in 30 days)
response.set_cookie('last_username', username, max_age=30*24*60*60)
return response
@app.route('/<username>/<int:week>/refresh', methods=['POST'])
def refresh_scores(username, week):

View file

@ -927,6 +927,45 @@ body {
transform: translateY(-1px);
}
/* Smaller refresh button */
.refresh-btn-small {
background: var(--accent);
color: white;
border: none;
border-radius: 4px;
padding: 4px 8px;
font-size: 12px;
cursor: pointer;
transition: all 0.2s ease;
margin-right: 8px;
}
.refresh-btn-small:hover {
background: var(--accent-hover);
transform: translateY(-1px);
}
/* Change username button */
.change-username-btn {
background: var(--bg-secondary);
color: var(--text-primary);
border: 1px solid var(--border-primary);
border-radius: 4px;
padding: 4px 8px;
font-size: 12px;
text-decoration: none;
cursor: pointer;
transition: all 0.2s ease;
display: inline-block;
}
.change-username-btn:hover {
background: var(--bg-tertiary);
border-color: var(--accent);
color: var(--accent);
transform: translateY(-1px);
}
/* Timezone info */
.timezone-info {
font-size: 0.8rem;

View file

@ -12,11 +12,12 @@
<span class="current-week">Week {{ week }}</span>
<a href="/{{ user.username }}/{{ week + 1 }}{% if is_debug_mode %}?debug=true{% endif %}" class="week-btn">Week {{ week + 1 }} &rarr;</a>
</div>
<!-- Server-side refresh -->
<!-- Server-side refresh and navigation -->
<div class="refresh-nav">
<form method="post" action="/{{ user.username }}/{{ week }}/refresh{% if is_debug_mode %}?debug=true{% endif %}" style="display: inline;">
<button type="submit" class="refresh-btn">🔄 Refresh Scores</button>
<button type="submit" class="refresh-btn-small">Refresh</button>
</form>
<a href="/change-username" class="change-username-btn">Change Username</a>
</div>
<!-- Debug Time Picker (only shown when ?debug=true) -->

View file

@ -13,7 +13,7 @@
<!-- Username input form -->
<form class="username-form" method="get" action="/dashboard">
<input type="text" name="username" placeholder="Enter Sleeper username" required>
<input type="text" name="username" placeholder="Enter Sleeper username" value="{{ last_username }}" required>
<button type="submit">View Dashboard</button>
</form>