Implement Change Password form

This commit is contained in:
Mario Hüttel 2021-11-09 14:50:57 +01:00
parent 95b0ff4933
commit 323e857aa5
6 changed files with 58 additions and 2 deletions

View File

@ -7,5 +7,6 @@ urlpatterns = [
path('stocks/', parts_views.StockView.as_view(), name='parts-stocks'), path('stocks/', parts_views.StockView.as_view(), name='parts-stocks'),
path('logout/', parts_views.logout_view, name='logout'), path('logout/', parts_views.logout_view, name='logout'),
path('login/', parts_views.login_view, name='login'), path('login/', parts_views.login_view, name='login'),
path('changepw/', parts_views.ChangePasswordView.as_view(), name='parts-change-pw'),
path('stocks/<slug:uuid>', parts_views.StockViewDetail.as_view(), name='parts-stocks-detail'), path('stocks/<slug:uuid>', parts_views.StockViewDetail.as_view(), name='parts-stocks-detail'),
] ]

View File

@ -5,6 +5,8 @@ from django.contrib.auth.models import User
from django.http import HttpResponse from django.http import HttpResponse
from .navbar import NavBar from .navbar import NavBar
from django.contrib.auth.forms import AuthenticationForm as AuthForm from django.contrib.auth.forms import AuthenticationForm as AuthForm
from django.contrib.auth.forms import PasswordChangeForm
from django.contrib.auth import update_session_auth_hash
from django.views import View from django.views import View
import django.forms as forms import django.forms as forms
from django.views.generic import TemplateView, DetailView from django.views.generic import TemplateView, DetailView
@ -53,6 +55,38 @@ class BaseTemplateMixin(object):
return self.get(request) return self.get(request)
class ChangePasswordView(LoginRequiredMixin, BaseTemplateMixin, TemplateView):
template_name = 'parts/change-pw.html'
navbar_selected = 'Main'
base_title = 'Change Password'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['form'] = PasswordChangeForm(self.request.user)
return context
def post(self, request, *args, **kwargs):
if 'submit-change-pw' not in request.POST:
return super().post(request, *args, **kwargs)
form = PasswordChangeForm(request.user, data=request.POST)
if form.is_valid():
user = form.save()
update_session_auth_hash(request, user)
return redirect('parts-main')
else:
pass
context = self.get_context_data(**kwargs)
if form.errors:
context['form'] = form
return self.render_to_response(context)
class MainView(BaseTemplateMixin, TemplateView): class MainView(BaseTemplateMixin, TemplateView):
template_name = 'parts/main.html' template_name = 'parts/main.html'
navbar_selected = 'Main' navbar_selected = 'Main'

View File

@ -1 +0,0 @@
:root{--close-button:url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M18.984 6.422 13.406 12l5.578 5.578-1.406 1.406L12 13.406l-5.578 5.578-1.406-1.406L10.594 12 5.016 6.422l1.406-1.406L12 10.594l5.578-5.578z'/%3E%3C/svg%3E");--loupe-icon:url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath fill='%23929292' d='M16.041 15.856a.995.995 0 0 0-.186.186A6.97 6.97 0 0 1 11 18c-1.933 0-3.682-.782-4.95-2.05S4 12.933 4 11s.782-3.682 2.05-4.95S9.067 4 11 4s3.682.782 4.95 2.05S18 9.067 18 11a6.971 6.971 0 0 1-1.959 4.856zm5.666 4.437-3.675-3.675A8.967 8.967 0 0 0 20 11c0-2.485-1.008-4.736-2.636-6.364S13.485 2 11 2 6.264 3.008 4.636 4.636 2 8.515 2 11s1.008 4.736 2.636 6.364S8.515 20 11 20a8.967 8.967 0 0 0 5.618-1.968l3.675 3.675a.999.999 0 1 0 1.414-1.414z'/%3E%3C/svg%3E")}.auto-search{display:block;position:relative;width:100%}.auto-search input{border:1px solid #d7d7d7;box-shadow:none;box-sizing:border-box;font-size:16px;padding:12px 45px 12px 10px;width:100%}.auto-search input:focus{border:1px solid #858585;outline:none}.auto-search input::-ms-clear{display:none}.auto-search ul{box-sizing:border-box;list-style:none;overflow:auto;padding:0}.auto-search ul li{cursor:pointer;margin:0;overflow:hidden;padding:10px;position:relative}.auto-search ul li:not(:last-child){border-top:none}.auto-search ul li[disabled]{background:#ececec;opacity:.5;pointer-events:none}.auto-search .auto-expanded{border:1px solid #858585;outline:none}.auto-search.loupe:before{filter:invert(60%)}.auto-is-loading:after{animation:auto-spinner .6s linear infinite;border-color:#d9d9d9 grey grey #d9d9d9;border-radius:50%;border-style:solid;border-width:2px;bottom:0;box-sizing:border-box;content:"";height:20px;margin:auto;position:absolute;right:10px;top:0;width:20px}.auto-is-loading .auto-clear{display:none}@keyframes auto-spinner{to{transform:rotate(1turn)}}li.loupe:before{bottom:auto;top:15px}.loupe input{padding:12px 45px 12px 40px}.loupe:before{background-image:var(--loupe-icon);bottom:0;content:"";height:17px;left:10px;margin:auto;position:absolute;top:0;width:17px}.auto-selected:before{opacity:1}.auto-clear{align-items:center;background-color:transparent;border:none;bottom:0;cursor:pointer;display:flex;height:auto;justify-content:center;margin:auto;position:absolute;right:0;top:0;width:40px}.auto-clear:before{content:var(--close-button);height:24px;line-height:100%;width:24px}.auto-clear span{display:none}.auto-wrapper{background-color:#fff;border:1px solid #858585;border-top:none;display:none;overflow:hidden}.auto-wrapper ul>.loupe{padding-left:40px}.auto-wrapper.auto-is-active{display:block;margin-top:-1px;position:absolute;width:100%;z-index:99999}.auto-selected{background-color:#e6e6e6}.auto-selected+li:before{border-top:none}.auto-error{border:1px solid #ff3838}.auto-error::placeholder{color:#f66;opacity:1}.hidden{display:none}

View File

@ -0,0 +1,6 @@
/*
* So not show the asterisk on required fields in crispy forms
*/
.asteriskField {
display: none;
}

View File

@ -6,7 +6,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<link href="{% static 'css/bootstrap.min.css' %}" rel="stylesheet"> <link href="{% static 'css/bootstrap.min.css' %}" rel="stylesheet">
<link href="{% static 'css/icons/bootstrap-icons.css' %}" rel="stylesheet"> <link href="{% static 'css/icons/bootstrap-icons.css' %}" rel="stylesheet">
<link href="{% static 'css/autocomplete.css' %}" rel="stylesheet"> <link href="{% static 'css/shimatta-kenkyusho-base.css' %}" rel="stylesheet">
<title>{{ base.title }}</title> <title>{{ base.title }}</title>
{% block customhead %} {% block customhead %}
{% endblock customhead %} {% endblock customhead %}

View File

@ -0,0 +1,16 @@
{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% block content %}
<div class="container justify-content-center text-center">
<h1>Change Password</h1>
<div class="row justify-content-center">
<div class="col-lg-5">
<form method="post">
{% csrf_token %}
{{form|crispy}}
<input type="submit" name="submit-change-pw" class="form-control btn-primary" value="Change Password">
</form>
</div>
</div>
</div>
{% endblock content %}