Gallery-Archivist/archivist/apps/authentication/views.py

69 lines
2.9 KiB
Python
Raw Normal View History

2023-10-07 21:27:49 +00:00
# Create your views here.
from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login
2023-11-10 09:13:04 +00:00
2023-12-06 19:39:32 +00:00
from apps.user.models import UserProfile # Importing the UserProfile model
2023-11-10 09:13:04 +00:00
2023-12-06 19:39:32 +00:00
from .forms import LoginForm, SignUpForm # Importing form classes for login and signup
2023-10-07 21:27:49 +00:00
2023-12-06 19:39:32 +00:00
# View function for user login
2023-10-07 21:27:49 +00:00
def login_view(request):
2023-12-06 19:39:32 +00:00
# Check if the user is already authenticated, if so, redirect them to the home page
if request.user.is_authenticated:
2023-12-06 19:39:32 +00:00
next_page = request.GET.get('next', '/') # Get the 'next' parameter from the URL, default to '/'
return redirect(next_page)
2023-12-06 19:39:32 +00:00
form = LoginForm(request.POST or None) # Create a login form instance
2023-10-07 21:27:49 +00:00
2023-12-06 19:39:32 +00:00
msg = None # Initialize a message variable
2023-10-07 21:27:49 +00:00
if request.method == "POST":
if form.is_valid():
username = form.cleaned_data.get("username")
password = form.cleaned_data.get("password")
2023-12-06 19:39:32 +00:00
# Authenticate user using the provided username and password
2023-10-07 21:27:49 +00:00
user = authenticate(username=username, password=password)
2023-12-06 19:39:32 +00:00
2023-10-07 21:27:49 +00:00
if user is not None:
2023-12-06 19:39:32 +00:00
login(request, user) # Log in the authenticated user
next_page = request.GET.get('next', '/') # Get the 'next' parameter from the URL
return redirect(next_page) # Redirect to the 'next' page after successful login
2023-10-07 21:27:49 +00:00
else:
2023-12-06 19:39:32 +00:00
msg = 'Invalid credentials' # Set error message for invalid credentials
2023-10-07 21:27:49 +00:00
else:
2023-12-06 19:39:32 +00:00
msg = 'Error validating the form' # Set error message for form validation error
2023-10-07 21:27:49 +00:00
return render(request, "accounts/login.html", {"form": form, "msg": msg})
2023-12-06 19:39:32 +00:00
# View function for user registration
2023-10-07 21:27:49 +00:00
def register_user(request):
2023-12-06 19:39:32 +00:00
msg = None # Initialize a message variable
success = False # Initialize a success flag
2023-10-07 21:27:49 +00:00
if request.method == "POST":
2023-12-06 19:39:32 +00:00
form = SignUpForm(request.POST) # Create a signup form instance
2023-10-07 21:27:49 +00:00
if form.is_valid():
2023-12-06 19:39:32 +00:00
form.save() # Save the user details from the form
2023-10-07 21:27:49 +00:00
username = form.cleaned_data.get("username")
raw_password = form.cleaned_data.get("password1")
2023-12-06 19:39:32 +00:00
# Authenticate the newly registered user
2023-10-07 21:27:49 +00:00
user = authenticate(username=username, password=raw_password)
2023-11-10 09:13:04 +00:00
2023-12-06 19:39:32 +00:00
# Create a UserProfile instance associated with the registered user
2023-11-10 09:13:04 +00:00
profile = UserProfile(user=user)
profile.save()
2023-10-07 21:27:49 +00:00
2023-12-06 19:39:32 +00:00
msg = 'User created - please <a href="/login">login</a>.' # Set success message with a login link
success = True # Set success flag to True
2023-10-07 21:27:49 +00:00
else:
2023-12-06 19:39:32 +00:00
msg = 'Form is not valid' # Set error message for invalid form data
2023-10-07 21:27:49 +00:00
else:
2023-12-06 19:39:32 +00:00
form = SignUpForm() # Create an empty signup form instance for GET requests
2023-10-07 21:27:49 +00:00
return render(request, "accounts/register.html", {"form": form, "msg": msg, "success": success})