diff --git a/tournaments/custom_views.py b/tournaments/custom_views.py index 6469c21..d3503dc 100644 --- a/tournaments/custom_views.py +++ b/tournaments/custom_views.py @@ -31,6 +31,17 @@ class CustomLoginView(auth_views.LoginView): # Fall back to default return reverse('index') + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + + # Check for inactive user in session + inactive_user_email = self.request.session.get('inactive_user_email') + if inactive_user_email: + context['inactive_user_email'] = inactive_user_email + context['show_resend_activation'] = True + + return context + def get(self, request, *args, **kwargs): # Clear any potential password reset session data keys_to_clear = [key for key in request.session.keys() @@ -38,6 +49,10 @@ class CustomLoginView(auth_views.LoginView): for key in keys_to_clear: del request.session[key] + # Clear inactive user session data on GET request (fresh login page) + request.session.pop('inactive_user_email', None) + request.session.pop('inactive_user_id', None) + storage = messages.get_messages(request) for _ in storage: pass diff --git a/tournaments/forms.py b/tournaments/forms.py index 94578d2..2ca881d 100644 --- a/tournaments/forms.py +++ b/tournaments/forms.py @@ -338,29 +338,52 @@ class EmailOrUsernameAuthenticationForm(AuthenticationForm): username = self.cleaned_data.get('username') password = self.cleaned_data.get('password') - print(f"Login attempt with username/email: {username}") # Debug print logger.info(f"Login attempt with username/email: {username}") if username and password: + # Check if user exists first (either by username or email) + user_exists = None + try: + # Try to find user by username first + user_exists = CustomUser.objects.get(username__iexact=username) + except CustomUser.DoesNotExist: + # Try to find user by email + try: + user_exists = CustomUser.objects.get(email__iexact=username) + except CustomUser.DoesNotExist: + pass + + # If user exists but is inactive, provide specific feedback + if user_exists and not user_exists.is_active: + # Store the inactive user in session for template use + if hasattr(self, 'request') and self.request.session: + self.request.session['inactive_user_email'] = user_exists.email + self.request.session['inactive_user_id'] = str(user_exists.id) + + raise forms.ValidationError( + "Votre compte n'est pas encore activé. Veuillez cliquer sur le lien d'activation envoyé à votre adresse e-mail.", + code='inactive_account' + ) + + # Try regular authentication self.user_cache = authenticate( self.request, username=username, password=password ) - print(f"Authentication result: {self.user_cache}") # Debug print - logger.info(f"Authentication result: {self.user_cache}") - if self.user_cache is None: - print("Authentication failed") # Debug print logger.warning("Authentication failed") raise forms.ValidationError( "Identifiant/E-mail ou mot de passe incorrect. Les champs sont sensibles à la casse.", code='invalid_login' ) else: - print(f"Authentication successful for user: {self.user_cache}") # Debug print logger.info(f"Authentication successful for user: {self.user_cache}") + # Clear any inactive user session data on successful login + if hasattr(self, 'request') and self.request.session: + self.request.session.pop('inactive_user_email', None) + self.request.session.pop('inactive_user_id', None) self.confirm_login_allowed(self.user_cache) return self.cleaned_data diff --git a/tournaments/templates/registration/login.html b/tournaments/templates/registration/login.html index d4ac8e7..6bde7f3 100644 --- a/tournaments/templates/registration/login.html +++ b/tournaments/templates/registration/login.html @@ -13,11 +13,9 @@
{% if form.non_field_errors %}
- {% if form.non_field_errors %} - {% for error in form.non_field_errors %} -

{{ error }}

- {% endfor %} - {% endif %} + {% for error in form.non_field_errors %} +

{{ error }}

+ {% endfor %} {% for field in form %} {% for error in field.errors %} @@ -26,6 +24,30 @@ {% endfor %}
{% endif %} + + + {% if inactive_user_email %} +
+

Besoin d'aide ?

+

Si vous n'avez pas reçu l'e-mail d'activation ou si le lien a expiré, vous pouvez en demander un nouveau :

+ +
+ {% csrf_token %} + + {% if request.GET.next %} + + {% endif %} + +
+ +

+ Le lien sera envoyé à : {{ inactive_user_email }} +

+
+ {% endif %} +
{% csrf_token %} {% if request.GET.next and 'reset' not in request.GET.next and 'password_reset' not in request.GET.next %} diff --git a/tournaments/urls.py b/tournaments/urls.py index 6867a7c..5ea47c7 100644 --- a/tournaments/urls.py +++ b/tournaments/urls.py @@ -60,7 +60,7 @@ urlpatterns = [ path('logout/', views.custom_logout, name='custom_logout'), path('utils/xls-to-csv/', views.xls_to_csv, name='xls-to-csv'), path('signup/', views.signup, name='signup'), # URL pattern for signup -# path('profile/', views.profile, name='profile'), # URL pattern for signup + path('resend-activation/', views.resend_activation_email, name='resend-activation'), path('my-tournaments/', views.my_tournaments, name='my-tournaments'), # URL pattern for signup path('all_my_ended_tournaments/', views.all_my_ended_tournaments, name='all-my-ended-tournaments'), # URL pattern for signup path('tournaments//cancel-registration/', views.cancel_registration, name='cancel_registration'), diff --git a/tournaments/views.py b/tournaments/views.py index 0db52eb..4d606a6 100644 --- a/tournaments/views.py +++ b/tournaments/views.py @@ -730,6 +730,43 @@ def send_verification_email(request, user, next_url): email.content_subtype = "html" email.send() +def resend_activation_email(request): + """View to resend activation email for inactive users.""" + if request.method == 'POST': + username_or_email = request.POST.get('username_or_email', '').strip() + + if not username_or_email: + messages.error(request, 'Veuillez fournir un nom d\'utilisateur ou un e-mail.') + return redirect('custom-login') + + # Try to find the user + user = None + try: + # Try by username first + user = CustomUser.objects.get(username__iexact=username_or_email) + except CustomUser.DoesNotExist: + try: + # Try by email + user = CustomUser.objects.get(email__iexact=username_or_email) + except CustomUser.DoesNotExist: + messages.error(request, 'Aucun compte trouvé avec cet identifiant.') + return redirect('custom-login') + + # Check if user is already active + if user.is_active: + messages.info(request, 'Votre compte est déjà activé. Vous pouvez vous connecter.') + return redirect('custom-login') + + # Send the activation email + next_url = request.POST.get('next', '') + send_verification_email(request, user, next_url) + + messages.success(request, f'Un nouveau lien d\'activation a été envoyé à {user.email}.') + return redirect('custom-login') + + # If GET request, redirect to login + return redirect('custom-login') + @login_required def profile(request): user = request.user # Get the currently authenticated user