From a6febae8a8fe62f4bbd23a5c50f0188e8dd77ba3 Mon Sep 17 00:00:00 2001 From: Jonny Ervine Date: Tue, 7 Oct 2025 22:03:36 +0800 Subject: [PATCH] Refactoring fixes and adding functionality back --- motm_app/db_setup.py | 6 +- motm_app/main.py | 60 +++- motm_app/s3_config.py | 8 +- motm_app/static/images/simpsons-monkeys.jpg | 1 + motm_app/templates/admin_dashboard.html | 79 +++++ motm_app/templates/admin_dashboard_new.html | 1 + motm_app/templates/base.html | 2 +- motm_app/templates/error_new.html | 1 + motm_app/templates/index.html | 2 +- motm_app/templates/index_new.html | 3 +- motm_app/templates/match_comments_new.html | 1 + motm_app/templates/motm_admin.html | 366 +++++++++++--------- motm_app/templates/motm_vote.html | 104 +++--- motm_app/templates/motm_vote_new.html | 105 +++--- motm_app/templates/poty_chart.html | 6 + motm_app/templates/vote_thanks.html | 5 +- motm_app/templates/vote_thanks_new.html | 6 +- 17 files changed, 458 insertions(+), 298 deletions(-) create mode 100644 motm_app/static/images/simpsons-monkeys.jpg diff --git a/motm_app/db_setup.py b/motm_app/db_setup.py index 61f5bd6..87b8f49 100644 --- a/motm_app/db_setup.py +++ b/motm_app/db_setup.py @@ -261,9 +261,9 @@ class DatabaseConfigManager: # Create sample fixtures (only if they don't exist) fixtures_data = [ - {'fixture_number': 1, 'date': datetime(2024, 1, 15), 'home_team': 'HKFC C', 'away_team': 'KCC A', 'venue': 'HKFC'}, - {'fixture_number': 2, 'date': datetime(2024, 1, 22), 'home_team': 'USRC A', 'away_team': 'HKFC C', 'venue': 'USRC'}, - {'fixture_number': 3, 'date': datetime(2024, 1, 29), 'home_team': 'HKFC C', 'away_team': 'Valley A', 'venue': 'HKFC'}, + {'fixture_number': 1, 'date': datetime(2025, 1, 15), 'home_team': 'HKFC C', 'away_team': 'KCC A', 'venue': 'HKFC'}, + {'fixture_number': 2, 'date': datetime(2025, 1, 22), 'home_team': 'USRC A', 'away_team': 'HKFC C', 'venue': 'USRC'}, + {'fixture_number': 3, 'date': datetime(2025, 1, 29), 'home_team': 'HKFC C', 'away_team': 'Valley A', 'venue': 'HKFC'}, ] for fixture_data in fixtures_data: diff --git a/motm_app/main.py b/motm_app/main.py index 4bc82cf..c608b4f 100644 --- a/motm_app/main.py +++ b/motm_app/main.py @@ -269,8 +269,21 @@ def match_comments(): return render_template('error.html', message="Database not initialized. Please go to Database Setup to initialize the database.") _oppo = row[0]['nextclub'] - commentDate = row[0]['nextdate'].strftime('%Y-%m-%d') - _matchDate = row[0]['nextdate'].strftime('%Y_%m_%d') + + # Handle case where nextdate is None - use most recent comment date + if row[0]['nextdate']: + commentDate = row[0]['nextdate'].strftime('%Y-%m-%d') + _matchDate = row[0]['nextdate'].strftime('%Y-%m-%d') + else: + # Get the most recent comment date + sql_recent = text("SELECT matchDate FROM _motmcomments ORDER BY matchDate DESC LIMIT 1") + recent_result = sql_read(sql_recent) + if recent_result: + commentDate = recent_result[0]['matchDate'] + _matchDate = recent_result[0]['matchDate'] + else: + commentDate = '2025-01-15' # Fallback + _matchDate = '2025-01-15' # Get HKFC logo from clubs table using signed URLs (with authentication) hkfcLogo = s3_asset_service.get_asset_url('images/hkfc_logo.png') # Default fallback @@ -376,7 +389,24 @@ def vote_thanks(): sql3 = text("INSERT INTO _motmcomments (matchDate, comment) VALUES (:match_date, :comment)") sql_write(sql3, {'match_date': _matchDate, 'comment': _fixed_comments}) - return render_template('vote_thanks.html') + # Get Simpsons monkeys image URL with fallback + try: + # First try to get from S3 + simpsons_url = s3_asset_service.get_asset_url('images/simpsons-monkeys.jpg') + print(f"DEBUG: Simpsons image URL: {simpsons_url}") + + # If S3 is disabled or URL is fallback, use static + if simpsons_url.startswith('/static/'): + print("DEBUG: Using fallback static URL") + else: + print("DEBUG: Using S3 URL") + + except Exception as e: + print(f"DEBUG: Error getting Simpsons image: {e}") + # Fallback to static URL + simpsons_url = "/static/images/simpsons-monkeys.jpg" + + return render_template('vote_thanks.html', simpsons_image_url=simpsons_url) else: return 'Ouch ... something went wrong here' except Exception as e: @@ -513,7 +543,15 @@ def motm_admin(): form.nextOppoClub.choices = [(oppo['hockeyclub'], oppo['hockeyclub']) for oppo in clubs] form.currMotM.choices = [(player['playernumber'], player['playerforenames'] + " " + player['playersurname']) for player in players] form.currDotD.choices = [(player['playernumber'], player['playerforenames'] + " " + player['playersurname']) for player in players] - clubLogo = settings[0]['oppologo'] + # Get the opposition logo using S3 service + clubLogo = s3_asset_service.get_asset_url('images/default_logo.png') # Default fallback + if settings and settings[0]['nextclub']: + nextClub = settings[0]['nextclub'] + # Get the club logo from the clubs table + sql_club_logo = text("SELECT logo_url FROM clubs WHERE hockey_club = :club_name") + club_logo_result = sql_read(sql_club_logo, {'club_name': nextClub}) + if club_logo_result and club_logo_result[0]['logo_url']: + clubLogo = s3_asset_service.get_logo_url(club_logo_result[0]['logo_url'], nextClub) return render_template('motm_admin.html', form=form, nextOppoLogo=clubLogo) @@ -1451,6 +1489,11 @@ def get_next_fixture(): # Get opponent club information opponent_club_info = get_opponent_club_info(fixture['opponent']) + # Get the opponent logo URL using S3 service + opponent_logo_url = s3_asset_service.get_asset_url('images/default_logo.png') # Default fallback + if opponent_club_info and opponent_club_info.get('logo_url'): + opponent_logo_url = s3_asset_service.get_logo_url(opponent_club_info['logo_url'], opponent_club_info['club_name']) + # Format the fixture data for JSON response fixture_data = { 'success': True, @@ -1461,6 +1504,7 @@ def get_next_fixture(): 'opponent': fixture['opponent'], 'opponent_club': get_opponent_club_name(fixture['opponent']), 'opponent_club_info': opponent_club_info, + 'opponent_logo_url': opponent_logo_url, 'is_home': fixture['is_home'], 'home_team': fixture['home_team'], 'away_team': fixture['away_team'], @@ -1683,9 +1727,9 @@ def poty_results(): # Only include players with votes if motm_total > 0 or dotd_total > 0: results.append({ - 'playername': player['playername'], - 'motmtotal': motm_total, - 'dotdtotal': dotd_total + 'playerName': player['playername'], # Fixed field name to match JavaScript + 'motmTotal': motm_total, # Fixed field name to match JavaScript + 'dotdTotal': dotd_total # Fixed field name to match JavaScript }) print(f"Dynamic POTY Results: {results}") @@ -1703,7 +1747,7 @@ def voting_chart(): if date_result: matchDate = str(date_result[0]['nextdate']).replace('-', '') else: - matchDate = '20251012' # Default fallback + matchDate = '20251015' # Default fallback return render_template('vote_chart.html', _matchDate=matchDate) diff --git a/motm_app/s3_config.py b/motm_app/s3_config.py index c5b1ca1..4e85b76 100644 --- a/motm_app/s3_config.py +++ b/motm_app/s3_config.py @@ -203,7 +203,13 @@ class S3AssetService: region_name=config.get('aws_region', 'us-east-1'), endpoint_url=endpoint_url, use_ssl=use_ssl, - verify=True # Enable SSL certificate verification + verify=True, # Enable SSL certificate verification + config=boto3.session.Config( + s3={ + 'addressing_style': 'path' + }, + signature_version='s3v4' + ) ) else: # Create AWS S3 client diff --git a/motm_app/static/images/simpsons-monkeys.jpg b/motm_app/static/images/simpsons-monkeys.jpg new file mode 100644 index 0000000..31d95e8 --- /dev/null +++ b/motm_app/static/images/simpsons-monkeys.jpg @@ -0,0 +1 @@ +iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg== diff --git a/motm_app/templates/admin_dashboard.html b/motm_app/templates/admin_dashboard.html index fad0f81..ced4664 100644 --- a/motm_app/templates/admin_dashboard.html +++ b/motm_app/templates/admin_dashboard.html @@ -56,6 +56,15 @@ +
+ + +
+
MOTM Management
+ Reset & drop columns +
+
+
@@ -150,6 +159,76 @@
+ +
+ +
+
diff --git a/motm_app/templates/admin_dashboard_new.html b/motm_app/templates/admin_dashboard_new.html index fad0f81..579456f 100644 --- a/motm_app/templates/admin_dashboard_new.html +++ b/motm_app/templates/admin_dashboard_new.html @@ -278,3 +278,4 @@
{% endblock %} + diff --git a/motm_app/templates/base.html b/motm_app/templates/base.html index 7207d95..eabf31e 100644 --- a/motm_app/templates/base.html +++ b/motm_app/templates/base.html @@ -306,7 +306,7 @@

- {% if current_year %}{{ current_year }}{% else %}2024{% endif %} + {% if current_year %}{{ current_year }}{% else %}2025{% endif %}

diff --git a/motm_app/templates/error_new.html b/motm_app/templates/error_new.html index a5e112e..605fc9b 100644 --- a/motm_app/templates/error_new.html +++ b/motm_app/templates/error_new.html @@ -33,3 +33,4 @@ {% endblock %} + diff --git a/motm_app/templates/index.html b/motm_app/templates/index.html index 416ef13..8438a43 100644 --- a/motm_app/templates/index.html +++ b/motm_app/templates/index.html @@ -227,7 +227,7 @@
Current Season
-

2024-2025 Hockey Season

+

2025-2026 Hockey Season

diff --git a/motm_app/templates/index_new.html b/motm_app/templates/index_new.html index 416ef13..ad158a0 100644 --- a/motm_app/templates/index_new.html +++ b/motm_app/templates/index_new.html @@ -227,7 +227,7 @@
Current Season
-

2024-2025 Hockey Season

+

2025-2026 Hockey Season

@@ -235,3 +235,4 @@ {% endblock %} + diff --git a/motm_app/templates/match_comments_new.html b/motm_app/templates/match_comments_new.html index afd7211..25ea438 100644 --- a/motm_app/templates/match_comments_new.html +++ b/motm_app/templates/match_comments_new.html @@ -74,3 +74,4 @@ {% endblock %} + diff --git a/motm_app/templates/motm_admin.html b/motm_app/templates/motm_admin.html index a24e346..4b6752e 100644 --- a/motm_app/templates/motm_admin.html +++ b/motm_app/templates/motm_admin.html @@ -1,176 +1,212 @@ - - - HKFC Men's C Team - MotM and DotD vote admin - - - - - - -

HKFC Men's C Team MotM and DotD online vote admin page

-
- - Back to Admin Dashboard - +{% extends "base.html" %} + +{% block title %}MOTM Management - HKFC Men's C Team MOTM System{% endblock %} + +{% block content %} + +
+
+
+
+

+ + MOTM Management +

+

Manage Man of the Match and Dick of the Day settings

+ + Back to Admin Dashboard + +
+
- {% with messages = get_flashed_messages() %} - {% if messages %} - {% for message in messages %} - - {% endfor %} - {% endif %} - {% endwith %} - - -
-

+

+ +
+
+
+
+
+ Next Match Configuration +
+
+
{{ form.csrf_token }} - HKFC C Next Opponent: -
-
-
-
- -
-
-
- - - View HK Hockey Fixtures + + View HK Hockey Fixtures - -
+
+
- -
-
-
- Date: - {{ form.nextMatchDate(class_="form-control", id="nextMatchDate") }} -
-
-
-
-
-
-
- Opposition - {{ form.nextOppoTeam(class_="form-control", id="nextOppoTeam") }} -
-
-
-
-
-
- Current Man of the Match: - {{ form.currMotM(class_="form-control") }} -
-
-
-
- Current Dick of the Day: - {{ form.currDotD(class_="form-control") }} -
-
-
- {% if not form.currMotM.choices or form.currMotM.choices|length == 0 %} -
-
-
- Note: No players available for previous MOTM/DotD. This is normal if you haven't set up a match squad yet. You can still save the match details. -
-
-
- {% endif %} -

- {{ form.saveButton(class_="btn btn-success") }} - {{ form.activateButton(class_="btn btn-primary") }} - Cancel -

-
-
-
-
-

- + + +
+
+ + {{ form.nextMatchDate(class_="form-control", **{"id": "nextMatchDate"}) }} +
+
+ + {{ form.nextOppoTeam(class_="form-control", **{"id": "nextOppoTeam"}) }} +
+
+ + +
+
+ + {{ form.currMotM(class_="form-select") }} +
+
+ + {{ form.currDotD(class_="form-select") }} +
+
+ + + {% if not form.currMotM.choices or form.currMotM.choices|length == 0 %} +
+
+
+ + Note: No players available for previous MOTM/DotD. This is normal if you haven't set up a match squad yet. You can still save the match details. +
+
+
+ {% endif %} + + +
+
+
+ {{ form.saveButton(class_="btn btn-success") }} + {{ form.activateButton(class_="btn btn-primary") }} + + Cancel + +
+
+
+ +
+
+
+
- - - + } + + // Show fixture information + let clubInfo = ''; + if (data.opponent_club_info) { + const club = data.opponent_club_info; + const confidence = club.match_result ? club.match_result.confidence : 'unknown'; + const matchType = club.match_result ? club.match_result.match_type : 'unknown'; + + clubInfo = '
'; + clubInfo += 'Club: ' + club.club_name; + if (club.logo_url) { + clubInfo += ' | Logo'; + } + clubInfo += ' | Match: ' + matchType + ' (' + confidence + ')'; + clubInfo += ''; + } + + infoElement.innerHTML = 'Next Match: ' + + data.date_formatted + ' vs ' + data.opponent + + ' (' + (data.is_home ? 'Home' : 'Away') + ' - ' + data.venue + ')' + + '
Division: ' + data.division + ' | Time: ' + data.time + '' + + clubInfo; + infoElement.style.display = 'block'; + + statusElement.innerHTML = 'Fixture loaded!'; + + // Clear status message after 3 seconds + setTimeout(function() { + statusElement.innerHTML = ''; + }, 3000); + } else { + statusElement.innerHTML = '' + data.message + ''; + infoElement.style.display = 'none'; + } + loadBtn.disabled = false; + }) + .catch(error => { + console.error('Error:', error); + statusElement.innerHTML = 'Error loading fixture'; + infoElement.style.display = 'none'; + loadBtn.disabled = false; + }); + } + + // Auto-load fixture on page load (optional) + document.addEventListener('DOMContentLoaded', function() { + // Uncomment the next line if you want to auto-load the fixture when the page loads + // loadNextFixture(); + }); + +{% endblock %} diff --git a/motm_app/templates/motm_vote.html b/motm_app/templates/motm_vote.html index 98f9329..4e3dc72 100644 --- a/motm_app/templates/motm_vote.html +++ b/motm_app/templates/motm_vote.html @@ -38,6 +38,7 @@
+{% if comment and comment != "No comments added yet" %}
@@ -47,28 +48,27 @@
- {% for item in comment %} -
-

- - {{ item.comment }} - -

-
- {% endfor %} +
+

+ + {{ comment }} + +

+
+{% endif %}
-
- +
+
- Man of the Match + Vote for MOTM and DotD
@@ -79,7 +79,7 @@
- {% for player in data %}
+
+ + +
+ +
+ + +
+
-
- -
-
-
- -
- -
-
-
- Dick of the Day -
-
-
-
- {{ form.csrf_token }} - - - -
- - -
- -
-
@@ -198,18 +179,27 @@ {% endblock %} + diff --git a/motm_app/templates/poty_chart.html b/motm_app/templates/poty_chart.html index 015ea61..aa6f77f 100644 --- a/motm_app/templates/poty_chart.html +++ b/motm_app/templates/poty_chart.html @@ -36,6 +36,12 @@ google.charts.load('current', { // load json data function loadData(jsonData) { + if (jsonData.length === 0) { + // Show message when no data + $('#chart_div').html('

No Vote Data Available

No players have received MOTM or DotD votes yet.

Votes will appear here once players start receiving votes.

'); + return; + } + $.each(jsonData, function(index, row) { data.addRow([ row.playerName, diff --git a/motm_app/templates/vote_thanks.html b/motm_app/templates/vote_thanks.html index cf36afa..88066b7 100644 --- a/motm_app/templates/vote_thanks.html +++ b/motm_app/templates/vote_thanks.html @@ -23,10 +23,11 @@
- Counting votes + style="max-height: 300px;" + onerror="this.src='/static/images/simpsons-monkeys.jpg';">
diff --git a/motm_app/templates/vote_thanks_new.html b/motm_app/templates/vote_thanks_new.html index cf36afa..787d695 100644 --- a/motm_app/templates/vote_thanks_new.html +++ b/motm_app/templates/vote_thanks_new.html @@ -23,10 +23,11 @@
- Counting votes + style="max-height: 300px;" + onerror="this.src='/static/images/simpsons-monkeys.jpg';">
@@ -42,3 +43,4 @@
{% endblock %} +