From 00f6c6576a29269c48354f0aab1ce6c3b5d68b3a Mon Sep 17 00:00:00 2001 From: Jonny Ervine Date: Sun, 16 Nov 2025 12:47:19 +0800 Subject: [PATCH] UPdate device tracking cookie --- motm_app/helm-chart/motm-app/DEPLOYMENT.md | 3 ++ motm_app/helm-chart/motm-app/README.md | 3 ++ .../helm-chart/motm-app/scripts/deploy.sh | 3 ++ .../motm-app/templates/_helpers.tpl | 3 ++ .../helm-chart/motm-app/templates/hpa.yaml | 3 ++ .../helm-chart/motm-app/templates/pdb.yaml | 3 ++ .../helm-chart/motm-app/templates/pvc.yaml | 3 ++ .../motm-app/templates/serviceaccount.yaml | 3 ++ motm_app/main.py | 41 +++++++++++++++++-- 9 files changed, 61 insertions(+), 4 deletions(-) diff --git a/motm_app/helm-chart/motm-app/DEPLOYMENT.md b/motm_app/helm-chart/motm-app/DEPLOYMENT.md index 2a7a065..3b7ee02 100644 --- a/motm_app/helm-chart/motm-app/DEPLOYMENT.md +++ b/motm_app/helm-chart/motm-app/DEPLOYMENT.md @@ -364,3 +364,6 @@ For issues and questions: + + + diff --git a/motm_app/helm-chart/motm-app/README.md b/motm_app/helm-chart/motm-app/README.md index 9650743..57e936b 100644 --- a/motm_app/helm-chart/motm-app/README.md +++ b/motm_app/helm-chart/motm-app/README.md @@ -244,3 +244,6 @@ For issues and questions, please refer to the application documentation or creat + + + diff --git a/motm_app/helm-chart/motm-app/scripts/deploy.sh b/motm_app/helm-chart/motm-app/scripts/deploy.sh index 5ba20d9..8be9c34 100755 --- a/motm_app/helm-chart/motm-app/scripts/deploy.sh +++ b/motm_app/helm-chart/motm-app/scripts/deploy.sh @@ -261,3 +261,6 @@ main "$@" + + + diff --git a/motm_app/helm-chart/motm-app/templates/_helpers.tpl b/motm_app/helm-chart/motm-app/templates/_helpers.tpl index c00f6be..3612eca 100644 --- a/motm_app/helm-chart/motm-app/templates/_helpers.tpl +++ b/motm_app/helm-chart/motm-app/templates/_helpers.tpl @@ -70,3 +70,6 @@ Create the name of the service account to use + + + diff --git a/motm_app/helm-chart/motm-app/templates/hpa.yaml b/motm_app/helm-chart/motm-app/templates/hpa.yaml index bb3f74c..c942c4a 100644 --- a/motm_app/helm-chart/motm-app/templates/hpa.yaml +++ b/motm_app/helm-chart/motm-app/templates/hpa.yaml @@ -37,3 +37,6 @@ spec: + + + diff --git a/motm_app/helm-chart/motm-app/templates/pdb.yaml b/motm_app/helm-chart/motm-app/templates/pdb.yaml index 0b803a7..1d17549 100644 --- a/motm_app/helm-chart/motm-app/templates/pdb.yaml +++ b/motm_app/helm-chart/motm-app/templates/pdb.yaml @@ -23,3 +23,6 @@ spec: + + + diff --git a/motm_app/helm-chart/motm-app/templates/pvc.yaml b/motm_app/helm-chart/motm-app/templates/pvc.yaml index 9351a07..d2e7689 100644 --- a/motm_app/helm-chart/motm-app/templates/pvc.yaml +++ b/motm_app/helm-chart/motm-app/templates/pvc.yaml @@ -26,3 +26,6 @@ spec: + + + diff --git a/motm_app/helm-chart/motm-app/templates/serviceaccount.yaml b/motm_app/helm-chart/motm-app/templates/serviceaccount.yaml index 8c7ae36..6f2571c 100644 --- a/motm_app/helm-chart/motm-app/templates/serviceaccount.yaml +++ b/motm_app/helm-chart/motm-app/templates/serviceaccount.yaml @@ -17,3 +17,6 @@ metadata: + + + diff --git a/motm_app/main.py b/motm_app/main.py index 24193c5..d373fe1 100644 --- a/motm_app/main.py +++ b/motm_app/main.py @@ -19,7 +19,7 @@ importlib.reload(database) importlib.reload(db_config) from app import app, randomUrlSuffix -from flask import Flask, flash, render_template, request, redirect, url_for, jsonify +from flask import Flask, flash, render_template, request, redirect, url_for, jsonify, make_response from sqlalchemy import text from flask_wtf import FlaskForm from flask_bootstrap import Bootstrap @@ -36,6 +36,9 @@ from fixture_scraper import FixtureScraper, get_next_hkfc_c_fixture, get_opponen from club_scraper import ClubScraper, get_hk_hockey_clubs, expand_club_abbreviation from s3_config import s3_config_manager, s3_asset_service +# Persistent device ID cookie name +DEVICE_COOKIE_NAME = 'motm_device_id' + # Custom authentication class that uses database class DatabaseBasicAuth(BasicAuth): def check_credentials(self, username, password): @@ -146,6 +149,23 @@ def generate_device_id(request): return device_id +def get_or_create_device_id(request): + """ + Return a persistent device identifier using a long-lived cookie. + Falls back to a header/IP fingerprint only if absolutely necessary. + + Returns a tuple of (device_id, created) where created indicates whether + a new cookie needs to be set on the response. + """ + # Prefer existing cookie to uniquely identify a device/browser + cookie_device_id = request.cookies.get(DEVICE_COOKIE_NAME) + if cookie_device_id: + return cookie_device_id, False + + # Create a new random UUID (more stable than header/IP fingerprints) + new_device_id = uuid.uuid4().hex + return new_device_id, True + def is_admin_authenticated(request): """Check if the current request is authenticated as admin""" @@ -490,8 +510,8 @@ def vote_thanks(): update_player_totals(_motm) update_player_totals(_dotd) - # Generate device identifier and record vote for tracking - device_id = generate_device_id(request) + # Generate or retrieve persistent device identifier and record vote for tracking + device_id, device_created = get_or_create_device_id(request) sql_device = text(""" INSERT INTO device_votes (device_id, fixture_date, motm_player_number, dotd_player_number, motm_player_name, dotd_player_name, ip_address, user_agent) @@ -531,7 +551,20 @@ def vote_thanks(): # Fallback to static URL simpsons_url = "/static/images/simpsons-monkeys.jpg" - return render_template('vote_thanks.html', simpsons_image_url=simpsons_url) + # Build response and set device ID cookie if newly created + response = make_response(render_template('vote_thanks.html', simpsons_image_url=simpsons_url)) + if device_created: + # Two years in seconds + max_age_seconds = 60 * 60 * 24 * 730 + response.set_cookie( + DEVICE_COOKIE_NAME, + device_id, + max_age=max_age_seconds, + httponly=True, + samesite='Lax', + secure=bool(request.is_secure) + ) + return response else: return 'Ouch ... something went wrong here' except Exception as e: