From 6e08034b57d1ce34e15b8273498f59f1d8f0f182 Mon Sep 17 00:00:00 2001
From: Dan Braghis <dan.braghis@torchbox.com>
Date: Tue, 9 Jan 2018 16:05:06 +0000
Subject: [PATCH] Add password change form

---
 opentech/apply/users/urls.py                  | 10 ++++++
 opentech/apply/users/views.py                 |  8 +++--
 opentech/users/templates/users/account.html   |  5 +++
 .../templates/users/change_password.html      | 36 +++++++++++++++++++
 4 files changed, 57 insertions(+), 2 deletions(-)
 create mode 100644 opentech/users/templates/users/change_password.html

diff --git a/opentech/apply/users/urls.py b/opentech/apply/users/urls.py
index dd1ea0b17..76e5050bf 100644
--- a/opentech/apply/users/urls.py
+++ b/opentech/apply/users/urls.py
@@ -22,6 +22,16 @@ urlpatterns = [
     # Log out
     url(r'^logout/$', auth_views.LogoutView.as_view(next_page='/'), name='logout'),
 
+    # Password change
+    url(
+        r'^password/$',
+        auth_views.PasswordChangeView.as_view(
+            template_name="users/change_password.html",
+            success_url=reverse_lazy('users:account')
+        ),
+        name='password_change',
+    ),
+
     # Password reset
     url(r'^reset/$', auth_views.PasswordResetView.as_view(
         template_name='users/password_reset/form.html',
diff --git a/opentech/apply/users/views.py b/opentech/apply/users/views.py
index 004fa0aa6..4ffcd7a58 100644
--- a/opentech/apply/users/views.py
+++ b/opentech/apply/users/views.py
@@ -1,8 +1,12 @@
 from django.contrib.auth.decorators import login_required
-from django.template.response import TemplateResponse
+from django.shortcuts import render
 
+from wagtail.wagtailadmin.views.account import password_management_enabled
 
 @login_required(login_url='/user/login')
 def account(request):
     "Account page placeholder view"
-    return TemplateResponse(request, 'users/account.html', {})
+
+    return render(request, 'users/account.html', {
+        'show_change_password': password_management_enabled() and request.user.has_usable_password(),
+    })
diff --git a/opentech/users/templates/users/account.html b/opentech/users/templates/users/account.html
index 76ce008a2..725d61531 100644
--- a/opentech/users/templates/users/account.html
+++ b/opentech/users/templates/users/account.html
@@ -1,7 +1,12 @@
 {% extends 'base.html' %}
+{% load i18n %}
 
 {% block title %}Account{% endblock %}
 
 {% block content %}
 <h2>Welcome {{ user }}</h2>
+
+{% if show_change_password %}
+    <a href="{% url 'users:password_change' %}">{% trans "Change password" %}</a>
+{% endif %}
 {% endblock %}
diff --git a/opentech/users/templates/users/change_password.html b/opentech/users/templates/users/change_password.html
new file mode 100644
index 000000000..efde07945
--- /dev/null
+++ b/opentech/users/templates/users/change_password.html
@@ -0,0 +1,36 @@
+{% extends 'base.html' %}
+{% load i18n %}
+
+{% block title %}{% trans "Change password" %}{% endblock %}
+
+
+{% block content %}
+    {% if form.non_field_errors %}
+        <div class="messages">
+            <ul>
+                {% for error in form.non_field_errors %}
+                <li class="error">{{ error }}</li>
+                {% endfor %}
+            </ul>
+        </div>
+    {% endif %}
+
+    {% if form.errors %}
+        <div class="messages">
+            {% if form.errors.items|length == 1 %}{% trans "Please correct the error below." %}{% else %}{% trans "Please correct the errors below." %}{% endif %}
+
+        </div>
+    {% endif %}
+
+    <form action="{% url 'users:password_change' %}" method="POST" novalidate>
+        {% csrf_token %}
+
+        {% for field in form %}
+         {{ field.errors }}
+         {{ field.label_tag }}
+         {{ field }}
+        {% endfor %}
+
+        <button type="submit">{% trans 'Reset password' %}</button>
+    </form>
+{% endblock %}
-- 
GitLab