diff --git a/accounts/urls.py b/accounts/urls.py index ce3cf81..02435c4 100644 --- a/accounts/urls.py +++ b/accounts/urls.py @@ -13,4 +13,5 @@ urlpatterns = [ path('background/upload/', views.upload_background, name='upload_background'), path('background/delete/', views.delete_background, name='delete_background'), path('focus-station/', views.save_focus_station, name='save_focus_station'), + path('change-password/', views.change_password, name='change_password'), ] diff --git a/accounts/views.py b/accounts/views.py index f257d6a..03d59c1 100644 --- a/accounts/views.py +++ b/accounts/views.py @@ -1,9 +1,9 @@ import base64 import json from django.conf import settings -from django.contrib.auth import authenticate, login, get_user_model +from django.contrib.auth import authenticate, login, get_user_model, update_session_auth_hash from django.contrib.auth.decorators import login_required -from django.contrib.auth.forms import UserCreationForm, AuthenticationForm +from django.contrib.auth.forms import UserCreationForm, AuthenticationForm, PasswordChangeForm from django.http import JsonResponse from django.shortcuts import render, redirect from django.views.decorators.csrf import csrf_exempt @@ -71,6 +71,7 @@ def settings_view(request): context = { 'profile': profile, 'has_lastfm': profile.has_lastfm(), + 'password_form': PasswordChangeForm(request.user), } return render(request, 'accounts/settings.html', context) @@ -181,6 +182,23 @@ def save_focus_station(request): return JsonResponse({'ok': True}) +@login_required +@require_http_methods(['POST']) +def change_password(request): + form = PasswordChangeForm(request.user, request.POST) + if form.is_valid(): + user = form.save() + update_session_auth_hash(request, user) + return redirect('settings') + profile = request.user.profile + return render(request, 'accounts/settings.html', { + 'profile': profile, + 'has_lastfm': profile.has_lastfm(), + 'password_form': form, + 'password_form_open': True, + }) + + @login_required @require_http_methods(['POST']) def lastfm_disconnect(request): diff --git a/templates/accounts/settings.html b/templates/accounts/settings.html index 3fe8571..ec72c1f 100644 --- a/templates/accounts/settings.html +++ b/templates/accounts/settings.html @@ -72,7 +72,28 @@

Account

Logged in as {{ request.user.username }}

-
+ +
+ Change password + + {% csrf_token %} + {% for field in password_form %} +
+ + {{ field }} + {% if field.errors %} +
{{ field.errors|join:", " }}
+ {% endif %} +
+ {% endfor %} + {% if password_form.non_field_errors %} +
{{ password_form.non_field_errors|join:", " }}
+ {% endif %} +
+ +
+ +
{% csrf_token %}