diff --git a/motm_app/main.py b/motm_app/main.py index ae05703..04b4a41 100644 --- a/motm_app/main.py +++ b/motm_app/main.py @@ -517,6 +517,177 @@ def delete_team(team_id): return redirect(url_for('team_management')) +# ==================== DATA IMPORT ==================== + +@app.route('/admin/import', methods=['GET', 'POST']) +@basic_auth.required +def data_import(): + """Import data from Hong Kong Hockey Association""" + form = DataImportForm() + + if form.validate_on_submit(): + if form.import_data.data: + imported_clubs = 0 + imported_teams = 0 + imported_players = 0 + + if form.import_clubs.data: + # Import clubs based on Hong Kong Hockey Association data + clubs_data = [ + {'hockey_club': 'HKFC', 'logo_url': '/static/images/hkfc_logo.png'}, + {'hockey_club': 'KCC', 'logo_url': '/static/images/kcc_logo.png'}, + {'hockey_club': 'USRC', 'logo_url': '/static/images/usrc_logo.png'}, + {'hockey_club': 'Valley', 'logo_url': '/static/images/valley_logo.png'}, + {'hockey_club': 'SSSC', 'logo_url': '/static/images/sssc_logo.png'}, + {'hockey_club': 'Dragons', 'logo_url': '/static/images/dragons_logo.png'}, + {'hockey_club': 'Kai Tak', 'logo_url': '/static/images/kaitak_logo.png'}, + {'hockey_club': 'RHOBA', 'logo_url': '/static/images/rhoba_logo.png'}, + {'hockey_club': 'Elite', 'logo_url': '/static/images/elite_logo.png'}, + {'hockey_club': 'Aquila', 'logo_url': '/static/images/aquila_logo.png'}, + {'hockey_club': 'HKJ', 'logo_url': '/static/images/hkj_logo.png'}, + {'hockey_club': 'Sirius', 'logo_url': '/static/images/sirius_logo.png'}, + {'hockey_club': 'Shaheen', 'logo_url': '/static/images/shaheen_logo.png'}, + {'hockey_club': 'Diocesan', 'logo_url': '/static/images/diocesan_logo.png'}, + {'hockey_club': 'Rhino', 'logo_url': '/static/images/rhino_logo.png'}, + {'hockey_club': 'Khalsa', 'logo_url': '/static/images/khalsa_logo.png'}, + {'hockey_club': 'HKCC', 'logo_url': '/static/images/hkcc_logo.png'}, + {'hockey_club': 'Police', 'logo_url': '/static/images/police_logo.png'}, + {'hockey_club': 'Recreio', 'logo_url': '/static/images/recreio_logo.png'}, + {'hockey_club': 'CSD', 'logo_url': '/static/images/csd_logo.png'}, + {'hockey_club': 'Dutch', 'logo_url': '/static/images/dutch_logo.png'}, + {'hockey_club': 'HKUHC', 'logo_url': '/static/images/hkuhc_logo.png'}, + {'hockey_club': 'Kaitiaki', 'logo_url': '/static/images/kaitiaki_logo.png'}, + {'hockey_club': 'Antlers', 'logo_url': '/static/images/antlers_logo.png'}, + {'hockey_club': 'Marcellin', 'logo_url': '/static/images/marcellin_logo.png'}, + {'hockey_club': 'Skyers', 'logo_url': '/static/images/skyers_logo.png'}, + {'hockey_club': 'JR', 'logo_url': '/static/images/jr_logo.png'}, + {'hockey_club': 'IUHK', 'logo_url': '/static/images/iuhk_logo.png'}, + {'hockey_club': '144U', 'logo_url': '/static/images/144u_logo.png'}, + {'hockey_club': 'HKU', 'logo_url': '/static/images/hku_logo.png'}, + ] + + for club_data in clubs_data: + # Check if club already exists + sql_check = text("SELECT hockey_club FROM clubs WHERE hockey_club = :club_name") + existing = sql_read(sql_check, {'club_name': club_data['hockey_club']}) + + if not existing: + sql = text("INSERT INTO clubs (hockey_club, logo_url) VALUES (:club_name, :logo_url)") + sql_write(sql, club_data) + imported_clubs += 1 + + if form.import_teams.data: + # Import teams based on Hong Kong Hockey Association divisions + teams_data = [ + # Premier Division + {'club': 'HKFC', 'team': 'A', 'display_name': 'HKFC A', 'league': 'Premier Division'}, + {'club': 'KCC', 'team': 'A', 'display_name': 'KCC A', 'league': 'Premier Division'}, + {'club': 'USRC', 'team': 'A', 'display_name': 'USRC A', 'league': 'Premier Division'}, + {'club': 'Valley', 'team': 'A', 'display_name': 'Valley A', 'league': 'Premier Division'}, + + # 1st Division + {'club': 'HKFC', 'team': 'B', 'display_name': 'HKFC B', 'league': '1st Division'}, + {'club': 'KCC', 'team': 'B', 'display_name': 'KCC B', 'league': '1st Division'}, + {'club': 'USRC', 'team': 'B', 'display_name': 'USRC B', 'league': '1st Division'}, + {'club': 'Valley', 'team': 'B', 'display_name': 'Valley B', 'league': '1st Division'}, + + # 2nd Division + {'club': 'HKFC', 'team': 'C', 'display_name': 'HKFC C', 'league': '2nd Division'}, + {'club': 'KCC', 'team': 'C', 'display_name': 'KCC C', 'league': '2nd Division'}, + {'club': 'USRC', 'team': 'C', 'display_name': 'USRC C', 'league': '2nd Division'}, + {'club': 'Valley', 'team': 'C', 'display_name': 'Valley C', 'league': '2nd Division'}, + + # 3rd Division + {'club': 'SSSC', 'team': 'C', 'display_name': 'SSSC C', 'league': '3rd Division'}, + {'club': 'Dragons', 'team': 'A', 'display_name': 'Dragons A', 'league': '3rd Division'}, + {'club': 'Kai Tak', 'team': 'B', 'display_name': 'Kai Tak B', 'league': '3rd Division'}, + {'club': 'RHOBA', 'team': 'A', 'display_name': 'RHOBA A', 'league': '3rd Division'}, + {'club': 'Elite', 'team': 'B', 'display_name': 'Elite B', 'league': '3rd Division'}, + {'club': 'HKFC', 'team': 'F', 'display_name': 'HKFC F', 'league': '3rd Division'}, + {'club': 'Aquila', 'team': 'A', 'display_name': 'Aquila A', 'league': '3rd Division'}, + {'club': 'HKJ', 'team': 'B', 'display_name': 'HKJ B', 'league': '3rd Division'}, + {'club': 'Sirius', 'team': 'A', 'display_name': 'Sirius A', 'league': '3rd Division'}, + {'club': 'Shaheen', 'team': 'B', 'display_name': 'Shaheen B', 'league': '3rd Division'}, + {'club': 'RHOBA', 'team': 'B', 'display_name': 'RHOBA B', 'league': '3rd Division'}, + + # 4th Division + {'club': 'Khalsa', 'team': 'C', 'display_name': 'Khalsa C', 'league': '4th Division'}, + {'club': 'HKCC', 'team': 'C', 'display_name': 'HKCC C', 'league': '4th Division'}, + {'club': 'Valley', 'team': 'D', 'display_name': 'Valley D', 'league': '4th Division'}, + {'club': 'Police', 'team': 'A', 'display_name': 'Police A', 'league': '4th Division'}, + {'club': 'Recreio', 'team': 'A', 'display_name': 'Recreio A', 'league': '4th Division'}, + {'club': 'CSD', 'team': 'A', 'display_name': 'CSD A', 'league': '4th Division'}, + {'club': 'HKFC', 'team': 'G', 'display_name': 'HKFC G', 'league': '4th Division'}, + {'club': 'Dutch', 'team': 'B', 'display_name': 'Dutch B', 'league': '4th Division'}, + {'club': 'RHOBA', 'team': 'C', 'display_name': 'RHOBA C', 'league': '4th Division'}, + {'club': 'HKUHC', 'team': 'A', 'display_name': 'HKUHC A', 'league': '4th Division'}, + {'club': 'Kaitiaki', 'team': 'A', 'display_name': 'Kaitiaki', 'league': '4th Division'}, + + # 5th Division + {'club': 'KCC', 'team': 'D', 'display_name': 'KCC D', 'league': '5th Division'}, + {'club': 'Kai Tak', 'team': 'C', 'display_name': 'Kai Tak C', 'league': '5th Division'}, + {'club': 'Dragons', 'team': 'B', 'display_name': 'Dragons B', 'league': '5th Division'}, + {'club': 'Antlers', 'team': 'C', 'display_name': 'Antlers C', 'league': '5th Division'}, + {'club': 'Valley', 'team': 'E', 'display_name': 'Valley E', 'league': '5th Division'}, + {'club': 'Elite', 'team': 'C', 'display_name': 'Elite C', 'league': '5th Division'}, + {'club': 'HKFC', 'team': 'H', 'display_name': 'HKFC H', 'league': '5th Division'}, + {'club': 'Aquila', 'team': 'B', 'display_name': 'Aquila B', 'league': '5th Division'}, + {'club': 'Sirius', 'team': 'B', 'display_name': 'Sirius B', 'league': '5th Division'}, + {'club': 'Marcellin', 'team': 'A', 'display_name': 'Marcellin A', 'league': '5th Division'}, + {'club': 'Recreio', 'team': 'B', 'display_name': 'Recreio B', 'league': '5th Division'}, + {'club': 'Diocesan', 'team': 'B', 'display_name': 'Diocesan B', 'league': '5th Division'}, + + # 6th Division + {'club': 'Rhino', 'team': 'B', 'display_name': 'Rhino B', 'league': '6th Division'}, + {'club': 'Skyers', 'team': 'A', 'display_name': 'Skyers A', 'league': '6th Division'}, + {'club': 'JR', 'team': 'A', 'display_name': 'JR', 'league': '6th Division'}, + {'club': 'HKCC', 'team': 'D', 'display_name': 'HKCC D', 'league': '6th Division'}, + {'club': 'KCC', 'team': 'E', 'display_name': 'KCC E', 'league': '6th Division'}, + {'club': 'HKJ', 'team': 'C', 'display_name': 'HKJ C', 'league': '6th Division'}, + {'club': 'IUHK', 'team': 'A', 'display_name': 'IUHK A', 'league': '6th Division'}, + {'club': 'Valley', 'team': 'F', 'display_name': 'Valley F', 'league': '6th Division'}, + {'club': '144U', 'team': 'A', 'display_name': '144U A', 'league': '6th Division'}, + {'club': 'HKU', 'team': 'A', 'display_name': 'HKU A', 'league': '6th Division'}, + ] + + for team_data in teams_data: + # Check if team already exists + sql_check = text("SELECT club, team FROM teams WHERE club = :club AND team = :team") + existing = sql_read(sql_check, {'club': team_data['club'], 'team': team_data['team']}) + + if not existing: + sql = text("INSERT INTO teams (club, team, display_name, league) VALUES (:club, :team, :display_name, :league)") + sql_write(sql, team_data) + imported_teams += 1 + + if form.import_players.data: + # Import sample players for HKFC C team + players_data = [ + {'player_number': 1, 'player_forenames': 'John', 'player_surname': 'Smith', 'player_nickname': 'Smithers', 'player_team': 'HKFC C'}, + {'player_number': 2, 'player_forenames': 'Mike', 'player_surname': 'Jones', 'player_nickname': 'Jonesy', 'player_team': 'HKFC C'}, + {'player_number': 3, 'player_forenames': 'David', 'player_surname': 'Brown', 'player_nickname': 'Brownie', 'player_team': 'HKFC C'}, + {'player_number': 4, 'player_forenames': 'Chris', 'player_surname': 'Wilson', 'player_nickname': 'Willy', 'player_team': 'HKFC C'}, + {'player_number': 5, 'player_forenames': 'Tom', 'player_surname': 'Taylor', 'player_nickname': 'Tayls', 'player_team': 'HKFC C'}, + ] + + for player_data in players_data: + # Check if player already exists + sql_check = text("SELECT playerNumber FROM _HKFC_players WHERE playerNumber = :player_number") + existing = sql_read(sql_check, {'player_number': player_data['player_number']}) + + if not existing: + sql = text("INSERT INTO _HKFC_players (playerNumber, playerForenames, playerSurname, playerNickname, playerTeam) VALUES (:player_number, :forenames, :surname, :nickname, :team)") + sql_write(sql, player_data) + imported_players += 1 + + flash(f'Import completed! {imported_clubs} clubs, {imported_teams} teams, {imported_players} players imported.', 'success') + return redirect(url_for('data_import')) + elif form.cancel.data: + return redirect(url_for('data_import')) + + return render_template('data_import.html', form=form) + + @app.route('/admin/squad/submit', methods=['POST']) @basic_auth.required def match_squad_submit(): diff --git a/motm_app/templates/data_import.html b/motm_app/templates/data_import.html new file mode 100644 index 0000000..73be9c0 --- /dev/null +++ b/motm_app/templates/data_import.html @@ -0,0 +1,120 @@ + + +
+ + +This import feature populates the database with clubs and teams from the Hong Kong Hockey Association Men's League Standings.
+Note: Only new records will be imported. Existing clubs and teams will be skipped to prevent duplicates.
+Add, edit, and manage hockey teams
+ +Import clubs and teams from Hong Kong Hockey Association
+Select players for the match squad