Skip to content

Commit

Permalink
Combine password/settings/email change pages
Browse files Browse the repository at this point in the history
  • Loading branch information
anorthall committed Jun 18, 2023
1 parent 033c8b4 commit ea3eff1
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 98 deletions.
10 changes: 1 addition & 9 deletions app/templates/users/_sidebar_account.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,7 @@
<i class="bi bi-ui-checks"></i> Trip fields
</a>

<a class="nav-link {% active_link 'users:password_update' %}" href="{% url 'users:password_update' %}">
<i class="bi bi-key-fill"></i> Password
</a>

<a class="nav-link {% active_link 'users:email' %}" href="{% url 'users:email' %}">
<i class="bi bi-envelope-at-fill"></i> Email
</a>

<a class="nav-link {% active_link 'users:settings_update' %}" href="{% url 'users:settings_update' %}">
<a class="nav-link {% active_link 'users:account_update' %}" href="{% url 'users:account_settings' %}">
<i class="bi bi-gear-fill"></i> Settings
</a>

Expand Down
18 changes: 18 additions & 0 deletions app/templates/users/account_settings.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{% extends "base_account.html" %}
{% load crispy_forms_tags %}

{% block title %}Settings{% endblock %}

{% block main %}
<div class="mb-5">
{% crispy settings_form %}
</div>

<div class="mb-5">
{% crispy email_form %}
</div>

<div class="mb-3">
{% crispy password_form %}
</div>
{% endblock %}
2 changes: 1 addition & 1 deletion app/templates/users/verify_email_change.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ <h3 class="subtitle">
email for a unique code which has been sent to you. You can then click
the link in the email, or enter the code into the box below. If you
did not receive the code, and it isn't in your email spam folder, please
submit the <a href="{% url 'users:email' %}">email
submit the <a href="{% url 'users:account_settings' %}">email
change</a> form again to resend it. Alternatively, send me an
<a href="mailto:admin@caves.app">email</a>
and I will be able to help.
Expand Down
37 changes: 18 additions & 19 deletions app/users/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from crispy_bootstrap5.bootstrap5 import FloatingField
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Div, Fieldset, Layout, Submit
from crispy_forms.layout import Div, Field, Fieldset, Layout, Submit
from django import forms
from django.contrib import auth
from django.contrib.auth import get_user_model
Expand Down Expand Up @@ -49,23 +49,23 @@ def __init__(self, request, *args, **kwargs):
# noinspection PyTypeChecker
class PasswordChangeForm(auth.forms.PasswordChangeForm):
def __init__(self, *args, **kwargs):
super(PasswordChangeForm, self).__init__(*args, **kwargs)
super().__init__(*args, **kwargs)
self.fields["old_password"].widget.attrs.pop("autofocus", None)
self.fields["new_password1"].help_text = ""
self.fields["new_password2"].help_text = (
"Your password can't be too similar to your other personal "
"information, must contain at least 8 characters, cannot be entirely "
"numeric and must not be a commonly used password."
"<div class='mw-35'>Your password can't be too similar to your other "
"personal information, must contain at least 8 characters, cannot be "
"entirely numeric and must not be a commonly used password.</div>"
)
self.helper = FormHelper()
self.helper.form_method = "post"
self.helper.form_class = "mw-35"
self.helper.layout = Layout(
Fieldset(
"Change password",
"old_password",
"new_password1",
"new_password2",
Submit("submit", "Change password"),
Field("old_password", css_class="mw-35"),
Field("new_password1", css_class="mw-35"),
Field("new_password2", css_class="mw-35"),
Submit("password_submit", "Change password"),
)
)

Expand All @@ -77,7 +77,7 @@ def __init__(self, *args, **kwargs):
self.helper.form_method = "post"
self.helper.layout = Layout(
FloatingField("email"),
Submit("submit", "Reset password", css_class="btn-lg w-100"),
Submit("password_submit", "Reset password", css_class="btn-lg w-100"),
)


Expand Down Expand Up @@ -287,7 +287,7 @@ def __init__(self, *args, **kwargs):
css_class="row row-cols-1 row-cols-lg-3 mt-4",
),
),
Submit("submit", "Save changes", css_class="btn-lg w-100 mt-4"),
Submit("settings_submit", "Update settings", css_class="mt-3"),
)


Expand Down Expand Up @@ -401,7 +401,7 @@ def clean(self):


# noinspection PyTypeChecker
class UserChangeEmailForm(forms.Form):
class EmailChangeForm(forms.Form):
email = forms.EmailField(
label="New email address",
max_length=255,
Expand All @@ -418,18 +418,17 @@ class UserChangeEmailForm(forms.Form):
help_text="For security reasons, please enter your existing password.",
)

def __init__(self, user, *args, **kwargs):
def __init__(self, *args, **kwargs):
self.user = kwargs.pop("user")
super().__init__(*args, **kwargs)
self.user = user
self.helper = FormHelper()
self.helper.form_method = "post"
self.helper.form_class = "mw-35"
self.helper.layout = Layout(
Fieldset(
"Change email address",
"email",
"password",
Submit("submit", "Update email"),
Field("email", css_class="mw-35"),
Field("password", css_class="mw-35"),
Submit("email_submit", "Update email"),
)
)

Expand Down
30 changes: 22 additions & 8 deletions app/users/tests/test_custom_user_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,12 @@ def test_user_registration_with_duplicate_username(self):
def test_change_user_email_with_invalid_password(self):
self.client.force_login(self.user)
response = self.client.post(
reverse("users:email"),
{"email": "new-email@caves.app", "password": "invalid"},
reverse("users:account_settings"),
{
"email": "new-email@caves.app",
"password": "invalid",
"email_submit": "Save",
},
follow=True,
)
self.assertEqual(response.status_code, 200)
Expand All @@ -367,8 +371,12 @@ def test_change_user_email_with_invalid_password(self):
def test_change_user_email_with_email_already_in_use(self):
self.client.force_login(self.user)
response = self.client.post(
reverse("users:email"),
{"email": self.user.email, "password": "password"},
reverse("users:account_settings"),
{
"email": self.user.email,
"password": "password",
"email_submit": "Save",
},
follow=True,
)
self.assertEqual(response.status_code, 200)
Expand All @@ -380,8 +388,12 @@ def test_change_user_email(self):

# Submit the change email form
response = self.client.post(
reverse("users:email"),
{"email": "new-email@caves.app", "password": "password"},
reverse("users:account_settings"),
{
"email": "new-email@caves.app",
"password": "password",
"email_submit": "Save",
},
follow=True,
)
self.assertEqual(response.status_code, 200)
Expand Down Expand Up @@ -467,12 +479,13 @@ def test_submit_updates_to_settings(self):
"""Test submitting updates to a user's settings"""
self.client.force_login(self.user)
response = self.client.post(
reverse("users:settings_update"),
reverse("users:account_settings"),
{
"privacy": "Friends",
"timezone": "US/Central",
"units": "Imperial",
"public_statistics": True,
"settings_submit": "Save",
},
follow=True,
)
Expand Down Expand Up @@ -505,11 +518,12 @@ def test_change_user_password(self):
"""Test changing a user's password"""
self.client.force_login(self.user)
response = self.client.post(
reverse("users:password_update"),
reverse("users:account_settings"),
{
"old_password": "password",
"new_password1": "new_password",
"new_password2": "new_password",
"password_submit": "Save",
},
follow=True,
)
Expand Down
14 changes: 1 addition & 13 deletions app/users/tests/test_pages_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,6 @@ def test_password_reset_confirm_page_loads(self):
)
self.assertEqual(response.status_code, 200)

def test_password_change_page_loads(self):
"""Test that the password change page loads"""
self.client.force_login(self.user)
response = self.client.get(reverse("users:password_update"))
self.assertEqual(response.status_code, 200)

def test_email_change_page_loads(self):
"""Test that the email change page loads"""
self.client.force_login(self.user)
response = self.client.get(reverse("users:email"))
self.assertEqual(response.status_code, 200)

def test_verify_email_change_page_loads(self):
"""Test that the verify email change page loads"""
self.client.force_login(self.user)
Expand All @@ -93,7 +81,7 @@ def test_profile_update_page_loads(self):
def test_settings_update_page_loads(self):
"""Test that the settings update page loads"""
self.client.force_login(self.user)
response = self.client.get(reverse("users:settings_update"))
response = self.client.get(reverse("users:account_settings"))
self.assertEqual(response.status_code, 200)

def test_profile_picture_update_page_loads(self):
Expand Down
4 changes: 1 addition & 3 deletions app/users/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@
urlpatterns = [
path("login/", views.Login.as_view(), name="login"),
path("logout/", views.Logout.as_view(), name="logout"),
path("email/", views.UpdateEmail.as_view(), name="email"),
path("view/", views.Account.as_view(), name="account_detail"),
path("settings/", views.SettingsUpdate.as_view(), name="settings_update"),
path("settings/", views.AccountSettings.as_view(), name="account_settings"),
path(
"custom-fields/",
views.CustomFieldsUpdate.as_view(),
name="custom_fields_update",
),
path("profile/", views.ProfileUpdate.as_view(), name="profile_update"),
path("profile/photo/", views.AvatarUpdate.as_view(), name="profile_photo_update"),
path("password/", views.PasswordChangeView.as_view(), name="password_update"),
path("password/reset/", views.PasswordResetView.as_view(), name="password_reset"),
path(
"password/reset/confirm/<uidb64>/<token>/",
Expand Down
Loading

0 comments on commit ea3eff1

Please sign in to comment.