Refactor code for improved readability and consistency

- Cleaned up whitespace and formatting across multiple files for better readability.
This commit is contained in:
JSC
2025-07-02 10:37:48 +02:00
parent e63c7a0767
commit 171dbb9b63
19 changed files with 361 additions and 260 deletions

View File

@@ -281,19 +281,19 @@ def update_profile():
from flask import request
from app.database import db
from app.models.user import User
data = request.get_json()
if not data:
return {"error": "No data provided"}, 400
user_data = get_current_user()
if not user_data:
return {"error": "User not authenticated"}, 401
user = User.query.get(int(user_data["id"]))
if not user:
return {"error": "User not found"}, 404
# Update allowed fields
if "name" in data:
name = data["name"].strip()
@@ -302,10 +302,10 @@ def update_profile():
if len(name) > 100:
return {"error": "Name too long (max 100 characters)"}, 400
user.name = name
try:
db.session.commit()
# Return fresh user data from database
updated_user = {
"id": str(user.id),
@@ -319,11 +319,8 @@ def update_profile():
"plan": user.plan.to_dict() if user.plan else None,
"credits": user.credits,
}
return {
"message": "Profile updated successfully",
"user": updated_user
}
return {"message": "Profile updated successfully", "user": updated_user}
except Exception as e:
db.session.rollback()
return {"error": f"Failed to update profile: {str(e)}"}, 500
@@ -337,50 +334,50 @@ def change_password():
from app.database import db
from app.models.user import User
from werkzeug.security import check_password_hash
data = request.get_json()
if not data:
return {"error": "No data provided"}, 400
user_data = get_current_user()
if not user_data:
return {"error": "User not authenticated"}, 401
user = User.query.get(int(user_data["id"]))
if not user:
return {"error": "User not found"}, 404
new_password = data.get("new_password")
current_password = data.get("current_password")
if not new_password:
return {"error": "New password is required"}, 400
# Password validation
if len(new_password) < 6:
return {"error": "Password must be at least 6 characters long"}, 400
# Check authentication method: if user logged in via password, require current password
# If user logged in via OAuth, they can change password without current password
current_auth_method = user_data.get("provider", "unknown")
if user.password_hash and current_auth_method == "password":
# User has a password AND logged in via password, require current password for verification
if not current_password:
return {"error": "Current password is required to change password"}, 400
return {
"error": "Current password is required to change password"
}, 400
if not check_password_hash(user.password_hash, current_password):
return {"error": "Current password is incorrect"}, 400
# If user logged in via OAuth (google, github, etc.), they can change password without current password
# Set the new password
try:
user.set_password(new_password)
db.session.commit()
return {
"message": "Password updated successfully"
}
return {"message": "Password updated successfully"}
except Exception as e:
db.session.rollback()
return {"error": f"Failed to update password: {str(e)}"}, 500