From e2e499495414248917de79ea807cdfba9f92a8a0 Mon Sep 17 00:00:00 2001 From: Aroy-Art Date: Wed, 6 Dec 2023 20:39:32 +0100 Subject: [PATCH] Add: code comments --- archivist/apps/authentication/views.py | 56 +++++++++++++------------- 1 file changed, 29 insertions(+), 27 deletions(-) diff --git a/archivist/apps/authentication/views.py b/archivist/apps/authentication/views.py index f5ab108..797927c 100644 --- a/archivist/apps/authentication/views.py +++ b/archivist/apps/authentication/views.py @@ -2,65 +2,67 @@ from django.shortcuts import render, redirect from django.contrib.auth import authenticate, login -#from django.contrib.auth.models import User -from apps.user.models import UserProfile +from apps.user.models import UserProfile # Importing the UserProfile model -from .forms import LoginForm, SignUpForm +from .forms import LoginForm, SignUpForm # Importing form classes for login and signup +# View function for user login def login_view(request): + # Check if the user is already authenticated, if so, redirect them to the home page if request.user.is_authenticated: - # User is already logged in, redirect them to the home page - next_page = request.GET.get('next', '/') + next_page = request.GET.get('next', '/') # Get the 'next' parameter from the URL, default to '/' return redirect(next_page) - form = LoginForm(request.POST or None) + + form = LoginForm(request.POST or None) # Create a login form instance - msg = None + msg = None # Initialize a message variable if request.method == "POST": - if form.is_valid(): username = form.cleaned_data.get("username") password = form.cleaned_data.get("password") + + # Authenticate user using the provided username and password user = authenticate(username=username, password=password) + if user is not None: - login(request, user) - next_page = request.GET.get('next', '/') - return redirect(next_page) + 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 else: - msg = 'Invalid credentials' + msg = 'Invalid credentials' # Set error message for invalid credentials else: - msg = 'Error validating the form' + msg = 'Error validating the form' # Set error message for form validation error return render(request, "accounts/login.html", {"form": form, "msg": msg}) +# View function for user registration def register_user(request): - msg = None - success = False + msg = None # Initialize a message variable + success = False # Initialize a success flag if request.method == "POST": - form = SignUpForm(request.POST) + form = SignUpForm(request.POST) # Create a signup form instance + if form.is_valid(): - form.save() + form.save() # Save the user details from the form username = form.cleaned_data.get("username") raw_password = form.cleaned_data.get("password1") + + # Authenticate the newly registered user user = authenticate(username=username, password=raw_password) - #user = User.objects.create_user(username=username, password=raw_password) - - # Create a UserProfile instance for the user + # Create a UserProfile instance associated with the registered user profile = UserProfile(user=user) profile.save() - msg = 'User created - please login.' - success = True - - # return redirect("/login/") - + msg = 'User created - please login.' # Set success message with a login link + success = True # Set success flag to True else: - msg = 'Form is not valid' + msg = 'Form is not valid' # Set error message for invalid form data else: - form = SignUpForm() + form = SignUpForm() # Create an empty signup form instance for GET requests return render(request, "accounts/register.html", {"form": form, "msg": msg, "success": success})