From c605e7e66ee10d13073fc632ed6e468a8b867ce2 Mon Sep 17 00:00:00 2001 From: Aksiznarf-Uar Date: Thu, 10 Oct 2024 12:41:47 +0200 Subject: [PATCH 1/3] Password change works now --- src/nac/urls.py | 8 ++++++-- src/nac/views.py | 28 ++++++++++++++++++++++++++-- src/templates/account_settings.html | 7 +++++++ src/templates/base.html | 7 ++++++- src/templates/change_password.html | 12 ++++++++++++ 5 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 src/templates/account_settings.html create mode 100644 src/templates/change_password.html diff --git a/src/nac/urls.py b/src/nac/urls.py index c0ab455..2022073 100644 --- a/src/nac/urls.py +++ b/src/nac/urls.py @@ -8,7 +8,9 @@ DeviceCreateView, DeviceRoleProdAutocomplete, DeviceRoleInstAutocomplete, - AuthorizationGroupAutocomplete + AuthorizationGroupAutocomplete, + AccountSettings, + change_password, ) urlpatterns = [ @@ -20,5 +22,7 @@ path("devices/new/", DeviceCreateView.as_view(), name="device_new"), path("DeviceRoleProd-autocomplete/", DeviceRoleProdAutocomplete.as_view(), name="DeviceRoleProd-autocomplete"), path("DeviceRoleInst-autocomplete/", DeviceRoleInstAutocomplete.as_view(), name="DeviceRoleInst-autocomplete"), - path("authorization-group-autocomplete/", AuthorizationGroupAutocomplete.as_view(), name="authorization-group-autocomplete") + path("authorization-group-autocomplete/", AuthorizationGroupAutocomplete.as_view(), name="authorization-group-autocomplete"), + path("account-settings/", AccountSettings.as_view(), name="account-settings"), + path("change_password/", change_password, name='change_password'), ] diff --git a/src/nac/views.py b/src/nac/views.py index af9119f..9a49fce 100644 --- a/src/nac/views.py +++ b/src/nac/views.py @@ -1,11 +1,14 @@ -from django.views.generic import TemplateView, ListView, DetailView +from django.views.generic import TemplateView, ListView, DetailView, View from django.views.generic.edit import UpdateView, DeleteView, CreateView from django.db.models import Q from django.urls import reverse_lazy from dal import autocomplete from .models import Device, DeviceRoleProd, AuthorizationGroup, DeviceRoleInst from .forms import DeviceForm - +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 class HomePageView(TemplateView): template_name = "home.html" @@ -104,3 +107,24 @@ def get_queryset(self): qs = qs.filter(name__istartswith=self.q) return qs + + +class AccountSettings(TemplateView): + template_name = "account_settings.html" + + +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, 'Your password was successfully updated!') + return redirect('change_password') + else: + messages.error(request, 'Please correct the error below.') + else: + form = PasswordChangeForm(request.user) + return render(request, 'change_password.html', { + 'form': form + }) diff --git a/src/templates/account_settings.html b/src/templates/account_settings.html new file mode 100644 index 0000000..801a429 --- /dev/null +++ b/src/templates/account_settings.html @@ -0,0 +1,7 @@ +{% extends "base.html" %} +{% block content %} +
+

Account Settings

+ Change password +
+{% endblock content %} \ No newline at end of file diff --git a/src/templates/base.html b/src/templates/base.html index e68e35f..d244299 100644 --- a/src/templates/base.html +++ b/src/templates/base.html @@ -65,7 +65,12 @@ Add Device - + +