2023-10-25 09:35:24 -04:00
|
|
|
from django.shortcuts import render
|
|
|
|
from django.views import View
|
2023-10-25 10:10:23 -04:00
|
|
|
from django.http import HttpResponse, HttpRequest
|
2023-10-25 09:35:24 -04:00
|
|
|
from django.contrib.auth.models import User
|
|
|
|
from django.db.models.query import QuerySet
|
|
|
|
|
|
|
|
from ..status_code import *
|
|
|
|
from ..settings import *
|
2023-10-25 10:10:23 -04:00
|
|
|
from ..forms.change_password import ChangePasswordForm
|
2023-10-25 09:35:24 -04:00
|
|
|
|
2023-10-25 10:10:23 -04:00
|
|
|
class ChangePasswordView(View):
|
|
|
|
def get(self, request: HttpRequest):
|
2023-10-25 09:35:24 -04:00
|
|
|
return render(request, "change_password.html")
|
|
|
|
|
2023-10-25 10:10:23 -04:00
|
|
|
def post(self, request: HttpRequest):
|
|
|
|
|
|
|
|
form: ChangePasswordForm = ChangePasswordForm(request.POST)
|
|
|
|
if not form.is_valid():
|
2023-10-25 09:35:24 -04:00
|
|
|
return HttpResponse(INVALID_USERNAME_PASSWORD)
|
|
|
|
|
2023-10-25 10:10:23 -04:00
|
|
|
username: str = form.cleaned_data['username']
|
|
|
|
current_password: str = form.cleaned_data['current_password']
|
|
|
|
new_password: str = form.cleaned_data['new_password']
|
2023-10-25 09:35:24 -04:00
|
|
|
|
|
|
|
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.set_password(new_password)
|
|
|
|
user.save()
|
|
|
|
|
|
|
|
return HttpResponse(PASSWORD_UPDATED)
|