diff --git a/tournaments/custom_views.py b/tournaments/custom_views.py index 01d54bb..4e3d2f1 100644 --- a/tournaments/custom_views.py +++ b/tournaments/custom_views.py @@ -10,19 +10,33 @@ class CustomLoginView(auth_views.LoginView): def get_success_url(self): # First check the 'next' parameter which has higher priority next_url = self.request.POST.get('next') or self.request.GET.get('next') + + # Check if the next URL is a password reset page and avoid that redirect if next_url and next_url.strip(): + # Avoid redirecting to password reset pages after login + if 'reset' in next_url or 'password_reset' in next_url: + # Redirect to profile or index instead + return reverse('profile') return next_url # Then check if we have a stored referrer URL referrer = self.request.session.get('login_referrer') if referrer: - # Clear the stored referrer to prevent reuse - del self.request.session['login_referrer'] - return referrer + # Avoid redirecting to password reset pages from stored referrer + if 'reset' not in referrer and 'password_reset' not in referrer: + # Clear the stored referrer to prevent reuse + del self.request.session['login_referrer'] + return referrer # Fall back to default return reverse('index') def get(self, request, *args, **kwargs): + # Clear any potential password reset session data + keys_to_clear = [key for key in request.session.keys() + if 'reset' in key or 'password' in key] + for key in keys_to_clear: + del request.session[key] + messages.get_messages(request).used = True return super().get(request, *args, **kwargs) diff --git a/tournaments/forms.py b/tournaments/forms.py index cc4d314..872514b 100644 --- a/tournaments/forms.py +++ b/tournaments/forms.py @@ -12,6 +12,11 @@ from django.utils.encoding import force_bytes class CustomUserCreationForm(UserCreationForm): usable_password = None + def clean_licence_id(self): + licence_id = self.cleaned_data.get('licence_id') + if licence_id: + return licence_id.replace(' ', '').strip().upper() + return licence_id class Meta: model = CustomUser @@ -33,6 +38,22 @@ class CustomUserCreationForm(UserCreationForm): class SimpleCustomUserCreationForm(UserCreationForm): usable_password = None + def clean_licence_id(self): + licence_id = self.cleaned_data.get('licence_id') + if licence_id: + return licence_id.replace(' ', '').strip().upper() + return licence_id + + def clean_phone(self): + phone = self.cleaned_data.get('phone') + if phone: + # Remove all spaces + phone = phone.replace(' ', '') + # Basic regex for phone numbers, matching common formats + if not re.match(r"^\+?\d{10,15}$", phone): + raise forms.ValidationError("Entrer un numéro de téléphone valide.") + return phone + class Meta: model = CustomUser fields = UserCreationForm.Meta.fields + ('email', 'phone', 'first_name', 'last_name', 'licence_id', 'country') @@ -125,16 +146,8 @@ class AddPlayerForm(forms.Form): 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}") - + if licence_id: + licence_id = licence_id.replace(' ', '').strip().upper() return licence_id def clean_last_name(self): @@ -193,6 +206,22 @@ class ProfileUpdateForm(forms.ModelForm): # Remove autofocus from the 'username' field self.fields['username'].widget.attrs.pop("autofocus", None) + def clean_licence_id(self): + licence_id = self.cleaned_data.get('licence_id') + if licence_id: + return licence_id.replace(' ', '').upper() + return licence_id + + def clean_phone(self): + phone = self.cleaned_data.get('phone') + if phone: + # Remove all spaces + phone = phone.replace(' ', '') + # Basic regex for phone numbers, matching common formats + if not re.match(r"^\+?\d{10,15}$", phone): + raise forms.ValidationError("Entrer un numéro de téléphone valide.") + return phone + class Meta: model = CustomUser fields = ['first_name', 'last_name', 'licence_id', 'username', 'email', 'phone'] diff --git a/tournaments/services/email_service.py b/tournaments/services/email_service.py index 3d14cf9..4406d68 100644 --- a/tournaments/services/email_service.py +++ b/tournaments/services/email_service.py @@ -76,7 +76,7 @@ class TournamentEmailService: body_parts.append(f"Votre inscription en liste d'attente du tournoi {tournament_details_str} est confirmée.") else: body_parts.append(f"Votre inscription au tournoi {tournament_details_str} est confirmée.") - if tournament.team_sort == TeamSortingType.RANK: + if tournament.team_sorting == TeamSortingType.RANK: cloture_date = tournament.local_registration_federal_limit().strftime("%d/%m/%Y à %H:%M") loc = "" if cloture_date is not None: diff --git a/tournaments/services/tournament_registration.py b/tournaments/services/tournament_registration.py index 0233edb..d42ae9e 100644 --- a/tournaments/services/tournament_registration.py +++ b/tournaments/services/tournament_registration.py @@ -48,6 +48,13 @@ class TournamentRegistrationService: if not self.context['add_player_form'].is_valid(): return + # Clear existing messages if the form is valid + from django.contrib.messages import get_messages + storage = get_messages(self.request) + # Iterate through the storage to clear it + for _ in storage: + pass + player_data = self.context['add_player_form'].cleaned_data licence_id = player_data.get('licence_id', '').upper() @@ -112,6 +119,12 @@ class TournamentRegistrationService: self.context['registration_successful'] = True def handle_get_request(self): + from django.contrib.messages import get_messages + storage = get_messages(self.request) + # Iterate through the storage to clear it + for _ in storage: + pass + self.context['add_player_form'] = AddPlayerForm() self.context['team_form'] = self.initialize_team_form() self.initialize_session_data() diff --git a/tournaments/templates/profile.html b/tournaments/templates/profile.html index 89fd5f4..2e6a1a7 100644 --- a/tournaments/templates/profile.html +++ b/tournaments/templates/profile.html @@ -20,27 +20,34 @@ {% load static %} {% load tz %} +{% if form.errors or password_change_form.errors %}
{{ error }}
+{{ error }}
- {% endfor %} -