Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

website: updating settings file #4035

Merged
merged 4 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
157 changes: 153 additions & 4 deletions src/website/core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def require_env_var(env_var: str) -> str:
ALLOWED_HOSTS = parse_env_list('ALLOWED_HOSTS', default='localhost,127.0.0.1')

# ---------------------------------------------------------
# Applications
# Installed Apps
# ---------------------------------------------------------
INSTALLED_APPS = [
# Django Defaults
Expand Down Expand Up @@ -115,9 +115,11 @@ def require_env_var(env_var: str) -> str:
CORS_ALLOWED_ORIGIN_REGEXES = parse_env_list('CORS_ORIGIN_REGEX_WHITELIST')
CSRF_TRUSTED_ORIGINS = parse_env_list('CSRF_TRUSTED_ORIGINS')

# If no CORS settings provided, consider defaulting to empty lists
CORS_ALLOWED_ORIGINS = CORS_ALLOWED_ORIGINS if CORS_ALLOWED_ORIGINS else []
CORS_ALLOWED_ORIGIN_REGEXES = CORS_ALLOWED_ORIGIN_REGEXES if CORS_ALLOWED_ORIGIN_REGEXES else []
# Ensure no trailing slashes and correct schemes
CORS_ALLOWED_ORIGINS = [origin.rstrip('/') for origin in CORS_ALLOWED_ORIGINS]
CORS_ALLOWED_ORIGIN_REGEXES = [regex.rstrip(
'/') for regex in CORS_ALLOWED_ORIGIN_REGEXES]
CSRF_TRUSTED_ORIGINS = [origin.rstrip('/') for origin in CSRF_TRUSTED_ORIGINS]

# Security cookies
CSRF_COOKIE_SECURE = not DEBUG
Expand Down Expand Up @@ -269,3 +271,150 @@ def require_env_var(env_var: str) -> str:
'scrollingContainer': '#scrolling-container',
},
}

# ---------------------------------------------------------
# File Upload Settings
# ---------------------------------------------------------
# Increase these values as needed to handle larger uploads
FILE_UPLOAD_MAX_MEMORY_SIZE = 10485760 # 10 MB
DATA_UPLOAD_MAX_MEMORY_SIZE = 10485760 # 10 MB

# ---------------------------------------------------------
# SSL and Proxy Settings (if behind a reverse proxy)
# ---------------------------------------------------------
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
USE_X_FORWARDED_HOST = True
Comment on lines +285 to +286
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Ensure SSL and proxy settings align with deployment environment

Enabling SECURE_PROXY_SSL_HEADER and USE_X_FORWARDED_HOST is appropriate when the application is behind a reverse proxy that sets the X-Forwarded-Proto header, such as Nginx. If this isn't always the case, consider making these settings conditional based on an environment variable to prevent potential misconfigurations in other environments.


# ---------------------------------------------------------
# Logging Configuration
# ---------------------------------------------------------
LOG_DIR = BASE_DIR / 'logs'
LOG_DIR.mkdir(exist_ok=True) # Ensure log directory exists
Comment on lines +291 to +292
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Avoid side effects in settings.py by removing directory creation

Creating directories within settings.py can lead to unintended side effects since settings are imported at various points during the application's lifecycle. It's recommended to avoid I/O operations in settings files. Consider removing LOG_DIR.mkdir(exist_ok=True) and ensuring that the log directory is created during the application's startup sequence or via deployment scripts.


LOGGING = {
'version': 1,
'disable_existing_loggers': False,
# Formatters
'formatters': {
'verbose': {
'format': '[%(asctime)s] %(levelname)s %(name)s [%(filename)s:%(lineno)d] %(message)s',
'datefmt': '%Y-%m-%d %H:%M:%S'
},
'simple': {
'format': '%(levelname)s %(message)s'
},
},
# Handlers
'handlers': {
'console': {
'class': 'logging.StreamHandler',
'formatter': 'verbose',
'level': 'DEBUG' if DEBUG else 'INFO',
},
'file': {
'class': 'logging.FileHandler',
'filename': LOG_DIR / 'django.log',
'formatter': 'verbose',
'level': 'INFO',
},
'error_file': {
'class': 'logging.FileHandler',
'filename': LOG_DIR / 'django_errors.log',
'formatter': 'verbose',
'level': 'ERROR',
},
},
# Loggers
'loggers': {
# Django Logs
'django': {
'handlers': ['console', 'file', 'error_file'],
'level': 'INFO',
'propagate': True,
},
# Cloudinary Logs
'cloudinary': {
'handlers': ['console', 'file', 'error_file'],
'level': 'INFO',
'propagate': True,
},
# Event App Logs
'apps.event': {
'handlers': ['console', 'file', 'error_file'],
'level': 'DEBUG' if DEBUG else 'INFO',
'propagate': False,
},
# CleanAir App Logs
'apps.cleanair': {
'handlers': ['console', 'file', 'error_file'],
'level': 'DEBUG' if DEBUG else 'INFO',
'propagate': False,
},
# AfricanCities App Logs
'apps.africancities': {
'handlers': ['console', 'file', 'error_file'],
'level': 'DEBUG' if DEBUG else 'INFO',
'propagate': False,
},
# Publications App Logs
'apps.publications': {
'handlers': ['console', 'file', 'error_file'],
'level': 'DEBUG' if DEBUG else 'INFO',
'propagate': False,
},
# Press App Logs
'apps.press': {
'handlers': ['console', 'file', 'error_file'],
'level': 'DEBUG' if DEBUG else 'INFO',
'propagate': False,
},
# Impact App Logs
'apps.impact': {
'handlers': ['console', 'file', 'error_file'],
'level': 'DEBUG' if DEBUG else 'INFO',
'propagate': False,
},
# FAQs App Logs
'apps.faqs': {
'handlers': ['console', 'file', 'error_file'],
'level': 'DEBUG' if DEBUG else 'INFO',
'propagate': False,
},
# Highlights App Logs
'apps.highlights': {
'handlers': ['console', 'file', 'error_file'],
'level': 'DEBUG' if DEBUG else 'INFO',
'propagate': False,
},
# Career App Logs
'apps.career': {
'handlers': ['console', 'file', 'error_file'],
'level': 'DEBUG' if DEBUG else 'INFO',
'propagate': False,
},
# Partners App Logs
'apps.partners': {
'handlers': ['console', 'file', 'error_file'],
'level': 'DEBUG' if DEBUG else 'INFO',
'propagate': False,
},
# Board App Logs
'apps.board': {
'handlers': ['console', 'file', 'error_file'],
'level': 'DEBUG' if DEBUG else 'INFO',
'propagate': False,
},
# Team App Logs
'apps.team': {
'handlers': ['console', 'file', 'error_file'],
'level': 'DEBUG' if DEBUG else 'INFO',
'propagate': False,
},
# ExternalTeams App Logs
'apps.externalteams': {
'handlers': ['console', 'file', 'error_file'],
'level': 'DEBUG' if DEBUG else 'INFO',
'propagate': False,
},
}
}
3 changes: 1 addition & 2 deletions src/website/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@ python manage.py collectstatic --noinput

# Start Gunicorn server to serve the Django application
echo "Starting Gunicorn server..."
exec gunicorn core.wsgi:application --bind 0.0.0.0:8000 --timeout 600 --log-level info
# exec gunicorn core.wsgi:application --bind 0.0.0.0:8000 --timeout 600 --workers ${GUNICORN_WORKERS:-3} --log-level info
exec gunicorn core.wsgi:application --bind 0.0.0.0:8000 --timeout 600 --workers 3 --log-level info
Loading