Compare commits

..

2 Commits

Author SHA1 Message Date
Peter Stockings
c21a7890f3 Allows users to hide their check-ins from other users 2026-02-24 13:17:09 +11:00
Peter Stockings
d6885a8339 Fix timezone to Sydney 2026-02-23 08:32:18 +11:00
12 changed files with 286 additions and 56 deletions

View File

@@ -1,7 +1,10 @@
from datetime import datetime, timezone, timedelta
from flask import Flask
from app.config import Config
from app.db import init_db, close_db
SYDNEY_TZ = timezone(timedelta(hours=11))
def create_app():
app = Flask(__name__)
@@ -11,6 +14,15 @@ def create_app():
init_db(app)
app.teardown_appcontext(close_db)
# Jinja2 filter: convert UTC to Sydney time
@app.template_filter('sydney')
def sydney_time_filter(dt, fmt='%d %b %Y, %H:%M'):
if dt is None:
return ''
if dt.tzinfo is None:
dt = dt.replace(tzinfo=timezone.utc)
return dt.astimezone(SYDNEY_TZ).strftime(fmt)
# Register blueprints
from app.routes.auth import bp as auth_bp
from app.routes.dashboard import bp as dashboard_bp

View File

@@ -1,6 +1,8 @@
from flask import Blueprint, jsonify
from flask import Blueprint, jsonify, session
from app import SYDNEY_TZ
from app.auth import login_required
from app.db import query
from app.db import query, query_one
from datetime import timezone
bp = Blueprint("api", __name__, url_prefix="/api")
@@ -9,6 +11,12 @@ bp = Blueprint("api", __name__, url_prefix="/api")
@login_required
def chart_data(user_id):
"""Return weight & BMI over time for Chart.js."""
# Privacy guard: don't expose private user data to others
if user_id != session.get("user_id"):
target = query_one("SELECT is_private FROM users WHERE id = %s", (user_id,))
if target and target["is_private"]:
return jsonify({"labels": [], "weights": [], "bmis": []})
checkins = query(
"""SELECT weight_kg, bmi, checked_in_at
FROM checkins WHERE user_id = %s
@@ -16,7 +24,7 @@ def chart_data(user_id):
(user_id,),
)
labels = [c["checked_in_at"].strftime("%d %b") for c in checkins]
labels = [c["checked_in_at"].replace(tzinfo=timezone.utc).astimezone(SYDNEY_TZ).strftime("%d %b") for c in checkins]
weights = [float(c["weight_kg"]) for c in checkins]
bmis = [float(c["bmi"]) if c["bmi"] else None for c in checkins]
@@ -41,6 +49,7 @@ def comparison():
(SELECT weight_kg FROM checkins WHERE user_id = u.id ORDER BY checked_in_at DESC LIMIT 1) as current_weight
FROM users u
WHERE (SELECT COUNT(*) FROM checkins WHERE user_id = u.id) > 0
AND u.is_private = FALSE
ORDER BY u.display_name
""")
@@ -72,6 +81,12 @@ def comparison():
@login_required
def weekly_change(user_id):
"""Return weekly weight changes for bar chart."""
# Privacy guard: don't expose private user data to others
if user_id != session.get("user_id"):
target = query_one("SELECT is_private FROM users WHERE id = %s", (user_id,))
if target and target["is_private"]:
return jsonify({"labels": [], "changes": []})
checkins = query(
"""SELECT weight_kg, checked_in_at
FROM checkins WHERE user_id = %s
@@ -88,7 +103,7 @@ def weekly_change(user_id):
prev_w = float(checkins[i - 1]["weight_kg"])
curr_w = float(checkins[i]["weight_kg"])
change = round(curr_w - prev_w, 1)
label = checkins[i]["checked_in_at"].strftime("%d %b")
label = checkins[i]["checked_in_at"].replace(tzinfo=timezone.utc).astimezone(SYDNEY_TZ).strftime("%d %b")
labels.append(label)
changes.append(change)

View File

@@ -16,6 +16,7 @@ def signup():
gender = request.form.get("gender") or None
goal_weight_kg = request.form.get("goal_weight_kg") or None
starting_weight_kg = request.form.get("starting_weight_kg") or None
is_private = request.form.get("is_private") == "on"
# Validation
if not username or not password:
@@ -35,9 +36,9 @@ def signup():
# Create user
password_hash = generate_password_hash(password)
user = execute_returning(
"""INSERT INTO users (username, password_hash, display_name, height_cm, age, gender, goal_weight_kg, starting_weight_kg)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s) RETURNING id""",
(username, password_hash, display_name or username, height_cm, age, gender, goal_weight_kg, starting_weight_kg),
"""INSERT INTO users (username, password_hash, display_name, height_cm, age, gender, goal_weight_kg, starting_weight_kg, is_private)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s) RETURNING id""",
(username, password_hash, display_name or username, height_cm, age, gender, goal_weight_kg, starting_weight_kg, is_private),
)
session["user_id"] = user["id"]

View File

@@ -48,9 +48,10 @@ def index():
SELECT c.*, u.display_name, u.username
FROM checkins c
JOIN users u ON c.user_id = u.id
WHERE u.is_private = FALSE OR u.id = %s
ORDER BY c.checked_in_at DESC
LIMIT 10
""")
""", (user["id"],))
# Milestones
milestones = query(

View File

@@ -21,6 +21,7 @@ def index():
(SELECT COUNT(*) FROM checkins WHERE user_id = u.id) as total_checkins,
(SELECT checked_in_at FROM checkins WHERE user_id = u.id ORDER BY checked_in_at DESC LIMIT 1) as last_checkin
FROM users u
WHERE u.is_private = FALSE
ORDER BY u.created_at
""")

View File

@@ -22,14 +22,15 @@ def update():
gender = request.form.get("gender") or None
goal_weight_kg = request.form.get("goal_weight_kg") or None
starting_weight_kg = request.form.get("starting_weight_kg") or None
is_private = request.form.get("is_private") == "on"
execute(
"""UPDATE users
SET display_name = %s, height_cm = %s, age = %s, gender = %s,
goal_weight_kg = %s, starting_weight_kg = %s
goal_weight_kg = %s, starting_weight_kg = %s, is_private = %s
WHERE id = %s""",
(display_name or user["username"], height_cm, age, gender,
goal_weight_kg, starting_weight_kg, user["id"]),
goal_weight_kg, starting_weight_kg, is_private, user["id"]),
)
if request.headers.get("HX-Request"):

View File

@@ -43,7 +43,9 @@
--shadow-glow: 0 0 20px rgba(59, 130, 246, 0.15);
}
*, *::before, *::after {
*,
*::before,
*::after {
margin: 0;
padding: 0;
box-sizing: border-box;
@@ -88,7 +90,9 @@ body {
font-size: 1.15rem;
}
.nav-icon { font-size: 1.4rem; }
.nav-icon {
font-size: 1.4rem;
}
.nav-links {
display: flex;
@@ -118,7 +122,9 @@ body {
background: var(--accent-glow);
}
.nav-link-icon { font-size: 1.1rem; }
.nav-link-icon {
font-size: 1.1rem;
}
.nav-toggle {
display: none;
@@ -139,7 +145,10 @@ body {
transition: all 0.2s;
}
.nav-actions { display: flex; align-items: center; }
.nav-actions {
display: flex;
align-items: center;
}
/* ========== CONTAINER ========== */
.container {
@@ -149,7 +158,9 @@ body {
}
/* ========== FLASH MESSAGES ========== */
.flash-messages { margin-bottom: 1rem; }
.flash-messages {
margin-bottom: 1rem;
}
.flash {
padding: 0.75rem 1rem;
@@ -162,9 +173,23 @@ body {
animation: slideIn 0.3s ease;
}
.flash-success { background: var(--success-bg); color: var(--success); border: 1px solid rgba(16, 185, 129, 0.2); }
.flash-error { background: var(--danger-bg); color: var(--danger); border: 1px solid rgba(239, 68, 68, 0.2); }
.flash-info { background: rgba(6, 182, 212, 0.1); color: var(--info); border: 1px solid rgba(6, 182, 212, 0.2); }
.flash-success {
background: var(--success-bg);
color: var(--success);
border: 1px solid rgba(16, 185, 129, 0.2);
}
.flash-error {
background: var(--danger-bg);
color: var(--danger);
border: 1px solid rgba(239, 68, 68, 0.2);
}
.flash-info {
background: rgba(6, 182, 212, 0.1);
color: var(--info);
border: 1px solid rgba(6, 182, 212, 0.2);
}
.flash-close {
background: none;
@@ -175,7 +200,9 @@ body {
opacity: 0.7;
}
.flash-close:hover { opacity: 1; }
.flash-close:hover {
opacity: 1;
}
/* ========== PAGE HEADERS ========== */
.page-header {
@@ -263,7 +290,9 @@ body {
transition: opacity 0.2s;
}
.stat-card:hover::before { opacity: 1; }
.stat-card:hover::before {
opacity: 1;
}
.stat-label {
font-size: 0.75rem;
@@ -287,8 +316,13 @@ body {
margin-top: 0.25rem;
}
.stat-change.positive { color: var(--success); }
.stat-change.negative { color: var(--danger); }
.stat-change.positive {
color: var(--success);
}
.stat-change.negative {
color: var(--danger);
}
/* ========== FORMS ========== */
.form-group {
@@ -321,7 +355,9 @@ body {
box-shadow: 0 0 0 3px var(--accent-glow);
}
.form-input::placeholder { color: var(--text-muted); }
.form-input::placeholder {
color: var(--text-muted);
}
select.form-input {
appearance: none;
@@ -343,7 +379,10 @@ select.form-input {
align-items: flex-end;
}
.form-inline .form-group { flex: 1; margin-bottom: 0; }
.form-inline .form-group {
flex: 1;
margin-bottom: 0;
}
/* ========== BUTTONS ========== */
.btn {
@@ -396,9 +435,19 @@ select.form-input {
background: var(--bg-card);
}
.btn-sm { padding: 0.4rem 0.8rem; font-size: 0.8rem; }
.btn-lg { padding: 0.85rem 1.75rem; font-size: 1rem; }
.btn-block { width: 100%; }
.btn-sm {
padding: 0.4rem 0.8rem;
font-size: 0.8rem;
}
.btn-lg {
padding: 0.85rem 1.75rem;
font-size: 1rem;
}
.btn-block {
width: 100%;
}
/* ========== TABLES ========== */
.table-wrap {
@@ -431,9 +480,13 @@ td {
color: var(--text-secondary);
}
tr:last-child td { border-bottom: none; }
tr:last-child td {
border-bottom: none;
}
tr:hover td { background: var(--bg-card-hover); }
tr:hover td {
background: var(--bg-card-hover);
}
/* ========== LEADERBOARD ========== */
.rank-badge {
@@ -447,10 +500,25 @@ tr:hover td { background: var(--bg-card-hover); }
font-size: 0.8rem;
}
.rank-1 { background: linear-gradient(135deg, #fbbf24, #f59e0b); color: #1a1a1a; }
.rank-2 { background: linear-gradient(135deg, #cbd5e1, #94a3b8); color: #1a1a1a; }
.rank-3 { background: linear-gradient(135deg, #d97706, #b45309); color: white; }
.rank-other { background: var(--bg-secondary); color: var(--text-muted); }
.rank-1 {
background: linear-gradient(135deg, #fbbf24, #f59e0b);
color: #1a1a1a;
}
.rank-2 {
background: linear-gradient(135deg, #cbd5e1, #94a3b8);
color: #1a1a1a;
}
.rank-3 {
background: linear-gradient(135deg, #d97706, #b45309);
color: white;
}
.rank-other {
background: var(--bg-secondary);
color: var(--text-muted);
}
.progress-bar-track {
width: 100%;
@@ -476,7 +544,9 @@ tr:hover td { background: var(--bg-card-hover); }
border-bottom: 1px solid var(--border);
}
.activity-item:last-child { border-bottom: none; }
.activity-item:last-child {
border-bottom: none;
}
.activity-avatar {
width: 32px;
@@ -492,7 +562,9 @@ tr:hover td { background: var(--bg-card-hover); }
flex-shrink: 0;
}
.activity-content { flex: 1; }
.activity-content {
flex: 1;
}
.activity-name {
font-weight: 600;
@@ -594,7 +666,9 @@ tr:hover td { background: var(--bg-card-hover); }
font-weight: 500;
}
.auth-card .auth-footer a:hover { text-decoration: underline; }
.auth-card .auth-footer a:hover {
text-decoration: underline;
}
/* ========== GRID LAYOUTS ========== */
.grid-2 {
@@ -611,17 +685,34 @@ tr:hover td { background: var(--bg-card-hover); }
/* ========== ANIMATIONS ========== */
@keyframes slideIn {
from { transform: translateY(-8px); opacity: 0; }
to { transform: translateY(0); opacity: 1; }
from {
transform: translateY(-8px);
opacity: 0;
}
to {
transform: translateY(0);
opacity: 1;
}
}
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
from {
opacity: 0;
}
.htmx-added { animation: slideIn 0.3s ease; }
.htmx-settling { animation: fadeIn 0.2s ease; }
to {
opacity: 1;
}
}
.htmx-added {
animation: slideIn 0.3s ease;
}
.htmx-settling {
animation: fadeIn 0.2s ease;
}
/* ========== DELETE BUTTON ========== */
.btn-icon {
@@ -671,6 +762,58 @@ tr:hover td { background: var(--bg-card-hover); }
margin: 0 auto 1rem;
}
/* ========== TOGGLE SWITCH ========== */
.toggle-switch {
position: relative;
display: inline-block;
width: 44px;
height: 24px;
flex-shrink: 0;
}
.toggle-switch input {
opacity: 0;
width: 0;
height: 0;
}
.toggle-slider {
position: absolute;
inset: 0;
background: var(--bg-secondary);
border: 1px solid var(--border);
border-radius: 24px;
cursor: pointer;
transition: all 0.3s ease;
}
.toggle-slider::before {
content: '';
position: absolute;
left: 3px;
top: 50%;
transform: translateY(-50%);
width: 16px;
height: 16px;
background: var(--text-muted);
border-radius: 50%;
transition: all 0.3s ease;
}
.toggle-switch input:checked+.toggle-slider {
background: var(--accent);
border-color: var(--accent);
}
.toggle-switch input:checked+.toggle-slider::before {
left: 23px;
background: white;
}
.toggle-switch input:focus+.toggle-slider {
box-shadow: 0 0 0 3px var(--accent-glow);
}
/* ========== RESPONSIVE ========== */
@media (max-width: 768px) {
.navbar {
@@ -689,13 +832,21 @@ tr:hover td { background: var(--bg-card-hover); }
padding: 0.5rem;
}
.nav-links.open { display: flex; }
.nav-links.open {
display: flex;
}
.nav-toggle { display: flex; }
.nav-toggle {
display: flex;
}
.nav-actions { display: none; }
.nav-actions {
display: none;
}
.container { padding: 1rem; }
.container {
padding: 1rem;
}
.stats-grid {
grid-template-columns: repeat(2, 1fr);
@@ -705,7 +856,8 @@ tr:hover td { background: var(--bg-card-hover); }
grid-template-columns: 1fr;
}
.grid-2, .grid-3 {
.grid-2,
.grid-3 {
grid-template-columns: 1fr;
}
@@ -714,19 +866,31 @@ tr:hover td { background: var(--bg-card-hover); }
align-items: stretch;
}
.form-inline .form-group { margin-bottom: 0.75rem; }
.form-inline .form-group {
margin-bottom: 0.75rem;
}
.page-header h1 { font-size: 1.4rem; }
.page-header h1 {
font-size: 1.4rem;
}
.stat-value { font-size: 1.4rem; }
.stat-value {
font-size: 1.4rem;
}
.auth-card { margin: 1rem; padding: 1.5rem; }
.auth-card {
margin: 1rem;
padding: 1.5rem;
}
.table-wrap {
font-size: 0.8rem;
}
th, td { padding: 0.5rem 0.65rem; }
th,
td {
padding: 0.5rem 0.65rem;
}
}
@media (max-width: 480px) {

View File

@@ -75,7 +75,7 @@
<div style="font-weight: 600; font-size: 1.1rem;">{{ '%.1f' % (c.weight_kg | float) }} kg</div>
<div class="activity-detail">
{% if c.bmi %}BMI {{ '%.1f' % (c.bmi | float) }} · {% endif %}
{{ c.checked_in_at.strftime('%d %b %Y, %H:%M') }}
{{ c.checked_in_at | sydney }}
</div>
</div>
</div>
@@ -131,7 +131,7 @@
<div class="activity-content">
<div class="activity-name">{{ a.display_name or a.username }}</div>
<div class="activity-detail">Logged {{ '%.1f' % (a.weight_kg | float) }} kg · {{
a.checked_in_at.strftime('%d %b, %H:%M') }}</div>
a.checked_in_at | sydney('%d %b, %H:%M') }}</div>
</div>
</div>
{% endfor %}

View File

@@ -1,5 +1,5 @@
<tr id="checkin-{{ c.id }}">
<td>{{ c.checked_in_at.strftime('%d %b %Y, %H:%M') }}</td>
<td>{{ c.checked_in_at | sydney }}</td>
<td style="font-weight: 600;">{{ '%.1f' % (c.weight_kg | float) }} kg</td>
<td>{{ '%.1f' % (c.bmi | float) if c.bmi else '—' }}</td>
<td>{{ c.notes or '—' }}</td>

View File

@@ -50,6 +50,21 @@
</div>
</div>
<div class="form-group" style="margin-top: 0.75rem; padding-top: 0.75rem; border-top: 1px solid var(--border);">
<label class="toggle-label" for="is_private"
style="display: flex; align-items: center; gap: 0.75rem; cursor: pointer;">
<span class="toggle-switch">
<input type="checkbox" id="is_private" name="is_private" {{ 'checked' if user.is_private }}>
<span class="toggle-slider"></span>
</span>
<span>
<span style="font-weight: 600; color: var(--text-primary);">🔒 Private Account</span>
<span style="display: block; font-size: 0.8rem; color: var(--text-muted); margin-top: 0.15rem;">Only
you can see your check-ins</span>
</span>
</label>
</div>
<button type="submit" class="btn btn-primary" style="margin-top: 0.5rem;">Save Changes</button>
</form>
</div>

View File

@@ -61,6 +61,22 @@
step="0.1">
</div>
<div class="form-group" style="margin-top: 0.5rem;">
<label class="toggle-label" for="is_private"
style="display: flex; align-items: center; gap: 0.75rem; cursor: pointer;">
<span class="toggle-switch">
<input type="checkbox" id="is_private" name="is_private">
<span class="toggle-slider"></span>
</span>
<span>
<span style="font-weight: 600; color: var(--text-primary);">🔒 Private Account</span>
<span
style="display: block; font-size: 0.8rem; color: var(--text-muted); margin-top: 0.15rem;">Only
you can see your check-ins</span>
</span>
</label>
</div>
<button type="submit" class="btn btn-primary btn-block btn-lg" style="margin-top: 0.5rem;">Create
Account</button>
</form>

View File

@@ -0,0 +1,4 @@
-- Migration 002: Add private account flag
-- Allows users to hide their check-ins from other users
ALTER TABLE users ADD COLUMN IF NOT EXISTS is_private BOOLEAN DEFAULT FALSE;