From eb059a78e760fef170543f29e119f4a69187998b Mon Sep 17 00:00:00 2001
From: Carlos Vega <carlos.vega@uni.lu>
Date: Mon, 19 Nov 2018 10:45:05 +0100
Subject: [PATCH] Issue #272 added webpage, url, and view to change password.

---
 .../templates/doctors/change_password.html    | 51 +++++++++++++++++++
 smash/web/urls.py                             |  2 +
 smash/web/views/__init__.py                   |  1 +
 smash/web/views/password.py                   | 22 ++++++++
 4 files changed, 76 insertions(+)
 create mode 100644 smash/web/templates/doctors/change_password.html
 create mode 100644 smash/web/views/password.py

diff --git a/smash/web/templates/doctors/change_password.html b/smash/web/templates/doctors/change_password.html
new file mode 100644
index 00000000..17ee4981
--- /dev/null
+++ b/smash/web/templates/doctors/change_password.html
@@ -0,0 +1,51 @@
+{% extends "_base.html" %}
+{% load static %}
+{% load filters %}
+
+{% block styles %}
+    {{ block.super }}
+    <!-- DataTables -->
+    <link rel="stylesheet" href="{% static 'AdminLTE/plugins/datatables/dataTables.bootstrap.css' %}">
+    <style type="text/css">
+        .hidden_form_field{
+            display: none;
+        }
+        form.password_change{
+        	display: grid;
+    		max-width: 400px;
+        }
+        button[type=submit]{
+        	margin-top: 15px;
+        }
+    </style>
+{% endblock styles %}
+
+{% block ui_active_tab %}'workers'{% endblock ui_active_tab %}
+
+{% block title %}{{ block.super }} - Change password {% endblock %}
+
+{% block page_header %} Changing password for worker <b>{{ worker }}</b> with username: <b>{{ user }}</b> {% endblock page_header %}
+
+{% block breadcrumb %}
+    {% include "doctors/breadcrumb.html" %}
+{% endblock breadcrumb %}
+
+{% block maincontent %}
+
+    {% block content %}
+
+<form class="password_change" method="post">
+  {% csrf_token %}
+  {{ form }}
+  <button type="submit">Save changes</button>
+</form>
+
+
+{% endblock %}
+
+{% endblock maincontent %}
+
+{% block scripts %}
+    {{ block.super }}
+
+{% endblock scripts %}
\ No newline at end of file
diff --git a/smash/web/urls.py b/smash/web/urls.py
index 7fd886c2..9535617e 100644
--- a/smash/web/urls.py
+++ b/smash/web/urls.py
@@ -33,6 +33,8 @@ urlpatterns = [
         {'exception': Exception('Not Found')}
     ),
 
+    url(r'^change_password/$', views.password.change_password, name='change_password'),
+
     ####################
     #   APPOINTMENTS   #
     ####################
diff --git a/smash/web/views/__init__.py b/smash/web/views/__init__.py
index fa58ba75..78203279 100644
--- a/smash/web/views/__init__.py
+++ b/smash/web/views/__init__.py
@@ -97,3 +97,4 @@ import redcap
 import rooms
 import uploaded_files
 import study
+import password
\ No newline at end of file
diff --git a/smash/web/views/password.py b/smash/web/views/password.py
new file mode 100644
index 00000000..51550730
--- /dev/null
+++ b/smash/web/views/password.py
@@ -0,0 +1,22 @@
+from django.contrib import messages
+from django.contrib.auth import update_session_auth_hash
+from django.contrib.auth.forms import PasswordChangeForm
+from django.shortcuts import render, redirect
+from . import wrap_response
+from web.models.worker import Worker
+
+def change_password(request):
+    if request.method == 'POST':
+        form = PasswordChangeForm(request.user, request.POST)
+        if form.is_valid():
+            user = form.save()
+            update_session_auth_hash(request, user)  # Important!
+            messages.success(request, 'The password for {} was successfully updated!'.format(request.user))
+            return redirect('web.views.workers')
+        else:
+            messages.error(request, 'Please correct the error below.')
+    else:
+        form = PasswordChangeForm(request.user)
+
+    worker = Worker.get_by_user(request.user)
+    return wrap_response(request, 'doctors/change_password.html', {'form': form, 'worker': worker})
\ No newline at end of file
-- 
GitLab