mirror of
https://github.com/joaovitoriasilva/endurain.git
synced 2026-01-09 15:57:59 -05:00
Refactored the server settings module for better structure and clarity, including splitting base and public schemas, updating SQLAlchemy model to use Mapped/mapped_column, and improving type hints and docstrings. Enhanced API endpoints with detailed documentation, added file validation for login photo upload, and improved error handling. The __init__.py now provides explicit exports for all key components.
97 lines
2.9 KiB
Python
97 lines
2.9 KiB
Python
from fastapi import HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
|
|
import server_settings.schema as server_settings_schema
|
|
import server_settings.models as server_settings_models
|
|
|
|
import core.logger as core_logger
|
|
|
|
|
|
def get_server_settings(db: Session) -> server_settings_models.ServerSettings:
|
|
"""
|
|
Retrieve singleton server settings from database.
|
|
|
|
Args:
|
|
db: Database session.
|
|
|
|
Returns:
|
|
ServerSettings instance or None if not found.
|
|
|
|
Raises:
|
|
HTTPException: If database error occurs.
|
|
"""
|
|
try:
|
|
# Get the user from the database
|
|
return (
|
|
db.query(server_settings_models.ServerSettings)
|
|
.filter(server_settings_models.ServerSettings.id == 1)
|
|
.first()
|
|
)
|
|
except Exception as err:
|
|
# Log the exception
|
|
core_logger.print_to_log(
|
|
f"Error in get_server_settings: {err}", "error", exc=err
|
|
)
|
|
# Raise an HTTPException with a 500 Internal Server Error status code
|
|
raise HTTPException(
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
detail=f"Internal Server Error: {err}",
|
|
) from err
|
|
|
|
|
|
def edit_server_settings(
|
|
server_settings: server_settings_schema.ServerSettingsEdit, db: Session
|
|
) -> server_settings_models.ServerSettings:
|
|
"""
|
|
Update server settings in database.
|
|
|
|
Args:
|
|
server_settings: New settings to apply.
|
|
db: Database session.
|
|
|
|
Returns:
|
|
Updated ServerSettings instance.
|
|
|
|
Raises:
|
|
HTTPException: If settings not found or database error.
|
|
"""
|
|
try:
|
|
# Get the server_settings from the database
|
|
db_server_settings = get_server_settings(db)
|
|
|
|
if db_server_settings is None:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Server settings not found",
|
|
headers={"WWW-Authenticate": "Bearer"},
|
|
)
|
|
|
|
# Dictionary of the fields to update if they are not None
|
|
server_settings_data = server_settings.model_dump(exclude_unset=True)
|
|
# Iterate over the fields and update the db_user dynamically
|
|
for key, value in server_settings_data.items():
|
|
setattr(db_server_settings, key, value)
|
|
|
|
# Commit the transaction
|
|
db.commit()
|
|
# Refresh the object to ensure it reflects database state
|
|
db.refresh(db_server_settings)
|
|
|
|
return db_server_settings
|
|
except HTTPException as http_err:
|
|
raise http_err
|
|
except Exception as err:
|
|
# Rollback the transaction
|
|
db.rollback()
|
|
|
|
# Log the exception
|
|
core_logger.print_to_log(
|
|
f"Error in edit_server_settings: {err}", "error", exc=err
|
|
)
|
|
|
|
# Raise an HTTPException with a 500 Internal Server Error status code
|
|
raise HTTPException(
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
detail=f"Internal Server Error: {err}",
|
|
) from err
|