2024-11-24 07:45:02 +00:00
|
|
|
from django.contrib import messages
|
|
|
|
from django.shortcuts import redirect, render
|
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
|
|
|
|
from .models import User
|
2024-11-23 12:28:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
def index(request):
|
2024-11-24 07:45:02 +00:00
|
|
|
# return HttpResponse("Hello, world. You're at the polls index.")
|
|
|
|
return render(request, 'index.html')
|
|
|
|
|
|
|
|
|
|
|
|
def register(request: HttpRequest):
|
|
|
|
|
|
|
|
# if the form is not submitted yet, return the form
|
|
|
|
if request.method != 'POST':
|
|
|
|
return render(request, 'register.html', {'errors': False})
|
|
|
|
|
|
|
|
email = request.POST.get('email')
|
|
|
|
password = request.POST.get('password')
|
|
|
|
confirm_password = request.POST.get('confirm_password')
|
|
|
|
|
|
|
|
errors = False
|
|
|
|
|
|
|
|
if not email:
|
|
|
|
messages.error(request, 'Email is required')
|
|
|
|
errors = True
|
|
|
|
|
|
|
|
if not password or len(password) < 8:
|
|
|
|
messages.error(request, 'Password must be at least 8 characters long')
|
|
|
|
errors = True
|
|
|
|
|
|
|
|
if password != confirm_password:
|
|
|
|
messages.error(request, 'Passwords do not match')
|
|
|
|
errors = True
|
|
|
|
|
|
|
|
if not errors:
|
|
|
|
|
|
|
|
if User.objects.filter(email=email).exists():
|
|
|
|
messages.error(request, 'Email already exists')
|
|
|
|
else:
|
|
|
|
user = User.objects.create_user(
|
|
|
|
username=username, email=email, password=password) # type: ignore
|
|
|
|
user.save()
|
|
|
|
messages.success(request, 'Account created successfully')
|
|
|
|
return redirect('login')
|
|
|
|
|
|
|
|
return render(request, 'register.html')
|