1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2025-07-31 12:41:08 +00:00

Moved password changing to a separate view

This commit is contained in:
Real-Gecko 2020-06-16 14:36:53 +06:00
parent 2951d0b035
commit b85e246aed
7 changed files with 60 additions and 33 deletions

View file

@ -0,0 +1,29 @@
{% extends "base.html" %}
{% load bootstrap4 %}
{% load i18n %}
{% load icons %}
{% block title %}{%trans "Change Password" %}{% endblock title %}
{% block content %}
<div class="card">
<div class="card-header">
<h4 class="card-title">{%trans "Change Password" %}: {{ user }}</h4>
</div>
<div class="card-body">
<form method="post" id="password-change">
{% csrf_token %}
{% bootstrap_form form layout='horizontal' %}
</form>
</div>
<div class="card-footer">
<div class="float-right">
<a class="btn btn-primary" href="javascript:history.back()">{% icon 'times' %} {% trans "Cancel" %}</a>
<button type="submit" form="password-change" class="btn btn-success">
{% icon 'check' %} {% trans "Change" %}
</button>
</div>
</div>
</div>
{% endblock content %}

View file

@ -1,5 +1,6 @@
{% extends "base.html" %}
{% load i18n %}
{% load icons %}
{% load tags_fingerprint %}
{% block title %}{% trans "Profile" %}{% endblock %}
{% block content %}
@ -16,6 +17,9 @@
<div class="row">
<div class="col-lg-12">
<h3 class="page-header">{% trans "Edit Profile" %}</h3>
{% if perms.accounts.change_password %}
<a href="{% url 'change_password' %}" class="btn btn-primary">{% icon 'lock' %} {% trans "Change Password" %}</a>
{% endif %}
<form method="post" action="" role="form" aria-label="Edit user info form">{% csrf_token %}
<div class="form-group">
<label class="col-sm-2 col-form-label">{% trans "Login" %}</label>
@ -41,34 +45,6 @@
</div>
</div>
</form>
{% if perms.accounts.change_password %}
<h3 class="page-header">{% trans "Edit Password" %}</h3>
<form method="post" action="" role="form" aria-label="Edit user password form">{% csrf_token %}
<div class="form-group">
<label class="col-sm-2 col-form-label">{% trans "Old" %}</label>
<div class="col-sm-4">
<input type="password" class="form-control" name="oldpasswd" value="">
</div>
</div>
<div class="form-group bridge_name_form_group_dhcp">
<label class="col-sm-2 col-form-label">{% trans "New" %}</label>
<div class="col-sm-4">
<input type="password" class="form-control" name="passwd1" value="">
</div>
</div>
<div class="form-group bridge_name_form_group_dhcp">
<label class="col-sm-2 col-form-label">{% trans "Retry" %}</label>
<div class="col-sm-4">
<input type="password" class="form-control" name="passwd2" value="">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary">{% trans "Change" %}</button>
</div>
</div>
</form>
{% endif %}
<h3 class="page-header">{% trans "SSH Keys" %}</h3>
{% if publickeys %}
<div class="col-lg-12">

View file

@ -1,5 +1,6 @@
from django.urls import path
from django.contrib.auth import views as auth_views
from django.urls import path
from . import views
urlpatterns = [
@ -7,4 +8,5 @@ urlpatterns = [
path('logout/', auth_views.LogoutView.as_view(template_name='logout.html'), name='logout'),
path('profile/', views.profile, name='profile'),
path('profile/<int:user_id>/', views.account, name='account'),
path('change_password/', views.change_password, name='change_password'),
]

View file

@ -1,9 +1,13 @@
import os
import sass
from django.contrib import messages
from django.contrib.auth import update_session_auth_hash
from django.contrib.auth.decorators import permission_required
from django.contrib.auth.forms import PasswordChangeForm
from django.core.validators import ValidationError
from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.shortcuts import redirect, render
from django.urls import reverse
from django.utils.translation import ugettext_lazy as _
@ -118,3 +122,19 @@ def account(request, user_id):
return HttpResponseRedirect(request.get_full_path())
return render(request, 'account.html', locals())
@permission_required('accounts.change_password', raise_exception=True)
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, _('Password Changed'))
return redirect('profile')
else:
messages.error(request, _('Wrong Data Provided'))
else:
form = PasswordChangeForm(request.user)
return render(request, 'accounts/change_password_form.html', {'form': form})