From ca5fc3e43184b8c3e3a1c4957fa611617255c2ca Mon Sep 17 00:00:00 2001 From: Soline Date: Thu, 19 Oct 2023 17:00:49 -0400 Subject: [PATCH 1/3] Rely on is_authenticated attribute of UserMixin and AnonymousUserMixin to handle the login information --- clockwork_web/browser_routes/settings.py | 108 +++-------- clockwork_web/login_routes.py | 4 +- clockwork_web/server_app.py | 4 +- clockwork_web/templates/base.html | 4 +- clockwork_web/user.py | 12 -- clockwork_web_test/test_browser_settings.py | 190 ++++++++++---------- 6 files changed, 129 insertions(+), 193 deletions(-) diff --git a/clockwork_web/browser_routes/settings.py b/clockwork_web/browser_routes/settings.py index 7b07883e..c47dede3 100644 --- a/clockwork_web/browser_routes/settings.py +++ b/clockwork_web/browser_routes/settings.py @@ -120,24 +120,13 @@ def route_set_nbr_items_per_page(): # Check if nbr_items_per_page is a positive integer if type(nbr_items_per_page) == int and nbr_items_per_page > 0: - if current_user.is_authenticated(): - # If it is, update this number in the current user's settings and - # retrieve the status code and status message associated to this - # operation - ( - status_code, - status_message, - ) = current_user.settings_nbr_items_per_page_set(nbr_items_per_page) - else: - # Otherwise, return an error - return ( - render_template_with_user_settings( - "error.html", - error_msg=gettext("The user is not authenticated."), - previous_request_args=previous_request_args, - ), - 403, # Forbidden - ) + # If it is, update this number in the current user's settings and + # retrieve the status code and status message associated to this + # operation + ( + status_code, + status_message, + ) = current_user.settings_nbr_items_per_page_set(nbr_items_per_page) if status_code == 200: # If a success has been return, redirect to the settings page @@ -195,18 +184,7 @@ def route_set_dark_mode(): # Set the dark mode value to True in the current user's web settings and # retrieve the status code and status message associated to the operation - if current_user.is_authenticated(): - (status_code, status_message) = current_user.settings_dark_mode_enable() - else: - # Otherwise, return an error - return ( - render_template_with_user_settings( - "error.html", - error_msg=gettext("The user is not authenticated."), - previous_request_args=previous_request_args, - ), - 403, # Forbidden - ) + (status_code, status_message) = current_user.settings_dark_mode_enable() if status_code == 200: # If a success has been returned @@ -237,20 +215,10 @@ def route_unset_dark_mode(): # Initialize the request arguments (it is further transferred to the HTML) previous_request_args = {} - if current_user.is_authenticated(): - # Set the dark mode value to False in the current user's web settings and - # retrieve the status code and status message associated to the operation - (status_code, status_message) = current_user.settings_dark_mode_disable() - else: - # Otherwise, return an error - return ( - render_template_with_user_settings( - "error.html", - error_msg=gettext("The user is not authenticated."), - previous_request_args=previous_request_args, - ), - 403, # Forbidden - ) + + # Set the dark mode value to False in the current user's web settings and + # retrieve the status code and status message associated to the operation + (status_code, status_message) = current_user.settings_dark_mode_disable() if status_code == 200: # If a success has been returned @@ -496,25 +464,13 @@ def route_set_date_format(): if date_format: # Check if the date format is supported if date_format in get_available_date_formats(): - if current_user.is_authenticated(): - # If the requested date format is expected, update the preferred - # date format of the current user and retrieve the status code - # and status message associated to this operation - ( - status_code, - status_message, - ) = current_user.settings_date_format_set(date_format) - - else: - # Otherwise, return an error - return ( - render_template_with_user_settings( - "error.html", - error_msg=gettext("The user is not authenticated."), - previous_request_args=previous_request_args, - ), - 403, # Forbidden - ) + # If the requested date format is expected, update the preferred + # date format of the current user and retrieve the status code + # and status message associated to this operation + ( + status_code, + status_message, + ) = current_user.settings_date_format_set(date_format) if status_code == 200: # If a success has been return, redirect to the home page @@ -578,24 +534,14 @@ def route_set_time_format(): if time_format: # Check if the date format is supported if time_format in get_available_time_formats(): - if current_user.is_authenticated(): - # If the requested time format is expected, update the preferred - # time format of the current user and retrieve the status code - # and status message associated to this operation - ( - status_code, - status_message, - ) = current_user.settings_time_format_set(time_format) - else: - # Otherwise, return an error - return ( - render_template_with_user_settings( - "error.html", - error_msg=gettext("The user is not authenticated."), - previous_request_args=previous_request_args, - ), - 403, # Forbidden - ) + + # If the requested time format is expected, update the preferred + # time format of the current user and retrieve the status code + # and status message associated to this operation + ( + status_code, + status_message, + ) = current_user.settings_time_format_set(time_format) if status_code == 200: # If a success has been return, redirect to the home page diff --git a/clockwork_web/login_routes.py b/clockwork_web/login_routes.py index 2244d4c7..6772e7b6 100644 --- a/clockwork_web/login_routes.py +++ b/clockwork_web/login_routes.py @@ -206,9 +206,9 @@ def route_callback(): from flask import current_app current_app.logger.debug( - "called login_user(user) for user with email %s, user.is_authenticated() is %s", + "called login_user(user) for user with email %s, user.is_authenticated is %s", user.mila_email_username, - user.is_authenticated(), + user.is_authenticated, ) # Send user back to homepage return redirect(url_for("index")) diff --git a/clockwork_web/server_app.py b/clockwork_web/server_app.py index eb997bd3..d8375f6d 100644 --- a/clockwork_web/server_app.py +++ b/clockwork_web/server_app.py @@ -151,7 +151,7 @@ def have_same_users(user1: str, user2: str): @babel.localeselector def get_locale(): # If the user is authenticated - if current_user and current_user.is_authenticated(): + if current_user and current_user.is_authenticated: return current_user.get_language() @@ -251,7 +251,7 @@ def index(): where people can click on the "login" button on the web interface. """ - if current_user.is_authenticated(): + if current_user.is_authenticated: app.logger.debug("in route for '/'; redirecting to jobs/") return redirect("jobs/") else: diff --git a/clockwork_web/templates/base.html b/clockwork_web/templates/base.html index 84ce5261..5a3917bd 100644 --- a/clockwork_web/templates/base.html +++ b/clockwork_web/templates/base.html @@ -70,7 +70,7 @@

{{ gettext("Alpha version!") }}

{% endif %} - {% if current_user.is_authenticated() %} + {% if current_user.is_authenticated %}