ft_transcendence/accounts/views/change_password.py

29 lines
1009 B
Python

from django.shortcuts import render
from django.views import View
from django.http import JsonResponse, HttpResponse, HttpRequest
from django.contrib.auth.models import User
from django.utils.decorators import method_decorator
from django.contrib.auth.decorators import login_required
from ..forms.change_password import ChangePasswordForm
from ..status_code import *
class ChangePasswordView(View):
def get(self, request: HttpRequest):
return render(request, "change_password.html", ChangePasswordForm)
@method_decorator(login_required, name='dispatch')
def post(self, request: HttpRequest):
form: ChangePasswordForm = ChangePasswordForm(request.POST)
if not form.is_valid():
return JsonResponse(form.errors)
new_password: str = form.cleaned_data['new_password']
user: User = request.user
user.set_password(new_password)
user.save()
return HttpResponse(USER_PASSWORD_UPDATED)