You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
padelclub_backend/tournaments/forms.py

156 lines
5.7 KiB

from django.contrib.auth.forms import UserCreationForm, UserChangeForm, PasswordResetForm
from django import forms
from .models import CustomUser
import re # Import the re module for regular expressions
from .utils.licence_validator import LicenseValidator
from django.core.mail import send_mail
from django.template.loader import render_to_string
from django.utils.http import urlsafe_base64_encode, urlsafe_base64_decode
from django.contrib.auth.tokens import default_token_generator
from django.contrib.sites.shortcuts import get_current_site
from django.utils.encoding import force_bytes
class CustomUserCreationForm(UserCreationForm):
class Meta:
model = CustomUser
fields = UserCreationForm.Meta.fields + ('umpire_code', 'clubs', 'phone', 'first_name', 'last_name', 'licence_id', 'country')
class SimpleCustomUserCreationForm(UserCreationForm):
class Meta:
model = CustomUser
fields = UserCreationForm.Meta.fields + ('email', 'phone', 'first_name', 'last_name', 'licence_id', 'country')
labels = {
'username': 'Nom d’utilisateur',
'email': 'E-mail',
'phone': 'Numéro de téléphone',
'first_name': 'Prénom',
'last_name': 'Nom de famille',
'licence_id': 'Numéro de licence',
'country': 'Pays',
'password1': 'Mot de passe',
'password2': 'Confirmer le mot de passe',
}
class CustomUserChangeForm(UserChangeForm):
class Meta:
model = CustomUser
fields = UserCreationForm.Meta.fields + ('umpire_code', 'clubs', 'phone', 'first_name', 'last_name', 'licence_id', 'country')
class SimpleForm(forms.Form):
# A single field for user input (e.g., a text field)
name = forms.CharField(label='Enter your name!', max_length=100)
class TournamentRegistrationForm(forms.Form):
#first_name = forms.CharField(label='Prénom', max_length=50)
#last_name = forms.CharField(label='Nom', max_length=50)
email = forms.EmailField(label='E-mail')
mobile_number = forms.CharField(
label='Téléphone (facultatif)',
max_length=15,
required=False
)
def is_registration_complete(self):
# Check if the minimum number of players (2) is met
return len(self.players) >= 2
def clean_mobile_number(self):
mobile_number = self.cleaned_data.get('mobile_number')
if mobile_number:
# Basic regex for mobile numbers, matching common formats
if not re.match(r"^\+?\d{10,15}$", mobile_number):
raise forms.ValidationError("Entrer un numéro de téléphone valide.")
return mobile_number
class AddPlayerForm(forms.Form):
licence_id = forms.CharField(label='Numéro de licence (avec la lettre)', max_length=20, required=False)
first_name = forms.CharField(label='Prénom', min_length=2, max_length=50, required=False)
last_name = forms.CharField(label='Nom', min_length=2, max_length=50, required=False)
first_tournament = False
def names_is_valid(self):
first_name = self.cleaned_data.get('first_name')
last_name = self.cleaned_data.get('last_name')
return len(first_name) >= 2 and len(last_name) >= 2
def clean_licence_id(self):
licence_id = self.cleaned_data.get('licence_id')
# Convert to uppercase
licence_id = licence_id.upper()
# Update the cleaned_data with the modified licence_id
self.cleaned_data['licence_id'] = licence_id
# Optionally, print the cleaned license ID for debugging
print(f"Cleaned Licence ID (inside clean_licence_id): {licence_id}")
return licence_id
def clean_last_name(self):
last_name = self.cleaned_data.get('last_name')
# Convert to uppercase
last_name = last_name.upper()
# Update the cleaned_data with the modified licence_id
self.cleaned_data['last_name'] = last_name
return last_name
def clean_first_name(self):
first_name = self.cleaned_data.get('first_name')
# Convert to capitalize
first_name = first_name.capitalize()
# Update the cleaned_data with the modified licence_id
self.cleaned_data['first_name'] = first_name
return first_name
class CustomPasswordResetForm(PasswordResetForm):
def save(self, *args, **kwargs):
"""
Override the save method to send a custom email.
"""
email = self.cleaned_data["email"]
users = self.get_users(email)
for user in users:
# Generate the token for password reset
token = default_token_generator.make_token(user)
uid = urlsafe_base64_encode(force_bytes(user.pk))
# Prepare the context for the email template
context = {
"email": user.email,
"domain": get_current_site(self.request).domain,
"site_name": "Padel Club",
"uid": uid,
"token": token,
"protocol": "http", # Use 'https' in production
}
# Render the email content from the template
subject = "Réinitialisation du mot de passe"
message = render_to_string("registration/password_reset_email.html", context)
# Send the email
send_mail(subject, message, None, [user.email])
class ProfileUpdateForm(forms.ModelForm):
class Meta:
model = CustomUser
fields = ['first_name', 'last_name', 'licence_id', 'username', 'email', 'phone']
labels = {
'username': 'Nom d’utilisateur',
'email': 'E-mail',
'phone': 'Numéro de téléphone',
'first_name': 'Prénom',
'last_name': 'Nom de famille',
'licence_id': 'Numéro de licence',
}