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

117 lines
4.2 KiB

from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from django import forms
from .models import CustomUser
import re # Import the re module for regular expressions
from .utils.licence_validator import LicenseValidator
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 license (avec la lettre)', max_length=20)
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
def clean(self):
cleaned_data = super().clean()
# Retrieve the cleaned licence_id (from the previous step)
licence_id = cleaned_data.get('licence_id')
last_name = cleaned_data.get('last_name')
# Return the cleaned data with any modifications applied
return cleaned_data