Skip to content

Commit 456b72c

Browse files
committed
Add docstrings to authentication module functions for improved clarity and maintainability
1 parent 8b256c0 commit 456b72c

File tree

1 file changed

+39
-12
lines changed
  • document_analysis/document_analysis

1 file changed

+39
-12
lines changed

document_analysis/document_analysis/auth.py

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,32 @@
1-
import functools
1+
import functools # The functools module in Python deals with higher-order functions, simple terms they are functions which take other functions as arguments. The primary object that the functools module deals with is the decorator, which is a function that wraps another function or method. The primary purpose of decorators is to modify or extend the behavior of functions or methods. The functools module provides several functions that can be used to create decorators. The module also provides a few other higher-order functions, which are used to manipulate or create other functions.
22
from flask import (
33
Blueprint, flash, g, redirect, render_template, request, session, url_for
44
)
5-
from werkzeug.security import check_password_hash, generate_password_hash
5+
from werkzeug.security import check_password_hash, generate_password_hash
66
from document_analysis.db import get_db
77

8-
bp = Blueprint('auth', __name__, url_prefix='/auth')
8+
bp = Blueprint('auth', __name__, url_prefix='/auth') # Create a Blueprint named 'auth' with the name of the current module (__name__) and the URL prefix '/auth'.
99

1010
def login_required(view):
11-
@functools.wraps(view)
11+
"""It's a decorator that will redirect to the login page if the user is not logged in.
12+
13+
Args:
14+
view (function): The view function to be decorated.
15+
16+
Returns:
17+
function: The wrapped view function that includes the login check.
18+
"""
19+
@functools.wraps(view) # The functools.wraps() function is a decorator that takes a function used in a decorator and adds the functionality of copying over the function name, docstring, arguments list, etc. This allows you to wrap a function in a decorator without losing the information of the original function.
1220
def wrapped_view(**kwargs):
13-
if g.user is None:
21+
if g.user is None: # g is a special object that is unique for each request. It is used to store data that might be accessed by multiple functions during the request. The connection is stored and reused instead of creating a new connection if get_db is called a second time in the same request.
1422
return redirect(url_for('auth.login'))
1523
return view(**kwargs)
1624
return wrapped_view
1725

18-
@bp.before_app_request
19-
def load_logged_in_user():
26+
@bp.before_app_request # Register a function that runs before the view function, no matter what URL is requested. This is used to load a user from the session.
27+
def load_logged_in_user():
28+
"""It's a function that loads the logged in user from the session.
29+
"""
2030
user_id = session.get('user_id')
2131

2232
if user_id is None:
@@ -26,8 +36,13 @@ def load_logged_in_user():
2636
'SELECT * FROM user WHERE id = ?', (user_id,)
2737
).fetchone()
2838

29-
@bp.route('/register', methods=('GET', 'POST'))
39+
@bp.route('/register', methods=('GET', 'POST')) # Register a new view function for the '/register' URL that accepts both GET and POST requests.
3040
def register():
41+
"""It's a view function that registers a new user.
42+
43+
Returns:
44+
str: It returns the rendered template of the register.html file.
45+
"""
3146
if request.method == 'POST':
3247
username = request.form['username']
3348
password = request.form['password']
@@ -52,11 +67,17 @@ def register():
5267
return redirect(url_for('auth.login'))
5368

5469
flash(error)
55-
70+
71+
# If the request method is GET or the data is invalid, the register page should be shown.
5672
return render_template('auth/register.html')
5773

58-
@bp.route('/login', methods=('GET', 'POST'))
74+
@bp.route('/login', methods=('GET', 'POST')) # Register a new view function for the '/login' URL that accepts both GET and POST requests.
5975
def login():
76+
"""It's a view function that logs in a user.
77+
78+
Returns:
79+
str: It returns the rendered template of the login.html file.
80+
"""
6081
if request.method == 'POST':
6182
username = request.form['username']
6283
password = request.form['password']
@@ -77,10 +98,16 @@ def login():
7798
return redirect(url_for('index'))
7899

79100
flash(error)
80-
101+
102+
# If the request method is GET or the data is invalid, the login page should be shown.
81103
return render_template('auth/login.html')
82104

83-
@bp.route('/logout')
105+
@bp.route('/logout') # Register a new view function for the '/logout' URL.
84106
def logout():
107+
"""It's a view function that logs out a user.
108+
109+
Returns:
110+
str: It redirects the user to the index page.
111+
"""
85112
session.clear()
86113
return redirect(url_for('index'))

0 commit comments

Comments
 (0)