from django.shortcuts import render from django.views import View from django.http import HttpResponse, HttpRequest from django.contrib.auth.models import User from django.db.models.query import QuerySet from ..status_code import * from ..settings import * from ..forms.change_password import ChangePasswordForm class ChangePasswordView(View): def get(self, request: HttpRequest): return render(request, "change_password.html") def post(self, request: HttpRequest): form: ChangePasswordForm = ChangePasswordForm(request.POST) if not form.is_valid(): return HttpResponse(INVALID_USERNAME_PASSWORD) 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.set_password(new_password) user.save() return HttpResponse(PASSWORD_UPDATED)