Skip to content

Commit b5ce823

Browse files
authored
Merge pull request #237 from RSE-Sheffield/feat/211-change-password
Add change password feature
2 parents a0729a9 + 6a1c397 commit b5ce823

File tree

4 files changed

+98
-10
lines changed

4 files changed

+98
-10
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
{% extends "base_manager.html" %}
2+
3+
{% block content %}
4+
<div class="container mx-auto px-4 py-8 mt-4">
5+
<div>
6+
<h1>Change password</h1>
7+
<p class="subtitle">Use the form below to update your password.</p>
8+
</div>
9+
<div class="card custom-card shadow-sm my-4">
10+
<div class="card-body">
11+
12+
<form method="post">{% csrf_token %}
13+
<div>
14+
{% if form.errors %}
15+
<p class="alert alert-warning" role="alert">
16+
Please correct any errors below.
17+
</p>
18+
{% endif %}
19+
20+
<p>
21+
Please enter your old password for security reasons. Then enter your new password twice
22+
so we can verify you typed it in correctly.
23+
</p>
24+
25+
<fieldset>
26+
<!-- Old password -->
27+
<div>
28+
{{ form.old_password.errors }}
29+
{{ form.old_password.label_tag }}
30+
{{ form.old_password }}
31+
</div>
32+
33+
<div>
34+
{{ form.new_password1.errors }}
35+
<div>{{ form.new_password1.label_tag }} {{ form.new_password1 }}</div>
36+
{% if form.new_password1.help_text %}
37+
<div class="form-text"{% if form.new_password1.id_for_label %}
38+
id="{{ form.new_password1.id_for_label }}_helptext"{% endif %}>{{ form.new_password1.help_text|safe }}</div>
39+
{% endif %}
40+
</div>
41+
42+
<div>
43+
{{ form.new_password2.errors }}
44+
<div>{{ form.new_password2.label_tag }} {{ form.new_password2 }}</div>
45+
{% if form.new_password2.help_text %}
46+
<div class="form-text "{% if form.new_password2.id_for_label %}
47+
id="{{ form.new_password2.id_for_label }}_helptext"{% endif %}>{{ form.new_password2.help_text|safe }}</div>
48+
{% endif %}
49+
</div>
50+
51+
</fieldset>
52+
53+
<div>
54+
<input type="submit" value="Change my password" class="btn btn-primary">
55+
</div>
56+
57+
</div>
58+
</form>
59+
</div>
60+
</div>
61+
</div>
62+
{% endblock %}

home/templates/home/profile.html

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,22 @@ <h1>Profile</h1>
3030
<input type="email" autocomplete="email" name="email" id="id_email" class="form-control"
3131
placeholder="Enter your email address" value="{{ user.email }}" required>
3232
</div>
33-
34-
<div class="text-center">
35-
<button type="submit" class="btn btn-primary" style="background-color: #6933AD; border: none;">
36-
<i class="bx bxs-user-check"></i> Update Profile
37-
</button>
38-
<!-- <a href="{% url 'home' %}" class="btn btn-primary" style="background-color: #6933AD; border: none; margin-left: 10px;">
39-
Cancel
40-
</a> -->
41-
</div>
33+
<button type="submit" class="btn btn-primary">
34+
<i class="bx bxs-user-check"></i> Update Profile
35+
</button>
4236
</form>
4337
</div>
4438
</div>
39+
<div class="card custom-card shadow-sm my-4">
40+
<div class="card-body">
41+
<h2>Security</h2>
42+
<p>
43+
<a href="{% url 'password_change' %}" class="btn btn-primary">
44+
<i class="bx bxs-key"></i>
45+
Change password
46+
</a>
47+
</p>
48+
</div>
49+
</div>
4550
</div>
4651
{% endblock %}

home/urls.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
__author__ = "Farhad Allian"
22

3+
4+
import django.urls
35
from django.urls import path, re_path
46

7+
58
from . import views
69

710
urlpatterns = [
@@ -14,6 +17,16 @@
1417
name="signup",
1518
),
1619
path("profile/", views.ProfileView.as_view(), name="profile"),
20+
# Change password using built-in authentication view
21+
# https://docs.djangoproject.com/en/5.2/topics/auth/default/#built-in-auth-views
22+
# https://docs.djangoproject.com/en/5.2/topics/auth/default/#django.contrib.auth.views.PasswordChangeView
23+
path("profile/change-password/",
24+
views.PasswordChangeView.as_view(
25+
template_name="home/password_change_form.html",
26+
success_url=django.urls.reverse_lazy("profile"),
27+
),
28+
name="password_change"),
29+
# Password reset by email (for lost passwords)
1730
path(
1831
"password_reset/",
1932
views.CustomPasswordResetView.as_view(),

home/views.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
from typing import Optional
21

2+
from typing import Optional
3+
import django.contrib.auth.views
34
import invitations.models
45
import invitations.views
56
from django.contrib import messages
@@ -399,6 +400,12 @@ def get_success_url(self):
399400
return reverse_lazy("myorganisation")
400401

401402

403+
404+
class PasswordChangeView(django.contrib.auth.views.PasswordChangeView):
405+
def form_valid(self, form):
406+
messages.success(self.request, "Your password has been changed.")
407+
return super().form_valid(form=form)
408+
402409
class OrganisationMembershipListView(LoginRequiredMixin, OrganisationRequiredMixin, ListView):
403410
model = OrganisationMembership
404411
context_object_name = "memberships"
@@ -476,3 +483,4 @@ def form_valid(self, form):
476483

477484
class HelpView(LoginRequiredMixin, TemplateView):
478485
template_name = "help.html"
486+

0 commit comments

Comments
 (0)