From a20899b930b5702babfddb8d3fc5e0a2975002c4 Mon Sep 17 00:00:00 2001 From: Daniel Gray Date: Fri, 19 Jul 2024 10:51:27 +0200 Subject: [PATCH] updated code --- app/accounts/forms.py | 25 ++++++++++++++++++++++++- app/accounts/urls.py | 16 +++++++++++----- app/accounts/views.py | 14 +++++++++++++- 3 files changed, 48 insertions(+), 7 deletions(-) diff --git a/app/accounts/forms.py b/app/accounts/forms.py index c58c33a4..a86ed2f2 100644 --- a/app/accounts/forms.py +++ b/app/accounts/forms.py @@ -1,5 +1,11 @@ from django import forms -from django.contrib.auth.forms import AuthenticationForm, UserCreationForm +from django.contrib.auth import get_user_model +from django.contrib.auth.forms import ( + AuthenticationForm, + PasswordResetForm, + UserCreationForm, +) +from django.contrib.auth.models import User from django.utils.translation import gettext_lazy as _ from users.models import CustomUser @@ -40,3 +46,20 @@ def __init__(self, *args, **kwargs): super(CustomAuthenticationForm, self).__init__(*args, **kwargs) for field in self.fields.values(): field.widget.attrs.update({"class": "form-control"}) + + +User = get_user_model() + + +class CustomPasswordResetForm(PasswordResetForm): + def clean_email(self): + email = self.cleaned_data["email"] + users = User.objects.filter(email=email) + if not users.exists(): + raise forms.ValidationError("No user is associated with this email address.") + + for user in users: + if not user.is_active or not user.is_staff: + raise forms.ValidationError("This account is inactive or not a staff account.") + + return email diff --git a/app/accounts/urls.py b/app/accounts/urls.py index 47ea7482..f7c34250 100644 --- a/app/accounts/urls.py +++ b/app/accounts/urls.py @@ -2,17 +2,23 @@ from django.urls import path from . import views +from .views import CustomPasswordResetView app_name = "accounts" urlpatterns = [ path("register/", views.register, name="accounts_register"), - path("login/", auth_views.LoginView.as_view(template_name="accounts/login.html"), name="login"), + # path("login/", auth_views.LoginView.as_view(template_name="accounts/login.html"), name="login"), + # path( + # "password_reset/", + # auth_views.PasswordResetView.as_view( + # template_name="accounts/password_reset_form.html", + # success_url="/accounts/password_reset/done/", + # ), + # name="password_reset", + # ), path( "password_reset/", - auth_views.PasswordResetView.as_view( - template_name="accounts/password_reset_form.html", - success_url="/accounts/password_reset/done/", - ), + CustomPasswordResetView.as_view(), name="password_reset", ), path( diff --git a/app/accounts/views.py b/app/accounts/views.py index eac5524f..f6d65275 100644 --- a/app/accounts/views.py +++ b/app/accounts/views.py @@ -1,12 +1,17 @@ from django.contrib.auth import get_user_model from django.contrib.auth import login as auth_login +from django.contrib.auth.views import PasswordResetView from django.shortcuts import redirect, render from django.utils.encoding import force_str from django.utils.http import urlsafe_base64_decode from accounts.service.active_email import SendActiveEmailService -from .forms import CustomAuthenticationForm, CustomUserCreationForm +from .forms import ( + CustomAuthenticationForm, + CustomPasswordResetForm, + CustomUserCreationForm, +) from .tokens import account_activation_token @@ -50,6 +55,7 @@ def activate(request, uidb64, token): user = None if user is not None and account_activation_token.check_token(user, token): + user.is_staff = True user.is_active = True user.save() auth_login(request, user) @@ -84,3 +90,9 @@ def resend_activation(request): request, "accounts/resend_activation.html", {"error": "Email address not found."} ) return render(request, "accounts/resend_activation.html") + + +class CustomPasswordResetView(PasswordResetView): + form_class = CustomPasswordResetForm + template_name = "accounts/password_reset_form.html" + success_url = "/accounts/password_reset/done/"