mirror of
https://github.com/sassanix/Warracker.git
synced 2026-01-05 21:19:42 -06:00
Update deprecated utcnow() usage
DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.now(datetime.UTC).
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
# backend/auth_routes.py
|
||||
# Updated: 2025-01-24 - Fixed API endpoints for notifications
|
||||
from flask import Blueprint, request, jsonify, current_app
|
||||
from datetime import datetime, timedelta
|
||||
from datetime import datetime, UTC, timedelta
|
||||
import uuid
|
||||
import smtplib
|
||||
from 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 info
|
||||
ip_address = request.remote_addr
|
||||
user_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 info
|
||||
ip_address = request.remote_addr
|
||||
user_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 token
|
||||
reset_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 user
|
||||
cur.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!'}), 400
|
||||
|
||||
user_id = token_info[0]
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# backend/auth_utils.py
|
||||
import jwt
|
||||
from datetime import datetime, timedelta
|
||||
from datetime import datetime, UTC, timedelta
|
||||
from flask import current_app, request, jsonify
|
||||
from functools import wraps
|
||||
import 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')
|
||||
|
||||
@@ -12,7 +12,7 @@ import time
|
||||
import atexit
|
||||
import smtplib
|
||||
import logging
|
||||
from datetime import datetime, date, timedelta
|
||||
from datetime import datetime, date, UTC, timedelta
|
||||
from email.mime.multipart import MIMEMultipart
|
||||
from email.mime.text import MIMEText
|
||||
from threading import Lock
|
||||
@@ -353,7 +353,7 @@ def process_email_notifications(all_warranties, eligible_user_ids, is_manual, ge
|
||||
server.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 building
|
||||
cur.execute("""
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
# backend/oidc_handler.py
|
||||
import os
|
||||
import 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 snippet
|
||||
from 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 table
|
||||
ip_address = request.remote_addr
|
||||
user_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 model
|
||||
db_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)',
|
||||
|
||||
Reference in New Issue
Block a user