@ -34,6 +34,10 @@ from django.views.decorators.csrf import csrf_exempt
from django . core . files . storage import default_storage
from django . core . files . storage import default_storage
from django . core . files . base import ContentFile
from django . core . files . base import ContentFile
from django . core . mail import EmailMessage
from django . core . mail import EmailMessage
from django . views . decorators . csrf import csrf_protect
from . services . tournament_registration import TournamentRegistrationService
from . services . tournament_unregistration import TournamentUnregistrationService
# Local application imports
# Local application imports
from . models import (
from . models import (
@ -70,6 +74,7 @@ from api.tokens import account_activation_token
from tournaments . models . device_token import DeviceToken
from tournaments . models . device_token import DeviceToken
from tournaments . models . player_enums import PlayerDataSource , PlayerSexType
from tournaments . models . player_enums import PlayerDataSource , PlayerSexType
from django . views . generic . edit import UpdateView
from django . views . generic . edit import UpdateView
from . forms import CustomPasswordChangeForm
def index ( request ) :
def index ( request ) :
@ -125,10 +130,12 @@ def tournament_info(request, tournament_id):
registered_user = None
registered_user = None
team_registration = None
team_registration = None
is_captain = False
is_captain = False
user_can_register = True
if request . user . is_authenticated :
if request . user . is_authenticated :
# Assuming user's licence_id is stored in the user profile (e.g., request.user.licence_id)
# Assuming user's licence_id is stored in the user profile (e.g., request.user.licence_id)
user_licence_id = request . user . licence_id
user_licence_id = request . user . licence_id
if user_licence_id is not None :
if user_licence_id is not None :
validator = LicenseValidator ( user_licence_id )
validator = LicenseValidator ( user_licence_id )
stripped_license = validator . stripped_license
stripped_license = validator . stripped_license
@ -143,11 +150,14 @@ def tournament_info(request, tournament_id):
if registered_user :
if registered_user :
is_captain = registered_user . captain
is_captain = registered_user . captain
team_registration = registered_user . team_registration
team_registration = registered_user . team_registration
else :
user_register_check = tournament . user_register_check ( request . user )
return render ( request , ' tournaments/tournament_info.html ' , {
return render ( request , ' tournaments/tournament_info.html ' , {
' tournament ' : tournament ,
' tournament ' : tournament ,
' team ' : team_registration ,
' team ' : team_registration ,
' is_captain ' : is_captain
' is_captain ' : is_captain ,
' user_register_check ' : user_register_check
} )
} )
@ -596,376 +606,30 @@ def profile(request):
' user_name ' : user . username
' user_name ' : user . username
} )
} )
from django . views . decorators . csrf import csrf_protect
@csrf_protect
@csrf_protect
def register_tournament ( request , tournament_id ) :
def register_tournament ( request , tournament_id ) :
tournament = get_object_or_404 ( Tournament , id = tournament_id )
tournament = get_object_or_404 ( Tournament , id = tournament_id )
registration_successful = False # Flag for registration status
service = TournamentRegistrationService ( request , tournament )
team_form = None
service . initialize_context ( )
add_player_form = None
if ' user_without_licence ' not in request . session :
request . session [ ' user_without_licence ' ] = False
user_without_licence = request . session [ ' user_without_licence ' ]
# Process forms
if request . method == ' POST ' :
if request . method == ' POST ' :
team_form = TournamentRegistrationForm ( request . POST )
service . handle_post_request ( )
# Check if the add player form is submitted
add_player_form = AddPlayerForm ( request . POST )
if ' add_player ' in request . POST and add_player_form . is_valid ( ) :
player_data = add_player_form . cleaned_data
# Validate the license ID before adding the player
licence_id = player_data [ ' licence_id ' ] . upper ( )
# Instantiate your custom validator and validate the license ID
validator = LicenseValidator ( licence_id )
if validator . validate_license ( ) is False and tournament . license_is_required is True :
if len ( licence_id ) == 0 :
if len ( request . session . get ( ' team_registration ' , [ ] ) ) == 0 :
messages . error ( request , " Le numéro de licence est obligatoire. " )
else :
messages . error ( request , " Le numéro de licence de votre partenaire est obligatoire. " )
else :
else :
messages . error ( request , " Le numéro de licence est invalide, la lettre ne correspond pas. " )
service . handle_get_request ( )
return render ( request , ' register_tournament.html ' , {
' team_form ' : team_form ,
' add_player_form ' : add_player_form ,
' tournament ' : tournament ,
' registration_successful ' : registration_successful ,
' current_players ' : request . session [ ' team_registration ' ] ,
' user_without_licence ' : user_without_licence
} )
# Check if the player with the same licence_id already exists in the session
existing_players = [ player [ ' licence_id ' ] for player in request . session [ ' team_registration ' ] ]
if validator . validate_license ( ) and licence_id in existing_players :
messages . error ( request , " Ce joueur est déjà dans l ' équipe. " )
return render ( request , ' register_tournament.html ' , {
' team_form ' : team_form ,
' add_player_form ' : add_player_form ,
' tournament ' : tournament ,
' registration_successful ' : registration_successful ,
' current_players ' : request . session [ ' team_registration ' ] ,
' user_without_licence ' : user_without_licence
} )
else :
# Check if a PlayerRegistration with the same licence_id already exists in the database
stripped_license = validator . stripped_license
if validator . validate_license ( ) and validate_license_id ( stripped_license , tournament ) and tournament . license_is_required is True :
messages . error ( request , " Un joueur avec ce numéro de licence est déjà inscrit dans une équipe. " )
return render ( request , ' register_tournament.html ' , {
' team_form ' : team_form ,
' add_player_form ' : add_player_form ,
' tournament ' : tournament ,
' registration_successful ' : registration_successful ,
' current_players ' : request . session [ ' team_registration ' ] ,
' user_without_licence ' : user_without_licence
} )
elif add_player_form . names_is_valid ( ) :
if player_data . get ( ' rank ' , None ) is None :
if request . session . get ( ' last_rank ' , None ) is None :
data , found = get_player_name_from_csv ( tournament . federal_category , None )
if data :
request . session [ ' last_rank ' ] = data [ ' rank ' ]
request . session [ ' is_woman ' ] = data [ ' is_woman ' ]
request . session . modified = True
player_data [ ' rank ' ] = request . session . get ( ' last_rank ' , 0 )
player_data [ ' is_woman ' ] = request . session . get ( ' is_woman ' , False )
request . session [ ' team_registration ' ] . append ( player_data )
if request . user . is_authenticated and request . user . licence_id is None :
request . session [ ' user_without_licence ' ] = False
request . user . licence_id = validator . computed_licence_id
request . user . save ( )
request . session . modified = True # Ensure session is updated
add_player_form = AddPlayerForm ( )
else :
if add_player_form . first_tournament is False :
# Retrieve player names from the CSV file
data , found = get_player_name_from_csv ( tournament . federal_category , licence_id )
if found and data :
player_data [ ' first_name ' ] = data [ ' first_name ' ]
player_data [ ' last_name ' ] = data [ ' last_name ' ]
player_data [ ' rank ' ] = data [ ' rank ' ]
player_data [ ' is_woman ' ] = data [ ' is_woman ' ]
# If validation passes, add the player to the session without clearing previous ones
request . session [ ' team_registration ' ] . append ( player_data )
request . session . modified = True # Ensure session is updated
add_player_form = AddPlayerForm ( )
else :
if data :
request . session [ ' last_rank ' ] = data [ ' rank ' ]
request . session [ ' is_woman ' ] = data [ ' is_woman ' ]
request . session . modified = True # Ensure session is updated
add_player_form . first_tournament = True
if add_player_form . names_is_valid ( ) is False :
if len ( request . session . get ( ' team_registration ' , [ ] ) ) == 0 :
messages . error ( request , " Pour confirmer votre inscription votre prénom et votre nom sont obligatoires. " )
else :
messages . error ( request , " Pour rajouter un partenaire, son prénom et son nom sont obligatoires. " )
return render ( request , ' register_tournament.html ' , {
' team_form ' : team_form ,
' add_player_form ' : add_player_form ,
' tournament ' : tournament ,
' registration_successful ' : registration_successful ,
' current_players ' : request . session [ ' team_registration ' ] ,
' user_without_licence ' : user_without_licence
} )
# Check if the team registration form is valid and finalize the registration
elif ' register_team ' in request . POST and team_form . is_valid ( ) :
waiting_list_position = tournament . get_waiting_list_position ( )
registration_date = timezone . now ( ) . replace ( microsecond = 0 )
team_registration = TeamRegistration . objects . create (
tournament = tournament ,
registration_date = registration_date
)
stripped_license = None
if request . user . is_authenticated and request . user . licence_id is not None :
stripped_license = LicenseValidator ( request . user . licence_id ) . stripped_license
# Create PlayerRegistration objects for each player in the session
for player_data in request . session [ ' team_registration ' ] :
is_captain = False
player_licence_id = player_data [ ' licence_id ' ]
if player_licence_id is not None and stripped_license is not None :
if player_licence_id . startswith ( stripped_license ) :
is_captain = True
is_woman = player_data . get ( ' is_woman ' , False )
rank = player_data . get ( ' rank ' , 0 )
computed_rank = None
sex = PlayerSexType . MALE
if is_woman is None :
computed_rank = rank
else :
computed_rank = rank
is_woman = player_data . get ( ' is_woman ' , False ) == True
if is_woman :
sex = PlayerSexType . FEMALE
if tournament . federal_category == FederalCategory . MEN and is_woman :
computed_rank = str ( int ( computed_rank ) + FederalCategory . female_in_male_assimilation_addition ( int ( rank ) ) )
player_registration = PlayerRegistration . objects . create (
team_registration = team_registration ,
captain = is_captain ,
source = PlayerDataSource . ONLINE_REGISTRATION ,
first_name = player_data . get ( ' first_name ' , None ) ,
last_name = player_data . get ( ' last_name ' , None ) ,
points = player_data . get ( ' points ' , None ) ,
assimilation = player_data . get ( ' assimilation ' , None ) ,
tournament_played = player_data . get ( ' tournament_count ' , None ) ,
ligue_name = player_data . get ( ' ligue_name ' , None ) ,
club_name = player_data . get ( ' club_name ' , None ) ,
birthdate = player_data . get ( ' birth_year ' , None ) ,
sex = sex ,
rank = rank ,
computed_rank = computed_rank ,
licence_id = player_data [ ' licence_id ' ] ,
email = team_form . cleaned_data [ ' email ' ] ,
phone_number = team_form . cleaned_data [ ' mobile_number ' ]
)
player_registration . save ( )
team_registration . set_weight ( )
team_registration . save ( )
request . session [ ' team_registration ' ] = [ ]
registration_successful = True
# Send confirmation email
tournament_details_str = tournament . build_tournament_details_str ( )
name_str = tournament . build_name_details_str ( )
email_subject = f " Confirmation d ' inscription au tournoi { tournament_details_str } { name_str } "
if waiting_list_position > = 0 :
email_subject = f " En liste d ' attente du tournoi { tournament_details_str } { name_str } "
inscription_date = timezone . now ( ) . strftime ( " %d / % m/ % Y à % H: % M " )
team_members = [ player . name ( ) for player in team_registration . playerregistration_set . all ( ) ]
team_members_str = " et " . join ( team_members )
email_body = f " Bonjour, \n \n Votre inscription au tournoi { tournament_details_str } { name_str } est confirmée. "
if tournament . get_waiting_list_position ( ) > = 0 :
email_body = f " Bonjour, \n \n Votre inscription en liste d ' attente du tournoi { tournament_details_str } { name_str } est confirmée. "
email_body + = f " \n \n Date d ' inscription: { inscription_date } "
email_body + = f " \n \n Équipe inscrite: { team_members_str } "
email_body + = f " \n \n Le tournoi commencera le { tournament . start_date . strftime ( ' %d / % m/ % Y ' ) } "
email_body + = f " @ { tournament . event . club . name } "
email_body + = f " \n \n Voir les informations sur { request . build_absolute_uri ( f ' /tournament/ { tournament . id } / ' ) } "
email_body + = " \n \n Pour toute question, veuillez contacter votre juge-arbitre. Si vous n ' êtes pas à l ' origine de cette inscription, merci de le contacter rapidement. "
email_body + = f " \n { tournament . event . creator . full_name ( ) } \n { tournament . event . creator . email } "
email_body + = " \n \n Ceci est un e-mail automatique, veuillez ne pas y répondre. "
email_body + = " \n \n Cordialement, \n \n Padel Club "
email = EmailMessage (
subject = email_subject ,
body = email_body ,
to = [ team_form . cleaned_data [ ' email ' ] ]
)
email . send ( )
else :
add_player_form = AddPlayerForm ( )
request . session [ ' team_registration ' ] = [ ]
initial_data = { }
player_data = { }
# Add the authenticated user to the session as the first player if not already added
if request . user . is_authenticated :
user_licence_id = request . user . licence_id
initial_data = {
' email ' : request . user . email ,
' phone ' : request . user . phone ,
}
if user_licence_id is not None :
validator = LicenseValidator ( user_licence_id )
existing_players = [ player [ ' licence_id ' ] for player in request . session [ ' team_registration ' ] ]
if user_licence_id not in existing_players :
# Add the authenticated user as the first player in the session
player_data = {
' first_name ' : request . user . first_name ,
' last_name ' : request . user . last_name . upper ( ) ,
' email ' : request . user . email ,
' phone ' : request . user . phone ,
' licence_id ' : validator . computed_licence_id
}
data , found = get_player_name_from_csv ( tournament . federal_category , user_licence_id )
if found and data :
player_data [ ' rank ' ] = data [ ' rank ' ]
player_data [ ' points ' ] = data . get ( ' points ' , None )
player_data [ ' assimilation ' ] = data . get ( ' assimilation ' , None )
player_data [ ' tournament_count ' ] = data . get ( ' tournament_count ' , None )
player_data [ ' ligue_name ' ] = data . get ( ' ligue_name ' , None )
player_data [ ' club_name ' ] = data . get ( ' club_name ' , None )
player_data [ ' birth_year ' ] = data . get ( ' birth_year ' , None )
request . session [ ' team_registration ' ] . insert ( 0 , player_data ) # Add them as the first player
request . session . modified = True # Ensure session is updated
else :
player_data = {
' first_name ' : request . user . first_name ,
' last_name ' : request . user . last_name . upper ( ) ,
}
add_player_form = AddPlayerForm ( initial = player_data )
request . session [ ' user_without_licence ' ] = True
request . session . modified = True # Ensure session is updated
user_without_licence = True
team_form = TournamentRegistrationForm ( initial = initial_data )
return render ( request , ' register_tournament.html ' , {
' team_form ' : team_form ,
' add_player_form ' : add_player_form ,
' tournament ' : tournament ,
' registration_successful ' : registration_successful ,
' current_players ' : request . session [ ' team_registration ' ] ,
' user_without_licence ' : request . session [ ' user_without_licence ' ]
} )
return render ( request , ' register_tournament.html ' , service . context )
@login_required
@login_required
def unregister_tournament ( request , tournament_id ) :
def unregister_tournament ( request , tournament_id ) :
tournament = get_object_or_404 ( Tournament , id = tournament_id )
tournament = get_object_or_404 ( Tournament , id = tournament_id )
service = TournamentUnregistrationService ( request , tournament )
if not tournament or not tournament . is_unregistration_possible ( ) :
if not service . can_unregister ( ) :
messages . error ( request , " Le désistement n ' est plus possible pour ce tournoi. " )
return redirect ( ' tournament-info ' , tournament_id = tournament_id )
user_licence_id = request . user . licence_id
if not user_licence_id :
messages . error ( request , " Vous ne pouvez pas vous désinscrire car vous n ' avez pas de numéro de licence associé. " )
return redirect ( ' tournament-info ' , tournament_id = tournament_id )
return redirect ( ' tournament-info ' , tournament_id = tournament_id )
player_registration = PlayerRegistration . objects . filter (
service . process_unregistration ( )
licence_id__startswith = user_licence_id ,
team_registration__tournament_id = tournament_id ,
source = PlayerDataSource . ONLINE_REGISTRATION ,
) . first ( ) # Get the first match, if any
other_player = None
if player_registration :
team_registration = player_registration . team_registration # Get the related TeamRegistration
other_player = team_registration . get_other_player ( player_registration )
unregistered_team = UnregisteredTeam . objects . create (
tournament = tournament ,
unregistration_date = timezone . now ( ) ,
)
for player in team_registration . playerregistration_set . all ( ) :
unregistered_player = UnregisteredPlayer . objects . create (
unregistered_team = unregistered_team ,
first_name = player . first_name ,
last_name = player . last_name ,
licence_id = player . licence_id ,
)
unregistered_player . save ( )
unregistered_team . save ( )
team_registration . delete ( )
else :
messages . error ( request , " La désincription a échouée. Veuillez contacter le juge-arbitre. " )
return redirect ( ' tournament-info ' , tournament_id = tournament_id )
return redirect ( ' tournament-info ' , tournament_id = tournament_id )
request . session [ ' team_registration ' ] = [ ]
# Get the tournament information and player details before deleting
if tournament and request . user . email : # Ensure we have valid tournament and user email
tournament_details_str = tournament . build_tournament_details_str ( )
name_str = tournament . build_name_details_str ( )
email_subject = f " Confirmation de désistement du tournoi { tournament_details_str } { name_str } "
email_body = f " Bonjour, \n \n Vous venez de vous désinscrire du tournoi { tournament_details_str } { name_str } "
email_body + = f " du { tournament . start_date . strftime ( ' %d / % m/ % Y ' ) } "
email_body + = f " au { tournament . event . club . name } "
if other_player is not None :
email_body + = f " \n \n Vous étiez inscrit avec { other_player . name ( ) } , n ' oubliez pas de prévenir votre partenaire. "
email_body + = f " \n \n Voir les informations sur { request . build_absolute_uri ( f ' /tournament/ { tournament . id } / ' ) } "
email_body + = " \n \n Pour toute question, veuillez contacter votre juge-arbitre. Si vous n ' êtes pas à l ' origine de cette inscription, merci de le contacter rapidement. "
email_body + = f " \n { tournament . event . creator . full_name ( ) } \n { tournament . event . creator . email } "
email_body + = " \n \n Ceci est un e-mail automatique, veuillez ne pas y répondre. "
email = EmailMessage (
subject = email_subject ,
body = email_body ,
to = [ request . user . email ]
)
email . send ( )
return redirect ( ' tournament-info ' , tournament_id = tournament_id )
def validate_license_id ( licence_id , tournament ) :
teams = TeamRegistration . objects . filter ( tournament = tournament )
# Filter out walkouts and unregistered teams
teams = teams . filter ( walk_out = False )
# Check if any player in any team in the tournament already has this licence_id
# Normalize the licence ID before querying
# Loop through each team and check if any of its players has the same licence_id
for team in teams :
for player in team . playerregistration_set . all ( ) :
if player . licence_id is not None and player . licence_id . startswith ( licence_id ) :
return True
# If all checks pass, return True (you can add further logic here if needed)
return False
class CustomPasswordResetConfirmView ( PasswordResetConfirmView ) :
class CustomPasswordResetConfirmView ( PasswordResetConfirmView ) :
template_name = ' registration/password_reset_confirm.html ' # Custom template
template_name = ' registration/password_reset_confirm.html ' # Custom template
@ -1034,8 +698,6 @@ def my_tournaments(request):
' user_name ' : user . username
' user_name ' : user . username
} )
} )
from . forms import CustomPasswordChangeForm
class ProfileUpdateView ( UpdateView ) :
class ProfileUpdateView ( UpdateView ) :
model = CustomUser
model = CustomUser
form_class = ProfileUpdateForm
form_class = ProfileUpdateForm