|
|
|
|
@ -33,6 +33,23 @@ from .forms import SimpleForm |
|
|
|
|
from django.core.mail import EmailMessage |
|
|
|
|
from datetime import timedelta |
|
|
|
|
from django.utils import timezone |
|
|
|
|
from django.shortcuts import render, redirect |
|
|
|
|
from django.contrib.auth.forms import UserCreationForm |
|
|
|
|
from django.contrib.auth import login |
|
|
|
|
|
|
|
|
|
from django.urls import reverse |
|
|
|
|
from django.contrib import messages |
|
|
|
|
from .forms import TournamentRegistrationForm, AddPlayerForm |
|
|
|
|
|
|
|
|
|
from .utils.licence_validator import LicenseValidator |
|
|
|
|
from .utils.player_search import get_player_name_from_csv |
|
|
|
|
|
|
|
|
|
from django.contrib.auth.decorators import login_required |
|
|
|
|
from .forms import SimpleCustomUserCreationForm |
|
|
|
|
from django.contrib.sites.shortcuts import get_current_site |
|
|
|
|
from django.template.loader import render_to_string |
|
|
|
|
from django.utils.http import urlsafe_base64_encode, urlsafe_base64_decode |
|
|
|
|
from django.utils.encoding import force_bytes |
|
|
|
|
|
|
|
|
|
def index(request): |
|
|
|
|
|
|
|
|
|
@ -85,10 +102,23 @@ def future_tournaments(club_id): |
|
|
|
|
|
|
|
|
|
def tournament_info(request, tournament_id): |
|
|
|
|
tournament = get_object_or_404(Tournament, pk=tournament_id) |
|
|
|
|
is_registered = False |
|
|
|
|
|
|
|
|
|
if request.user.is_authenticated: |
|
|
|
|
# Assuming user's licence_id is stored in the user profile (e.g., request.user.profile.licence_id) |
|
|
|
|
user_licence_id = request.user.licence_id |
|
|
|
|
# Check if there is a PlayerRegistration for this user in this tournament |
|
|
|
|
is_registered = PlayerRegistration.objects.filter( |
|
|
|
|
licence_id__startswith=user_licence_id, |
|
|
|
|
team_registration__tournament=tournament |
|
|
|
|
).exists() |
|
|
|
|
|
|
|
|
|
return render(request, 'tournaments/tournament_info.html', { |
|
|
|
|
'tournament': tournament, |
|
|
|
|
'is_registered': is_registered, |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def tournaments(request): |
|
|
|
|
|
|
|
|
|
filter = int(request.GET.get('filter')) |
|
|
|
|
@ -315,7 +345,8 @@ def activate(request, uidb64, token): |
|
|
|
|
if user is not None and account_activation_token.check_token(user, token): |
|
|
|
|
user.is_active = True |
|
|
|
|
user.save() |
|
|
|
|
return HttpResponse('Votre email est confirmé. Vous pouvez maintenant vous connecter.') |
|
|
|
|
login(request, user) |
|
|
|
|
return redirect('index') # Redirect to the homepage or any other page you prefer |
|
|
|
|
else: |
|
|
|
|
return HttpResponse('Le lien est invalide.') |
|
|
|
|
|
|
|
|
|
@ -483,3 +514,204 @@ def send_email(mail, name): |
|
|
|
|
|
|
|
|
|
email = EmailMessage(subject, body, to=[mail]) |
|
|
|
|
email.send() |
|
|
|
|
|
|
|
|
|
@csrf_exempt |
|
|
|
|
def signup(request): |
|
|
|
|
if request.method == 'POST': |
|
|
|
|
form = SimpleCustomUserCreationForm(request.POST) |
|
|
|
|
if form.is_valid(): |
|
|
|
|
user = form.save(commit=False) |
|
|
|
|
user.is_active = False # Deactivate account until email is verified |
|
|
|
|
user.save() |
|
|
|
|
|
|
|
|
|
# Send verification email |
|
|
|
|
send_verification_email(request, user) |
|
|
|
|
request.session['pre_login_username'] = user.username |
|
|
|
|
request.session['pre_login_password'] = user.password # Store hashed password, or handle with caution |
|
|
|
|
|
|
|
|
|
messages.success(request, "Votre compte a été créé ! Veuillez vérifier votre e-mail pour confirmer votre compte.") |
|
|
|
|
return redirect('login') # Redirect to login page or a custom message page |
|
|
|
|
else: |
|
|
|
|
form = SimpleCustomUserCreationForm() |
|
|
|
|
return render(request, 'registration/signup.html', {'form': form}) |
|
|
|
|
|
|
|
|
|
def send_verification_email(request, user): |
|
|
|
|
|
|
|
|
|
current_site = get_current_site(request) |
|
|
|
|
mail_subject = 'Activez votre compte Padel Club !' |
|
|
|
|
message = render_to_string('tournaments/acc_active_email.html', { |
|
|
|
|
'user': user, |
|
|
|
|
'domain': current_site.domain, |
|
|
|
|
'uid': urlsafe_base64_encode(force_bytes(user.pk)), |
|
|
|
|
'token': account_activation_token.make_token(user), |
|
|
|
|
}) |
|
|
|
|
email = EmailMessage(mail_subject, message, to=[user.email]) |
|
|
|
|
email.send() |
|
|
|
|
|
|
|
|
|
@login_required |
|
|
|
|
def profile(request): |
|
|
|
|
user = request.user # Get the currently authenticated user |
|
|
|
|
|
|
|
|
|
# Query tournaments where the user is registered |
|
|
|
|
tournaments = Tournament.objects.all() |
|
|
|
|
|
|
|
|
|
return render(request, 'registration/profile.html', { |
|
|
|
|
'tournaments': tournaments, |
|
|
|
|
'user_name': user.username |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
def register_tournament(request, tournament_id): |
|
|
|
|
tournament = get_object_or_404(Tournament, id=tournament_id) |
|
|
|
|
registration_successful = False # Flag for registration status |
|
|
|
|
team_form = None |
|
|
|
|
add_player_form = None |
|
|
|
|
|
|
|
|
|
# Process forms |
|
|
|
|
if request.method == 'POST': |
|
|
|
|
team_form = TournamentRegistrationForm(request.POST) |
|
|
|
|
add_player_form = AddPlayerForm(request.POST) |
|
|
|
|
# Check if the add player form is submitted |
|
|
|
|
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: |
|
|
|
|
messages.error(request, f"Le numéro de licence est invalide, la lettre ne correspond pas. {validator.computed_license_key}") |
|
|
|
|
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'], |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
# 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 licence_id in existing_players: |
|
|
|
|
messages.error(request, 'This player is already added to the team.') |
|
|
|
|
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'], |
|
|
|
|
}) |
|
|
|
|
else: |
|
|
|
|
# Check if a PlayerRegistration with the same licence_id already exists in the database |
|
|
|
|
stripped_license = validator.stripped_license |
|
|
|
|
if validate_license_id(stripped_license, tournament): |
|
|
|
|
messages.error(request, 'A player with this licence ID is already registered in a team.') |
|
|
|
|
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'], |
|
|
|
|
}) |
|
|
|
|
elif add_player_form.names_is_valid(): |
|
|
|
|
request.session['team_registration'].append(player_data) |
|
|
|
|
request.session.modified = True # Ensure session is updated |
|
|
|
|
else: |
|
|
|
|
if add_player_form.first_tournament is False: |
|
|
|
|
# Retrieve player names from the CSV file |
|
|
|
|
first_name, last_name = get_player_name_from_csv(licence_id) |
|
|
|
|
if first_name and last_name: |
|
|
|
|
player_data['first_name'] = first_name |
|
|
|
|
player_data['last_name'] = last_name |
|
|
|
|
# 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 |
|
|
|
|
else: |
|
|
|
|
add_player_form.first_tournament = True |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Check if the team registration form is valid and finalize the registration |
|
|
|
|
elif 'register_team' in request.POST and team_form.is_valid(): |
|
|
|
|
team_registration = TeamRegistration.objects.create( |
|
|
|
|
tournament=tournament, |
|
|
|
|
registration_date=timezone.now() |
|
|
|
|
) |
|
|
|
|
# Create PlayerRegistration objects for each player in the session |
|
|
|
|
for player_data in request.session['team_registration']: |
|
|
|
|
player_registration = PlayerRegistration.objects.create( |
|
|
|
|
team_registration=team_registration, |
|
|
|
|
first_name=player_data['first_name'], |
|
|
|
|
last_name=player_data['last_name'], |
|
|
|
|
licence_id=player_data['licence_id'], |
|
|
|
|
email = player_data.get('email', None), |
|
|
|
|
phone_number = player_data.get('phone_number', None) |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
request.session['team_registration'] = [] |
|
|
|
|
registration_successful = True |
|
|
|
|
else: |
|
|
|
|
request.session['team_registration'] = [] |
|
|
|
|
initial_data = {} |
|
|
|
|
# Add the authenticated user to the session as the first player if not already added |
|
|
|
|
if request.user.is_authenticated: |
|
|
|
|
initial_data = { |
|
|
|
|
'email': request.user.email, |
|
|
|
|
'phone': request.user.phone, |
|
|
|
|
} |
|
|
|
|
existing_players = [player['licence_id'] for player in request.session['team_registration']] |
|
|
|
|
if request.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': request.user.licence_id, |
|
|
|
|
} |
|
|
|
|
request.session['team_registration'].insert(0, player_data) # Add them as the first player |
|
|
|
|
request.session.modified = True # Ensure session is updated |
|
|
|
|
|
|
|
|
|
team_form = TournamentRegistrationForm(initial=initial_data) |
|
|
|
|
add_player_form = AddPlayerForm() |
|
|
|
|
|
|
|
|
|
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'], |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@login_required |
|
|
|
|
def unregister_tournament(request, tournament_id): |
|
|
|
|
user_licence_id = request.user.licence_id |
|
|
|
|
player_registration = PlayerRegistration.objects.filter( |
|
|
|
|
licence_id__startswith=user_licence_id, |
|
|
|
|
team_registration__tournament_id=tournament_id |
|
|
|
|
).first() # Get the first match, if any |
|
|
|
|
|
|
|
|
|
if player_registration: |
|
|
|
|
team_registration = player_registration.team_registration # Get the related TeamRegistration |
|
|
|
|
team_registration.delete() # Delete the team registration |
|
|
|
|
|
|
|
|
|
request.session['team_registration'] = [] |
|
|
|
|
|
|
|
|
|
return redirect('tournament-info', tournament_id=tournament_id) |
|
|
|
|
|
|
|
|
|
def validate_license_id(licence_id, tournament): |
|
|
|
|
teams = TeamRegistration.objects.filter(tournament=tournament) |
|
|
|
|
|
|
|
|
|
# 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.startswith(licence_id): |
|
|
|
|
return True |
|
|
|
|
|
|
|
|
|
# If all checks pass, return True (you can add further logic here if needed) |
|
|
|
|
return False |
|
|
|
|
|