régler problème avec les liens TRN
This commit is contained in:
+56
-10
@@ -1,7 +1,7 @@
|
||||
from flask import Blueprint, render_template, redirect, url_for, flash, request, jsonify
|
||||
from flask_login import login_required, current_user
|
||||
from extensions import db, hash_password
|
||||
from models import User, ROLES, ESPORT_GAMES, PlayerDisponibility
|
||||
from models import User, ROLES, ESPORT_GAMES, PlayerDisponibility, UserGamertag, GAME_PLATFORMS
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
users_bp = Blueprint('users', __name__, url_prefix='/users')
|
||||
@@ -34,10 +34,9 @@ def edit_user(user_id):
|
||||
|
||||
if role not in ROLES:
|
||||
flash('Invalid role selected.', 'danger')
|
||||
return render_template('pages/edit_user.html', user=user, roles=ROLES, esport_games=ESPORT_GAMES)
|
||||
return render_template('pages/edit_user.html', user=user, roles=ROLES, esport_games=ESPORT_GAMES, game_platforms=GAME_PLATFORMS)
|
||||
|
||||
selected_games = request.form.getlist('games')
|
||||
trn_username = request.form.get('trn_username', '').strip()
|
||||
discord_username = request.form.get('discord_username', '').strip()
|
||||
league_os_profile = request.form.get('league_os_profile', '').strip()
|
||||
|
||||
@@ -47,10 +46,33 @@ def edit_user(user_id):
|
||||
user.role = role
|
||||
user.is_active_account = is_active
|
||||
user.games = ','.join(selected_games) if selected_games else None
|
||||
user.trn_username = trn_username or None
|
||||
user.discord_username = discord_username or None
|
||||
user.league_os_profile = league_os_profile or None
|
||||
|
||||
# Handle gamertags - save or delete based on form input
|
||||
# First, get all existing gamertags for this user
|
||||
existing_gamertags = {gt.game: gt for gt in user.gamertags}
|
||||
|
||||
for game in selected_games:
|
||||
gamertag = request.form.get(f'gamertag_{game}', '').strip()
|
||||
platform = request.form.get(f'platform_{game}', '').strip() if GAME_PLATFORMS.get(game) else None
|
||||
|
||||
existing = existing_gamertags.get(game)
|
||||
if gamertag:
|
||||
if existing:
|
||||
existing.gamertag = gamertag
|
||||
existing.platform = platform
|
||||
else:
|
||||
gt = UserGamertag(user_id=user.id, game=game, gamertag=gamertag, platform=platform)
|
||||
db.session.add(gt)
|
||||
elif existing:
|
||||
db.session.delete(existing)
|
||||
|
||||
# Remove gamertags for games that are no longer selected
|
||||
for game in existing_gamertags:
|
||||
if game not in selected_games:
|
||||
db.session.delete(existing_gamertags[game])
|
||||
|
||||
password = request.form.get('password')
|
||||
if password:
|
||||
user.password_hash = hash_password(password)
|
||||
@@ -59,7 +81,8 @@ def edit_user(user_id):
|
||||
flash(f'User {user.full_name} updated successfully!', 'success')
|
||||
return redirect(url_for('users.list_users'))
|
||||
|
||||
return render_template('pages/edit_user.html', user=user, roles=ROLES, esport_games=ESPORT_GAMES)
|
||||
user_gamertags = {gt.game: {'gamertag': gt.gamertag, 'platform': gt.platform} for gt in user.gamertags}
|
||||
return render_template('pages/edit_user.html', user=user, roles=ROLES, esport_games=ESPORT_GAMES, game_platforms=GAME_PLATFORMS, user_gamertags=user_gamertags)
|
||||
|
||||
@users_bp.route('/<int:user_id>/delete', methods=['POST'])
|
||||
@login_required
|
||||
@@ -135,22 +158,44 @@ def edit_profile():
|
||||
phone = request.form.get('phone')
|
||||
|
||||
selected_games = request.form.getlist('games')
|
||||
trn_username = request.form.get('trn_username', '').strip()
|
||||
discord_username = request.form.get('discord_username', '').strip()
|
||||
league_os_profile = request.form.get('league_os_profile', '').strip()
|
||||
|
||||
if email != current_user.email and User.query.filter_by(email=email).first():
|
||||
flash('Email already in use.', 'danger')
|
||||
return render_template('pages/edit_profile.html', user=current_user, esport_games=ESPORT_GAMES)
|
||||
return render_template('pages/edit_profile.html', user=current_user, esport_games=ESPORT_GAMES, game_platforms=GAME_PLATFORMS)
|
||||
|
||||
current_user.full_name = full_name
|
||||
current_user.email = email
|
||||
current_user.phone = phone
|
||||
current_user.games = ','.join(selected_games) if selected_games else None
|
||||
current_user.trn_username = trn_username or None
|
||||
current_user.discord_username = discord_username or None
|
||||
current_user.league_os_profile = league_os_profile or None
|
||||
|
||||
# Handle gamertags - save or delete based on form input
|
||||
# First, get all existing gamertags for this user
|
||||
existing_gamertags = {gt.game: gt for gt in current_user.gamertags}
|
||||
|
||||
for game in selected_games:
|
||||
gamertag = request.form.get(f'gamertag_{game}', '').strip()
|
||||
platform = request.form.get(f'platform_{game}', '').strip() if GAME_PLATFORMS.get(game) else None
|
||||
|
||||
existing = existing_gamertags.get(game)
|
||||
if gamertag:
|
||||
if existing:
|
||||
existing.gamertag = gamertag
|
||||
existing.platform = platform
|
||||
else:
|
||||
gt = UserGamertag(user_id=current_user.id, game=game, gamertag=gamertag, platform=platform)
|
||||
db.session.add(gt)
|
||||
elif existing:
|
||||
db.session.delete(existing)
|
||||
|
||||
# Remove gamertags for games that are no longer selected
|
||||
for game in existing_gamertags:
|
||||
if game not in selected_games:
|
||||
db.session.delete(existing_gamertags[game])
|
||||
|
||||
password = request.form.get('password')
|
||||
if password:
|
||||
current_user.password_hash = hash_password(password)
|
||||
@@ -159,7 +204,8 @@ def edit_profile():
|
||||
flash('Profile updated successfully!', 'success')
|
||||
return redirect(url_for('users.profile'))
|
||||
|
||||
return render_template('pages/edit_profile.html', user=current_user, esport_games=ESPORT_GAMES)
|
||||
user_gamertags = {gt.game: {'gamertag': gt.gamertag, 'platform': gt.platform} for gt in current_user.gamertags}
|
||||
return render_template('pages/edit_profile.html', user=current_user, esport_games=ESPORT_GAMES, game_platforms=GAME_PLATFORMS, user_gamertags=user_gamertags)
|
||||
|
||||
|
||||
# Day names for disponibility
|
||||
@@ -332,4 +378,4 @@ def delete_disponibility(disponibility_id):
|
||||
db.session.delete(disponibility)
|
||||
db.session.commit()
|
||||
|
||||
return jsonify({'success': True})
|
||||
return jsonify({'success': True})
|
||||
Reference in New Issue
Block a user