ft_transcendence/django/trancendence/accounts/views/delete.py

35 lines
1.1 KiB
Python

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.delete import DeleteForm
class DeleteView(View):
def get(self, request: HttpRequest):
return render(request, "delete.html", {"form": DeleteForm})
def post(self, request: HttpRequest):
form: DeleteForm = DeleteForm(request.POST)
if (not form.is_valid()):
return HttpResponse(INVALID_USERNAME_PASSWORD)
username: str = form.cleaned_data['username']
password: str = form.cleaned_data['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(password)):
return HttpResponse(INVALID_USERNAME_PASSWORD)
user.delete()
return HttpResponse(USER_DELETED)