🛡️ Warracker is an open source, self-hostable warranty tracker to monitor expirations, store receipts, files. You own the data, your rules!
Update deprecated utcnow() usage
TECee96ba53115e
diff --git a/backend/auth_routes.py b/backend/auth_routes.pyindex 6d54b73..b5d2916 100644--- a/backend/auth_routes.py+++ b/backend/auth_routes.py@@ -1,7 +1,7 @@# backend/auth_routes.py# Updated: 2025-01-24 - Fixed API endpoints for notificationsfrom flask import Blueprint, request, jsonify, current_app-from datetime import datetime, timedelta+from datetime import datetime, UTC, timedeltaimport uuidimport smtplibfrom email.mime.multipart import MIMEMultipart@@ -117,13 +117,13 @@ def register():token = generate_token(user_id)# Update last login- cur.execute('UPDATE users SET last_login = %s WHERE id = %s', (datetime.utcnow(), user_id))+ cur.execute('UPDATE users SET last_login = %s WHERE id = %s', (datetime.now(UTC), user_id))# Store session infoip_address = request.remote_addruser_agent = request.headers.get('User-Agent', '')session_token = str(uuid.uuid4())- expires_at = datetime.utcnow() + current_app.config['JWT_EXPIRATION_DELTA']+ expires_at = datetime.now(UTC) + current_app.config['JWT_EXPIRATION_DELTA']cur.execute('INSERT INTO user_sessions (user_id, session_token, expires_at, ip_address, user_agent, login_method) VALUES (%s, %s, %s, %s, %s, %s)',@@ -182,13 +182,13 @@ def login():token = generate_token(user_id)# Update last login- cur.execute('UPDATE users SET last_login = %s WHERE id = %s', (datetime.utcnow(), user_id))+ cur.execute('UPDATE users SET last_login = %s WHERE id = %s', (datetime.now(UTC), user_id))# Store session infoip_address = request.remote_addruser_agent = request.headers.get('User-Agent', '')session_token = str(uuid.uuid4())- expires_at = datetime.utcnow() + current_app.config['JWT_EXPIRATION_DELTA']+ expires_at = datetime.now(UTC) + current_app.config['JWT_EXPIRATION_DELTA']cur.execute('INSERT INTO user_sessions (user_id, session_token, expires_at, ip_address, user_agent, login_method) VALUES (%s, %s, %s, %s, %s, %s)',@@ -347,7 +347,7 @@ def request_password_reset():# Generate reset tokenreset_token = str(uuid.uuid4())- expires_at = datetime.utcnow() + timedelta(hours=24)+ expires_at = datetime.now(UTC) + timedelta(hours=24)# Delete any existing tokens for this usercur.execute('DELETE FROM password_reset_tokens WHERE user_id = %s', (user_id,))@@ -656,7 +656,7 @@ def reset_password():cur.execute('SELECT user_id, expires_at FROM password_reset_tokens WHERE token = %s', (token,))token_info = cur.fetchone()- if not token_info or token_info[1] < datetime.utcnow():+ if not token_info or token_info[1] < datetime.now(UTC):return jsonify({'message': 'Invalid or expired token!'}), 400user_id = token_info[0]diff --git a/backend/auth_utils.py b/backend/auth_utils.pyindex 421bc4c..af9be13 100644--- a/backend/auth_utils.py+++ b/backend/auth_utils.py@@ -1,6 +1,6 @@# backend/auth_utils.pyimport jwt-from datetime import datetime, timedelta+from datetime import datetime, UTC, timedeltafrom flask import current_app, request, jsonifyfrom functools import wrapsimport re@@ -14,8 +14,8 @@ except ImportError:def generate_token(user_id):"""Generate a JWT token for the user"""payload = {- 'exp': datetime.utcnow() + current_app.config['JWT_EXPIRATION_DELTA'],- 'iat': datetime.utcnow(),+ 'exp': datetime.now(UTC) + current_app.config['JWT_EXPIRATION_DELTA'],+ 'iat': datetime.now(UTC),'sub': str(user_id)}return jwt.encode(payload, current_app.config['SECRET_KEY'], algorithm='HS256')diff --git a/backend/notifications.py b/backend/notifications.pyindex 5a72648..e45eef6 100644--- a/backend/notifications.py+++ b/backend/notifications.py@@ -12,7 +12,7 @@ import timeimport atexitimport smtplibimport logging-from datetime import datetime, date, timedelta+from datetime import datetime, date, UTC, timedeltafrom email.mime.multipart import MIMEMultipartfrom email.mime.text import MIMETextfrom threading import Lock@@ -353,7 +353,7 @@ def process_email_notifications(all_warranties, eligible_user_ids, is_manual, geserver.login(smtp_username, smtp_password)emails_sent = 0- utc_now = datetime.utcnow()+ utc_now = datetime.now(UTC)timestamp = int(utc_now.timestamp())for email, user_data in users_warranties.items():@@ -535,7 +535,7 @@ def send_expiration_notifications(manual_trigger=False, get_db_connection=None,if not manual_trigger:with conn.cursor() as cur:- utc_now = datetime.utcnow()+ utc_now = datetime.now(UTC)# Check if required columns exist for dynamic query buildingcur.execute("""diff --git a/backend/oidc_handler.py b/backend/oidc_handler.pyindex 26f6c24..6dc9efd 100644--- a/backend/oidc_handler.py+++ b/backend/oidc_handler.py@@ -1,7 +1,7 @@# backend/oidc_handler.pyimport osimport uuid-from datetime import datetime # Ensure timedelta is imported if used, though not in this snippet+from datetime import datetime, UTC # Ensure timedelta is imported if used, though not in this snippetfrom flask import Blueprint, jsonify, redirect, url_for, current_app, request, session# Import shared extensions and utilities@@ -193,14 +193,14 @@ def oidc_callback_route():app_session_token = generate_token(user_id) # Generate app-specific JWT# Update last login timestamp- cur.execute('UPDATE users SET last_login = %s WHERE id = %s', (datetime.utcnow(), user_id))+ cur.execute('UPDATE users SET last_login = %s WHERE id = %s', (datetime.now(UTC), user_id))# Log OIDC session in user_sessions tableip_address = request.remote_addruser_agent = request.headers.get('User-Agent', '')# Use a different UUID for session_token in DB if needed, or re-use app_session_token if appropriate for your session modeldb_session_token = str(uuid.uuid4())- expires_at = datetime.utcnow() + current_app.config['JWT_EXPIRATION_DELTA']+ expires_at = datetime.now(UTC) + current_app.config['JWT_EXPIRATION_DELTA']cur.execute('INSERT INTO user_sessions (user_id, session_token, expires_at, ip_address, user_agent, login_method) VALUES (%s, %s, %s, %s, %s, %s)',