You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: document_analysis/document_analysis/auth.py
+39-12Lines changed: 39 additions & 12 deletions
Original file line number
Diff line number
Diff line change
@@ -1,22 +1,32 @@
1
-
importfunctools
1
+
importfunctools# 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.
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'.
9
9
10
10
deflogin_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.
12
20
defwrapped_view(**kwargs):
13
-
ifg.userisNone:
21
+
ifg.userisNone:# 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.
14
22
returnredirect(url_for('auth.login'))
15
23
returnview(**kwargs)
16
24
returnwrapped_view
17
25
18
-
@bp.before_app_request
19
-
defload_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
+
defload_logged_in_user():
28
+
"""It's a function that loads the logged in user from the session.
29
+
"""
20
30
user_id=session.get('user_id')
21
31
22
32
ifuser_idisNone:
@@ -26,8 +36,13 @@ def load_logged_in_user():
26
36
'SELECT * FROM user WHERE id = ?', (user_id,)
27
37
).fetchone()
28
38
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.
30
40
defregister():
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
+
"""
31
46
ifrequest.method=='POST':
32
47
username=request.form['username']
33
48
password=request.form['password']
@@ -52,11 +67,17 @@ def register():
52
67
returnredirect(url_for('auth.login'))
53
68
54
69
flash(error)
55
-
70
+
71
+
# If the request method is GET or the data is invalid, the register page should be shown.
56
72
returnrender_template('auth/register.html')
57
73
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.
59
75
deflogin():
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
+
"""
60
81
ifrequest.method=='POST':
61
82
username=request.form['username']
62
83
password=request.form['password']
@@ -77,10 +98,16 @@ def login():
77
98
returnredirect(url_for('index'))
78
99
79
100
flash(error)
80
-
101
+
102
+
# If the request method is GET or the data is invalid, the login page should be shown.
81
103
returnrender_template('auth/login.html')
82
104
83
-
@bp.route('/logout')
105
+
@bp.route('/logout')# Register a new view function for the '/logout' URL.
0 commit comments