1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2024-12-25 15:45:23 +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" %} {% extends "base.html" %}
{% load i18n %} {% load i18n %}
{% load icons %}
{% load tags_fingerprint %} {% load tags_fingerprint %}
{% block title %}{% trans "Profile" %}{% endblock %} {% block title %}{% trans "Profile" %}{% endblock %}
{% block content %} {% block content %}
@ -16,6 +17,9 @@
<div class="row"> <div class="row">
<div class="col-lg-12"> <div class="col-lg-12">
<h3 class="page-header">{% trans "Edit Profile" %}</h3> <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 %} <form method="post" action="" role="form" aria-label="Edit user info form">{% csrf_token %}
<div class="form-group"> <div class="form-group">
<label class="col-sm-2 col-form-label">{% trans "Login" %}</label> <label class="col-sm-2 col-form-label">{% trans "Login" %}</label>
@ -41,34 +45,6 @@
</div> </div>
</div> </div>
</form> </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> <h3 class="page-header">{% trans "SSH Keys" %}</h3>
{% if publickeys %} {% if publickeys %}
<div class="col-lg-12"> <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.contrib.auth import views as auth_views
from django.urls import path
from . import views from . import views
urlpatterns = [ urlpatterns = [
@ -7,4 +8,5 @@ urlpatterns = [
path('logout/', auth_views.LogoutView.as_view(template_name='logout.html'), name='logout'), path('logout/', auth_views.LogoutView.as_view(template_name='logout.html'), name='logout'),
path('profile/', views.profile, name='profile'), path('profile/', views.profile, name='profile'),
path('profile/<int:user_id>/', views.account, name='account'), 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 os
import sass 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.core.validators import ValidationError
from django.http import HttpResponseRedirect from django.http import HttpResponseRedirect
from django.shortcuts import render from django.shortcuts import redirect, render
from django.urls import reverse from django.urls import reverse
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
@ -118,3 +122,19 @@ def account(request, user_id):
return HttpResponseRedirect(request.get_full_path()) return HttpResponseRedirect(request.get_full_path())
return render(request, 'account.html', locals()) 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})

View file

@ -11,7 +11,6 @@
<h2 class="page-header">{% trans "Create Compute" %}</h2> <h2 class="page-header">{% trans "Create Compute" %}</h2>
</div> </div>
</div> </div>
{% bootstrap_messages %}
<div class="row"> <div class="row">
<div class="thumbnail col-sm-10 offset-1"> <div class="thumbnail col-sm-10 offset-1">
<form id="create-update" action="" method="post"> <form id="create-update" action="" method="post">

View file

@ -1,4 +1,5 @@
{% load static %} {% load static %}
{% load bootstrap4 %}
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
@ -36,7 +37,7 @@
{% include 'navbar.html' %} {% include 'navbar.html' %}
<div role="main" class="container"> <div role="main" class="container">
{% bootstrap_messages %}
{% block content %}{% endblock %} {% block content %}{% endblock %}
</div> <!-- /container --> </div> <!-- /container -->

View file

@ -42,7 +42,7 @@
<a class="dropdown-item disabled" href="#"> <a class="dropdown-item disabled" href="#">
{% trans "Language" %}: <span class="badge badge-secondary">{{ LANGUAGE_CODE }}</span> {% trans "Language" %}: <span class="badge badge-secondary">{{ LANGUAGE_CODE }}</span>
</a> </a>
<a class="dropdown-item {% view_active request 'profile' %}" href="{% url 'profile' %}"><i class="fa fa-fw fa-pencil-square-o"></i> {% trans "Profile" %}</a> <a class="dropdown-item {% view_active request 'profile' %}" href="{% url 'profile' %}">{% icon 'vcard' %} {% trans "Profile" %}</a>
<div class="dropdown-divider"></div> <div class="dropdown-divider"></div>
<a class="dropdown-item" href="{% url 'logout' %}"><i class="fa fa-fw fa-power-off"></i> {% trans "Log Out" %}</a> <a class="dropdown-item" href="{% url 'logout' %}"><i class="fa fa-fw fa-power-off"></i> {% trans "Log Out" %}</a>
</div> </div>