-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Entirely rework the accounts front-end to simplify things and align better with built-in Django: - Use Bootstrap infrastructure to render forms plainly in templates (see previous commit). - Rename forms and views to correspond to the builtin ones, but ensure that the builtin ones can't accidentally be imported. - Use a base template for all account-related pages to ensure consistency. - Reuse the exact wording in Django templates in more places to be able to reuse Django translations seamlessly. - Avoid duplicate URLs that can be confusing if you hit the wrong one.
- Loading branch information
1 parent
31d187d
commit cbf8c96
Showing
20 changed files
with
227 additions
and
284 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,10 +2,10 @@ | |
|
||
from django.test import TestCase | ||
|
||
from accounts.forms import CustomUserCreationForm | ||
from accounts.forms import UserCreationForm | ||
|
||
|
||
class CustomUserCreationFormTest(TestCase): | ||
class UserCreationFormTest(TestCase): | ||
def setUp(self): | ||
self.username = "testuser" | ||
self.email = "[email protected]" | ||
|
@@ -15,7 +15,7 @@ def setUp(self): | |
self.password2 = "sadilar2024" | ||
|
||
def test_valid_data(self): | ||
form = CustomUserCreationForm( | ||
form = UserCreationForm( | ||
{ | ||
"username": self.username, | ||
"email": self.email, | ||
|
@@ -29,7 +29,7 @@ def test_valid_data(self): | |
self.assertTrue(form.is_valid()) | ||
|
||
def test_blank_data(self): | ||
form = CustomUserCreationForm({}) | ||
form = UserCreationForm({}) | ||
self.assertFalse(form.is_valid()) | ||
|
||
self.assertEqual( | ||
|
@@ -45,7 +45,7 @@ def test_blank_data(self): | |
) | ||
|
||
def test_invalid_email(self): | ||
form = CustomUserCreationForm( | ||
form = UserCreationForm( | ||
{ | ||
"username": self.username, | ||
"email": "not a valid email", | ||
|
@@ -64,7 +64,7 @@ def test_invalid_email(self): | |
) | ||
|
||
def test_passwords_do_not_match(self): | ||
form = CustomUserCreationForm( | ||
form = UserCreationForm( | ||
{ | ||
"username": self.username, | ||
"email": self.email, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,55 @@ | ||
from django.contrib.auth import authenticate | ||
from django.contrib.auth import login as auth_login | ||
from django.contrib.auth.views import LoginView as _LoginView | ||
from django.contrib.auth.views import PasswordChangeView as _PasswordChangeView | ||
from django.contrib.auth.views import ( | ||
PasswordResetConfirmView as _PasswordResetConfirmView, | ||
) | ||
from django.contrib.auth.views import PasswordResetView as _PasswordResetView | ||
from django.shortcuts import redirect, render | ||
|
||
from .forms import CustomAuthenticationForm, CustomUserCreationForm | ||
from .forms import ( | ||
AuthenticationForm, | ||
PasswordChangeForm, | ||
PasswordResetForm, | ||
SetPasswordForm, | ||
UserCreationForm, | ||
) | ||
|
||
|
||
def register(request): | ||
if request.method == "POST": | ||
form = CustomUserCreationForm(request.POST) | ||
form = UserCreationForm(request.POST) | ||
if form.is_valid(): | ||
user = form.save(commit=False) | ||
user.is_staff = True | ||
user.save() | ||
auth_login(request, user) | ||
return redirect("home") | ||
else: | ||
form = CustomUserCreationForm() | ||
return render(request, "accounts/register.html", {"form": form}) | ||
form = UserCreationForm() | ||
return render(request, "registration/register.html", {"form": form}) | ||
|
||
|
||
def user_login(request): | ||
if request.method == "POST": | ||
form = CustomAuthenticationForm(request, data=request.POST) | ||
if form.is_valid(): | ||
user = form.get_user() | ||
auth_login(request, user) | ||
return redirect("home") | ||
else: | ||
form = CustomAuthenticationForm() | ||
# We subclass the builtin views where we want to supply our own forms. We | ||
# (mostly) stick to the expected template names, and they are therefore | ||
# automatically picked up, even in views where we don't subclass the builtin | ||
# views. | ||
class LoginView(_LoginView): | ||
form_class = AuthenticationForm | ||
|
||
|
||
class PasswordChangeView(_PasswordChangeView): | ||
form_class = PasswordChangeForm | ||
# The builtin view expects password_change_form.html, but so does the admin | ||
# interface, and we don't want to replace that one as well, so we use a | ||
# custom name. | ||
template_name = "registration/password_change_form2.html" | ||
|
||
|
||
class PasswordResetView(_PasswordResetView): | ||
form_class = PasswordResetForm | ||
|
||
|
||
return render(request, "accounts/login.html", {"form": form}) | ||
class PasswordResetConfirmView(_PasswordResetConfirmView): | ||
form_class = SetPasswordForm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.