Skip to content

Commit

Permalink
updated code
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-gray-tangent committed Jul 19, 2024
1 parent bcf597a commit a20899b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
25 changes: 24 additions & 1 deletion app/accounts/forms.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
16 changes: 11 additions & 5 deletions app/accounts/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
14 changes: 13 additions & 1 deletion app/accounts/views.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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/"

0 comments on commit a20899b

Please sign in to comment.