register: use ModelForm, and print all errors

This commit is contained in:
2023-10-31 21:14:24 +01:00
parent 54cc1b1705
commit a0c0d813b6
20 changed files with 203 additions and 258 deletions

View File

@ -1,36 +1,29 @@
from django.shortcuts import render
from django.views import View
from django.http import HttpResponse, HttpRequest
from django.http import JsonResponse, HttpResponse, HttpRequest
from django.contrib.auth.models import User
from django.db.models.query import QuerySet
from django.utils.decorators import method_decorator
from django.contrib.auth.decorators import login_required
from ..status_code import *
from ..settings import *
from ..forms.change_password import ChangePasswordForm
from ..status_code import *
class ChangePasswordView(View):
def get(self, request: HttpRequest):
return render(request, "change_password.html")
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 HttpResponse(INVALID_USERNAME_PASSWORD)
return JsonResponse(form.errors)
username: str = form.cleaned_data['username']
current_password: str = form.cleaned_data['current_password']
new_password: str = form.cleaned_data['new_password']
query: QuerySet = User.objects.filter(username=username)
if (not query.exists()):
return HttpResponse(INVALID_USERNAME_PASSWORD)
user: User = User.objects.get(username=username)
if (not user.check_password(current_password)):
return HttpResponse(INVALID_USERNAME_PASSWORD)
user: User = request.user
user.set_password(new_password)
user.save()
return HttpResponse(PASSWORD_UPDATED)
return HttpResponse(USER_PASSWORD_UPDATED)